def main(): # Initialize the library if not glfw.init(): raise RuntimeError("Failed to initialize GLFW") glfw.window_hint(glfw.SAMPLES, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # To make MacOS happy; should not be needed glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # Create a windowed mode window and its OpenGL context window = glfw.create_window(1024, 768, "Tutorial 09 - VBO Indexing", None, None) if not window: glfw.terminate() raise RuntimeError("Failed to open GLFW window.") # Make the window's context current glfw.make_context_current(window) # Ensure we can capture the escape key being pressed below glfw.set_input_mode(window, glfw.STICKY_KEYS, True) # Hide the mouse and enable unlimited mouvement glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) # Set the mouse at the center of the screen glfw.poll_events() glfw.set_cursor_pos(window, 1024/2, 768/2) # Dark blue background glClearColor(0.0, 0.0, 0.4, 0.0) # Enable depth test glEnable(GL_DEPTH_TEST) # Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS) vao = glGenVertexArrays(1) glBindVertexArray(vao) # Create and compile our GLSL program from the shaders shader = Shader("StandardShading.vertexshader", "StandardShading.fragmentshader") shader.compile() # Create Controls object controls = Controls(window) # Load the texture using any two methods Texture = loadDDS("uvmap2.DDS") # Read our .obj file vertices, uvs, normals = loadOBJ("suzanne.obj",invert_v=True) indices,indexed_vertices,indexed_uvs,indexed_normals = indexVBO(vertices,uvs,normals) # Create the vertex buffer object vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, indexed_vertices.nbytes, indexed_vertices, GL_STATIC_DRAW) # Create the uv buffer object uvbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, uvbo) glBufferData(GL_ARRAY_BUFFER, indexed_uvs.nbytes, indexed_uvs, GL_STATIC_DRAW) # Create normal buffer object nbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, nbo) glBufferData(GL_ARRAY_BUFFER, indexed_normals.nbytes, indexed_normals, GL_STATIC_DRAW) # Generate a buffer for the indices as well ibo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, ibo) glBufferData(GL_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW) # For speed computation lastTime = glfw.get_time() nbFrames = 0 # Loop until the user closes the window while( glfw.get_key(window, glfw.KEY_ESCAPE ) != glfw.PRESS and glfw.window_should_close(window) == 0 ): # Measure speed currentTime = glfw.get_time() nbFrames += 1 if currentTime-lastTime >= 1.0: # If last prinf() was more than 1sec ago # printf and reset print("{:.5f} ms/frame".format(1000.0/float(nbFrames))) nbFrames = 0 lastTime += 1.0 # Clear the screen glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) # Use our shader shader.enable() # Compute the MVP matrix from keyboard and mouse input controls.update() # Send our transformation to the currently bound shader, # in the "MVP" uniform shader.setUniform("V", "mat4", np.array(controls.ViewMatrix,dtype=np.float32)) shader.setUniform("M", "mat4", np.array(controls.ModelMatrix,dtype=np.float32)) shader.setUniform("MVP", "mat4", controls.MVP) shader.setUniform("LightPosition_worldspace", "vec3", np.array([4,4,4],dtype=np.float32)) # Bind our texture in Texture Unit 0 glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, Texture) # Set our "myTextureSampler" sampler to user Texture Unit 0 shader.setUniform("myTextureSampler","sampler2D",0) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo) glVertexAttribPointer( 0, # attribute 0. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # 2nd attribute buffer : UVs glEnableVertexAttribArray(1) glBindBuffer(GL_ARRAY_BUFFER, uvbo) glVertexAttribPointer( 1, # attribute. 2, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # 3rd attribute buffer : UVs glEnableVertexAttribArray(2) glBindBuffer(GL_ARRAY_BUFFER, nbo) glVertexAttribPointer( 2, # attribute. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # Index buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo) # Draw the triangles ! glDrawElements( GL_TRIANGLES, # mode len(indices), # count GL_UNSIGNED_SHORT, # type None # element array buffer offset ) glDisableVertexAttribArray(0) glDisableVertexAttribArray(1) glDisableVertexAttribArray(2) # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() glfw.terminate()
glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) ##call back for mouse scroll glfw.set_scroll_callback(window, scroll_clb) ##make the context current glfw.make_context_current(window) ##load here the 3d meshes dh_indices, dh_buffer = ObjLoader.load_model("meshes/final.obj") ##dh_indices, dh_buffer=ObjLoader.load_model("meshes/cube.obj") shader = ShaderLoader.compile_shader("shaders/shader_vert.vs", "shaders/shader_frag.fs") shadowMapShader = Shader("shadowMap.vert", "shadowMap.frag") shadowMapShader.compile() displayShader = Shader("display.vert", "display.frag") displayShader.compile() ##VAO AND vbo VAO = glGenVertexArrays(1) vbo = glGenBuffers(1) ##VAO glBindVertexArray(VAO) ##Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, dh_buffer.nbytes, dh_buffer, GL_STATIC_DRAW) ##Vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, dh_buffer.itemsize * 8,
def main(): # Initialize the library if not glfw.init(): raise RuntimeError("Failed to initialize GLFW") glfw.window_hint(glfw.SAMPLES, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # To make MacOS happy; should not be needed glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # Create a windowed mode window and its OpenGL context window = glfw.create_window(1024, 768, "Tutorial 02 - Red triangle", None, None) if not window: glfw.terminate() raise RuntimeError("Failed to open GLFW window.") # Make the window's context current glfw.make_context_current(window) # Ensure we can capture the escape key being pressed below glfw.set_input_mode(window, glfw.STICKY_KEYS, True) # Dark blue background glClearColor(0.0, 0.0, 0.4, 0.0) # Create and compile our GLSL program from the shaders shader = Shader("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader") shader.compile() vao = glGenVertexArrays(1) glBindVertexArray(vao) # Define the verticies v = np.array([ -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 0.0, 1.0, 0.0, ], dtype=np.float32) # Create the vertex buffer object vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, v.nbytes, v, GL_STATIC_DRAW) # Loop until the user closes the window while (glfw.get_key(window, glfw.KEY_ESCAPE) != glfw.PRESS and glfw.window_should_close(window) == 0): # Clear the screen glClear(GL_COLOR_BUFFER_BIT) # Use our shader shader.enable() glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo) glVertexAttribPointer( 0, # attribute 0. No particular reason for 0, but must match the layout in the shader. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # Draw the triangle ! glDrawArrays(GL_TRIANGLES, 0, 3) # 3 indices starting at 0 -> 1 triangle glDisableVertexAttribArray(0) # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() del vao, vbo, shader glfw.terminate()
def main(): # Initialize the library if not glfw.init(): raise RuntimeError("Failed to initialize GLFW") glfw.window_hint(glfw.SAMPLES, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # To make MacOS happy; should not be needed glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # Create a windowed mode window and its OpenGL context window = glfw.create_window(1024, 768, "Tutorial 05 - Textured Cube", None, None) if not window: glfw.terminate() raise RuntimeError("Failed to open GLFW window.") # Make the window's context current glfw.make_context_current(window) # Ensure we can capture the escape key being pressed below glfw.set_input_mode(window, glfw.STICKY_KEYS, True) # Hide the mouse and enable unlimited mouvement glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) # Set the mouse at the center of the screen glfw.poll_events() glfw.set_cursor_pos(window, 1024 / 2, 768 / 2) # Dark blue background glClearColor(0.0, 0.0, 0.4, 0.0) # Enable depth test glEnable(GL_DEPTH_TEST) # Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS) vao = glGenVertexArrays(1) glBindVertexArray(vao) # Create and compile our GLSL program from the shaders shader = Shader("TransformVertexShader2.vertexshader", "TextureFragmentShader.fragmentshader") shader.compile() # Create Controls object controls = Controls(window) # Load the texture using any two methods #Texture = loadBMP("uvtemplate.bmp") Texture = loadDDS("uvtemplate.DDS") # Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle. # A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices g_vertex_buffer_data = np.array([ -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0 ], dtype=np.float32) g_uv_buffer_data = np.array([ 0.000059, 1.0 - 0.000004, 0.000103, 1.0 - 0.336048, 0.335973, 1.0 - 0.335903, 1.000023, 1.0 - 0.000013, 0.667979, 1.0 - 0.335851, 0.999958, 1.0 - 0.336064, 0.667979, 1.0 - 0.335851, 0.336024, 1.0 - 0.671877, 0.667969, 1.0 - 0.671889, 1.000023, 1.0 - 0.000013, 0.668104, 1.0 - 0.000013, 0.667979, 1.0 - 0.335851, 0.000059, 1.0 - 0.000004, 0.335973, 1.0 - 0.335903, 0.336098, 1.0 - 0.000071, 0.667979, 1.0 - 0.335851, 0.335973, 1.0 - 0.335903, 0.336024, 1.0 - 0.671877, 1.000004, 1.0 - 0.671847, 0.999958, 1.0 - 0.336064, 0.667979, 1.0 - 0.335851, 0.668104, 1.0 - 0.000013, 0.335973, 1.0 - 0.335903, 0.667979, 1.0 - 0.335851, 0.335973, 1.0 - 0.335903, 0.668104, 1.0 - 0.000013, 0.336098, 1.0 - 0.000071, 0.000103, 1.0 - 0.336048, 0.000004, 1.0 - 0.671870, 0.336024, 1.0 - 0.671877, 0.000103, 1.0 - 0.336048, 0.336024, 1.0 - 0.671877, 0.335973, 1.0 - 0.335903, 0.667969, 1.0 - 0.671889, 1.000004, 1.0 - 0.671847, 0.667979, 1.0 - 0.335851 ], dtype=np.float32) # Create the vertex buffer object vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.nbytes, g_vertex_buffer_data, GL_STATIC_DRAW) # Create the vertex uv buffer object uvbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, uvbo) glBufferData(GL_ARRAY_BUFFER, g_uv_buffer_data.nbytes, g_uv_buffer_data, GL_STATIC_DRAW) # Loop until the user closes the window while (glfw.get_key(window, glfw.KEY_ESCAPE) != glfw.PRESS and glfw.window_should_close(window) == 0): # Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Use our shader shader.enable() # Compute the MVP matrix from keyboard and mouse input controls.update() # Send our transformation to the currently bound shader, # in the "MVP" uniform shader.setUniform("MVP", "mat4", controls.MVP) # Bind our texture in Texture Unit 0 glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, Texture) # Set our "myTextureSampler" sampler to user Texture Unit 0 shader.setUniform("myTextureSampler", "sampler2D", 0) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo) glVertexAttribPointer( 0, # attribute 0. No particular reason for 0, but must match the layout in the shader. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # 2nd attribute buffer : colors glEnableVertexAttribArray(1) glBindBuffer(GL_ARRAY_BUFFER, uvbo) glVertexAttribPointer( 1, # attribute. No particular reason for 1, but must match the layout in the shader. 2, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # Draw the triangle ! glDrawArrays(GL_TRIANGLES, 0, 12 * 3) # 3 indices starting at 0 -> 1 triangle glDisableVertexAttribArray(0) glDisableVertexAttribArray(1) # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() glfw.terminate()
def main(): # Initialize the library if not glfw.init(): raise RuntimeError("Failed to initialize GLFW") glfw.window_hint(glfw.SAMPLES, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # To make MacOS happy; should not be needed glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # Create a windowed mode window and its OpenGL context window = glfw.create_window(1024, 768, "Tutorial 03 - Matrices", None, None) if not window: glfw.terminate() raise RuntimeError("Failed to open GLFW window.") # Make the window's context current glfw.make_context_current(window) # Ensure we can capture the escape key being pressed below glfw.set_input_mode(window, glfw.STICKY_KEYS, True) # Dark blue background glClearColor(0.0, 0.0, 0.4, 0.0) vao = glGenVertexArrays(1) glBindVertexArray(vao) # Create and compile our GLSL program from the shaders shader = Shader("SimpleTransform.vertexshader", "SingleColor.fragmentshader") shader.compile() # Projection matrix : 45 deg Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units Projection = glm_perspective(45.0 * np.pi / 180., 4.0 / 3.0, 0.1, 100.0) # Camera matrix View = glm_lookAt( [4, 3, 3], # Camera is at (4,3,3), in World Space [0, 0, 0], # and looks at the origin [0, 1, 0] # Head is up (set to 0,-1,0 to look upside-down) ) # Model matrix : an identity matrix (model will be at the origin) Model = np.eye(4) # Our ModelViewProjection : multiplication of our 3 matrices # (convert to 32-bit now that computations are done) MVP = np.array(np.dot(np.dot(Model, View), Projection), dtype=np.float32) # Define the verticies v = np.array([ -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 0.0, 1.0, 0.0, ], dtype=np.float32) # Create the vertex buffer object vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, v.nbytes, v, GL_STATIC_DRAW) # Loop until the user closes the window while (glfw.get_key(window, glfw.KEY_ESCAPE) != glfw.PRESS and glfw.window_should_close(window) == 0): # Clear the screen glClear(GL_COLOR_BUFFER_BIT) # Use our shader shader.enable() # Send our transformation to the currently bound shader, # in the "MVP" uniform shader.setUniform("MVP", "mat4", MVP) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo) glVertexAttribPointer( 0, # attribute 0. No particular reason for 0, but must match the layout in the shader. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # Draw the triangle ! glDrawArrays(GL_TRIANGLES, 0, 3) # 3 indices starting at 0 -> 1 triangle glDisableVertexAttribArray(0) # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() del vao, vbo, shader glfw.terminate()
def main(): # Initialize the library if not glfw.init(): raise RuntimeError("Failed to initialize GLFW") glfw.window_hint(glfw.SAMPLES, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # To make MacOS happy; should not be needed glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # Create a windowed mode window and its OpenGL context window = glfw.create_window(1024, 768, "Tutorial 05 - Textured Cube", None, None) if not window: glfw.terminate() raise RuntimeError("Failed to open GLFW window.") # Make the window's context current glfw.make_context_current(window) # Ensure we can capture the escape key being pressed below glfw.set_input_mode(window, glfw.STICKY_KEYS, True) # Dark blue background glClearColor(0.0, 0.0, 0.4, 0.0) # Enable depth test glEnable(GL_DEPTH_TEST) # Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS) vao = glGenVertexArrays(1) glBindVertexArray(vao) # Create and compile our GLSL program from the shaders shader = Shader( "TransformVertexShader2.vertexshader", "TextureFragmentShader.fragmentshader") shader.compile() # Projection matrix : 45 deg Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units Projection = glm_perspective(45.0*np.pi/180., 4.0 / 3.0, 0.1, 100.0) # Camera matrix View = glm_lookAt( [4,3,3], # Camera is at (4,3,3), in World Space [0,0,0], # and looks at the origin [0,1,0] # Head is up (set to 0,-1,0 to look upside-down) ) # Model matrix : an identity matrix (model will be at the origin) Model = np.eye(4) # Our ModelViewProjection : multiplication of our 3 matrices # (convert to 32-bit now that computations are done) MVP = np.array( np.dot(np.dot(Model, View), Projection), dtype=np.float32 ) # Load the texture using any two methods #Texture = loadBMP("uvtemplate.bmp") Texture = loadDDS("uvtemplate.DDS") # Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle. # A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices g_vertex_buffer_data = np.array([ -1.0,-1.0,-1.0, -1.0,-1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0,-1.0, -1.0,-1.0,-1.0, -1.0, 1.0,-1.0, 1.0,-1.0, 1.0, -1.0,-1.0,-1.0, 1.0,-1.0,-1.0, 1.0, 1.0,-1.0, 1.0,-1.0,-1.0, -1.0,-1.0,-1.0, -1.0,-1.0,-1.0, -1.0, 1.0, 1.0, -1.0, 1.0,-1.0, 1.0,-1.0, 1.0, -1.0,-1.0, 1.0, -1.0,-1.0,-1.0, -1.0, 1.0, 1.0, -1.0,-1.0, 1.0, 1.0,-1.0, 1.0, 1.0, 1.0, 1.0, 1.0,-1.0,-1.0, 1.0, 1.0,-1.0, 1.0,-1.0,-1.0, 1.0, 1.0, 1.0, 1.0,-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,-1.0, -1.0, 1.0,-1.0, 1.0, 1.0, 1.0, -1.0, 1.0,-1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0,-1.0, 1.0 ],dtype=np.float32) g_uv_buffer_data = np.array([ 0.000059, 1.0-0.000004, 0.000103, 1.0-0.336048, 0.335973, 1.0-0.335903, 1.000023, 1.0-0.000013, 0.667979, 1.0-0.335851, 0.999958, 1.0-0.336064, 0.667979, 1.0-0.335851, 0.336024, 1.0-0.671877, 0.667969, 1.0-0.671889, 1.000023, 1.0-0.000013, 0.668104, 1.0-0.000013, 0.667979, 1.0-0.335851, 0.000059, 1.0-0.000004, 0.335973, 1.0-0.335903, 0.336098, 1.0-0.000071, 0.667979, 1.0-0.335851, 0.335973, 1.0-0.335903, 0.336024, 1.0-0.671877, 1.000004, 1.0-0.671847, 0.999958, 1.0-0.336064, 0.667979, 1.0-0.335851, 0.668104, 1.0-0.000013, 0.335973, 1.0-0.335903, 0.667979, 1.0-0.335851, 0.335973, 1.0-0.335903, 0.668104, 1.0-0.000013, 0.336098, 1.0-0.000071, 0.000103, 1.0-0.336048, 0.000004, 1.0-0.671870, 0.336024, 1.0-0.671877, 0.000103, 1.0-0.336048, 0.336024, 1.0-0.671877, 0.335973, 1.0-0.335903, 0.667969, 1.0-0.671889, 1.000004, 1.0-0.671847, 0.667979, 1.0-0.335851 ],dtype=np.float32) # Create the vertex buffer object vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.nbytes, g_vertex_buffer_data, GL_STATIC_DRAW) # Create the vertex uv buffer object uvbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, uvbo) glBufferData(GL_ARRAY_BUFFER, g_uv_buffer_data.nbytes, g_uv_buffer_data, GL_STATIC_DRAW) # Loop until the user closes the window while( glfw.get_key(window, glfw.KEY_ESCAPE ) != glfw.PRESS and glfw.window_should_close(window) == 0 ): # Clear the screen glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) # Use our shader shader.enable() # Send our transformation to the currently bound shader, # in the "MVP" uniform shader.setUniform("MVP", "mat4", MVP) # Bind our texture in Texture Unit 0 glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, Texture) # Set our "myTextureSampler" sampler to user Texture Unit 0 shader.setUniform("myTextureSampler","sampler2D",0) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo) glVertexAttribPointer( 0, # attribute 0. No particular reason for 0, but must match the layout in the shader. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # 2nd attribute buffer : colors glEnableVertexAttribArray(1) glBindBuffer(GL_ARRAY_BUFFER, uvbo) glVertexAttribPointer( 1, # attribute. No particular reason for 1, but must match the layout in the shader. 2, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # Draw the triangle ! glDrawArrays(GL_TRIANGLES, 0, 12*3) # 3 indices starting at 0 -> 1 triangle glDisableVertexAttribArray(0) glDisableVertexAttribArray(1) # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() glfw.terminate()
def main(): # Initialize the library if not glfw.init(): raise RuntimeError("Failed to initialize GLFW") glfw.window_hint(glfw.SAMPLES, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # To make MacOS happy; should not be needed glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # Create a windowed mode window and its OpenGL context window = glfw.create_window(1024, 768, "Tutorial 16 - Shadows", None, None) if not window: glfw.terminate() raise RuntimeError("Failed to open GLFW window.") # Make the window's context current glfw.make_context_current(window) # We would expect width and height to be 1024 and 768 windowWidth = 1024 windowHeight = 768 # But on MacOS X with a retina screen it'll be 1024*2 and 768*2, # so we get the actual framebuffer size: windowWidth, windowHeight = glfw.get_framebuffer_size(window) # Ensure we can capture the escape key being pressed below glfw.set_input_mode(window, glfw.STICKY_KEYS, True) # Hide the mouse and enable unlimited mouvement glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) # Set the mouse at the center of the screen glfw.poll_events() glfw.set_cursor_pos(window, 1024 / 2, 768 / 2) # Dark blue background glClearColor(0.0, 0.0, 0.4, 0.0) # Enable depth test glEnable(GL_DEPTH_TEST) # Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS) vao = glGenVertexArrays(1) glBindVertexArray(vao) # Create and compile our GLSL program from the shaders depth_shader = Shader("DepthRTT.vertexshader", "DepthRTT.fragmentshader") depth_shader.compile() # Create Controls object controls = Controls(window) # Load the texture using any two methods Texture = loadDDS("uvmap3.DDS") # Read our .obj file vertices, uvs, normals = loadOBJ("room_thickwalls.obj", invert_v=True) indices, indexed_vertices, indexed_uvs, indexed_normals = indexVBO( vertices, uvs, normals) # Create the vertex buffer object vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, indexed_vertices.nbytes, indexed_vertices, GL_STATIC_DRAW) # Create the uv buffer object uvbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, uvbo) glBufferData(GL_ARRAY_BUFFER, indexed_uvs.nbytes, indexed_uvs, GL_STATIC_DRAW) # Create normal buffer object nbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, nbo) glBufferData(GL_ARRAY_BUFFER, indexed_normals.nbytes, indexed_normals, GL_STATIC_DRAW) # Generate a buffer for the indices as well ibo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, ibo) glBufferData(GL_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW) # --------------------------------------------- # Render to Texture - specific code begins here # --------------------------------------------- # The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer. fbo = glGenFramebuffers(1) glBindFramebuffer(GL_FRAMEBUFFER, fbo) # Depth texture. Slower than a depth buffer, but you can sample it later in your shader depthTexture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, depthTexture) glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, np.zeros((1024, 1024), dtype=np.uint16)) #glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT16,1024,1024,0,GL_DEPTH_COMPONENT,GL_FLOAT,0) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE) glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0) # No color output in the bound framebuffer, only depth. glDrawBuffer(GL_NONE) # Always check that our framebuffer is ok if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE: raise RuntimeError("Framebuffer not OK!") # Create and compile our GLSL program from the shaders shader = Shader("ShadowMapping.vertexshader", "ShadowMapping.fragmentshader") shader.compile() # For speed computation lastTime = glfw.get_time() nbFrames = 0 # Loop until the user closes the window while (glfw.get_key(window, glfw.KEY_ESCAPE) != glfw.PRESS and glfw.window_should_close(window) == 0): # Measure speed currentTime = glfw.get_time() nbFrames += 1 if currentTime - lastTime >= 1.0: # If last prinf() was more than 1sec ago # printf and reset print("{:.5f} ms/frame".format(1000.0 / float(nbFrames))) nbFrames = 0 lastTime += 1.0 # Render to our framebuffer glBindFramebuffer(GL_FRAMEBUFFER, fbo) glViewport(0, 0, 1024, 1024) # Render on the whole framebuffer, complete from the lower left corner to the upper right # We don't use bias in the shader, but instead we draw back faces, # which are already separated from the front faces by a small distance # (if your geometry is made this way) glEnable(GL_CULL_FACE) glCullFace( GL_BACK ) # Cull back-facing triangles -> draw only front-facing triangles # Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Use our shader depth_shader.enable() lightInvDir = np.array([0.5, 2, 2]) depthProjectionMatrix = glm_ortho(-10., 10., -10., 10., -10., 20.) depthViewMatrix = glm_lookAt(lightInvDir, [0., 0., 0.], [0., 1., 0.]) depthModelMatrix = np.eye(4) depthMVP = np.array(np.dot(np.dot(depthModelMatrix, depthViewMatrix), depthProjectionMatrix), dtype=np.float32) # Send our transformation to the currently bound shader, # in the "MVP" uniform depth_shader.setUniform("depthMVP", "mat4", depthMVP) # 1rst attribute buffer : vertices glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo) glVertexAttribPointer( 0, # The attribute we want to configure 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # Index buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo) # Draw the triangles ! glDrawElements( GL_TRIANGLES, # mode len(indices), # count GL_UNSIGNED_SHORT, # type None # element array buffer offset ) glDisableVertexAttribArray(0) # Render to the screen glBindFramebuffer(GL_FRAMEBUFFER, 0) glViewport(0, 0, windowWidth, windowHeight) # Render on the whole framebuffer, complete from the lower left corner to the upper right glEnable(GL_CULL_FACE) glCullFace(GL_BACK) # Cull back-facing triangles -> draw only front-facing triangles # Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Use our shader shader.enable() # Compute the MVP matrix from keyboard and mouse input controls.update() biasMatrix = np.array([[0.5, 0.0, 0.0, 0.0], [0.0, 0.5, 0.0, 0.0], [0.0, 0.0, 0.5, 0.0], [0.5, 0.5, 0.5, 1.0]]) depthBiasMVP = np.array(np.dot(depthMVP, biasMatrix), dtype=np.float32) # Send our transformation to the currently bound shader, # in the "MVP" uniform shader.setUniform("V", "mat4", np.array(controls.ViewMatrix, dtype=np.float32)) shader.setUniform("M", "mat4", np.array(controls.ModelMatrix, dtype=np.float32)) shader.setUniform("MVP", "mat4", controls.MVP) shader.setUniform("DepthBiasMVP", "mat4", depthBiasMVP) shader.setUniform("LightInvDirection_worldspace", "vec3", np.array(lightInvDir, dtype=np.float32)) # Bind our texture in Texture Unit 0 glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, Texture) # Set our "myTextureSampler" sampler to user Texture Unit 0 shader.setUniform("myTextureSampler", "sampler2D", 0) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, depthTexture) shader.setUniform("shadowMap", "sampler2DShadow", 1) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo) glVertexAttribPointer( 0, # attribute 0. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # 2nd attribute buffer : UVs glEnableVertexAttribArray(1) glBindBuffer(GL_ARRAY_BUFFER, uvbo) glVertexAttribPointer( 1, # attribute. 2, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # 3rd attribute buffer : UVs glEnableVertexAttribArray(2) glBindBuffer(GL_ARRAY_BUFFER, nbo) glVertexAttribPointer( 2, # attribute. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # Index buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo) # Draw the triangles ! glDrawElements( GL_TRIANGLES, # mode len(indices), # count GL_UNSIGNED_SHORT, # type None # element array buffer offset ) glDisableVertexAttribArray(0) glDisableVertexAttribArray(1) glDisableVertexAttribArray(2) # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() glfw.terminate()
def main(): # Initialize the library if not glfw.init(): raise RuntimeError("Failed to initialize GLFW") glfw.window_hint(glfw.SAMPLES, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # To make MacOS happy; should not be needed glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # Create a windowed mode window and its OpenGL context window = glfw.create_window(1024, 768, "Tutorial 04 - Colored Cube", None, None) if not window: glfw.terminate() raise RuntimeError("Failed to open GLFW window.") # Make the window's context current glfw.make_context_current(window) # Ensure we can capture the escape key being pressed below glfw.set_input_mode(window, glfw.STICKY_KEYS, True) # Dark blue background glClearColor(0.0, 0.0, 0.4, 0.0) # Enable depth test glEnable(GL_DEPTH_TEST) # Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS) vao = glGenVertexArrays(1) glBindVertexArray(vao) # Create and compile our GLSL program from the shaders shader = Shader("TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader") shader.compile() # Projection matrix : 45 deg Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units Projection = glm_perspective(45.0 * np.pi / 180., 4.0 / 3.0, 0.1, 100.0) # Camera matrix View = glm_lookAt( [4, 3, -3], # Camera is at (4,3,-3), in World Space [0, 0, 0], # and looks at the origin [0, 1, 0] # Head is up (set to 0,-1,0 to look upside-down) ) # Model matrix : an identity matrix (model will be at the origin) Model = np.eye(4) # Our ModelViewProjection : multiplication of our 3 matrices # (convert to 32-bit now that computations are done) MVP = np.array(np.dot(np.dot(Model, View), Projection), dtype=np.float32) # Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle. # A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices g_vertex_buffer_data = np.array([ -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0 ], dtype=np.float32) # One color for each vertex. They were generated randomly. g_color_buffer_data = np.array([ 0.583, 0.771, 0.014, 0.609, 0.115, 0.436, 0.327, 0.483, 0.844, 0.822, 0.569, 0.201, 0.435, 0.602, 0.223, 0.310, 0.747, 0.185, 0.597, 0.770, 0.761, 0.559, 0.436, 0.730, 0.359, 0.583, 0.152, 0.483, 0.596, 0.789, 0.559, 0.861, 0.639, 0.195, 0.548, 0.859, 0.014, 0.184, 0.576, 0.771, 0.328, 0.970, 0.406, 0.615, 0.116, 0.676, 0.977, 0.133, 0.971, 0.572, 0.833, 0.140, 0.616, 0.489, 0.997, 0.513, 0.064, 0.945, 0.719, 0.592, 0.543, 0.021, 0.978, 0.279, 0.317, 0.505, 0.167, 0.620, 0.077, 0.347, 0.857, 0.137, 0.055, 0.953, 0.042, 0.714, 0.505, 0.345, 0.783, 0.290, 0.734, 0.722, 0.645, 0.174, 0.302, 0.455, 0.848, 0.225, 0.587, 0.040, 0.517, 0.713, 0.338, 0.053, 0.959, 0.120, 0.393, 0.621, 0.362, 0.673, 0.211, 0.457, 0.820, 0.883, 0.371, 0.982, 0.099, 0.879 ], dtype=np.float32) # Create the vertex buffer object vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.nbytes, g_vertex_buffer_data, GL_STATIC_DRAW) # Create the vertex color buffer object cbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, cbo) glBufferData(GL_ARRAY_BUFFER, g_color_buffer_data.nbytes, g_color_buffer_data, GL_STATIC_DRAW) # Loop until the user closes the window while (glfw.get_key(window, glfw.KEY_ESCAPE) != glfw.PRESS and glfw.window_should_close(window) == 0): # Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Use our shader shader.enable() # Send our transformation to the currently bound shader, # in the "MVP" uniform shader.setUniform("MVP", "mat4", MVP) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vbo) glVertexAttribPointer( 0, # attribute 0. No particular reason for 0, but must match the layout in the shader. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # 2nd attribute buffer : colors glEnableVertexAttribArray(1) glBindBuffer(GL_ARRAY_BUFFER, cbo) glVertexAttribPointer( 1, # attribute. No particular reason for 1, but must match the layout in the shader. 3, # size GL_FLOAT, # type GL_FALSE, # normalized? 0, # stride None # array buffer offset ) # Draw the triangle ! glDrawArrays(GL_TRIANGLES, 0, 12 * 3) # 3 indices starting at 0 -> 1 triangle glDisableVertexAttribArray(0) glDisableVertexAttribArray(1) # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() del vao, vbo, shader glfw.terminate()