def clear(self): if not self.colormapinited: raise CallError if gl.getdisplaymode() in (GET.DMRGB, GET.DMRGBDOUBLE): gl.RGBcolor(200, 200, 200) # XXX rather light grey gl.clear() return gl.writemask(0xffffffff) gl.clear()
def reset(self): if self.wid > 0: gl.winset(self.wid) gl.clear() self.vout.initcolormap() self.queue = [] self.spares = [] self.written = 0 self.lastt = 0
def clearto(self, r, g, b): if not self.colormapinited: raise CallError if gl.getdisplaymode() in (GET.DMRGB, GET.DMRGBDOUBLE): gl.RGBcolor(r, g, b) gl.clear() return index = self.color0[0] self.fixcolor0 = 1 gl.mapcolor(index, r, g, b) gl.writemask(0xffffffff) gl.clear() gl.gflush()
def show(self): if self.wid < 0: gl.foreground() gl.prefsize(self.vout.width, self.vout.height) self.wid = gl.winopen(self.title) gl.clear() self.vout.initcolormap() gl.winset(self.wid) if not self.queue: self.vout.clear() return dt, d, cd = self.queue[-1] self.vout.showframe(d, cd)
def show(self): if self.wid < 0: gl.foreground() gl.prefsize(self.vin.width, self.vin.height) self.wid = gl.winopen(self.title) gl.clear() self.vin.initcolormap() gl.winset(self.wid) if self.qindex >= self.qsize: self.vin.clear() return dt, d, cd = self.vin.getrandomframe(self.qindex) self.vin.showframe(d, cd)
def main(): gl.foreground() gl.prefposition(500, 900, 500, 900) w = gl.winopen('CrissCross') gl.ortho2(0.0, 400.0, 0.0, 400.0) gl.color(GL.WHITE) gl.clear() gl.color(GL.RED) gl.bgnline() gl.v2f(0.0, 0.0) gl.v2f(400.0, 400.0) gl.endline() gl.bgnline() gl.v2f(400.0, 0.0) gl.v2f(0.0, 400.0) gl.endline() time.sleep(5)
def gltest(): import gl, fm gl.foreground() W, H = 1000, 800 gl.prefsize(W, H) wid = gl.winopen('gltest') gl.ortho2(0, W, H, 0) gl.color(7) gl.clear() gl.color(0) fp = openfile() TSTART() fmt = GLFormatter().init(5, 0, W) feedfile(fp, fmt) fmt.flush() TSTOP() import time time.sleep(5)
def initcolormap(self): self.colormapinited = 1 self.color0 = None self.fixcolor0 = 0 if self.format in ('rgb', 'jpeg', 'compress'): self.set_rgbmode() gl.RGBcolor(200, 200, 200) # XXX rather light grey gl.clear() return # This only works on an Entry-level Indigo from IRIX 4.0.5 if self.format == 'rgb8' and is_entry_indigo() and \ gl.gversion() == 'GL4DLG-4.0.': # Note trailing '.'! self.set_rgbmode() gl.RGBcolor(200, 200, 200) # XXX rather light grey gl.clear() gl.pixmode(GL.PM_SIZE, 8) return self.set_cmode() self.skipchrom = 0 if self.offset == 0: self.mask = 0x7ff else: self.mask = 0xfff if not self.quiet: sys.stderr.write('Initializing color map...') self._initcmap() gl.clear() if not self.quiet: sys.stderr.write(' Done.\n')
def testGL(): import gl, GL, fmt if sys.argv[1:]: file = sys.argv[1] else: file = 'test.html' data = open(file, 'r').read() W, H = 600, 600 gl.foreground() gl.prefsize(W, H) wid = gl.winopen('testGL') gl.ortho2(0, W, H, 0) gl.color(GL.WHITE) gl.clear() gl.color(GL.BLACK) b = fmt.GLBackEnd(wid) f = fmt.BaseFormatter(b.d, b) p = FormattingParser(f, GLStylesheet) p.feed(data) p.close() b.finish() # import time time.sleep(5)
#! /usr/bin/env python
def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(800, 600, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() window.makeContextCurrent() window.setKeyCallback(key_callback) err = gl.getError() if err: print("WINDOW OPEN ERROR:", err) shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE) shaderProgram.use() vao = gl.genVertexArrays(1)[0] vbo, ebo = gl.genBuffers(2) # 1. Bind Vertex Array Object gl.bindVertexArray(vao) # 2. Copy our vertices array in a buffer for OpenGL to use gl.bindBuffer(gl.ARRAY_BUFFER, vbo) gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) # 2. Copy our index array in a buffer for OpenGL to use gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo) gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # Color attribute gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset) gl.enableVertexAttribArray(1) # TexCoord attribute gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.texcoord.offset) gl.enableVertexAttribArray(2) # 4. unbind the vbo gl.bindBuffer(gl.ARRAY_BUFFER, 0) # 4. Unbind the VAO gl.bindVertexArray(0) # create the texture object, load the texture # Load and create a texture texture1, texture2 = gl.genTextures(2) # Texture 1 load_texture(texture1, 'learningopengl/resources/textures/container.jpg') # Texture 2 load_texture(texture2, 'learningopengl/resources/textures/awesomeface.png') while not window.shouldClose(): glfw.pollEvents() framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT) shaderProgram.use() t = time.perf_counter() translation = Mat4.Translation(Vec3(0.5, -0.5, 0.0)) rotation = Mat4.Rotation(t, Vec3(0.0, 0.0, 1.0)) trans = translation * rotation shaderProgram.uniforms.transform = trans shaderProgram.uniforms.ourTexture1 = texture1 shaderProgram.uniforms.ourTexture2 = texture2 gl.bindVertexArray(vao) gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) # gl.drawArrays(gl.TRIANGLES, 0, 3); gl.bindVertexArray(0) window.swapBuffers() gl.deleteVertexArrays([vao]) gl.deleteBuffers([vbo, ebo])
def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(WIDTH, HEIGHT, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() inputMap = InputMap() window.makeContextCurrent() window.setKeyCallback(inputMap.key_callback) window.setCursorPosCallback(inputMap.mouse_callback) # window.setScrollCallback(inputMap.scroll_callback) window.setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED) err = gl.getError() if err: print("WINDOW OPEN ERROR:", err) camera = Camera(Vec3(0.0, 0.0, 3.0)) lastX = 400 lastY = 300 firstMouse = True deltaTime = 0.0 lastFrame = 0.0 gl.enable(gl.DEPTH_TEST) shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE) shaderProgram.use() obj_model = generate_model(load_obj_file(io.StringIO(CUBE_OBJ), "")) vao, vbo, ebo = load_model_data(obj_model) # create the texture object, load the texture # Load and create a texture texture1, texture2 = gl.genTextures(2) # Texture 1 load_texture(texture1, 'learningopengl/resources/textures/container.jpg') # Texture 2 load_texture(texture2, 'learningopengl/resources/textures/awesomeface.png') while not window.shouldClose(): inputMap.begin_frame() glfw.pollEvents() input = inputMap.get_input() camera.processInput(input, 1.0/30.0) # TODO: see http://www.glfw.org/docs/latest/window.html#window_fbsize framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT) shaderProgram.use() shaderProgram.uniforms.ourTexture1 = texture1 shaderProgram.uniforms.ourTexture2 = texture2 # create transformations # view = Mat4.Translation(Vec3(0.0, 0.0, -3.0)) view = camera.getViewMatrix() projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0) # assign the transformations shaderProgram.uniforms.view = view shaderProgram.uniforms.projection = projection gl.bindVertexArray(vao) gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo) for i, position in enumerate(cubePositions): # Calculate the model matrix for each object and pass it to shader before drawing model = Mat4.Translation(position) angle = 20.0 * i; model = model * Mat4.Rotation(angle, Vec3(1.0, 0.3, 0.5)) shaderProgram.uniforms.model = model dc = obj_model['meshes'][0]['draw_call'] gl.drawElements(gl.TRIANGLES, dc['count'], gl.UNSIGNED_INT, dc['start'] * obj_model['vertex_buffer']['stride']) gl.bindVertexArray(0) window.swapBuffers() shaderProgram.delete() gl.deleteVertexArrays([vao]) gl.deleteBuffers([vbo, ebo])
#! /usr/bin/env python # Play CMIF movie files # Help function def help(): print 'Usage: Vplay [options] [file] ...' print print 'Options:' print '-M magnify : magnify the image by the given factor' print '-d : write some debug stuff on stderr' print '-l : loop, playing the movie over and over again' print '-m delta : drop frames closer than delta seconds (default 0.)' print '-n : don\'t wait after each file' print '-q : quiet, no informative messages' print '-r delta : regenerate input time base delta seconds apart' print '-s speed : speed change factor (default 1.0)' print '-t : use a 2nd thread for read-ahead' print '-x left : window offset from left of screen' print '-y top : window offset from top of screen' print '-w width : window width' print '-h height : window height' print '-b color : background color (white,black or (r,g,b))' print 'file ... : file(s) to play; default film.video' print print 'User interface:' print 'Press the left mouse button to stop or restart the movie.' print 'Press ESC or use the window manager Close or Quit command' print 'to close the window and play the next file (if any).' # Imported modules
# Classes to read and write CMIF video files.
def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(800, 600, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() window.makeContextCurrent() window.setKeyCallback(key_callback) err = gl.getError() if err: print("WINDOW OPEN ERROR:", err) shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE) shaderProgram.use() vao = gl.genVertexArrays(1)[0] vbo, ebo = gl.genBuffers(2) # 1. Bind Vertex Array Object gl.bindVertexArray(vao) # 2. Copy our vertices array in a buffer for OpenGL to use gl.bindBuffer(gl.ARRAY_BUFFER, vbo) gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) # 2. Copy our index array in a buffer for OpenGL to use gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo) gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # Color attribute gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset) gl.enableVertexAttribArray(1) # TexCoord attribute gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.texcoord.offset) gl.enableVertexAttribArray(2) # 4. unbind the vbo gl.bindBuffer(gl.ARRAY_BUFFER, 0) # 4. Unbind the VAO gl.bindVertexArray(0) # create the texture object, load the texture # Load and create a texture texture1, texture2 = gl.genTextures(2) # ==================== # Texture 1 # ==================== gl.bindTexture(gl.TEXTURE_2D, texture1) # All upcoming GL_TEXTURE_2D operations now have effect on our texture object # Set our texture parameters gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) # Set texture wrapping to GL_REPEAT gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) # Set texture filtering gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) # Load, create texture and generate mipmaps img = PIL.Image.open('learningopengl/resources/textures/container.jpg') img = PIL.ImageOps.flip(img) width, height = img.size data = img.convert("RGBA").tobytes("raw", "RGBA") # unsigned char* image = SOIL_load_image(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, 0, SOIL_LOAD_RGB); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data) gl.generateMipmap(gl.TEXTURE_2D) gl.bindTexture(gl.TEXTURE_2D, 0) # Unbind texture when done, so we won't accidentily mess up our texture. # =================== # Texture 2 # =================== gl.bindTexture(gl.TEXTURE_2D, texture2) # Set our texture parameters gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) # Set texture filtering gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) # Load, create texture and generate mipmaps img = PIL.Image.open('learningopengl/resources/textures/awesomeface.png') img = PIL.ImageOps.flip(img) width, height = img.size data = img.convert("RGBA").tobytes("raw", "RGBA") gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data) gl.generateMipmap(gl.TEXTURE_2D) # SOIL_free_image_data(image); gl.bindTexture(gl.TEXTURE_2D, 0); while not window.shouldClose(): glfw.pollEvents() framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT) shaderProgram.use() # Bind Textures using texture units gl.activeTexture(gl.TEXTURE0) gl.bindTexture(gl.TEXTURE_2D, texture1); gl.uniform1i(shaderProgram.getUniformLocation("ourTexture1"), 0); gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, texture2); gl.uniform1i(shaderProgram.getUniformLocation("ourTexture2"), 1); gl.bindVertexArray(vao) gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) # gl.drawArrays(gl.TRIANGLES, 0, 3); gl.bindVertexArray(0) window.swapBuffers() gl.deleteVertexArrays([vao]) gl.deleteBuffers([vbo, ebo])
def getevent(): while 1: # # Get next event from the processed queue, if any # if G.queue: event = G.queue[0] del G.queue[0] # print 'getevent from queue -->', event return event # # Get next event from the draw queue, if any, # but only if there is nothing in the system queue. # if G.drawqueue and not gl.qtest(): win = G.drawqueue[0] del G.drawqueue[0] gl.winset(win._gid) gl.color(win._bg) gl.clear() event = WE_DRAW, win, win._area # print 'getevent from drawqueue -->', event return event # # Get next event from system queue, blocking if necessary # until one is available. # Some cases immediately return the event, others do nothing # or append one or more events to the processed queue. # dev, val = gl.qread() # if dev == REDRAW: win = G.windowmap[` val `] old_area = win._area win._fixviewport() win._needredraw() if old_area <> win._area: # print 'getevent --> WE_SIZE' return WE_SIZE, win, None elif dev == KEYBD: if val == 3: raise KeyboardInterrupt # Control-C in window character = chr(val) if commands.has_key(character): return WE_COMMAND, G.focus, commands[character] return WE_CHAR, G.focus, character elif dev == LEFTARROWKEY: if val: return WE_COMMAND, G.focus, WC_LEFT elif dev == RIGHTARROWKEY: if val: return WE_COMMAND, G.focus, WC_RIGHT elif dev == UPARROWKEY: if val: return WE_COMMAND, G.focus, WC_UP elif dev == DOWNARROWKEY: if val: return WE_COMMAND, G.focus, WC_DOWN elif dev in (LEFTALTKEY, RIGHTALTKEY): if val: for code in codelist: gl.qdevice(code) else: for code in codelist: gl.unqdevice(code) elif dev in codelist: if val: event = G.focus._doshortcut(code2key[` dev `]) if event: return event elif dev == LEFTMOUSE: G.mousex = gl.getvaluator(MOUSEX) G.mousey = gl.getvaluator(MOUSEY) if val: type = WE_MOUSE_DOWN gl.qdevice(MOUSEX) gl.qdevice(MOUSEY) else: type = WE_MOUSE_UP gl.unqdevice(MOUSEX) gl.unqdevice(MOUSEY) return _mouseevent(type) elif dev == MOUSEX: G.mousex = val return _mouseevent(WE_MOUSE_MOVE) elif dev == MOUSEY: G.mousey = val return _mouseevent(WE_MOUSE_MOVE) elif dev == RIGHTMOUSE: # Menu button press/release if val: # Press event = G.focus._domenu() if event: return event elif dev == INPUTCHANGE: if G.focus: G.queue.append(WE_DEACTIVATE, G.focus, None) G.focus = G.windowmap[` val `] if G.focus: G.queue.append(WE_ACTIVATE, G.focus, None) elif dev in (WINSHUT, WINQUIT): return WE_CLOSE, G.windowmap[` val `], None else: print "*** qread() --> dev:", dev, "val:", val
def main(): # insure that we at least have an X display before continuing. import os try: display = os.environ["DISPLAY"] except: raise TestSkipped, "No $DISPLAY -- skipping gl test" # touch all the attributes of gl without doing anything if verbose: print "Touching gl module attributes..." for attr in glattrs: if verbose: print "touching: ", attr getattr(gl, attr) # create a small 'Crisscross' window if verbose: print 'Creating a small "CrissCross" window...' print "foreground" gl.foreground() if verbose: print "prefposition" gl.prefposition(500, 900, 500, 900) if verbose: print 'winopen "CrissCross"' w = gl.winopen("CrissCross") if verbose: print "clear" gl.clear() if verbose: print "ortho2" gl.ortho2(0.0, 400.0, 0.0, 400.0) if verbose: print "color WHITE" gl.color(GL.WHITE) if verbose: print "color RED" gl.color(GL.RED) if verbose: print "bgnline" gl.bgnline() if verbose: print "v2f" gl.v2f(0.0, 0.0) gl.v2f(400.0, 400.0) if verbose: print "endline" gl.endline() if verbose: print "bgnline" gl.bgnline() if verbose: print "v2i" gl.v2i(400, 0) gl.v2i(0, 400) if verbose: print "endline" gl.endline() if verbose: print "Displaying window for 2 seconds..." time.sleep(2) if verbose: print "winclose" gl.winclose(w)
def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(WIDTH, HEIGHT, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() inputMap = InputMap() window.makeContextCurrent() window.setKeyCallback(inputMap.key_callback) window.setCursorPosCallback(inputMap.mouse_callback) #window.setScrollCallback(inputMap.scroll_callback) window.setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED) err = gl.getError() if err: print("WINDOW OPEN ERROR:", err) camera = Camera(position=Vec3(0.0, 0.0, 3.0)) lastX = 400 lastY = 300 firstMouse = True deltaTime = 0.0 lastFrame = 0.0 gl.enable(gl.DEPTH_TEST) lampShaderProgram = Shader(LAMP_VERTEX_SHADER_SOURCE, LAMP_FRAGMENT_SHADER_SOURCE) colorShaderProgram = Shader(COLORS_VERTEX_SHADER_SOURCE, COLORS_FRAGMENT_SHADER_SOURCE) colorShaderProgram.use() lightVAO, containerVAO = gl.genVertexArrays(2) vbo, ebo = gl.genBuffers(2) # 1. Bind Vertex Array Object gl.bindVertexArray(containerVAO) # 2. Copy our vertices array in a buffer for OpenGL to use gl.bindBuffer(gl.ARRAY_BUFFER, vbo) gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # 4. Unbind the VAO gl.bindVertexArray(0) # 1. Bind Vertex Array Object gl.bindVertexArray(lightVAO) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # 4. Unbind the VAO gl.bindVertexArray(0) # 4. unbind the vbo gl.bindBuffer(gl.ARRAY_BUFFER, 0) # create the texture object, load the texture # Load and create a texture # texture1, texture2 = gl.genTextures(2) # Texture 1 # load_texture(texture1, 'learningopengl/resources/textures/container.jpg') # Texture 2 # load_texture(texture2, 'learningopengl/resources/textures/awesomeface.png') # window.setFrameBufferSizeCallback(lambda: ) # glfwSetFramebufferSizeCallback(window, framebuffer_size_callback) sb = True while not window.shouldClose(): inputMap.begin_frame() glfw.pollEvents() input = inputMap.get_input() camera.processInput(input, 1.0/30.0) framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT) colorShaderProgram.use() colorShaderProgram.uniforms.objectColor = Vec3(1.0, 0.5, 0.31) colorShaderProgram.uniforms.lightColor = Vec3(1.0, 0.5, 1.0) # create transformations # view = Mat4.Translation(Vec3(0.0, 0.0, -3.0)) view = camera.getViewMatrix() projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0) # assign the transformations colorShaderProgram.uniforms.view = view colorShaderProgram.uniforms.projection = projection # draw the container gl.bindVertexArray(containerVAO) model = Mat4.Identity() colorShaderProgram.uniforms.model = model gl.drawArrays(gl.TRIANGLES, 0, 36) gl.bindVertexArray(0) # draw the lamp lampShaderProgram.use() lampShaderProgram.uniforms.view = view lampShaderProgram.uniforms.projection = projection lightPos = Vec3(1.2, 1.0, 2.0) model = Mat4.Translation(lightPos) * Mat4.Scale(0.2) lampShaderProgram.uniforms.model = model gl.bindVertexArray(lightVAO) gl.drawArrays(gl.TRIANGLES, 0, 36) gl.bindVertexArray(0) window.swapBuffers() lampShaderProgram.delete() colorShaderProgram.delete() gl.deleteVertexArrays([containerVAO, lightVAO ]) gl.deleteBuffers([vbo, ebo])
import gl, GL
#! /usr/bin/env python # Play synchronous video and audio. # Highly experimental! import sys import getopt import string import os import VFile import aifc import gl, GL, DEVICE import al, AL def usage(): sys.stderr.write( \ 'usage: aplay [-o offset] [-q qsize] videofile audiofile\n') sys.exit(2) def main(): offset = 0 qsize = 0 # This defaults to 1/10 second of sound videofile = 'film.video' audiofile = 'film.aiff' try: opts, args = getopt.getopt(sys.argv[1:], 'o:q:') except getopt.error, msg: sys.stderr.write(msg + '\n') usage() try: for o, a in opts: if o == '-o': offset = string.atoi(a)
import sys
def reset(self): if self.wid > 0: gl.winset(self.wid) gl.clear() self.vin.initcolormap() self.qindex = 0
def getevent(): while 1: # # Get next event from the processed queue, if any # if G.queue: event = G.queue[0] del G.queue[0] #print 'getevent from queue -->', event return event # # Get next event from the draw queue, if any, # but only if there is nothing in the system queue. # if G.drawqueue and not gl.qtest(): win = G.drawqueue[0] del G.drawqueue[0] gl.winset(win._gid) gl.color(win._bg) gl.clear() event = WE_DRAW, win, win._area #print 'getevent from drawqueue -->', event return event # # Get next event from system queue, blocking if necessary # until one is available. # Some cases immediately return the event, others do nothing # or append one or more events to the processed queue. # dev, val = gl.qread() # if dev == REDRAW: win = G.windowmap[ ` val `] old_area = win._area win._fixviewport() win._needredraw() if old_area <> win._area: #print 'getevent --> WE_SIZE' return WE_SIZE, win, None elif dev == KEYBD: if val == 3: raise KeyboardInterrupt # Control-C in window character = chr(val) if commands.has_key(character): return WE_COMMAND, G.focus, commands[character] return WE_CHAR, G.focus, character elif dev == LEFTARROWKEY: if val: return WE_COMMAND, G.focus, WC_LEFT elif dev == RIGHTARROWKEY: if val: return WE_COMMAND, G.focus, WC_RIGHT elif dev == UPARROWKEY: if val: return WE_COMMAND, G.focus, WC_UP elif dev == DOWNARROWKEY: if val: return WE_COMMAND, G.focus, WC_DOWN elif dev in (LEFTALTKEY, RIGHTALTKEY): if val: for code in codelist: gl.qdevice(code) else: for code in codelist: gl.unqdevice(code) elif dev in codelist: if val: event = G.focus._doshortcut(code2key[ ` dev `]) if event: return event elif dev == LEFTMOUSE: G.mousex = gl.getvaluator(MOUSEX) G.mousey = gl.getvaluator(MOUSEY) if val: type = WE_MOUSE_DOWN gl.qdevice(MOUSEX) gl.qdevice(MOUSEY) else: type = WE_MOUSE_UP gl.unqdevice(MOUSEX) gl.unqdevice(MOUSEY) return _mouseevent(type) elif dev == MOUSEX: G.mousex = val return _mouseevent(WE_MOUSE_MOVE) elif dev == MOUSEY: G.mousey = val return _mouseevent(WE_MOUSE_MOVE) elif dev == RIGHTMOUSE: # Menu button press/release if val: # Press event = G.focus._domenu() if event: return event elif dev == INPUTCHANGE: if G.focus: G.queue.append(WE_DEACTIVATE, G.focus, None) G.focus = G.windowmap[ ` val `] if G.focus: G.queue.append(WE_ACTIVATE, G.focus, None) elif dev in (WINSHUT, WINQUIT): return WE_CLOSE, G.windowmap[ ` val `], None else: print '*** qread() --> dev:', dev, 'val:', val
def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(800, 600, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() window.makeContextCurrent() window.setKeyCallback(key_callback) err = gl.getError() if err: print("WINDOW OPEN ERROR:", err) shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE) shaderProgram.use() vao = gl.genVertexArrays(1)[0] vbo, ebo = gl.genBuffers(2) print(vao, vbo, ebo) # 1. Bind Vertex Array Object gl.bindVertexArray(vao) # 2. Copy our vertices array in a buffer for OpenGL to use gl.bindBuffer(gl.ARRAY_BUFFER, vbo) gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) # 2. Copy our index array in a buffer for OpenGL to use # gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo) # gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # Color attribute gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset) gl.enableVertexAttribArray(1) # 4. unbind the vbo gl.bindBuffer(gl.ARRAY_BUFFER, 0) # 4. Unbind the VAO gl.bindVertexArray(0) while not window.shouldClose(): glfw.pollEvents() framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT) shaderProgram.use() gl.bindVertexArray(vao) # gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) gl.drawArrays(gl.TRIANGLES, 0, 3); gl.bindVertexArray(0) window.swapBuffers() gl.deleteVertexArrays([vao]) gl.deleteBuffers([vbo, ebo])
def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(WIDTH, HEIGHT, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() window.makeContextCurrent() window.setKeyCallback(key_callback) err = gl.getError() if err: print("WINDOW OPEN ERROR:", err) gl.enable(gl.DEPTH_TEST) shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE) shaderProgram.use() vao = gl.genVertexArrays(1)[0] vbo, ebo = gl.genBuffers(2) # 1. Bind Vertex Array Object gl.bindVertexArray(vao) # 2. Copy our vertices array in a buffer for OpenGL to use gl.bindBuffer(gl.ARRAY_BUFFER, vbo) gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) # 2. Copy our index array in a buffer for OpenGL to use #gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo) #gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # Color attribute # gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset) # gl.enableVertexAttribArray(1) # TexCoord attribute gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.texcoord.offset) gl.enableVertexAttribArray(2) # 4. unbind the vbo gl.bindBuffer(gl.ARRAY_BUFFER, 0) # 4. Unbind the VAO gl.bindVertexArray(0) # create the texture object, load the texture # Load and create a texture texture1, texture2 = gl.genTextures(2) # Texture 1 load_texture(texture1, 'learningopengl/resources/textures/container.jpg') # Texture 2 load_texture(texture2, 'learningopengl/resources/textures/awesomeface.png') sb = True while not window.shouldClose(): glfw.pollEvents() framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT) shaderProgram.use() shaderProgram.uniforms.ourTexture1 = texture1 shaderProgram.uniforms.ourTexture2 = texture2 # create transformations view = Mat4.Translation(Vec3(0.0, 0.0, -3.0)) projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0) # assign the transformations shaderProgram.uniforms.view = view shaderProgram.uniforms.projection = projection gl.bindVertexArray(vao) for i, position in enumerate(cubePositions): # Calculate the model matrix for each object and pass it to shader before drawing model = Mat4.Translation(position) angle = 20.0 * i; model = model * Mat4.Rotation(angle, Vec3(1.0, 0.3, 0.5)) shaderProgram.uniforms.model = model gl.drawArrays(gl.TRIANGLES, 0, 36) gl.bindVertexArray(0) window.swapBuffers() shaderProgram.delete() gl.deleteVertexArrays([vao]) gl.deleteBuffers([vbo, ebo])
xborder = yborder = 0 if xwsiz: vin.xorigin = (xwsiz - width)/2 width = xwsiz if ywsiz: vin.yorigin = (ywsiz - height)/2 height = ywsiz if xoff <> None and yoff <> None: scrheight = gl.getgdesc(GL.GD_YPMAX) gl.prefposition(xoff, xoff+width-1, \ scrheight-yoff-height, scrheight-yoff-1) else: gl.prefsize(width, height) win = gl.winopen(filename) gl.clear() if quiet: vin.quiet = 1 vin.initcolormap() if bgcolor: r, g, b = bgcolor vin.clearto(r,g,b) gl.qdevice(ESCKEY) gl.qdevice(WINSHUT) gl.qdevice(WINQUIT) gl.qdevice(LEFTMOUSE) stop = 0
def draw(self): self.camera.setCurrent() if self.state.won: for device in self.device_bag.objs: self.pmanager.addSystem(gl.ParticleSystem(device.pos + (.1,), globjects.Firework, 10, 0)) for task in self.state.deadtasks: if not task.successful: continue if isinstance(task, model.task.CreateCogTask): self.pmanager.addSystem(gl.ParticleSystem(task.pos + (1,), globjects.SmokePuff, 5 * task.diam ** 2, task.diam * .25)) elif isinstance(task, model.task.RemoveCogTask): self.pmanager.addSystem(gl.ParticleSystem(task.cog.pos + (1,), globjects.SmokePuff, 5 * task.cog.diam ** 2, task.cog.diam * .25)) elif isinstance(task, model.task.BuildPlatformTask): self.pmanager.addSystem(gl.ParticleSystem( (task.pos[0], task.pos[1] + .4, 1.2), globjects.SmokePuff, 5, .25)) elif isinstance(task, model.task.BuildLadderTask): self.pmanager.addSystem(gl.ParticleSystem( (task.pos[0], task.pos[1], 1.2), globjects.SmokePuff, 10, .25)) self.state.deadtasks.clear() for belt in self.state.deadbelts: self.pmanager.addSystem(globjects.SnappedBelt(belt.part1.pos + (.1,), belt.part2.pos + (.1,))) self.state.deadbelts.clear() self.pmanager.tick() self.cog_bag.update(self.state.cogs) if self.state.pgrid.dirty: self.plat_bag.update(self.state.pgrid.platform_dict()) self.ladder_bag.update([x for x, y in self.state.pgrid if y[1]]) self.elf_bag.update(self.state.elves) self.belt_bag.update(self.state.belts) self.device_bag.update(self.state.devices | set([self.state.spring])) self.task_bag.update(self.state.tasks) gl.clear() for bag in [self.elf_bag, self.cog_bag, self.belt_bag, self.plat_bag, self.ladder_bag, self.device_bag, self.task_bag]: bag.render() self.boundary.render() self.cursor.render() self.pmanager.render() if not self.menu_mode: self.interface_overlay.render() self.gametext_overlay.render() if self.interface.tutorial: self.tutorial_overlay.render() if self.state.won: self.victory_overlay.render() else: self.title_overlay.render() self.menutext_overlay.render() self.state.pgrid.mark_clean() pygame.display.flip()
def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(WIDTH, HEIGHT, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() inputMap = InputMap() window.makeContextCurrent() window.setKeyCallback(inputMap.key_callback) window.setCursorPosCallback(inputMap.mouse_callback) #window.setScrollCallback(inputMap.scroll_callback) window.setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED) err = gl.getError() if err: print("WINDOW OPEN ERROR:", err) camera = Camera(position=Vec3(0.0, 0.0, 3.0)) lastX = 400 lastY = 300 firstMouse = True deltaTime = 0.0 lastFrame = 0.0 gl.enable(gl.DEPTH_TEST) lampShaderProgram = Shader(LAMP_VERTEX_SHADER_SOURCE, LAMP_FRAGMENT_SHADER_SOURCE) lightingShaderProgram = Shader(LIGHTING_VERTEX_SHADER_SOURCE, LIGHTING_FRAGMENT_SHADER_SOURCE) lightingShaderProgram.use() lightVAO, containerVAO = gl.genVertexArrays(2) vbo = gl.genBuffers(1)[0] # 1. Bind Vertex Array Object gl.bindVertexArray(containerVAO) # 2. Copy our vertices array in a buffer for OpenGL to use gl.bindBuffer(gl.ARRAY_BUFFER, vbo) gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # Normal attribute gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.normal.offset) gl.enableVertexAttribArray(1) # 4. Unbind the VAO gl.bindVertexArray(0) # 1. Bind Vertex Array Object gl.bindVertexArray(lightVAO) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # 4. Unbind the VAO gl.bindVertexArray(0) # 4. unbind the vbo gl.bindBuffer(gl.ARRAY_BUFFER, 0) lightPos = Vec3(1.2, 1.0, 2.0) lastFrame = time.perf_counter() while not window.shouldClose(): inputMap.begin_frame() glfw.pollEvents() currentFrame = time.perf_counter() deltaTime = currentFrame - lastFrame lastFrame = currentFrame input = inputMap.get_input() camera.processInput(input, 1.0/30.0) framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT) lightingShaderProgram.use() lightingShaderProgram.uniforms.light.position = lightPos lightingShaderProgram.uniforms.viewPos = camera.position lightColor = Vec3(math.sin(currentFrame) * 2.0, math.sin(currentFrame) * 0.7, math.sin(currentFrame) * 1.3) diffuseColor = lightColor * 0.3 ambientColor = diffuseColor * 0.2 lightingShaderProgram.uniforms.light.ambient = ambientColor lightingShaderProgram.uniforms.light.diffuse = diffuseColor lightingShaderProgram.uniforms.light.specular = Vec3(1.0, 1.0, 1.0) lightingShaderProgram.uniforms.material.ambient = Vec3(1.0, 0.5, 0.31) lightingShaderProgram.uniforms.material.diffuse = Vec3(1.0, 0.5, 0.31) lightingShaderProgram.uniforms.material.specular = Vec3(0.5, 0.5, 0.5) # Specular doesn't have full effect on this object's material lightingShaderProgram.uniforms.material.shininess = 32.0 # create transformations view = camera.getViewMatrix() projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0) # assign the transformations lightingShaderProgram.uniforms.view = view lightingShaderProgram.uniforms.projection = projection # draw the container gl.bindVertexArray(containerVAO) model = Mat4.Identity() lightingShaderProgram.uniforms.model = model gl.drawArrays(gl.TRIANGLES, 0, 36) gl.bindVertexArray(0) # draw the lamp lampShaderProgram.use() lampShaderProgram.uniforms.view = view lampShaderProgram.uniforms.projection = projection model = Mat4.Translation(lightPos) * Mat4.Scale(0.2) lampShaderProgram.uniforms.model = model gl.bindVertexArray(lightVAO) gl.drawArrays(gl.TRIANGLES, 0, 36) gl.bindVertexArray(0) window.swapBuffers() lampShaderProgram.delete() lightingShaderProgram.delete() gl.deleteVertexArrays([containerVAO, lightVAO]) gl.deleteBuffers([vbo])
def main(): # insure that we at least have an X display before continuing. import os try: display = os.environ['DISPLAY'] except: # Raise ImportError because regrtest.py handles it specially. raise ImportError, "No $DISPLAY -- skipping gl test" # touch all the attributes of gl without doing anything if verbose: print 'Touching gl module attributes...' for attr in glattrs: if verbose: print 'touching: ', attr getattr(gl, attr) # create a small 'Crisscross' window if verbose: print 'Creating a small "CrissCross" window...' print 'foreground' gl.foreground() if verbose: print 'prefposition' gl.prefposition(500, 900, 500, 900) if verbose: print 'winopen "CrissCross"' w = gl.winopen('CrissCross') if verbose: print 'clear' gl.clear() if verbose: print 'ortho2' gl.ortho2(0.0, 400.0, 0.0, 400.0) if verbose: print 'color WHITE' gl.color(GL.WHITE) if verbose: print 'color RED' gl.color(GL.RED) if verbose: print 'bgnline' gl.bgnline() if verbose: print 'v2f' gl.v2f(0.0, 0.0) gl.v2f(400.0, 400.0) if verbose: print 'endline' gl.endline() if verbose: print 'bgnline' gl.bgnline() if verbose: print 'v2i' gl.v2i(400, 0) gl.v2i(0, 400) if verbose: print 'endline' gl.endline() if verbose: print 'Displaying window for 2 seconds...' time.sleep(2) if verbose: print 'winclose' gl.winclose(w)