-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyopengl_app.py
160 lines (134 loc) · 5.95 KB
/
pyopengl_app.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
# TODO:
# A. Placeholder
# Library Imports
import random
from timeit import default_timer
import pygame
import numpy
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.arrays import vbo
# Local Imports
import nsvt_geometry as geo
import nsvt_config as config
class PyopenglApp():
def __init__(self, wrapper_):
# MEMBER VARIABLE DEFINITIONS
# References to external objects
self.wrapper = wrapper_
# Initialization method for PyGame library
pygame.init()
# PyGame Parameters
self.display_width = config.DEFAULT_GUI_WIDTH
self.display_height = config.DEFAULT_GUI_HEIGHT
self.gameDisplay = pygame.display.set_mode((self.display_width, self.display_height),
pygame.OPENGL | pygame.DOUBLEBUF | pygame.HWSURFACE)
pygame.display.set_caption('OpenGL App Test')
# Shapes
self.shapes = []
for i in range(100):
shapeX = random.randrange(5, 16) / 200
shapeY = random.randrange(5, 16) / 200
shapeZ = random.randrange(5, 16) / 200
moveX = random.randrange(-50, 50) / 10
moveY = random.randrange(-50, 50) / 10
moveZ = random.randrange(-50, 50) / 10
rotateX = random.randrange(-90, 91)
rotateY = random.randrange(-90, 91)
rotateZ = random.randrange(-90, 91)
self.shapes.append(self.wrapper.shapeGen.generateCuboid(shapeX, shapeY, shapeZ))
geo.translateVertices(self.shapes[-1].vertices, moveX, moveY, moveZ)
# self.shapes[-1].vertices = geo.rotateVertices(self.shapes[-1].vertices, rotateX, rotateY, rotateZ)
geo.rotateVertices(self.shapes[-1].vertices, rotateX, rotateY, rotateZ)
geo.updateTriangleVertices(self.shapes[-1].vertices, self.shapes[-1].triangles,
self.shapes[-1].triangleVertices)
# PyOpenGL
# SETUP OPERATIONS
glViewport(0, 0, self.display_width, self.display_height)
glClearColor(0.0, 0.5, 0.5, 1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
glPointSize(5)
# Set camera perspective
gluPerspective(25, (self.display_width / self.display_height), 0.1, 50.0)
# Translate and rotate to initial position
glTranslatef(-0.5, -0.5, -10.0)
# glRotatef(20, 0, 0, 0)
# SHADER OPERATIONS
# (placeholder)
# VBO OPERATIONS
self.allVertices = numpy.concatenate([shape.vertices for shape in self.shapes], 0)
self.allTriangleVertices = numpy.concatenate([shape.triangleVertices for shape in self.shapes], 0)
self.vbo = vbo.VBO(self.allVertices)
self.vbo2 = vbo.VBO(self.allTriangleVertices)
# App Control
self.frameTime = default_timer()
self.clock = pygame.time.Clock()
self.crashed = False
fpsVals = []
# APPLICATION LOOP
while not self.crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
glTranslatef(0, 0, 1)
if event.key == pygame.K_q:
glTranslatef(0, 0, -1)
if event.key == pygame.K_s:
glTranslatef(0, 1, 0)
if event.key == pygame.K_w:
glTranslatef(0, -1, 0)
if event.key == pygame.K_a:
glTranslatef(1, 0, 0)
if event.key == pygame.K_d:
glTranslatef(-1, 0, 0)
# These lines track the amount of time that has elapsed since the last self.after() call
self.timeDelta = default_timer() - self.frameTime
self.frameTime = default_timer()
self.currentFPS = int(round(1 / self.timeDelta))
fpsVals.append(self.currentFPS)
# Updates the graphics
self.drawGraphicsOpenGL()
pygame.quit()
print(fpsVals)
quit()
def drawGraphicsOpenGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear the current display
# glRotatef(0.3, 1, 1, 1) # 3-degree rotation around the unit vector
# VBO OPERATIONS
self.allVertices = numpy.concatenate([shape.vertices for shape in self.shapes], 0)
self.allTriangleVertices = numpy.concatenate([shape.triangleVertices for shape in self.shapes], 0)
self.vbo = vbo.VBO(self.allVertices)
self.vbo2 = vbo.VBO(self.allTriangleVertices)
for shape in self.shapes:
randomAxis = (random.randrange(-100, 100), random.randrange(-100, 100), random.randrange(-100, 100))
randomAngle = random.randrange(-90, 91) / 10
geo.rotateVerticesAxisAngle(shape.vertices, randomAxis[0], randomAxis[1], randomAxis[2], randomAngle)
geo.updateTriangleVertices(shape.vertices, shape.triangles, shape.triangleVertices)
tempCounter = 0
try:
# VERTICES
self.vbo.bind()
try:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, self.vbo)
glDrawArrays(GL_POINTS, 0, len(self.allVertices))
finally:
self.vbo.unbind()
glDisableClientState(GL_VERTEX_ARRAY);
# TRIANGLES
self.vbo2.bind()
try:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, self.vbo2)
glDrawArrays(GL_TRIANGLES, 0, len(self.allTriangleVertices) * 3)
finally:
self.vbo2.unbind()
glDisableClientState(GL_VERTEX_ARRAY);
finally:
# Shader stuff will go here
# print("PLACEHOLDER 1")
tempCounter += 1
pygame.display.flip()