# cube Element Buffer Object glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_indices.nbytes, cube_indices, GL_STATIC_DRAW) # cube vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(0)) # cube textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(12)) textures = glGenTextures(1) load_texture("textures/crate.jpg", textures) # instance VBO instance_array = [] offset = 1 for z in range(0, 100, 2): for y in range(0, 100, 2): for x in range(0, 100, 2): translation = pyrr.Vector3([0.0, 0.0, 0.0]) translation.x = x + offset translation.y = y + offset translation.z = z + offset instance_array.append(translation) len_of_instance_array = len(
glBindVertexArray(VAO) # Cube Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, cube_buffer.nbytes, cube_buffer, GL_STATIC_DRAW) # Cube Element Buffer Object glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_indices.nbytes, cube_indices, GL_STATIC_DRAW) # Cube vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(0)) # Cube textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(12)) textures = glGenTextures(3) crate = load_texture("textures/crate.jpg", textures[0]) metal = load_texture("textures/metal.jpg", textures[1]) brick = load_texture("textures/brick.jpg", textures[2]) # picking texture and a frame buffer object pick_texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, pick_texture) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1280, 720, 0, GL_RGB, GL_FLOAT, None) FBO = glGenFramebuffers(1) glBindFramebuffer(GL_FRAMEBUFFER, FBO) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pick_texture, 0) glBindFramebuffer(GL_FRAMEBUFFER, 0) glBindTexture(GL_TEXTURE_2D, 0) glUseProgram(shader)
##Vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, dh_buffer.itemsize * 8, ctypes.c_void_p(0)) ##textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, dh_buffer.itemsize * 8, ctypes.c_void_p(12)) ##normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, dh_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(1) load_texture("meshes/final.png", textures) ##load_texture("meshes/metal.jpg",textures) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glEnable(GL_CULL_FACE) glEnable(GL_TEXTURE_2D) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glPointSize(5.0) shadowMapSize = 512 #These are spotlight, world rotation, and camera settings. The light position and direction are updated every frame rotation = 0
# floor vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(0)) # floor textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(12)) # floor normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(3) load_texture("meshes/cube.jpg", textures[0]) load_texture("meshes/monkey.jpg", textures[1]) load_texture("meshes/floor.jpg", textures[2]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix( 45, WIDTH / HEIGHT, 0.1, 100) cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([6, 4, 0])) monkey_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-4, 4, -4])) floor_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 0]))
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]) glBufferData(GL_ARRAY_BUFFER, monkey_buffer.nbytes, monkey_buffer, GL_STATIC_DRAW) # monkey vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(0)) # monkey textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(12)) # monkey normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(2) load_texture("meshes/chair_texture.png", textures[0]) load_texture("meshes/table_texture.jpg", textures[1]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, 1280 / 720, 0.1, 100) chibi_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, -10])) monkey_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, -1.5, 3])) # eye, target, up view = pyrr.matrix44.create_look_at(pyrr.Vector3([0, 0, 8]), pyrr.Vector3([0, 0, 0]), pyrr.Vector3([0, 1, 0]))
# floor EBO glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[2]) glBufferData(GL_ELEMENT_ARRAY_BUFFER, floor_indices.nbytes, floor_indices, GL_STATIC_DRAW) # floor vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(0)) # floor textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(12)) # floor normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(4) load_texture("meshes/table.jpg", textures[0]) load_texture("meshes/sofa1.jpg", textures[1]) load_texture("meshes/tv2.jpg", textures[2]) load_texture("meshes/floor.jpg", textures[3]) glUseProgram(shader) glClearColor(252.0/255.0, 220.0/255.0, 223.0/255.0, 1.0) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, 1280 / 720, 0.1, 100) sofa_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 20])) tv_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, -5])) floor_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 0]))
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, plane_indices.nbytes, plane_indices, GL_STATIC_DRAW) # plane vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, plane_buffer.itemsize * 5, ctypes.c_void_p(0)) # plane textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, plane_buffer.itemsize * 5, ctypes.c_void_p(12)) textures = glGenTextures(2) load_texture("meshes/chibi.png", textures[0]) # create texture for the plane glBindTexture(GL_TEXTURE_2D, textures[1]) # texture wrapping params glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # texture filtering params glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1280, 720, 0, GL_RGBA, GL_UNSIGNED_BYTE, None) glBindTexture(GL_TEXTURE_2D, 0) depth_buff = glGenRenderbuffers(1) glBindRenderbuffer(GL_RENDERBUFFER, depth_buff)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) # Element Buffer Object EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertices.itemsize * 5, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vertices.itemsize * 5, ctypes.c_void_p(12)) texture = glGenTextures(3) cube1_texture = load_texture("textures/crate.jpg", texture[0]) cube2_texture = load_texture("textures/earth.png", texture[1]) cube3_texture = load_texture("textures/smile.png", texture[2]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix( 45, 1280 / 720, 0.1, 100) # projection = pyrr.matrix44.create_orthogonal_projection_matrix(0, 1280, 1, 720, -1000, 1000) t_cube1 = pyrr.matrix44.create_from_translation(pyrr.Vector3((1, 0, 0))) t_cube2 = pyrr.matrix44.create_from_translation(pyrr.Vector3((-1, 0, 0))) t_cube3 = pyrr.matrix44.create_from_translation(pyrr.Vector3((0, 1, -3)))
glBufferData(GL_ARRAY_BUFFER, triangle_vertices.nbytes, triangle_vertices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, triangle_vertices.itemsize * 6, ctypes.c_void_p(0)) glEnableVertexAttribArray(2) glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, triangle_vertices.itemsize * 6, ctypes.c_void_p(12)) # glBindVertexArray(0) textures = glGenTextures(2) cube_texture = load_texture( "/home/melvin/Documents/Python/OpenGL/Attila/textures/crate.jpg", textures[0]) quad_texture = load_texture( "/home/melvin/Documents/Python/OpenGL/Attila/textures/cat.png", textures[1]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix( 45, 1280 / 720, 0.1, 100) cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([1, 0, 0])) quad_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-1, 0, 0]))
# tower vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, tower_buffer.itemsize * 8, ctypes.c_void_p(0)) # tower textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, tower_buffer.itemsize * 8, ctypes.c_void_p(12)) # tower normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, tower_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(4) load_texture("meshes/outono.jpg", textures[0]) load_texture("meshes/outono.jpg", textures[1]) load_texture("meshes/solo.jpg", textures[2]) load_texture("meshes/towertex.jpg", textures[3]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, WIDTH / HEIGHT, 0.1, 100) walled_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 0])) trees_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 0])) floor_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 0])) tower_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 0]))
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW) # Element Buffer Object EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertices.itemsize * 5, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vertices.itemsize * 5, ctypes.c_void_p(12)) texture = glGenTextures(3) cube1_texture = load_texture("textures/crate.jpg", texture[0]) cube2_texture = load_texture("textures/cat.png", texture[1]) cube3_texture = load_texture("textures/smiley.png", texture[2]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, 1280/720, 0.1, 100) cube1 = pyrr.matrix44.create_from_translation(pyrr.Vector3([1, 0, 0])) cube2 = pyrr.matrix44.create_from_translation(pyrr.Vector3([-1, 0, 0])) cube3 = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 1, -3])) # eye, target, up
def main(): global shader vertex_src = """ # version 330 layout(location = 0) in vec3 a_position; layout(location = 1) in vec2 a_texture; layout(location = 2) in vec3 a_normal; uniform mat4 model; uniform mat4 projection; uniform mat4 view; out vec2 v_texture; void main() { gl_Position = projection * view * model * vec4(a_position, 1.0); v_texture = a_texture; } """ fragment_src = """ # version 330 in vec2 v_texture; out vec4 out_color; uniform sampler2D s_texture; void main() { out_color = texture(s_texture, v_texture); } """ # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") if "Windows" in pltf.platform(): glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) else: glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # creating the window window = glfw.create_window(1280, 720, "My OpenGL window", None, None) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(window, 100, 100) # set the callback function for window resize glfw.set_window_size_callback(window, window_resize_clb) # set the mouse position callback glfw.set_cursor_pos_callback(window, mouse_look_clb) # set the keyboard input callback glfw.set_key_callback(window, key_input_clb) # capture the mouse cursor glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) # make the context current glfw.make_context_current(window) # load here the 3d meshes chibi_indices, chibi_buffer = ObjLoader.load_model("meshes/chibi.obj") plane_buffer = [ 0.0, 0.0, 0.0, 0.0, 0.0, 10.0, 0.0, 0.0, 1.0, 0.0, 10.0, 10.0, 0.0, 1.0, 1.0, 0.0, 10.0, 0.0, 0.0, 1.0 ] plane_buffer = np.array(plane_buffer, dtype=np.float32) plane_indices = [0, 1, 2, 2, 3, 0] plane_indices = np.array(plane_indices, dtype=np.uint32) # VAO and VBO VAO = glGenVertexArrays(2) VBO = glGenBuffers(2) EBO = glGenBuffers(1) # Chibi VAO glBindVertexArray(VAO[0]) shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER)) # Chibi Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[0]) glBufferData(GL_ARRAY_BUFFER, chibi_buffer.nbytes, chibi_buffer, GL_STATIC_DRAW) # chibi vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, chibi_buffer.itemsize * 8, ctypes.c_void_p(0)) # chibi textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, chibi_buffer.itemsize * 8, ctypes.c_void_p(12)) # chibi normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, chibi_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) # Plane VAO glBindVertexArray(VAO[1]) # Plane Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[1]) glBufferData(GL_ARRAY_BUFFER, plane_buffer.nbytes, plane_buffer, GL_STATIC_DRAW) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, plane_indices.nbytes, plane_indices, GL_STATIC_DRAW) # plane vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, plane_buffer.itemsize * 5, ctypes.c_void_p(0)) # plane textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, plane_buffer.itemsize * 5, ctypes.c_void_p(12)) textures = glGenTextures(2) load_texture("meshes/chibi.png", textures[0]) # create texture for the plane glBindTexture(GL_TEXTURE_2D, textures[1]) # texture wrapping params glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # texture filtering params glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1280, 720, 0, GL_RGBA, GL_UNSIGNED_BYTE, None) glBindTexture(GL_TEXTURE_2D, 0) depth_buff = glGenRenderbuffers(1) glBindRenderbuffer(GL_RENDERBUFFER, depth_buff) glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1280, 720) FBO = glGenFramebuffers(1) glBindFramebuffer(GL_FRAMEBUFFER, FBO) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buff) glBindFramebuffer(GL_FRAMEBUFFER, 0) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix( 45, 1280 / 720, 0.1, 100) chibi_pos_main = pyrr.matrix44.create_from_translation( pyrr.Vector3([0, 0, -5])) plane_pos = pyrr.matrix44.create_from_translation( pyrr.Vector3([-20, -3, -10])) model_loc = glGetUniformLocation(shader, "model") proj_loc = glGetUniformLocation(shader, "projection") view_loc = glGetUniformLocation(shader, "view") glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUseProgram(0) # the main application loop while not glfw.window_should_close(window): glfw.poll_events() do_movement() glClearColor(0, 0.1, 0.1, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) view = cam.get_view_matrix() glUseProgram(shader) glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time()) model = pyrr.matrix44.multiply(rot_y, chibi_pos_main) # draw the chibi character glBindVertexArray(VAO[0]) glBindTexture(GL_TEXTURE_2D, textures[0]) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, len(chibi_indices)) # draw the chibi to the custom frame buffer glBindFramebuffer(GL_FRAMEBUFFER, FBO) glClearColor(0.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glDrawArrays(GL_TRIANGLES, 0, len(chibi_indices)) glBindVertexArray(0) glBindFramebuffer(GL_FRAMEBUFFER, 0) # draw the plane glBindVertexArray(VAO[1]) glBindTexture(GL_TEXTURE_2D, textures[1]) glUniformMatrix4fv(model_loc, 1, GL_FALSE, plane_pos) glDrawElements(GL_TRIANGLES, len(plane_indices), GL_UNSIGNED_INT, None) glUseProgram(0) glfw.swap_buffers(window) # terminate glfw, free up allocated resources glfw.terminate()
def main(): global shader vertex_src = """ # version 330 layout(location = 0) in vec3 a_position; layout(location = 1) in vec2 a_texture; layout(location = 2) in vec3 a_offset; uniform mat4 model; uniform mat4 projection; uniform mat4 view; uniform mat4 move; out vec2 v_texture; void main() { vec3 final_pos = a_position + a_offset; gl_Position = projection * view * move * model * vec4(final_pos, 1.0f); v_texture = a_texture; } """ fragment_src = """ # version 330 in vec2 v_texture; out vec4 out_color; uniform sampler2D s_texture; void main() { out_color = texture(s_texture, v_texture); } """ # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") if "Windows" in pltf.platform(): glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) else: glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # creating the window window = glfw.create_window(WIDTH, HEIGHT, "My OpenGL window", None, None) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(window, 100, 100) # set the callback function for window resize glfw.set_window_size_callback(window, window_resize_clb) # set the mouse position callback glfw.set_cursor_pos_callback(window, mouse_look_clb) # set the keyboard input callback glfw.set_key_callback(window, key_input_clb) # capture the mouse cursor glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) # make the context current glfw.make_context_current(window) # positions texture_coords cube_buffer = [-0.5, -0.5, 0.5, 0.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, 0.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 0.5, -0.5, 0.5, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 0.0, -0.5, -0.5, -0.5, 1.0, 0.0, -0.5, -0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5, -0.5, 0.5, 1.0, 1.0, -0.5, -0.5, 0.5, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0, 0.0, -0.5, 0.5, -0.5, 1.0, 0.0, -0.5, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 1.0] cube_buffer = np.array(cube_buffer, dtype=np.float32) cube_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] cube_indices = np.array(cube_indices, dtype=np.uint32) # VAO, VBO and EBO VAO = glGenVertexArrays(1) VBO = glGenBuffers(1) EBO = glGenBuffers(1) # cube VAO glBindVertexArray(VAO) shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER)) # cube Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, cube_buffer.nbytes, cube_buffer, GL_STATIC_DRAW) # cube Element Buffer Object glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_indices.nbytes, cube_indices, GL_STATIC_DRAW) # cube vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(0)) # cube textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(12)) textures = glGenTextures(1) load_texture("textures/crate.jpg", textures) # instance VBO instance_array = [] offset = 1 for z in range(0, 100, 2): for y in range(0, 100, 2): for x in range(0, 100, 2): translation = pyrr.Vector3([0.0, 0.0, 0.0]) translation.x = x + offset translation.y = y + offset translation.z = z + offset instance_array.append(translation) len_of_instance_array = len(instance_array) # do this before you flatten the array instance_array = np.array(instance_array, np.float32).flatten() instanceVBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, instanceVBO) glBufferData(GL_ARRAY_BUFFER, instance_array.nbytes, instance_array, GL_STATIC_DRAW) glEnableVertexAttribArray(2) glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0)) glVertexAttribDivisor(2, 1) # 1 means, every instance will have it's own translate glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, WIDTH / HEIGHT, 0.1, 100) cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-50.0, -50.0, -200.0])) model_loc = glGetUniformLocation(shader, "model") proj_loc = glGetUniformLocation(shader, "projection") view_loc = glGetUniformLocation(shader, "view") move_loc = glGetUniformLocation(shader, "move") glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, cube_pos) glUseProgram(0) # the main application loop while not glfw.window_should_close(window): glfw.poll_events() # do_movement() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) move = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, glfw.get_time()*8])) glUseProgram(shader) glUniformMatrix4fv(move_loc, 1, GL_FALSE, move) view = cam.get_view_matrix() glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glDrawElementsInstanced(GL_TRIANGLES, len(cube_indices), GL_UNSIGNED_INT, None, len_of_instance_array) glUseProgram(0) glfw.swap_buffers(window) # terminate glfw, free up allocated resources glfw.terminate()
# monkey vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(0)) # monkey textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(12)) # monkey normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(2) load_texture("meshes/chibi.png", textures[0]) load_texture("meshes/monkey.jpg", textures[1]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix( 45, 1280 / 720, 0.1, 100) chibi_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, -5, -10])) monkey_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-4, 0, 0])) # eye, target, up view = pyrr.matrix44.create_look_at(pyrr.Vector3([0, 0, 8]),
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]) glBufferData(GL_ARRAY_BUFFER, monkey_buffer.nbytes, monkey_buffer, GL_STATIC_DRAW) # monkey vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(0)) # monkey textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(12)) # monkey normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(2) load_texture("textures/earth.png", textures[0]) load_texture("textures/crate.jpg", textures[1]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, 1280 / 720, 0.1, 100) chibi_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-4, 0, 0])) monkey_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([4, 0, 0])) # eye, target, up view = pyrr.matrix44.create_look_at(pyrr.Vector3([0, 0, 8]), pyrr.Vector3([0, 0, 0]), pyrr.Vector3([0, 1, 0]))
def turn_right(self): self.headAngle += self.TURN_DELTA rot = pyrr.matrix33.create_from_y_rotation(self.TURN_DELTA) self.headVector = pyrr.Vector3(pyrr.matrix33.apply_to_vector(rot, self.headVector)) car = Car() shader = compileProgram( compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER) ) texture = glGenTextures(2) load_texture("textures/crate.jpg", texture[0]) quad_texture = load_texture("textures/uv_grid.jpg", texture[1]) # Car ############################################# car_VAO = glGenVertexArrays(1) glBindVertexArray(car_VAO) car_VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, car_VBO) glBufferData(GL_ARRAY_BUFFER, car.buffer.nbytes, car.buffer, GL_STATIC_DRAW) car_EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, car_EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, car.indices.nbytes, car.indices, GL_STATIC_DRAW)
# Triangle Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[2]) glBufferData(GL_ARRAY_BUFFER, triangle_vertices.nbytes, triangle_vertices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, triangle_vertices.itemsize * 6, ctypes.c_void_p(0)) glEnableVertexAttribArray(2) glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, triangle_vertices.itemsize * 6, ctypes.c_void_p(12)) textures = glGenTextures(2) cube_texture = load_texture("textures/crate.jpg", textures[0]) quad_texture = load_texture("textures/cat.png", textures[1]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix( 45, 1280 / 720, 0.1, 100) cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([1, 0, 0])) quad_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-1, 0, 0])) triangle_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 1, -1])) # eye, target, up
##Vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, dh_buffer.itemsize * 8, ctypes.c_void_p(0)) ##textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, dh_buffer.itemsize * 8, ctypes.c_void_p(12)) ##normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, dh_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(2) load_texture("meshes/final.png", textures[0]) ##load_texture("meshes/metal.jpg",textures) glEnable(GL_TEXTURE_2D) ##PLANE VAO plane_buffer = [ 0.0, 0.0, 0.0, 0.0, 0.0, 10.0, 0.0, 0.0, 1.0, 0.0, 10.0, 0.0, 10.0, 1.0, 1.0, 0.0, 0.0, 10.0, 0.0, 1.0 ] '''plane_buffer = [ ## positions ##normals ##texcoords 25.0, -0.5, 25.0, 0.0, 1.0, 0.0, 25.0, 0.0, -25.0, -0.5, 25.0, 0.0, 1.0, 0.0, 0.0, 0.0, -25.0, -0.5, -25.0, 0.0, 1.0, 0.0, 0.0, 25.0, 25.0, -0.5, 25.0, 0.0, 1.0, 0.0, 25.0, 0.0,
pass def turn_right(self): pass car = Car() shader = compileProgram( compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER) ) texture = glGenTextures(2) load_texture("textures/pig_diffuse.jpg", texture[0]) quad_texture = load_texture("textures/uv_grid.jpg", texture[1]) # Car ############################################# car_VAO = glGenVertexArrays(1) glBindVertexArray(car_VAO) car_VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, car_VBO) glBufferData(GL_ARRAY_BUFFER, car.buffer.nbytes, car.buffer, GL_STATIC_DRAW) car_EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, car_EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, car.indices.nbytes, car.indices, GL_STATIC_DRAW)
# Triangle Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[2]) glBufferData(GL_ARRAY_BUFFER, triangle_vertices.nbytes, triangle_vertices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, triangle_vertices.itemsize * 6, ctypes.c_void_p(0)) glEnableVertexAttribArray(2) glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, triangle_vertices.itemsize * 6, ctypes.c_void_p(12)) ################################################### texture = glGenTextures(2) cube_texture = load_texture("textures/crate.jpg", texture[0]) quad_texture = load_texture("textures/smile.png", texture[1]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix( 45, 1280 / 720, 0.1, 100) cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3((1, 0, 0))) quad_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3((-1, 0, 0))) triangle_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3((0, 1, -1))) view = pyrr.matrix44.create_look_at( pyrr.Vector3((0.5, 0, 3)), # eye
def main(): global shader vertex_src = """ # version 330 layout(location = 0) in vec3 a_position; layout(location = 1) in vec2 a_texture; layout(location = 2) in vec3 a_normal; uniform mat4 model; uniform mat4 projection; uniform mat4 view; out vec2 v_texture; void main() { gl_Position = projection * view * model * vec4(a_position, 1.0); v_texture = a_texture; } """ fragment_src = """ # version 330 in vec2 v_texture; out vec4 out_color; uniform sampler2D s_texture; void main() { out_color = texture(s_texture, v_texture); } """ # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") if "Windows" in pltf.platform(): glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) else: glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # creating the window window = glfw.create_window(WIDTH, HEIGHT, "My OpenGL window", None, None) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(window, 100, 100) # set the callback function for window resize glfw.set_window_size_callback(window, window_resize_clb) # set the mouse position callback glfw.set_cursor_pos_callback(window, mouse_look_clb) # set the keyboard input callback glfw.set_key_callback(window, key_input_clb) # capture the mouse cursor glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) # make the context current glfw.make_context_current(window) # load here the 3d meshes cube_indices, cube_buffer = ObjLoader.load_model("meshes/cube.obj") monkey_indices, monkey_buffer = ObjLoader.load_model("meshes/monkey.obj") floor_indices, floor_buffer = ObjLoader.load_model("meshes/floor.obj") # VAO and VBO VAO = glGenVertexArrays(3) VBO = glGenBuffers(3) # cube VAO glBindVertexArray(VAO[0]) shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER)) # cube Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[0]) glBufferData(GL_ARRAY_BUFFER, cube_buffer.nbytes, cube_buffer, GL_STATIC_DRAW) # cube vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 8, ctypes.c_void_p(0)) # cube textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 8, ctypes.c_void_p(12)) # cube normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) # monkey VAO glBindVertexArray(VAO[1]) # monkey Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[1]) glBufferData(GL_ARRAY_BUFFER, monkey_buffer.nbytes, monkey_buffer, GL_STATIC_DRAW) # monkey vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(0)) # monkey textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(12)) # monkey normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, monkey_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) # floor VAO glBindVertexArray(VAO[2]) # floor Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[2]) glBufferData(GL_ARRAY_BUFFER, floor_buffer.nbytes, floor_buffer, GL_STATIC_DRAW) # floor vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(0)) # floor textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(12)) # floor normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, floor_buffer.itemsize * 8, ctypes.c_void_p(20)) glEnableVertexAttribArray(2) textures = glGenTextures(3) load_texture("meshes/cube.jpg", textures[0]) load_texture("meshes/monkey.jpg", textures[1]) load_texture("meshes/floor.jpg", textures[2]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, WIDTH / HEIGHT, 0.1, 100) cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([6, 4, 0])) monkey_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-4, 4, -4])) floor_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 0, 0])) model_loc = glGetUniformLocation(shader, "model") proj_loc = glGetUniformLocation(shader, "projection") view_loc = glGetUniformLocation(shader, "view") glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUseProgram(0) # the main application loop while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) view = cam.get_view_matrix() glUseProgram(shader) glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time()) model = pyrr.matrix44.multiply(rot_y, cube_pos) # draw the cube glBindVertexArray(VAO[0]) glBindTexture(GL_TEXTURE_2D, textures[0]) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, len(monkey_indices)) # draw the monkey glBindVertexArray(VAO[1]) glBindTexture(GL_TEXTURE_2D, textures[1]) glUniformMatrix4fv(model_loc, 1, GL_FALSE, monkey_pos) glDrawArrays(GL_TRIANGLES, 0, len(monkey_indices)) # draw the floor glBindVertexArray(VAO[2]) glBindTexture(GL_TEXTURE_2D, textures[2]) glUniformMatrix4fv(model_loc, 1, GL_FALSE, floor_pos) glDrawArrays(GL_TRIANGLES, 0, len(floor_indices)) glUseProgram(0) glfw.swap_buffers(window) # terminate glfw, free up allocated resources glfw.terminate()
# Element Buffer Object EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertices.itemsize * 5, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vertices.itemsize * 5, ctypes.c_void_p(12)) # Here we generate our two textures texture = glGenTextures(2) cube_texture1 = load_texture("/home/melvin/Documents/Python/OpenGL/Attila/textures/crate.jpg", texture[0]) cube_texture2 = load_texture("/home/melvin/Documents/Python/OpenGL/Attila/textures/brick.jpg", texture[1]) glUseProgram(shader) glClearColor(0, 0.3, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, 1280/720, 0.1, 100) # Here we say what the cube translations will be cube1_translation = pyrr.matrix44.create_from_translation(pyrr.Vector3([5, 0, -10])) cube2_translation = pyrr.matrix44.create_from_translation(pyrr.Vector3([-1, 0, -3]))
def main(): global shader vertex_src = """ # version 330 layout(location = 0) in vec3 a_position; layout(location = 1) in vec2 a_texture; layout(location = 2) in vec3 a_color; uniform mat4 model; uniform mat4 projection; uniform mat4 view; out vec3 v_color; out vec2 v_texture; void main() { gl_Position = projection * view * model * vec4(a_position, 1.0); v_texture = a_texture; v_color = a_color; } """ fragment_src = """ # version 330 in vec2 v_texture; in vec3 v_color; out vec4 out_color; uniform int switcher; uniform sampler2D s_texture; void main() { if (switcher == 0){ out_color = texture(s_texture, v_texture); } else if (switcher == 1){ out_color = vec4(v_color, 1.0); } } """ # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") if "Windows" in pltf.platform(): glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) else: glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # creating the window window = glfw.create_window(1280, 720, "My OpenGL window", None, None) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(window, 100, 100) # set the callback function for window resize glfw.set_window_size_callback(window, window_resize) # make the context current glfw.make_context_current(window) cube_vertices = [-0.5, -0.5, 0.5, 0.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, 0.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 0.5, -0.5, 0.5, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 0.0, -0.5, -0.5, -0.5, 1.0, 0.0, -0.5, -0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5, -0.5, 0.5, 1.0, 1.0, -0.5, -0.5, 0.5, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0, 0.0, -0.5, 0.5, -0.5, 1.0, 0.0, -0.5, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 1.0] cube_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] cube_vertices = np.array(cube_vertices, dtype=np.float32) cube_indices = np.array(cube_indices, dtype=np.uint32) quad_vertices = [-0.5, -0.5, 0, 0.0, 0.0, 0.5, -0.5, 0, 1.0, 0.0, 0.5, 0.5, 0, 1.0, 1.0, -0.5, 0.5, 0, 0.0, 1.0] quad_indices = [0, 1, 2, 2, 3, 0] quad_vertices = np.array(quad_vertices, dtype=np.float32) quad_indices = np.array(quad_indices, dtype=np.uint32) triangle_vertices = [-0.5, -0.5, 0, 1, 0, 0, 0.5, -0.5, 0, 0, 1, 0, 0.0, 0.5, 0, 0, 0, 1] triangle_vertices = np.array(triangle_vertices, dtype=np.float32) # Cube VAO VAO = glGenVertexArrays(3) VBO = glGenBuffers(3) EBO = glGenBuffers(2) # Cube VAO glBindVertexArray(VAO[0]) shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER)) # Cube Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[0]) glBufferData(GL_ARRAY_BUFFER, cube_vertices.nbytes, cube_vertices, GL_STATIC_DRAW) # Cube Element Buffer Object glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[0]) glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_indices.nbytes, cube_indices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube_vertices.itemsize * 5, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube_vertices.itemsize * 5, ctypes.c_void_p(12)) # Quad VAO glBindVertexArray(VAO[1]) # Quad Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[1]) glBufferData(GL_ARRAY_BUFFER, quad_vertices.nbytes, quad_vertices, GL_STATIC_DRAW) # Quad Element Buffer Object glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[1]) glBufferData(GL_ELEMENT_ARRAY_BUFFER, quad_indices.nbytes, quad_indices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube_vertices.itemsize * 5, ctypes.c_void_p(0)) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube_vertices.itemsize * 5, ctypes.c_void_p(12)) # Triangle VAO glBindVertexArray(VAO[2]) # Triangle Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[2]) glBufferData(GL_ARRAY_BUFFER, triangle_vertices.nbytes, triangle_vertices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, triangle_vertices.itemsize * 6, ctypes.c_void_p(0)) glEnableVertexAttribArray(2) glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, triangle_vertices.itemsize * 6, ctypes.c_void_p(12)) textures = glGenTextures(2) cube_texture = load_texture("textures/crate.jpg", textures[0]) quad_texture = load_texture("textures/cat.png", textures[1]) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, 1280 / 720, 0.1, 100) cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([1, 0, 0])) quad_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-1, 0, 0])) triangle_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0, 1, -1])) # eye, target, up view = pyrr.matrix44.create_look_at(pyrr.Vector3([0, 0, 3]), pyrr.Vector3([0, 0, 0]), pyrr.Vector3([0, 1, 0])) model_loc = glGetUniformLocation(shader, "model") proj_loc = glGetUniformLocation(shader, "projection") view_loc = glGetUniformLocation(shader, "view") switcher_loc = glGetUniformLocation(shader, "switcher") glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUseProgram(0) # the main application loop while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(shader) glUniform1i(switcher_loc, 0) rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time()) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time()) rotation = pyrr.matrix44.multiply(rot_x, rot_y) model = pyrr.matrix44.multiply(rotation, cube_pos) glBindVertexArray(VAO[0]) glBindTexture(GL_TEXTURE_2D, textures[0]) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glDrawElements(GL_TRIANGLES, len(cube_indices), GL_UNSIGNED_INT, None) model = pyrr.matrix44.multiply(rot_x, quad_pos) glBindVertexArray(VAO[1]) glBindTexture(GL_TEXTURE_2D, textures[1]) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glDrawElements(GL_TRIANGLES, len(quad_indices), GL_UNSIGNED_INT, None) model = pyrr.matrix44.multiply(rot_y, triangle_pos) glBindVertexArray(VAO[2]) glUniform1i(switcher_loc, 1) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 3) glUseProgram(0) glfw.swap_buffers(window) # terminate glfw, free up allocated resources glfw.terminate()
def main(): global shader vertex_src = """ # version 330 layout(location = 0) in vec3 a_position; layout(location = 1) in vec2 a_texture; uniform mat4 model; uniform mat4 projection; uniform mat4 view; out vec2 v_texture; void main() { gl_Position = projection * view * model * vec4(a_position, 1.0); v_texture = a_texture; } """ fragment_src = """ # version 330 in vec2 v_texture; out vec4 out_color; uniform sampler2D s_texture; uniform ivec3 icolor; uniform int switcher; void main() { if(switcher == 0){ out_color = texture(s_texture, v_texture); }else{ out_color = vec4(icolor.r/255.0, icolor.g/255.0, icolor.b/255.0, 1.0); } } """ # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") if "Windows" in pltf.platform(): glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) else: glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # creating the window window = glfw.create_window(1280, 720, "My OpenGL window", None, None) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(window, 100, 100) # set the callback function for window resize glfw.set_window_size_callback(window, window_resize_clb) # set the mouse position callback glfw.set_cursor_pos_callback(window, mouse_pos_clb) # set the button press position callback glfw.set_mouse_button_callback(window, mouse_button_callback) # make the context current glfw.make_context_current(window) cube_buffer = [-0.5, -0.5, 0.5, 0.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, 0.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 0.5, -0.5, 0.5, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 0.0, -0.5, -0.5, -0.5, 1.0, 0.0, -0.5, -0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5, -0.5, 0.5, 1.0, 1.0, -0.5, -0.5, 0.5, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0, 0.0, -0.5, 0.5, -0.5, 1.0, 0.0, -0.5, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 1.0] cube_buffer = np.array(cube_buffer, dtype=np.float32) cube_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] cube_indices = np.array(cube_indices, dtype=np.uint32) # VAO and VBO VAO = glGenVertexArrays(1) VBO = glGenBuffers(1) EBO = glGenBuffers(1) # Cube VAO glBindVertexArray(VAO) shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER)) # Cube Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, cube_buffer.nbytes, cube_buffer, GL_STATIC_DRAW) # Cube Element Buffer Object glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_indices.nbytes, cube_indices, GL_STATIC_DRAW) # Cube vertices glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(0)) # Cube textures glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(12)) textures = glGenTextures(3) crate = load_texture("textures/crate.jpg", textures[0]) metal = load_texture("textures/metal.jpg", textures[1]) brick = load_texture("textures/brick.jpg", textures[2]) # picking texture and a frame buffer object pick_texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, pick_texture) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1280, 720, 0, GL_RGB, GL_FLOAT, None) FBO = glGenFramebuffers(1) glBindFramebuffer(GL_FRAMEBUFFER, FBO) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pick_texture, 0) glBindFramebuffer(GL_FRAMEBUFFER, 0) glBindTexture(GL_TEXTURE_2D, 0) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) projection = pyrr.matrix44.create_perspective_projection_matrix(45, 1280 / 720, 0.1, 100) view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -4.0])) cube_positions = [(-2.0, 0.0, 0.0), (0.0, 0.0, 0.0), (2.0, 0.0, 0.0)] pick_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] model_loc = glGetUniformLocation(shader, "model") proj_loc = glGetUniformLocation(shader, "projection") view_loc = glGetUniformLocation(shader, "view") icolor_loc = glGetUniformLocation(shader, "icolor") switcher_loc = glGetUniformLocation(shader, "switcher") glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUseProgram(0) glClearColor(0, 0.1, 0.1, 1) # the main application loop while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) rot_y = pyrr.Matrix44.from_y_rotation(glfw.get_time() * 2) glUseProgram(shader) # draw to the default frame buffer glUniform1i(switcher_loc, 0) for i in range(len(cube_positions)): model = pyrr.matrix44.create_from_translation(cube_positions[i]) if i == 0: glBindTexture(GL_TEXTURE_2D, crate) if red_rot: glUniformMatrix4fv(model_loc, 1, GL_FALSE, rot_y @ model) else: glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) elif i == 1: glBindTexture(GL_TEXTURE_2D, metal) if green_rot: glUniformMatrix4fv(model_loc, 1, GL_FALSE, rot_y @ model) else: glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) else: glBindTexture(GL_TEXTURE_2D, brick) if blue_rot: glUniformMatrix4fv(model_loc, 1, GL_FALSE, rot_y @ model) else: glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glDrawElements(GL_TRIANGLES, len(cube_indices), GL_UNSIGNED_INT, None) # draw to the custom frame buffer object - pick buffer glUniform1i(switcher_loc, 1) glBindFramebuffer(GL_FRAMEBUFFER, FBO) glClearColor(0.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) for i in range(len(cube_positions)): pick_model = pyrr.matrix44.create_from_translation(cube_positions[i]) glUniform3iv(icolor_loc, 1, pick_colors[i]) glUniformMatrix4fv(model_loc, 1, GL_FALSE, pick_model) glDrawElements(GL_TRIANGLES, len(cube_indices), GL_UNSIGNED_INT, None) if picker: pick() glBindFramebuffer(GL_FRAMEBUFFER, 0) glUseProgram(0) glfw.swap_buffers(window) # terminate glfw, free up allocated resources glfw.terminate()