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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLUT.freeglut import *

import sys 

from OpenGL.GL import shaders

import numpy as np

from OpenGL.arrays import vbo

from OpenGL.arrays import ArrayDatatype as ADT
from OpenGL.raw.GL import _types

from ctypes import c_void_p

null = c_void_p(0)


strVertexShader = """#version 330

layout(location = 0) in vec4 position;

void main()
{
	gl_Position = position;
}
"""

strFragmentShader = """
#version 330

out vec4 outputColor;

void main()
{
	outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
"""



# 3D scene
class Scene:
    
    # initialization
    def __init__(self):

        glClearColor(0.0, 0.0, 0.0, 0.0) 

        if not glUseProgram:
            print('Missing Shader Objects!')
            sys.exit(1)

        # create shader
        vertex = shaders.compileShader(strVertexShader,GL_VERTEX_SHADER)        
        
        fragment = shaders.compileShader(strFragmentShader,GL_FRAGMENT_SHADER)
        
        try:
            self.program = shaders.compileProgram(vertex,fragment)
        except RuntimeError as err:
            print(err)         
        

        glUseProgram(self.program)

        self.vertexPositions = np.array([
            0.25, 0.25, 0.0, 1.0,
            0.25, -0.25, 0.0, 1.0,
            -0.25, -0.25, 0.0, 1.0,]
               ,'f') 
                            
        self.initializeVertexBuffer()    
        
        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)
        
    def initializeVertexBuffer(self):
    
        self.vertexBufferObject = glGenBuffers(1)
        
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBufferObject)
        
        glBufferData(GL_ARRAY_BUFFER, ADT.arrayByteCount(self.vertexPositions), ADT.voidDataPointer(self.vertexPositions), GL_STREAM_DRAW)

        glBindBuffer(GL_ARRAY_BUFFER, 0); #why???
        
    def computePositionOffsets(self):

        fLoopDuration = 5.0;
        fScale = np.pi * 2.0 / fLoopDuration;
        
        fElapsedTime = float(glutGet(GLUT_ELAPSED_TIME)) / 1000.0;

        fCurrTimeThroughLoop = np.mod(fElapsedTime, fLoopDuration);

        self.fXOffset = np.cos(fCurrTimeThroughLoop * fScale) * 0.5;
        self.fYOffset = np.sin(fCurrTimeThroughLoop * fScale) * 0.5;

    def adjustVertexData(self):

        k = 0
                
        tmpArray = self.vertexPositions.copy()
        
        while k < tmpArray.size:
                
            tmpArray[k] += self.fXOffset         
            tmpArray[k+1] += self.fYOffset 
            k += 4
            
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBufferObject);

        glBufferSubData(GL_ARRAY_BUFFER, 0, ADT.arrayByteCount(tmpArray), tmpArray)
        
        glBindBuffer(GL_ARRAY_BUFFER, 0);


        
    def step(self):
        
        step = None
       
    def render(self):    
    
        """Render the geometry for the scene."""
        self.fXOffset = 0.0
        self.fYOffset = 0.0

        self.computePositionOffsets()
        self.adjustVertexData()
        
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
         
        glUseProgram(self.program);

        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBufferObject);
        glEnableVertexAttribArray(0);
              
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0 , None);
        
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glDisableVertexAttribArray(0);
        
        glUseProgram(0);
        
        glutSwapBuffers()
        glutPostRedisplay();
        

class RenderWindow:
    def __init__(self, argv):
        
        
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
        glutInitWindowSize(500, 500)
        glutInitWindowPosition(0, 0)        
        
        self.window = glutCreateWindow(b"tutorial 1")
        
        glutReshapeFunc(self.reshape)
        glutDisplayFunc(self.draw)
        
        
        glutIdleFunc(self.draw)
        glutKeyboardFunc(self.keyPressed) # Checks for key strokes
               
        # Initialize our window.
        self.scene = Scene()

        glutMainLoop()
        
    def reshape(self, width, height):
        
        if height == 0:
           height = 1
        
        self.width = width
        self.height = height
        self.aspect = width/float(height)
        
        glViewport(0, 0, self.width, self.height)
        
    def keyPressed(self, *args):
        if args[0] == '\x1b':
            sys.exit()
        else:
            print(glGetString(GL_VERSION))

            glutPostRedisplay()

    def draw(self):
        
        # render
        self.scene.render()
    
    def update(self, *args):
        self.scene.step();
    
# main() function
def main():
    print('starting...')
    prog = RenderWindow(sys.argv)

# call main
if __name__ == '__main__':
    main()

This paste never expires. View raw. Pasted through import.