-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer_3d_art.py
117 lines (92 loc) · 2.94 KB
/
viewer_3d_art.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
from PIL import Image, ImageDraw
import random
import math
import cv2
import pygame
from pygame.locals import DOUBLEBUF, OPENGL
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import pickle
import argparse
# Cube vertices and surfaces
vertices = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1)
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6)
)
def setup_viewport(width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, (width / height), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0) # カメラの位置と向きを設定
def draw_cube(size):
glColor3f(0, 0, 0) # 黒色の立方体
glBegin(GL_QUADS)
for surface in surfaces:
for vertex in surface:
glVertex3fv([v * size for v in vertices[vertex]])
glEnd()
def rendering_image(current_cubes, img_size):
cnt = 40
# メインループ
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# ここで全体の回転を適用
glPushMatrix()
glRotatef(cnt, 1, 1, 1) # X軸を中心に回転
for x, y, z, angle, size in current_cubes:
glPushMatrix()
glTranslatef(x, y, z)
glRotatef(angle, 0, 1, 0)
#glRotatef(cnt, 1, 0, 0) # X軸を中心に回転
draw_cube(size) # 立方体を描画
glPopMatrix()
cnt += 1
glPopMatrix()
pygame.display.flip()
if cnt % 360 == 0:
pygame.time.wait(1000)
return
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--cubes-file', type=str, help='specify cubes files')
parser.add_argument('--img-size', default=128, type=int, help='num of cubes in 3D space')
#parser.add_argument('--cube-size', default=0.2, type=float, help='num of cubes in 3D space')
opt = parser.parse_args()
print(opt)
cubes_file = opt.cubes_file
img_size = (opt.img_size, opt.img_size)
#cube_size = opt.cube_size
pygame.init()
display = (img_size[0], img_size[1])
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glClearColor(1.0, 1.0, 1.0, 1.0) # 白色に設定
glEnable(GL_DEPTH_TEST)
# ここでビューポートと投影を設定
setup_viewport(display[0], display[1])
with open(cubes_file, "rb") as fp: # Unpickling
final_cubes = pickle.load(fp)
# 画像をレンダリング
rendering_image(final_cubes, img_size)