-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.py
134 lines (115 loc) · 4.6 KB
/
mesh.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
from gameobject import GameObject
from program import Program
from buffers import VertexBuffer, IndexBuffer
from camera import Camera
from pyglet.gl import *
from ctypes import *
from euclid3 import *
from config import *
class Mesh(GameObject):
def __init__(self, width, height):
self.program = Program()
self.vao = GLuint(0)
self.vbuf = None
self.ibuf = None
self.vertices = []
self.normals = []
self.indices = []
self.uvs = []
# we should really be getting the camera not creating a new instance..
self.camera = Camera(WIDTH, HEIGHT)
GameObject.__init__(self, width, height)
def set_data(self, *args, **kwargs):
self.vertices = kwargs.get('vertices', [])
self.normals = kwargs.get('normals', [])
self.indices = kwargs.get('indices', [])
self.uvs = kwargs.get('uvs', [])
self.colors = kwargs.get('colors', [])
# create a vao
glGenVertexArrays(1, self.vao)
# use it
glBindVertexArray(self.vao)
# create a vertexbuffer
if self.vertices:
self.vbuf = VertexBuffer(self.vertices)
#self.vbuf.set_attribute(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0)
#self.vbuf.set_attribute(1, 2, GL_FLOAT, GL_FALSE, 2, 0)
# create an indexbuffer
if self.indices:
self.ibuf = IndexBuffer(self.indices)
self.program.compile_shader_from_string(
b"""
#version 150
in vec2 position;
in vec3 color;
uniform mat4 proj, view, model;
out vec3 theColor;
void main()
{
theColor = color;
gl_Position = proj * view * model * vec4(position, 0.0, 1.0);
}
""", 'vertex')
self.program.compile_shader_from_string(
b"""
#version 150
in vec3 theColor;
out vec4 outputF;
void main()
{
outputF = vec4(theColor, 1.0);
}
""", 'fragment')
self.program.link()
self.program.validate()
self.program.bind()
#self.vbuf.set_attribute(self.program.get_handle(), "position", 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0)
#self.vbuf.set_attribute(self.program.get_handle(), "color", 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0)
# Specify the layout of the vertex data
position_attribute = glGetAttribLocation(self.program.get_handle(), b"position")
if position_attribute > -1:
glEnableVertexAttribArray(position_attribute)
glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0)
color_attribute = glGetAttribLocation(self.program.get_handle(), b"color")
if color_attribute > -1:
glEnableVertexAttribArray(color_attribute)
glVertexAttribPointer(color_attribute, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 2 * sizeof(GLfloat))
# stop the vao
glBindVertexArray(0)
def render(self):
GameObject.render(self)
self.program.bind()
view_location = glGetUniformLocation(self.program.get_handle(), b"view")
if view_location > -1:
v = self.camera.view
v = v[:]
v_ctype = (GLfloat * len(v))(*v)
glUniformMatrix4fv(view_location, 1, GL_FALSE, v_ctype)
proj_location = glGetUniformLocation(self.program.get_handle(), b"proj")
if proj_location > -1:
p = self.camera.projection
p = p[:]
p_ctype = (GLfloat * len(p))(*p)
glUniformMatrix4fv(proj_location, 1, GL_FALSE, p_ctype)
model_location = glGetUniformLocation(self.program.get_handle(), b"model")
if model_location > -1:
self.matrix = Matrix4.new_translate(self.position.x, self.position.y, self.position.z)
if self.parent:
m = Matrix4().identity()
m = self.parent.matrix * self.matrix
m = m[:]
else:
m = self.matrix[:]
m_ctype = (GLfloat * len(m))(*m)
glUniformMatrix4fv(model_location, 1, GL_FALSE, m_ctype)
# bind vao for use
glBindVertexArray(self.vao)
# Draw a rectangle from the 2 triangles using 6 indices
if self.indices:
glDrawElements(GL_TRIANGLES, len(self.indices), GL_UNSIGNED_INT, 0)
elif self.vertices:
pass #gldrawarray?
# stop the vao
glBindVertexArray(0)
def update(self, dt):
super().update(dt)