-
Notifications
You must be signed in to change notification settings - Fork 210
/
renderer.py
executable file
·123 lines (99 loc) · 2.66 KB
/
renderer.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
118
119
120
121
122
#!/usr/bin/env python3
import sys
import time
sys.path.append("lib/macosx")
sys.path.append("lib/linux")
from helpers import add_ones
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
np.set_printoptions(suppress=True)
class Renderer(object):
def __init__(self, W, H):
self.W, self.H = W, H
self.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)
)
self.edges = (
(0,1), (0,3), (0,4),
(2,1), (2,3), (2,7),
(6,3), (6,4), (6,7),
(5,1), (5,4), (5,7)
)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(self.W, self.H)
glutCreateWindow(b"OpenGL Offscreen")
glutHideWindow()
# not used
glutDisplayFunc(self.fakedraw)
#glutMainLoop()
def fakedraw(self):
pass
def draw(self, pos):
# set up 2d screen
glClearColor(0.3, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# set up camera
glPushMatrix()
glLoadIdentity()
gluPerspective(45, self.W/self.H, 0.1, 100.0)
glTranslatef(pos[0], pos[1], pos[2])
# draw stuff and track verts
verts = []
def draw_cube(offset):
glBegin(GL_LINES)
for edge in self.edges:
for vertex in edge:
vv = self.vertices[vertex]
vv = (vv[0]+offset[0], vv[1]+offset[1], vv[2]+offset[2])
verts.append(vv)
glVertex3fv(vv)
glEnd()
glColor(0.0, 1.0, 0.0)
draw_cube([0,0,-50])
glColor(1.0, 0.0, 0.0)
draw_cube([5,2,-30])
glColor(0.0, 0.0, 1.0)
draw_cube([-10,2,-30])
glColor(0.0, 1.0, 1.0)
draw_cube([5,5,-25])
glColor(1.0, 1.0, 0.0)
draw_cube([-5,-5,-35])
glColor(1.0, 1.0, 1.0)
draw_cube([-5,5,-45])
# extract "map"
projected = []
modelview = glGetDoublev(GL_MODELVIEW_MATRIX)
projection = glGetDoublev(GL_PROJECTION_MATRIX)
for v in verts:
cc = gluProject(v[0], v[1], v[2], modelview, projection)
cc = np.array(cc)
cc /= cc[2]
projected.append(cc[0:2])
# render to numpy buffer and return
glPixelStorei(GL_PACK_ALIGNMENT, 1)
ret = glReadPixels(0, 0, self.W, self.H, GL_RGBA, GL_UNSIGNED_BYTE)
ret = np.fromstring(ret, np.uint8).reshape((self.H, self.W, 4))
glutSwapBuffers()
glPopMatrix()
return np.copy(ret[:, :, 0:3]), np.array(projected)
if __name__ == "__main__":
W,H = 640,480
r = Renderer(W, H)
from display import Display2D
disp2d = Display2D(W, H)
i = 0
while 1:
draw, _ = r.draw([i,0,-10])
disp2d.paint(draw)
time.sleep(0.05)
i += 0.02