Skip to content

Instantly share code, notes, and snippets.

@Torxed
Created August 1, 2017 06:46
Show Gist options
  • Save Torxed/a67475773b39a8209b2841d9b08922ca to your computer and use it in GitHub Desktop.
Save Torxed/a67475773b39a8209b2841d9b08922ca to your computer and use it in GitHub Desktop.
import pyglet
key = pyglet.window.key
class main(pyglet.window.Window):
def __init__ (self, width=800, height=600, fps=False, *args, **kwargs):
super(main, self).__init__(width, height, *args, **kwargs)
self.x, self.y = 0, 0
self.alive = 1
def on_draw(self):
self.render()
def on_close(self):
self.alive = 0
def on_key_press(self, symbol, modifiers):
if symbol == key.ESCAPE: # [ESC]
self.alive = 0
else:
print(symbol)
def render(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #clear backbuffer and z-buffer
#calculate view matrix
viewMatrix = self.lookat(self.cameraPos, self.cameraTarget, self.cameraUp)
texnum = -1 #set texture to default
shader = '' #set shader to default
modelMatrixNum = -1 #set model matrix to default
glEnable(GL_TEXTURE_2D) #enable textures
for render in self.renderList: #loop through poly lists in renderList
try:
if render['tex'] != texnum: #if this poly list is using a different texture,
texnum = render['tex'] #bind it, and reset default
glBindTexture(GL_TEXTURE_2D, texnum)
except KeyError: #but if it doesn't have a texture
pass #just use the last one
try:
if shader != render['shd']: #if this poly list is using a different shader,
shader = render['shd']
self.shaders[shader].UseProgram() #change shader
self.shaders[shader].SetViewMatrix(viewMatrix)
self.shaders[shader].SetProjectionMatrix(self.projectionMatrix)
self.shaders[shader].SetColorMatrix(self.colorMatrix)
except KeyError: #but if it doesn't have a shader
pass #just use the last one
#now, calculate matrices that vary for each polygon and write them
try:
modelMatrix = render['mod'] #see if this polygon has any special modeling matrix
except KeyError:
#if not, just use the identity matrix
modelMatrix = np.identity(4)
# Set uniforms in shader program
self.shaders[shader].SetModelMatrix(modelMatrix)
# Write matrices
self.shaders[shader].WriteMatrices()
render['vrt'].draw(render['prm']) #actually draw polygon
glDisable(GL_TEXTURE_2D)
self.renderList = [] #clear renderList for next loop
self.flip()
def run(self):
while self.alive == 1:
self.render()
# -----------> This is key <----------
# This is what replaces pyglet.app.run()
# but is required for the GUI to not freeze
#
event = self.dispatch_events()
x = main()
x.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment