Skip to content

Instantly share code, notes, and snippets.

@hightemp
Last active May 8, 2017 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hightemp/2409eb6332bbe8ac639155f130fe4223 to your computer and use it in GitHub Desktop.
Save hightemp/2409eb6332bbe8ac639155f130fe4223 to your computer and use it in GitHub Desktop.
[Python] [OpenGL] Example of usage pyglet, pywavefront and OpenGL on python
#!/usr/bin/python
# -*- coding: UTF8 -*-
from os.path import *
import pyglet
from pyglet.gl import *
from pyglet.window import *
import pywavefront
import math
def fnApplicationStart():
global bIsLoaded
global aCameraVector
# global rx, ry, rz, dx, dy, dz
global objMesh
bIsLoaded = False
objMesh = None
aCameraVector = [0, 0, 0]
# dx = 0
# dy = 0
# dz = 0
try:
config = Config(sample_buffers=1,
samples=4,
depth_size=16,
double_buffer=True, )
window = pyglet.window.Window(resizable=True,
config=config)
except pyglet.window.NoSuchConfigException:
window = pyglet.window.Window(resizable=True)
@window.event
def on_resize(width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60., width / float(height), .1, 1000.)
glMatrixMode(GL_MODELVIEW)
return pyglet.event.EVENT_HANDLED
def on_timer(dt):
global aCameraVector
# rx += dx * 30
# ry += dy * 30
# rz += dz * 30
# rx %= 360
# ry %= 360
# rz %= 360
# dx = 0.01
# dy = 0.01
# dz = 0.01
@window.event
def on_mouse_drag(in_iX, in_iY, in_iDX, in_iDY, in_iButtons, in_iModifiers):
global aCameraVector
if in_iButtons & mouse.LEFT:
aCameraVector[0] += in_iDX
aCameraVector[1] += in_iDY
@window.event
def on_activate():
global bIsLoaded
global objMesh
if bIsLoaded:
return
print("[!] Load started")
bIsLoaded = True
sAbsolutePath = abspath('models/brain.obj')
objMesh = pywavefront.Wavefront(sAbsolutePath)
print("[!] Load finished")
def draw_axes(in_iPositionX, in_iPositionY, in_iPositionZ, in_iSize):
glDisable(GL_LIGHTING)
glPushMatrix()
glBegin(GL_LINES)
aVector = [
1, 0, 0,
0, 1, 0,
0, 0, 1
]
for iIndex in range(0, 8, 3):
glColor3f(aVector[iIndex+0], aVector[iIndex+1], aVector[iIndex+2])
glVertex3f(in_iSize*aVector[iIndex+0]+in_iPositionX, in_iSize*aVector[iIndex+1]+in_iPositionY, in_iSize*aVector[iIndex+2]+in_iPositionZ)
glVertex3f(-in_iSize*aVector[iIndex+0]+in_iPositionX, -in_iSize*aVector[iIndex+1]+in_iPositionY, -in_iSize*aVector[iIndex+2]+in_iPositionZ)
glEnd()
glPopMatrix()
glEnable(GL_LIGHTING)
@window.event
def on_draw():
global aCameraVector
global objMesh
glEnable(GL_DEPTH_TEST)
glAlphaFunc(GL_GREATER, 0.5);
glEnable(GL_ALPHA_TEST);
glEnable(GL_LIGHTING)
glEnable(GL_FOG)
# Set the fog color.
glFogfv(GL_FOG_COLOR, (GLfloat * 4)(0, 0, 0, 1))
# Say we have no preference between rendering speed and quality.
glHint(GL_FOG_HINT, GL_DONT_CARE)
# Specify the equation used to compute the blending factor.
glFogi(GL_FOG_MODE, GL_LINEAR)
# How close and far away fog starts and ends. The closer the start and end,
# the denser the fog in the fog range.
glFogf(GL_FOG_START, 60.0)
glFogf(GL_FOG_END, 200.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0, 0, -100)
glRotatef(aCameraVector[0], 0, 0, 1)
glRotatef(aCameraVector[1], 0, 1, 0)
glRotatef(aCameraVector[2], 1, 0, 0)
for iIndex in range(0, 7):
iAngle = iIndex * 50
glPushMatrix()
glTranslatef(40.0 * math.cos(math.radians(iAngle)), 40.0 * math.sin(math.radians(iAngle)), 0.0)
draw_axes(0, 0, 0, 10)
glLightfv(GL_LIGHT0 + iIndex, GL_POSITION, (GLfloat * 4)(0.0, 0.0, 0.0, 1))
glLightfv(GL_LIGHT0 + iIndex, GL_DIFFUSE, (GLfloat * 3)(200.0, 200.0, 200.0))
glLightfv(GL_LIGHT0 + iIndex, GL_QUADRATIC_ATTENUATION, (GLfloat * 1)(.45))
glLightfv(GL_LIGHT0 + iIndex, GL_AMBIENT, (GLfloat * 3)(math.cos(math.radians(iAngle)), math.sin(math.radians(iAngle)), math.sin(math.radians(iAngle))*math.cos(math.radians(iAngle))))
glEnable(GL_LIGHT0 + iIndex)
glPopMatrix()
glDisable(GL_LIGHTING)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA)
aVectors = [
0, 0, 0, 1, 1, 0,
1, 0, 0, 0, 1, 0,
0, 1, 0, 1, 1, 1,
0, -1, 0, 0, 0, 1,
0, 2, 0, 1, 0, 0,
-1, 0, 0, 0.3, 0, 0
]
for iSectorIndex in range(0, 35, 6):
glPushMatrix()
glScalef(20.0, 20.0, 20.0)
glRotatef(aVectors[iSectorIndex+0]*90, 0, 0, 1)
glRotatef(aVectors[iSectorIndex+1]*90, 0, 1, 0)
glRotatef(aVectors[iSectorIndex+2]*90, 1, 0, 0)
glBegin(GL_TRIANGLE_FAN)
glColor4f(aVectors[iSectorIndex+3], aVectors[iSectorIndex+4], aVectors[iSectorIndex+5], 0.8)
glVertex3f(0.01, 0.0, 0.0)
glVertex3f(0.9, 0.9, 0.9)
glVertex3f(0.9, -0.9, 0.9)
glVertex3f(0.9, -0.9, -0.9)
glVertex3f(0.9, 0.9, -0.9)
glVertex3f(0.9, 0.9, 0.9)
glEnd()
glPopMatrix()
glDisable(GL_BLEND)
glEnable(GL_LIGHTING)
draw_axes(0, 0, 0, 100)
if not bIsLoaded:
return
glPushMatrix()
glTranslatef(0, -12.5, 0)
glColor4f(1, 1, 1, 0.5)
objMesh.draw()
glPopMatrix()
pyglet.clock.schedule(on_timer)
pyglet.app.run()
if __name__ == '__main__':
fnApplicationStart()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment