-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounds.py
177 lines (160 loc) · 5.87 KB
/
bounds.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
172
173
174
175
176
177
from program import Program
from buffers import VertexBuffer, IndexBuffer
from camera import Camera
from pyglet.gl import *
from ctypes import *
from euclid3 import *
from utils import uint_to_rgb
from config import *
class Bounds:
def __init__(self, left, right, top, bottom):
self.minX = left
self.maxX = right
self.minY = top
self.maxY = bottom
self.parent = None
self.children = []
self.matrix = Matrix4().identity()
self.position = Vector3(0.0, 0.0, 0.0)
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)
r, g, b = uint_to_rgb(0x00FF00)
v = [
0.0, 0.0, r, g, b,
right, 0.0, r, g, b,
right, bottom, r, g, b,
0.0, bottom, r, g, b,
]
i = [
0, 1, 2,
2, 3, 0
]
self.set_data(vertices=v, indices=i)
def set_bounds(self, left, right, top, bottom):
self.position.x = left
self.position.y = top
self.minX = left
self.maxX = right
self.minY = top
self.maxY = bottom
self.matrix = Matrix4.new_translate(self.position.x, self.position.y, self.position.z)
def check_intersect(self, bounds):
if self.maxX < bounds.minX:
# a is left of b
return False
if self.minX > bounds.maxX:
# a is right of b
return False
if self.maxY < bounds.minY:
# a is above b
return False
if self.minY > bounds.maxY:
# a is below b
return False
# boxes overlap
return True
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):
pass
# 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_LINE_STRIP, len(self.indices), GL_UNSIGNED_INT, 0)
# elif self.vertices:
# pass #gldrawarray?
#
# # stop the vao
# glBindVertexArray(0)