def render(self, time): glUseProgram(self.shader) glBindVertexArray(self.vao) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32 ), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32 ), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.projection), dtype=np.float32 ), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) glBindVertexArray(0)
def render(self): glUseProgram(self.shader) glBindVertexArray(self.vao) # pass projection parameters to the shader fov_loc = glGetUniformLocation(self.shader, "fov"); glUniform1f(fov_loc, 45.0); aspect_loc = glGetUniformLocation(self.shader, "aspectRatio"); glUniform1f(aspect_loc, width/height); nearZ_loc = glGetUniformLocation(self.shader, "nearZ"); glUniform1f(nearZ_loc, -0.1); farZ_loc = glGetUniformLocation(self.shader, "farZ"); glUniform1f(farZ_loc, -10000.0); # ascontiguousarray puts the array in column major order glUniformMatrix4fv(self.mvMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix( ms.MatrixStack.modelview), dtype=np.float32)) glDrawArrays(GL_TRIANGLES, 0, self.numberOfVertices) glBindVertexArray(0)
def render(self): # active the program glUseProgram(self.shader) # bind the VAO, so that all of the linkages to the shader # are active glBindVertexArray(self.vao) # uniform variables within the shader are how we pass # in data that doesn't vary vertex to vertex. # pass projection parameters to the shader # (it would be more efficient to get these locs # when we set up the VAO. I put the code here for # clarity) fov_loc = glGetUniformLocation(self.shader, "fov") # send 1 float into the shader, into the uniform fov glUniform1f(fov_loc, 45.0) aspect_loc = glGetUniformLocation(self.shader, "aspectRatio") glUniform1f(aspect_loc, width / height) # Here, I'm still using negative z values for nearZ and # farZ, because that's consistent both with the math that I've # taught you so far and with the right hand rule. # By the end of lesson 25, we will use positive nearZ # and farZ values, as is consistent with the "standard" # perspective projection matrix. nearZ_loc = glGetUniformLocation(self.shader, "nearZ") glUniform1f(nearZ_loc, -0.1) farZ_loc = glGetUniformLocation(self.shader, "farZ") glUniform1f(farZ_loc, -10000.0) # Here we have to send the modelview matrix over ourselves. # Unlike what you have learned in the OPenGL Superbible, # In Core Profile, there are very few predefined variables in shaders. mvMatrixLoc = glGetUniformLocation(self.shader, "mvMatrix") # ascontiguousarray puts the array in column major order glUniformMatrix4fv( mvMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.modelview), dtype=np.float32 ), ) # Invoke the shaders. Linkages are already set up from the VBOs (via # the VAO), and from the # uniforms. # Vertex Data will come from the VBO # into the shader's "position" variable, which is defined # within the shader, and will need to output "gl_Position", # one of the few special variables in OpenGL 3.3 Core Profile # mode. glDrawArrays(GL_TRIANGLES, 0, self.numberOfVertices) # unset the current VAO glBindVertexArray(0)
def render(self, time): glUseProgram(self.shader) glBindVertexArray(self.vao) # pass projection parameters to the shader fov_loc = glGetUniformLocation(self.shader, "fov") glUniform1f(fov_loc, 45.0) aspect_loc = glGetUniformLocation(self.shader, "aspectRatio") glUniform1f(aspect_loc, 1.0) nearZ_loc = glGetUniformLocation(self.shader, "nearZ") glUniform1f(nearZ_loc, -5.0) farZ_loc = glGetUniformLocation(self.shader, "farZ") glUniform1f(farZ_loc, -150.00) time_loc = glGetUniformLocation(self.shader, "time") glUniform1f(time_loc, animation_time) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.projection), dtype=np.float32), ) glDrawArrays(GL_TRIANGLES, 0, self.numberOfVertices) glBindVertexArray(0)
def draw_ground(): # ascontiguousarray puts the array in column major order glLoadMatrixf(np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.modelview).T)) glColor3f(1.0,1.0,1.0) glBegin(GL_LINES) for x in range(-200,201,20): for z in range(-200,201,20): glVertex3f(float(-x), float(-50.0), float(z)) glVertex3f(float(x), float(-50.0), float(z)) glVertex3f(float(x), float(-50.0), float(-z)) glVertex3f(float(x), float(-50.0), float(z)) glEnd()
def render(self): with ms.push_matrix(ms.MatrixStack.model): # rotate the triangle along the positive z axis ms.translate(ms.MatrixStack.model, math.sin(glfw.get_time()), 0, 0) ms.rotateZ(ms.MatrixStack.model, glfw.get_time()) gl.glUseProgram(self.shader) gl.glBindVertexArray(self.vao) gl.glUniformMatrix4fv( self.mvpMatrixLoc, 1, gl.GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.modelviewprojection), dtype=np.float32)) gl.glDrawArrays(gl.GL_TRIANGLES, 0, self.numberOfVertices) gl.glBindVertexArray(0)
def draw_y_axis(): # ascontiguousarray puts the array in column major order glLoadMatrixf(np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.modelview).T)) glLineWidth(3.0) glBegin(GL_LINES) glVertex3f(0.0, 0.0, 0.0) glVertex3f(0.0, 1.0, 0.0) # arrow glVertex3f(0.0, 1.0, 0.0) glVertex3f(0.25, 0.75, 0.0) glVertex3f(0.0, 1.0, 0.0) glVertex3f(-0.25, 0.75, 0.0) glEnd()
def draw_ndc(): glLoadMatrixf( np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.modelview).T)) glColor3f(1.0, 1.0, 1.0) glLineWidth(3.0) glBegin(GL_LINES) glVertex3f(-1.0, -1.0, -1.0) glVertex3f(1.0, -1.0, -1.0) glVertex3f(1.0, -1.0, -1.0) glVertex3f(1.0, 1.0, -1.0) glVertex3f(1.0, 1.0, -1.0) glVertex3f(-1.0, 1.0, -1.0) glVertex3f(-1.0, 1.0, -1.0) glVertex3f(-1.0, -1.0, -1.0) glVertex3f(-1.0, -1.0, 1.0) glVertex3f(1.0, -1.0, 1.0) glVertex3f(1.0, -1.0, 1.0) glVertex3f(1.0, 1.0, 1.0) glVertex3f(1.0, 1.0, 1.0) glVertex3f(-1.0, 1.0, 1.0) glVertex3f(-1.0, 1.0, 1.0) glVertex3f(-1.0, -1.0, 1.0) # connect the squares glVertex3f(1.0, 1.0, -1.0) glVertex3f(1.0, 1.0, 1.0) glVertex3f(1.0, -1.0, -1.0) glVertex3f(1.0, -1.0, 1.0) glVertex3f(-1.0, 1.0, -1.0) glVertex3f(-1.0, 1.0, 1.0) glVertex3f(-1.0, -1.0, -1.0) glVertex3f(-1.0, -1.0, 1.0) glEnd()
ms.perspective( fov=45.0, aspectRatio=1.0, #since the viewport is always square nearZ=0.1, farZ=10000.0) # ms.ortho(left=-150.0, # right=150.0, # back=-150.0, # top=150.0, # near=1.0, # far=10000.0) glMatrixMode(GL_PROJECTION) # ascontiguousarray puts the array in column major order glLoadMatrixf( np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.projection).T)) # note - opengl matricies use degrees ms.translate(ms.MatrixStack.view, 0.0, 0.0, -moving_camera_r) ms.rotate_x(ms.MatrixStack.view, moving_camera_rot_x) ms.rotate_y(ms.MatrixStack.view, -moving_camera_rot_y) glMatrixMode(GL_MODELVIEW) draw_ground() if animation_time < 5.0 or (animation_time > 40.0 and animation_time < 45.0): draw_axises() else: draw_axises(grayed_out=True) with ms.PushMatrix(ms.MatrixStack.model):
def render(self, time, grayed_out=False): glDisable(GL_DEPTH_TEST) glUseProgram(self.shader) glBindVertexArray(self.vao) # pass projection parameters to the shader fov_loc = glGetUniformLocation(self.shader, "fov") glUniform1f(fov_loc, 45.0) aspect_loc = glGetUniformLocation(self.shader, "aspectRatio") glUniform1f(aspect_loc, 1.0) nearZ_loc = glGetUniformLocation(self.shader, "nearZ") glUniform1f(nearZ_loc, -5.0) farZ_loc = glGetUniformLocation(self.shader, "farZ") glUniform1f(farZ_loc, -150.00) # TODO, set the color with ms.push_matrix(ms.MatrixStack.model): # x axis with ms.push_matrix(ms.MatrixStack.model): ms.rotate_z(ms.MatrixStack.model, math.radians(-90.0)) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 1.0, 0.0, 0.0) if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.model), dtype=np.float32), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.view), dtype=np.float32), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.projection), dtype=np.float32), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) # y if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 0.0, 1.0, 0.0) # glColor3f(0.0,1.0,0.0) # green y if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.projection), dtype=np.float32), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) glBindVertexArray(0) glEnable(GL_DEPTH_TEST)
def handle_inputs() -> None: global rotation_around_paddle1 if glfw.get_key(window, glfw.KEY_E) == glfw.PRESS: rotation_around_paddle1 += 0.1 global square_rotation if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS: square_rotation += 0.1 global camera move_multiple = 15.0 if glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS: camera.rot_y -= 0.03 if glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS: camera.rot_y += 0.03 if glfw.get_key(window, glfw.KEY_PAGE_UP) == glfw.PRESS: camera.rot_x += 0.03 if glfw.get_key(window, glfw.KEY_PAGE_DOWN) == glfw.PRESS: camera.rot_x -= 0.03 # NEW -- reason about moving in 3D space just like placing # objects. If the up button is pressed if glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS: # describe the vector to move forwards. # -1 unit in the z direction is forwards # # Although we have not covered linear algebra and # matrix multiplication in depth, all coordinates # in OpenGL are in 4D space, (x,y,z,w), # where to convert to NDC, use (x/w,y/w,z/w) # # For most purposes, by setting w to 1, we can # think in normal 3D space # forwards = np.array([0.0, 0.0, -1.0, 1.0]) # push matrix on the view stack, as we are going # to use the view matrix to determine the new position, # but then will reset the value of the view matrix with ms.PushMatrix(ms.MatrixStack.view): ms.translate(ms.MatrixStack.view, camera.x, camera.y, camera.z) ms.rotate_y(ms.MatrixStack.view, camera.rot_y) ms.scale(ms.MatrixStack.view, 5.0, 5.0, 5.0) camera.x, camera.y, camera.z, _ = ( ms.getCurrentMatrix(ms.MatrixStack.view) @ forwards) if glfw.get_key(window, glfw.KEY_DOWN) == glfw.PRESS: backwards = np.array([0.0, 0.0, 1.0, 1.0]) with ms.PushMatrix(ms.MatrixStack.view): ms.translate(ms.MatrixStack.view, camera.x, camera.y, camera.z) ms.rotate_y(ms.MatrixStack.view, camera.rot_y) ms.scale(ms.MatrixStack.view, 5.0, 5.0, 5.0) camera.x, camera.y, camera.z, _ = ( ms.getCurrentMatrix(ms.MatrixStack.view) @ backwards) global paddle1, paddle2 if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS: paddle1.position[1] -= 10.0 if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS: paddle1.position[1] += 10.0 if glfw.get_key(window, glfw.KEY_K) == glfw.PRESS: paddle2.position[1] -= 10.0 if glfw.get_key(window, glfw.KEY_I) == glfw.PRESS: paddle2.position[1] += 10.0 global paddle_1_rotation, paddle_2_rotation if glfw.get_key(window, glfw.KEY_A) == glfw.PRESS: paddle1.rotation += 0.1 if glfw.get_key(window, glfw.KEY_D) == glfw.PRESS: paddle1.rotation -= 0.1 if glfw.get_key(window, glfw.KEY_J) == glfw.PRESS: paddle2.rotation += 0.1 if glfw.get_key(window, glfw.KEY_L) == glfw.PRESS: paddle2.rotation -= 0.1
def render(self, time, grayed_out=False): glDisable(GL_DEPTH_TEST) glUseProgram(self.shader) glBindVertexArray(self.vao) with ms.push_matrix(ms.MatrixStack.model): # x axis with ms.push_matrix(ms.MatrixStack.model): ms.rotate_z(ms.MatrixStack.model, math.radians(-90.0)) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 1.0, 0.0, 0.0) if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32 ), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32 ), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.projection), dtype=np.float32 ), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) # z # glColor3f(0.0,0.0,1.0) # blue z with ms.push_matrix(ms.MatrixStack.model): ms.rotate_y(ms.MatrixStack.model, math.radians(90.0)) ms.rotate_z(ms.MatrixStack.model, math.radians(90.0)) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 0.0, 0.0, 1.0) if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32 ), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32 ), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.projection), dtype=np.float32 ), ) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glDrawArrays(GL_LINES, 0, self.numberOfVertices) # y if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 0.0, 1.0, 0.0) # glColor3f(0.0,1.0,0.0) # green y if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32 ), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32 ), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.projection), dtype=np.float32 ), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) glBindVertexArray(0) glEnable(GL_DEPTH_TEST)