def __init__(self, src, actor=None): Component.__init__(self, actor) # TODO Include a mesh name (e.g. 'Dragon') as ID as well as src (e.g. '../res/models/Dragon.obj') self.src = src self.filepath = Context.getInstance().getResourcePath('models', src) # OpenGL version-dependent code (NOTE assumes major version = 3) self.vao = None if Context.getInstance().GL_version_minor > 0: # 3.1 (or greater?) self.vao = glGenVertexArrays(1) else: # 3.0 (or less?) self.vao = GLuint(0) glGenVertexArrays(1, self.vao) glBindVertexArray(self.vao) self.loadModel(self.filepath) self.vbo = VBO(self.meshData, GL_STATIC_DRAW) self.vbo.bind() glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * 4, self.vbo + 0) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * 4, self.vbo + 12) self.ebo = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ebo) glBufferData(GL_ELEMENT_ARRAY_BUFFER, len(self.elements) * 4, self.elements, GL_STATIC_DRAW)
def __init__(self, src, actor=None): Component.__init__(self, actor) # TODO Include a mesh name (e.g. 'Dragon') as ID as well as src (e.g. '../res/models/Dragon.obj') self.src = src self.filepath = Context.getInstance().getResourcePath('models', src) # OpenGL version-dependent code (NOTE assumes major version = 3) self.vao = None if Context.getInstance().GL_version_minor > 0: # 3.1 (or greater?) self.vao = glGenVertexArrays(1) else: # 3.0 (or less?) self.vao = GLuint(0) glGenVertexArrays(1, self.vao) glBindVertexArray(self.vao) self.loadModel(self.filepath) self.vbo = VBO(self.meshData, GL_STATIC_DRAW) self.vbo.bind() glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*4, self.vbo+0) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*4, self.vbo+12) self.ebo = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ebo) glBufferData(GL_ELEMENT_ARRAY_BUFFER, len(self.elements)*4, self.elements, GL_STATIC_DRAW)
def glGenVertexArray(): ''' Return the integer ID of the created vertex object array We always create one ID - we're not as flexible as the underlying OpenGL function which could create an array of several of them. ''' vao_id = GL.GLuint(0) vertex_array_object.glGenVertexArrays(1, vao_id) return vao_id.value
def __init__(self, stack, spacing): self.active = True self.stack_object = StackObject(stack, spacing) shape = self.stack_object.shape self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) tl = np.array((shape[2]*spacing[0], # x shape[1]*spacing[1], # y shape[0]*spacing[2])) # z # Vertex buffer: corners of cube. # x, y, z, texture_x, texture_y, texture_z vb = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # Corner 0. [tl[0], 0.0, 0.0, 1.0, 0.0, 0.0], [0.0, tl[1], 0.0, 0.0, 1.0, 0.0], [tl[0], tl[1], 0.0, 1.0, 1.0, 0.0], [0.0, 0.0, tl[2], 0.0, 0.0, 1.0], [tl[0], 0.0, tl[2], 1.0, 0.0, 1.0], [0.0, tl[1], tl[2], 0.0, 1.0, 1.0], [tl[0], tl[1], tl[2], 1.0, 1.0, 1.0]] # Corner 7. vb = np.array(vb, dtype=np.float32) vb = vb.flatten() # Triangles of cube. idx_out = np.array([[0, 2, 1], [2, 3, 1], # Triangle 0, triangle 1. [1, 4, 0], [1, 5, 4], [3, 5, 1], [3, 7, 5], [2, 7, 3], [2, 6, 7], [0, 6, 2], [0, 4, 6], [5, 6, 4], [5, 7, 6]], # Triangle 10, triangle 11. dtype=np.uint32) self.vtVBO = VBO(vb) self.vtVBO.bind() sc = 1.0/la.norm(tl) c = 0.5*tl self.transform = np.array(((sc, 0.0, 0.0, -sc*c[0]), (0.0, sc, 0.0, -sc*c[1]), (0.0, 0.0, sc, -sc*c[2]), (0.0, 0.0, 0.0, 1.0))) self.tex_transform = np.array(((1.0/tl[0], 0.0, 0.0, 0.0), (0.0, 1.0/tl[1], 0.0, 0.0), (0.0, 0.0, 1.0/tl[2], 0.0), (0.0, 0.0, 0.0, 1.0))) glBindVertexArray(0) self.elVBO = VBO(idx_out, target=GL_ELEMENT_ARRAY_BUFFER) self.elCount = len(idx_out.flatten())
def __init__(self): glfw.Init() #Init glfw glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3) #set openGL context version to 3.3 (the latest we can support) glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE) #tell OSX we don't give a shit about compatibility and want the newer GLSL versions glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) self.width, self.height = 1440, 900 self.aspect = self.width/float(self.height) self.win = glfw.CreateWindow(self.width, self.height, "test") #self.win = glfw.CreateWindow(self.width, self.height, "test", glfw.GetPrimaryMonitor(), None) self.exitNow = False glfw.MakeContextCurrent(self.win) glViewport(0, 0, self.width, self.height) glEnable(GL_DEPTH_TEST) glClearColor(0.5, 0.5, 0.5,1.0) #glfw.SetMouseButtonCallback(self.win, self.onMouseButton) glfw.SetKeyCallback(self.win, self.keyPressed) #glfw.SetWindowSizeCallback(self.win, self.onSize) self.objs = [] self.callbacks = {'keyPressed':[],'keyReleased':[],'logic':[]} self.objs.append(Box(0,0)) player = Player() self.objs.append(player) self.callbacks['keyPressed'].append(player.getCallback('keyPressed')) self.callbacks['keyReleased'].append(player.getCallback('keyReleased')) self.callbacks['logic'].append(player.getCallback('logic')) #self.callbacks['keyReleased'].append(player.getCallbacks(['keyReleased'])) for obj in self.objs: obj.geomArray = numpy.array(obj.geom, numpy.float32) obj.vao = vertex_array_object.glGenVertexArrays(1) glBindVertexArray(obj.vao) obj.vBuff = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, obj.vBuff) glBufferData(GL_ARRAY_BUFFER, 4*len(obj.geom), obj.geomArray, GL_DYNAMIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,None) #set the size & type of the argument to the shader #compile shaders obj.vertShader = shaders.compileShader(obj.vertShaderString,GL_VERTEX_SHADER) obj.fragShader = shaders.compileShader(obj.fragShaderString,GL_FRAGMENT_SHADER) try: obj.shader = shaders.compileProgram(obj.vertShader,obj.fragShader) except (GLError, RuntimeError) as err: print err glBindVertexArray(0) #unbind our VAO self.colorOffset = 255*3 self.OnInit()
def __init__(self, fn, spacing): self.active = True m = GLMesh() self.mesh = m sc = m.load_ply(fn) v_out, n_out, col_out, idx_out = m.generate_arrays() vb = np.concatenate((v_out, n_out, col_out), axis=1) self.elVBO = VBO(idx_out, target=GL_ELEMENT_ARRAY_BUFFER) self.elCount = len(idx_out.flatten()) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) self.vtVBO = VBO(vb) self.vtVBO.bind() glBindVertexArray(0) c = np.array((0, 0, 0)) self.transform = np.array(((sc, 0.0, 0.0, -sc*c[0]), (0.0, sc, 0.0, -sc*c[1]), (0.0, 0.0, sc, -sc*c[2]), (0.0, 0.0, 0.0, 1.0)))
def glGenVertexArray(): vao_id = GLuint(0) vertex_array_object.glGenVertexArrays(1, vao_id) return vao_id.value
def __init__(self): glfw.Init() #Init glfw glfw.WindowHint( glfw.CONTEXT_VERSION_MAJOR, 3) #set openGL context version to 3.3 (the latest we can support) glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.WindowHint( glfw.OPENGL_FORWARD_COMPAT, GL_TRUE ) #tell OSX we don't give a shit about compatibility and want the newer GLSL versions glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) self.width, self.height = 1440, 900 self.aspect = self.width / float(self.height) self.win = glfw.CreateWindow(self.width, self.height, "test") #self.win = glfw.CreateWindow(self.width, self.height, "test", glfw.GetPrimaryMonitor(), None) self.exitNow = False glfw.MakeContextCurrent(self.win) glViewport(0, 0, self.width, self.height) glEnable(GL_DEPTH_TEST) glClearColor(0.5, 0.5, 0.5, 1.0) #glfw.SetMouseButtonCallback(self.win, self.onMouseButton) glfw.SetKeyCallback(self.win, self.keyPressed) #glfw.SetWindowSizeCallback(self.win, self.onSize) self.objs = [] self.callbacks = {'keyPressed': [], 'keyReleased': [], 'logic': []} self.objs.append(Box(0, 0)) player = Player() self.objs.append(player) self.callbacks['keyPressed'].append(player.getCallback('keyPressed')) self.callbacks['keyReleased'].append(player.getCallback('keyReleased')) self.callbacks['logic'].append(player.getCallback('logic')) #self.callbacks['keyReleased'].append(player.getCallbacks(['keyReleased'])) for obj in self.objs: obj.geomArray = numpy.array(obj.geom, numpy.float32) obj.vao = vertex_array_object.glGenVertexArrays(1) glBindVertexArray(obj.vao) obj.vBuff = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, obj.vBuff) glBufferData(GL_ARRAY_BUFFER, 4 * len(obj.geom), obj.geomArray, GL_DYNAMIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, None) #set the size & type of the argument to the shader #compile shaders obj.vertShader = shaders.compileShader(obj.vertShaderString, GL_VERTEX_SHADER) obj.fragShader = shaders.compileShader(obj.fragShaderString, GL_FRAGMENT_SHADER) try: obj.shader = shaders.compileProgram(obj.vertShader, obj.fragShader) except (GLError, RuntimeError) as err: print err glBindVertexArray(0) #unbind our VAO self.colorOffset = 255 * 3 self.OnInit()