-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.py
171 lines (135 loc) · 4.59 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from Math import Vector2
from Math import Vector3
import numpy as np
import ctypes
import math
class Renderer(object):
ZOOM_MOVE = 0.5
PAN_MOVE = 0.12
def __init__(self, camera, node):
self.camera = camera
self.node = node
self.setupGlut()
self.setupCallbacks()
self.compileShaders()
self.buildBuffers()
self.scale = 0.0005
self.mouse = Vector2()
glutMainLoop()
def setupGlut(self):
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(self.camera.width, self.camera.height);
glutInitWindowPosition(100, 100);
glutCreateWindow('pySG')
glClearColor(0.0, 0.0, 0.0, 0.0)
def setupCallbacks(self):
glutDisplayFunc(self.display)
glutIdleFunc(self.display)
glutKeyboardFunc(self.keyboard)
glutSpecialFunc(self.keyboard)
glutMouseFunc(self.mouse)
glutMotionFunc(self.mouseMove)
glutMouseWheelFunc(self.mouseWheel)
def keyboard(self, key, x, y ):
if key == '\033':
glutLeaveMainLoop()
# glutDestroyWindow(glutGetWindow())
# self.camera.position -= self.camera.target.normalize() * self.CAMERA_MOVE
# elif key == GLUT_KEY_UP:
# elif key == GLUT_KEY_DOWN:
# self.camera.position -= self.camera.up.normalize() * self.CAMERA_MOVE
# elif key == GLUT_KEY_RIGHT:
# elif key == GLUT_KEY_LEFT:
# self.camera.position -= Vector3.cross(self.camera.up.normalize(),self.camera.target.normalize()) * self.CAMERA_MOVE
def mouse(self, button, state, x, y):
print button
if button == GLUT_MIDDLE_BUTTON:
self.mouse.set(x, y)
def mouseMove(self, x, y):
newMouse = Vector2(x, y)
delta = (newMouse - self.mouse).normalize()
self.mouse = newMouse
if delta.x > 0:
self.camera.position -= Vector3.cross(self.camera.up.normalize(),self.camera.target.normalize()) * delta.x * self.PAN_MOVE
else:
self.camera.position += Vector3.cross(self.camera.up.normalize(),self.camera.target.normalize()) * abs(delta.x) * self.PAN_MOVE
if delta.y < 0:
self.camera.position -= self.camera.up.normalize() * abs(delta.y) * self.PAN_MOVE
else:
self.camera.position += self.camera.up.normalize() * delta.y *self.PAN_MOVE
print delta
def mouseWheel(self, wheel, forward, x, y):
print forward
if forward == 1:
self.camera.position += self.camera.target.normalize() * self.ZOOM_MOVE
else:
self.camera.position -= self.camera.target.normalize() * self.ZOOM_MOVE
def compileShaders(self):
program = glCreateProgram()
vertex = glCreateShader(GL_VERTEX_SHADER)
fragment = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(vertex, self.vertex_shader)
glCompileShader(vertex)
glAttachShader(program, vertex)
glShaderSource(fragment, self.fragment_shader)
glCompileShader(fragment)
glAttachShader(program, fragment)
glLinkProgram(program)
glValidateProgram(program)
glUseProgram(program)
self.gWorld = glGetUniformLocation(program, 'gWorld')
def buildBuffers(self):
m = self.node.mesh
self.vertices = []
for v in m.vertices:
self.vertices += [v.x, v.y, v.z]
self.vertices = np.array(self.vertices, dtype=np.float32)
self.vertexBuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
glBufferData(GL_ARRAY_BUFFER, self.vertices.nbytes, self.vertices, GL_STATIC_DRAW)
self.faces = []
for f in m.faces:
self.faces += [f.vA-1, f.vB-1, f.vC-1]
self.faces = np.array(self.faces, dtype=np.uint32)
self.faceBuffer = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.faceBuffer)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.faces.nbytes, self.faces, GL_STATIC_DRAW)
def display(self):
glClear(GL_COLOR_BUFFER_BIT)
self.scale += 0.1
self.node.rotation.setEuler(self.scale/2, self.scale, 0)
m = self.camera.perspectiveTransform()*self.camera.cameraTransform()*self.node.transform.matrix
glUniformMatrix4fv(self.gWorld, 1, GL_TRUE, m.A)
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.faceBuffer)
glDrawElements(GL_TRIANGLES, len(self.faces), GL_UNSIGNED_INT, ctypes.c_void_p(0))
glDisableVertexAttribArray(0);
glutSwapBuffers()
vertex_shader = """
#version 330
layout (location = 0) in vec3 pos;
uniform mat4 gWorld;
out vec4 Color;
void main()
{
gl_Position = gWorld * vec4(pos, 1.0);
Color = vec4(clamp(pos, 0.0, 1.0), 1.0);
}
"""
fragment_shader = """
#version 330
in vec4 Color;
out vec4 FragColor;
void main()
{
FragColor = Color;
}
"""
# FragColor = vec4(1.0, 0.0, 0.0, 1.0);