class TestContext(testingcontext.getInteractive()): """Create a simple vertex shader.""" def __init__(self): super(TestContext, self).__init__() self.shader = None self.vbo = [] def OnInit(self): """Init""" vertex = shaders.compileShader( """ #version 120 void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } """, GL_VERTEX_SHADER) fragment = shaders.compileShader( """ #version 120 void main () { gl_FragColor = vec4(0, 1, 0, 1); } """, GL_FRAGMENT_SHADER) self.shader = shaders.compileProgram(vertex, fragment) self.vbo = vbo.VBO( array([ [0, 1, 0], [-1, -1, 0], [1, -1, 0], [2, -1, 0], [4, -1, 0], [4, 1, 0], [2, -1, 0], [4, 1, 0], [2, 1, 0], ], 'f') ) def Render(self, mode): """Render the geometry for the scene.""" shaders.glUseProgram(self.shader) try: self.vbo.bind() try: glEnableClientState(GL_VERTEX_ARRAY) GLVertexPointer(self.vbo) glDrawArrays(GL_TRIANGLES, 0, 9) finally: self.vbo.unbind() glDisableClientState(GL_VERTEX_ARRAY) finally: shaders.glUseProgram(0)
#! /usr/bin/env python '''=Texture Mapping (NeHe 6)= [nehe6.py-screen-0001.png Screenshot] This tutorial is based on the [http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06 NeHe6 tutorial] by Jeff Molofee and assumes that you are reading along with the tutorial, so that only changes from the tutorial are noted here. ''' from OpenGLContext import testingcontext BaseContext = testingcontext.getInteractive() from OpenGL.GL import * import time try: from PIL.Image import open except ImportError as err: from Image import open class TestContext(BaseContext): """NeHe 6 Demo""" initialPosition = ( 0, 0, 0 ) # set initial camera position, tutorial does the re-positioning '''OnInit is called by the Context class after initialization of the context has completed, and before any rendering is attempted. Within this method, you'll generally perform your global setup tasks.''' def OnInit(self):
#! /usr/bin/env python '''Test of transparent, sorted IndexedFaceSet geometry ''' from OpenGLContext import testingcontext BaseContext = testingcontext.getInteractive() from OpenGL.GL import * from OpenGL.GLU import * from OpenGLContext import drawcube, context from OpenGLContext.arrays import * import string, time, StringIO from OpenGLContext.scenegraph.basenodes import * def loadData( data ): file = StringIO.StringIO( data ) points = [] indices = [] readingPoints = 1 while readingPoints: line = file.readline().strip().split() if len(line) > 1: points.append( map( float, line )) else: readingPoints = 0 readingIndices = 1 while readingIndices: line = file.readline().strip().split() if len(line) > 1: indices.extend( map( int, line )) else: readingIndices = 0
#! /usr/bin/env python '''Test of the glDrawArrays function (draws flower)''' from OpenGLContext import testingcontext BaseContext, MainFunction = testingcontext.getInteractive() from OpenGL.GL import * from OpenGLContext.arrays import array import string from OpenGLContext.tests import flower_geometry class TestContext(BaseContext): def OnInit(self): """Initialisation""" print """Should see flower pattern in gray over white background""" def Render(self, mode=0): BaseContext.Render(self, mode) glVertexPointerd(flower_geometry.points_expanded) glNormalPointerf(flower_geometry.normals_expanded) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_NORMAL_ARRAY) glDrawArrays(GL_TRIANGLES, 0, len(flower_geometry.points_expanded)) if __name__ == "__main__": MainFunction(TestContext)
#! /usr/bin/env python '''Test of the glDrawArrays function (draws flower)''' from OpenGLContext import testingcontext BaseContext, MainFunction = testingcontext.getInteractive() from OpenGL.GL import * from OpenGLContext.arrays import array import string from OpenGLContext.tests import flower_geometry class TestContext( BaseContext): def OnInit( self ): """Initialisation""" print """Should see flower pattern in gray over white background""" def Render( self, mode = 0): BaseContext.Render( self, mode ) glVertexPointerd(flower_geometry.points_expanded ) glNormalPointerf(flower_geometry.normals_expanded ) glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glDrawArrays(GL_TRIANGLES, 0, len(flower_geometry.points_expanded)) if __name__ == "__main__": MainFunction ( TestContext)
#! /usr/bin/env python import numpy as np import sys # Get OpenGL try: from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * from OpenGL.arrays import vbo from OpenGL.GL import shaders except TypeError as te: print('Encountered a TypeError, PyOpenGL is outdated.') print('pip is known to install outdated PyOpenGL, this is a bug.') print('You can get a newer version by going to the link below') print('https://pypi.python.org/pypi/PyOpenGL/3.1.1a1') print('(this information current as of June 2016)') raise te try: import OpenGLContext except: print('The package OpenGLContext is missing.') print('It can be installed via') print('sudo pip install PyDispatcher PyVRML97 OpenGLContext') from OpenGLContext import testingcontext BaseContext = testingcontext.getInteractive()
import OpenGL.GL as GL import OpenGL.GL.shaders as GLshaders import OpenGL.arrays.vbo as GLvbo import numpy as np import OpenGLContext.testingcontext as GLCtc BaseContext = GLCtc.getInteractive() class TestContext(BaseContext): def OnInit(self): VERTEX_SHADER = GLshaders.compileShader("""#version 120 void main(){ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }""", GL.GL_VERTEX_SHADER) #Defining the vertex shader FRAGMENT_SHADER = GLshaders.compileShader("""#version 120 void main(){ gl_FragColor = vec4(0.2,1,0.5,1); }""", GL.GL_FRAGMENT_SHADER) #Defining the fragment shader myTriangle = np.array([ [ 0, 2, 0], [-1, 0, 0], [ 1, 0, 0] ], dtype='f') self.myShader = GLshaders.compileProgram(VERTEX_SHADER,FRAGMENT_SHADER) self.vbo = GLvbo.VBO(myTriangle)