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 draw(self): # build projection matrix pMatrix = glutils.perspective(45.0, self.aspect, 0.1, 100.0) # modelview matrix mvMatrix = glutils.lookAt(self.camera.eye, self.camera.center, self.camera.up) # render # generate ray-cube back-face texture texture = self.raycube.renderBackFace(pMatrix, mvMatrix) # set shader program glUseProgram(self.program) # set window dimensions glUniform2f(glGetUniformLocation(self.program, b"uWinDims"), float(self.width), float(self.height)) # texture unit 0 - back-faces of cube glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, texture) glUniform1i(glGetUniformLocation(self.program, b"texBackFaces"), 0) # texture unit 1 - 3D volume texture glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_3D, self.texVolume) glUniform1i(glGetUniformLocation(self.program, b"texVolume"), 1) # draw front face of cubes self.raycube.renderFrontFace(pMatrix, mvMatrix, self.program)
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): """Main loop""" glfw.set_time(0) # resets the GLFW timer to 0 t = 0.0 # timer to redraw the graphics at regular intervals while not glfw.window_should_close(self.win) and not self.exit_now: # Update every x seconds curr_time = glfw.get_time() if curr_time - t > 0.1: # change 0.1 to adjust the rendering frame rate # Update time t = curr_time # Clear and get ready for the next frame glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # 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]) # render self.scene.render(p_matrix, mv_matrix) self.scene.step() glfw.swap_buffers(self.win) # Checks for any UI events glfw.poll_events() glfw.terminate()
def step(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear 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]) self.scene.render(p_matrix, mv_matrix) # render self.scene.step() # step glfw.glfwSwapBuffers(self.win) glfw.glfwPollEvents()
def step(self): # 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.SwapBuffers(self.win) # Poll for and process events glfw.PollEvents()
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(): if not glfw.init(): print('glfw.init error') return window = glfw.create_window(640, 480, 'Cube', None, None) if not window: glfw.terminate() print('glfw.create_window error') return glfw.make_context_current(window) print(glGetString(GL_VERSION)) # init OpenGL glEnable(GL_DEPTH_TEST) glClearColor(0.1, 0.1, 0.1, 1) # glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ) # shader program = glutils.loadShaders(strVS, strFS) glUseProgram(program) # vertex buffer # position # normal positions = numpy.array([ -1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, -1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 0.0, -1.0, -1.0, 1.0, 0.0, -1.0, 0.0, -1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 1.0, -1.0, -1.0, 0.0, -1.0, 0.0, -1.0, -1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, -1.0, 0.0, 0.0, -1.0, -1.0, 1.0, -1.0, 0.0, 0.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, -1.0, ], numpy.float32) buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, buffer) glBufferData(GL_ARRAY_BUFFER, positions.itemsize * len(positions), positions, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, positions.itemsize * 6, None) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, positions.itemsize * 3, None) # Projections pMatrix = glutils.perspective(45, 640 / 480.0, 0.1, 100.0) mvMatrix = glutils.lookAt([4.0, 4.0, 3.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1]) pMatrixUniform = glGetUniformLocation(program, b'uPMatrix') mvMatrixUniform = glGetUniformLocation(program, b'uMVMatrix') glUniformMatrix4fv(pMatrixUniform, 1, GL_FALSE, pMatrix) glUniformMatrix4fv(mvMatrixUniform, 1, GL_FALSE, mvMatrix) # Lighting lightColorUniform = glGetUniformLocation(program, b'lightColor') glUniform3f(lightColorUniform, 0.1, 0.8, 0.1) objectColorUniform = glGetUniformLocation(program, b'objectColor') glUniform3f(objectColorUniform, 0.7, 0.7, 0.7) lightPosUniform = glGetUniformLocation(program, b'lightPos') glUniform3f(lightPosUniform, 2.0, 4, -3.0) t = 0 while not glfw.window_should_close(window): time.sleep(1 / 45) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # glBegin(GL_TRIANGLES) # glVertex2f(-0.5, -0.5) # glVertex2f( 0.0 , 0.5) # glVertex2f( 0.5, -0.5) # glEnd() # rotation animation t = (t + 1) % 360 # set shader angle in radians glUniform1f(glGetUniformLocation(program, 'uTheta'), math.radians(t)) # draw call glDrawArrays(GL_QUADS, 0, 24) glfw.swap_buffers(window) glfw.poll_events()
glEnable(GL_MULTISAMPLE) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded while not glfw.window_should_close(window): currentFrame = glfw.get_time() deltaTime = currentFrame - lastFrame lastFrame = currentFrame glfw.poll_events() do_movement() # Render here width, height = glfw.get_framebuffer_size(window) ratio = width / float(height) gl.glViewport(0, 0, width, height) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glFrustum(-ratio, ratio, -1, 1, 1, 2) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glClearColor(0.0,0.0,0.0,0.0) camera.eye=[5*math.sin(glfw.get_time()), 0 , 5* math.cos(glfw.get_time()) ] # modelview matrix cameraPos_=cameraPos+(0,0,-0.25*keys[glfw.KEY_X]) mvMatrix = glutils.lookAt(cameraPos_, cameraPos_+cameraFront, cameraUp) pMatrix = glutils.perspective(45, ratio, 0.1, 100.0) draw_things() # Swap front and back buffers glfw.swap_buffers(window) glfw.poll_events() glfw.terminate()