-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlie.py
130 lines (91 loc) · 3.41 KB
/
lie.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
from __future__ import division
import pygame
from pygame.locals import *
import THREE
from THREE.utils import Expando
from OpenGL.GL import *
from ctypes import *
width = 800
height = 600
pygame.init()
pygame.display.set_mode( (width, height), DOUBLEBUF|OPENGL )
scene = THREE.Scene()
camera = THREE.PerspectiveCamera( 75, width / height, 0.1, 1000 )
renderer = THREE.OpenGLRenderer
geometry = THREE.BoxGeometry( 1, 1, 1 )
material = THREE.MeshBasicMaterial(color = 0x00ff00 )
cube = THREE.Mesh( geometry, material )
scene.add( cube )
camera.position.z = 5
scene.updateMatrixWorld()
camera.updateMatrixWorld()
projectionMatrix = camera.projectionMatrix.clone().multiply( camera.matrixWorldInverse ).elements
cube.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, cube.matrixWorld )
modelViewMatrix = cube.modelViewMatrix.elements
diffuse = ( 0.0, 1.0, 0.0 )
opacity = 1.0
def flatten( vs ):
ret = []
for v in vs: ret.extend( [ v.x, v.y, v.z ] )
return ret
positions = flatten( cube.geometry.vertices )
positions = ( c_float * len( positions ) )( *positions )
def flatten2( idxs ):
ret = []
for idx in idxs: ret.extend( [ idx.a, idx.b, idx.c ] )
return ret
indices = flatten2( cube.geometry.faces )
indices = ( c_uint * len( indices ) )( *indices )
glPositionBuffer = glGenBuffers( 1 )
glBindBuffer( GL_ARRAY_BUFFER, glPositionBuffer )
glBufferData( GL_ARRAY_BUFFER, positions, GL_STATIC_DRAW )
glIndicesBuffer = glGenBuffers( 1 )
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, glIndicesBuffer )
glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW )
vertexGlsl = """
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
attribute vec3 position;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}"""
fragmentGlsl = """
uniform vec3 diffuse;
uniform float opacity;
void main() {
gl_FragColor = vec4( 1., 1., 1., 1. );
}"""
glVertex = glCreateShader( GL_VERTEX_SHADER )
glShaderSource( glVertex, vertexGlsl )
glCompileShader( glVertex )
glFragment = glCreateShader( GL_FRAGMENT_SHADER )
glShaderSource( glFragment, fragmentGlsl )
glCompileShader( glFragment )
glProgram = glCreateProgram()
glAttachShader( glProgram, glVertex )
glAttachShader( glProgram, glFragment )
glLinkProgram( glProgram )
glUseProgram( glProgram )
glProjectionMatrixUniform = glGetUniformLocation( glProgram, "projectionMatrix" )
glModelViewMatrixUniform = glGetUniformLocation( glProgram, "modelViewMatrix" )
glDiffuseUniform = glGetUniformLocation( glProgram, "diffuse" )
glOpacityUniform = glGetUniformLocation( glProgram, "opacity" )
glUniformMatrix4fv( glProjectionMatrixUniform, 1, False, projectionMatrix )
glUniformMatrix4fv( glModelViewMatrixUniform, 1, False, modelViewMatrix )
glUniform3f( glDiffuseUniform, diffuse[ 0 ], diffuse[ 1 ], diffuse[ 2 ] )
glUniform1f( glOpacityUniform, opacity )
glPositionAttrib = glGetAttribLocation( glProgram, "position" )
glEnableVertexAttribArray( glPositionAttrib )
glVertexAttribPointer( glPositionAttrib, 3, GL_FLOAT, False, 0, None )
glClearColor( 0.0, 0.0, 0.0, 1.0 )
glClear( GL_COLOR_BUFFER_BIT )
# glDrawArrays( GL_TRIANGLES, 0, positions.size )
glDrawElements( GL_TRIANGLES, len( indices ), GL_UNSIGNED_INT, None )
pygame.display.flip()
# renderer.render( scene, camera )
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.time.wait( 10 )