def run(self): # initializer timer glfw.glfwSetTime(0) t = 0.0 while not glfw.glfwWindowShouldClose(self.win) and not self.exitNow: # update every x seconds currT = glfw.glfwGetTime() if currT - t > 0.1: # update time t = currT # clear glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # build projection matrix pMatrix = glutils.perspective(45.0, self.aspect, 0.1, 100.0) mvMatrix = glutils.lookAt([ 3.0 * math.sin(glfw.glfwGetTime()), 0.0, 3.0 * math.cos(glfw.glfwGetTime()) ], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0]) # render self.scene.render(pMatrix, mvMatrix) # step self.scene.step() glfw.glfwSwapBuffers(self.win) # Poll for and process events glfw.glfwPollEvents() # end glfw.glfwTerminate()
def run(self): # initializer timer glfw.glfwSetTime(0) t = 0.0 while not glfw.glfwWindowShouldClose(self.win) and not self.exitNow: # update every x seconds currT = glfw.glfwGetTime() if currT - t > 0.1: # update time t = currT # clear glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # build projection matrix pMatrix = glutils.perspective(45.0, self.aspect, 0.1, 100.0) mvMatrix = glutils.lookAt([0.0, 0.0, -2.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0]) # render self.scene.render(pMatrix, mvMatrix) # step self.scene.step() glfw.glfwSwapBuffers(self.win) # Poll for and process events glfw.glfwPollEvents() # end glfw.glfwTerminate()
def run(self): # initializer timer glfw.glfwSetTime(0) t = 0.0 while not glfw.glfwWindowShouldClose(self.win) and not self.exitNow: # update every x seconds currT = glfw.glfwGetTime() if currT - t > 0.01: # update time t = currT # clear glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # render pMatrix = glutils.perspective(100.0, self.aspect, 0.1, 100.0) # modelview matrix mvMatrix = glutils.lookAt(self.camera.eye, self.camera.center, self.camera.up) # draw non-transparent object first self.box.render(pMatrix, mvMatrix) # render self.psys.render(pMatrix, mvMatrix, self.camera) # step self.step() glfw.glfwSwapBuffers(self.win) # Poll for and process events glfw.glfwPollEvents() # end glfw.glfwTerminate()
def mainLoop(self): ### Loop until glfw window is closed while not glfw.glfwWindowShouldClose(self.glfw_window): PyGL.glClear(PyGL.GL_COLOR_BUFFER_BIT) # update timing self.current_time = glfw.glfwGetTime() elapsed = self.current_time - self.previous_time self.previous_time = self.current_time # update GL/CEGUI self.updateGL() self.updateCEGUI(elapsed) # Swap front and back buffers glfw.glfwSwapBuffers(self.glfw_window) # Handle glfw events glfw.glfwPollEvents()
def run(self): glfw.glfwSetTime(0) # timer initialization t = 0.0 while not glfw.glfwWindowShouldClose(self.win) and not self.exit_now: current_t = glfw.glfwGetTime() if current_t -t > 0.1: t = current_t # time updated glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # | = bitwise or # build projection matrix p_matrix = glutils.perspective(45.0, self.aspect, 0.1, 100.0) mv_matrix = glutils.lookAt([0.0, 0.0, -2.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0]) # model-view matrix: [[eye pos], [look at {origin in this case)], [direction vector]] self.scene.render(p_matrix, mv_matrix) # render self.scene.step() # step glfw.glfwSwapBuffers(self.win) glfw.glfwPollEvents()
def run(self): glfw.glfwSetTime(0) t = 0.0 while not glfw.glfwWindowShouldClose(self.win) and not self.exitNow: currT = glfw.glfwGetTime() #每隔0.1s绘一次图 if currT - t > 0.1: t = currT #清除深度和颜色缓冲区 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #计算投影矩阵,45度视场,近/远裁剪平面的距离为0.1/100.0 pMatrix = glutils.perspective(45.0, self.aspect, 0.1, 100.0) #设置模型视图矩阵,眼睛位置设置在(0,0,-2),用一个向上的矢量(0,1,0)看向原点(0,0,0) mvMatirx = glutils.lookAt([0.0, 0.0, -2.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0]) self.scene.render(pMatrix, mvMatirx) self.scene.step() #交换前后缓冲区, 显示更新的三维图像(双缓冲,更加流畅的视觉效果) glfw.glfwSwapBuffers(self.win) #调用检查所有UI事件,将控制返回给while循环 glfw.glfwPollEvents() glfw.glfwTerminate()
def main(): # initialize glfw if not glfw.glfwInit(): return w_width, w_height = 800, 600 #glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.glfwCreateWindow(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.glfwTerminate() return glfw.glfwMakeContextCurrent(window) # glfw.glfwSetWindowSizeCallBack(window, window_resize) # positions colors texture coords cube = [ -0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, -0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0 ] cube = numpy.array(cube, dtype=numpy.float32) indices = [ 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8, 12, 13, 14, 14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20 ] indices = numpy.array(indices, dtype=numpy.uint32) vertex_shader = """ #version 330 in layout(location = 0) vec3 position; in layout(location = 1) vec3 color; in layout(location = 2) vec2 textureCoords; uniform mat4 transform; out vec3 newColor; out vec2 newTexture; void main() { gl_Position = transform * vec4(position, 1.0f); newColor = color; newTexture = textureCoords; } """ fragment_shader = """ #version 330 in vec3 newColor; in vec2 newTexture; out vec4 outColor; uniform sampler2D samplerTexture; void main() { outColor = texture(samplerTexture, newTexture) * vec4(newColor, 1.0f); } """ shader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER)) VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube, GL_STATIC_DRAW) EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices), indices, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #color glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(12)) glEnableVertexAttribArray(1) #texture glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(24)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image # image = Image.open("res/crate.jpg") image = Image.open("res/egg.jpg") img_data = numpy.array(list(image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) while not glfw.glfwWindowShouldClose(window): glfw.glfwPollEvents() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.glfwGetTime()) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.glfwGetTime()) transformLoc = glGetUniformLocation(shader, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y) glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None) glfw.glfwSwapBuffers(window) glfw.glfwTerminate()
def main(): # initialize glfw if not glfw.glfwInit(): return w_width, w_height = 800, 600 #glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.glfwCreateWindow(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.glfwTerminate() return glfw.glfwMakeContextCurrent(window) # glfw.set_window_size_callback(window, window_resize) obj = ObjLoader() obj.load_model("res/monkey/monkey_smooth.obj") texture_offset = len(obj.vertex_index)*12 normal_offset = (texture_offset + len(obj.texture_index)*8) shader = ShaderLoader.compile_shader("shaders/video_18_vert.vs", "shaders/video_18_frag.fs") VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) #positions glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset)) glEnableVertexAttribArray(1) #normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image image = Image.open("res/monkey/monkey.jpg") flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -3.0])) projection = pyrr.matrix44.create_perspective_projection_matrix(65.0, w_width / w_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0.0])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") transform_loc = glGetUniformLocation(shader, "transform") light_loc = glGetUniformLocation(shader, "light") glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) while not glfw.glfwWindowShouldClose(window): glfw.glfwPollEvents() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time() ) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.glfwGetTime() ) glUniformMatrix4fv(transform_loc, 1, GL_FALSE, rot_y) glUniformMatrix4fv(light_loc, 1, GL_FALSE, rot_y) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glfw.glfwSwapBuffers(window) glfw.glfwTerminate()
sys.exit() glfw.glfwMakeContextCurrent(window) glfw.glfwSetKeyCallback(window, on_key) while not glfw.glfwWindowShouldClose(window): width, height = glfw.glfwGetFramebufferSize(window) ratio = width / float(height) glViewport(0, 0, width, height) glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-ratio, ratio, -1, 1, 1, -1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glRotatef(glfw.glfwGetTime() * 50, 0, 0, 1) glBegin(GL_TRIANGLES) glColor3f(1, 0, 0) glVertex3f(-0.6, -0.4, 0) glColor3f(0, 1, 0) glVertex3f(0.6, -0.4, 0) glColor3f(0, 0, 1) glVertex3f(0, 0.6, 0) glEnd() # Swap front and back buffers glfw.glfwSwapBuffers(window) # Poll for and process events glfw.glfwPollEvents()
def __init__(self): # Initialize glfw if not glfw.glfwInit(): print (" Error : glfw failed to initialize") sys.exit (glfw.EXIT_FAILURE) ceguiGL3Renderer = True if ceguiGL3Renderer: glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 3) glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 2) glfw.glfwWindowHint(glfw.GLFW_OPENGL_FORWARD_COMPAT, PyGL.GL_TRUE) glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE, glfw.GLFW_OPENGL_CORE_PROFILE) else: glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 2) glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 1) glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE, glfw.GLFW_OPENGL_ANY_PROFILE) # our window hints ## http://www.glfw.org/docs/latest/window.html ## set our framebuffer related hints glfw.glfwWindowHint(glfw.GLFW_DEPTH_BITS, 24) glfw.glfwWindowHint(glfw.GLFW_STENCIL_BITS, 8) glfw.glfwWindowHint(glfw.GLFW_FOCUSED, True) fullScreen = False # create window if (not fullScreen): glfw_window = glfw.glfwCreateWindow(1024, 768, "PyCEGUI glfw3 Demo", None, None) else: glfw_window = glfw.glfwCreateWindow(1024, 768, "PyCEGUI glfw3 Demo", glfw.glfwGetPrimaryMonitor(), None) # check window created if not glfw_window: print (" Error : glfw failed to create a window") glfw.glfwTerminate() sys.exit() self.glfw_window = glfw_window glfw.glfwMakeContextCurrent(glfw_window) self.showglfwInfo() ## this does nothing on linux glfw.glfwSwapInterval(0) glfw.glfwSetInputMode(glfw_window, glfw.GLFW_CURSOR, glfw.GLFW_CURSOR_HIDDEN) # call backs glfw.glfwSetKeyCallback( glfw_window, self.on_key) glfw.glfwSetMouseButtonCallback( glfw_window, self.on_mouse) glfw.glfwSetCursorPosCallback( glfw_window, self.on_move) glfw.glfwSetWindowSizeCallback( glfw_window, self.on_resize) glfw.glfwSetCharCallback( glfw_window, self.on_char_callback) glfw.glfwSetFramebufferSizeCallback( glfw_window, self.on_framebuffer_size_callback) # initialise our CEGUI renderer ctx_major = glfw.glfwGetWindowAttrib(glfw_window, glfw.GLFW_CONTEXT_VERSION_MAJOR) ctx_minor = glfw.glfwGetWindowAttrib(glfw_window, glfw.GLFW_CONTEXT_VERSION_MINOR) forward_compat = glfw.glfwGetWindowAttrib(glfw_window, glfw.GLFW_OPENGL_FORWARD_COMPAT) if (not ceguiGL3Renderer): PyCEGUIOpenGLRenderer.OpenGLRenderer.bootstrapSystem() else : PyCEGUIOpenGLRenderer.OpenGL3Renderer.bootstrapSystem() # initialise PyCEGUI and resources rp = PyCEGUI.System.getSingleton().getResourceProvider() rp.setResourceGroupDirectory("schemes", CEGUI_PATH + "schemes") rp.setResourceGroupDirectory("imagesets", CEGUI_PATH + "imagesets") rp.setResourceGroupDirectory("fonts", CEGUI_PATH + "fonts") rp.setResourceGroupDirectory("layouts", CEGUI_PATH + "layouts") rp.setResourceGroupDirectory("looknfeels", CEGUI_PATH + "looknfeel") rp.setResourceGroupDirectory("schemas", CEGUI_PATH + "xml_schemas") PyCEGUI.ImageManager.setImagesetDefaultResourceGroup("imagesets") PyCEGUI.Font.setDefaultResourceGroup("fonts") PyCEGUI.Scheme.setDefaultResourceGroup("schemes") PyCEGUI.WidgetLookManager.setDefaultResourceGroup("looknfeels") PyCEGUI.WindowManager.setDefaultResourceGroup("layouts") parser = PyCEGUI.System.getSingleton().getXMLParser() if parser.isPropertyPresent("SchemaDefaultResourceGroup"): parser.setProperty("SchemaDefaultResourceGroup", "schemas") # Load schemes PyCEGUI.SchemeManager.getSingleton().createFromFile("TaharezLook.scheme") PyCEGUI.SchemeManager.getSingleton().createFromFile("WindowsLook.scheme") PyCEGUI.System.getSingleton().getDefaultGUIContext().getMouseCursor().setDefaultImage("TaharezLook/MouseArrow") # set root window root = PyCEGUI.WindowManager.getSingleton().createWindow("DefaultWindow", "background_wnd"); root.setArea( PyCEGUI.UVector2(PyCEGUI.UDim(0.0, 0),PyCEGUI.UDim(0.0, 0)) ,PyCEGUI.USize(PyCEGUI.UDim(1.0, 0),PyCEGUI.UDim(1.0, 0))) PyCEGUI.System.getSingleton().getDefaultGUIContext().setRootWindow(root) # load a layout layout = PyCEGUI.WindowManager.getSingleton().loadLayoutFromFile("TextDemo.layout") root.addChild(layout.getChild('TextDemo')) self.edit = root.getChild('TextDemo/MultiLineGroup/editMulti') # create label for our FPS self.labelFPS = PyCEGUI.WindowManager.getSingleton().createWindow("TaharezLook/Label", "FPSLabel") root.addChild(self.labelFPS) # create hello button button = PyCEGUI.WindowManager.getSingleton().createWindow("TaharezLook/Button", "HelloButton") button.setArea( PyCEGUI.UVector2(PyCEGUI.UDim(.50, 0),PyCEGUI.UDim(.92, 0)) ,PyCEGUI.USize(PyCEGUI.UDim(0.3, 0),PyCEGUI.UDim(0.05, 0))) button.setText("Hello") root.addChild(button) button.subscribeEvent(PyCEGUI.PushButton.EventClicked, self.OnbuttonClicked) # init simple timing self.previous_time = glfw.glfwGetTime() self.current_time = self.previous_time
def main(): # initialize glfw if not glfw.glfwInit(): return w_width, w_height = 800, 600 #glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.glfwCreateWindow(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.glfwTerminate() return glfw.glfwMakeContextCurrent(window) # glfw.set_window_size_callback(window, window_resize) # positions colors texture coords cube = [ -0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, -0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0 ] cube = numpy.array(cube, dtype=numpy.float32) indices = [ 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8, 12, 13, 14, 14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20 ] indices = numpy.array(indices, dtype=numpy.uint32) shader = ShaderLoader.compile_shader("shaders/video_14_vert.vs", "shaders/video_14_frag.fs") VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube, GL_STATIC_DRAW) EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices), indices, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #color glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(12)) glEnableVertexAttribArray(1) #texture glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(24)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image image = Image.open("res/egg.jpg") img_data = numpy.array(list(image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -3.0])) projection = pyrr.matrix44.create_perspective_projection_matrix( 45.0, w_width / w_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0.0])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) while not glfw.glfwWindowShouldClose(window): glfw.glfwPollEvents() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.glfwGetTime()) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.glfwGetTime()) transformLoc = glGetUniformLocation(shader, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y) glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None) glfw.glfwSwapBuffers(window) glfw.glfwTerminate()
def main(): # initialize glfw if not glfw.glfwInit(): return window = glfw.glfwCreateWindow(800, 600, "My OpenGL window", None, None) if not window: glfw.glfwTerminate() return glfw.glfwMakeContextCurrent(window) # positions colors cube = [ -0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, -0.5, 0.5, 0.5, 1.0, 1.0, 1.0, -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, 1.0, -0.5, 0.5, -0.5, 1.0, 1.0, 1.0 ] cube = numpy.array(cube, dtype=numpy.float32) indices = [ 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 4, 5, 1, 1, 0, 4, 6, 7, 3, 3, 2, 6, 5, 6, 2, 2, 1, 5, 7, 4, 0, 0, 3, 7 ] indices = numpy.array(indices, dtype=numpy.uint32) vertex_shader = """ #version 330 in vec3 position; in vec3 color; uniform mat4 transform; out vec3 newColor; void main() { gl_Position = transform * vec4(position, 1.0f); newColor = color; } """ fragment_shader = """ #version 330 in vec3 newColor; out vec4 outColor; void main() { outColor = vec4(newColor, 1.0f); } """ shader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER)) VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, 192, cube, GL_STATIC_DRAW) EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, 144, indices, GL_STATIC_DRAW) position = glGetAttribLocation(shader, "position") glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0)) glEnableVertexAttribArray(position) color = glGetAttribLocation(shader, "color") glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12)) glEnableVertexAttribArray(color) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) while not glfw.glfwWindowShouldClose(window): glfw.glfwPollEvents() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.glfwGetTime()) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.glfwGetTime()) # 向顶点shader中传入mvp 矩阵,控制顶点变换 transformLoc = glGetUniformLocation(shader, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y) glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None) glfw.glfwSwapBuffers(window) glfw.glfwTerminate()