def __init__(self): mesh = ObjLoader() mesh.load_model("../models/monkey.obj") num_verts = len(mesh.model_vertices) // 3 self.batch = pyglet.graphics.Batch() self.verts = self.batch.add(num_verts, GL_TRIANGLES, None, ('v3f', mesh.model_vertices), ('t2f', mesh.model_textures)) shader = ShaderLoader.compile_shader("shaders/video_13_vert.glsl", "shaders/video_13_frag.glsl") glUseProgram(shader) # vertices glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, self.verts.vertices) glEnableVertexAttribArray(0) # textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, self.verts.tex_coords) glEnableVertexAttribArray(1) projection = matrix44.create_perspective_projection_matrix( 45.0, 1280 / 720, 0.1, 100.0).flatten().astype("float32") view = matrix44.create_from_translation(Vector3( [0.0, 0.0, -2.0])).flatten().astype("float32") self.translation = matrix44.create_from_translation( Vector3([0.0, 0.0, -2.0])) c_projection = numpy.ctypeslib.as_ctypes(projection) c_view = numpy.ctypeslib.as_ctypes(view) view_loc = glGetUniformLocation(shader, b"view") proj_loc = glGetUniformLocation(shader, b"projection") self.model_loc = glGetUniformLocation(shader, b"model") glUniformMatrix4fv(view_loc, 1, GL_FALSE, c_view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, c_projection) # region texture settings and loading texture = GLuint(0) glGenTextures(1, texture) glBindTexture(GL_TEXTURE_2D, texture) # set the texture wrapping glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # set the texture filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # endregion image = pyglet.image.load('../models/monkey.jpg') image_data = image.get_data('RGB', image.pitch) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data)
def __init__(self): self.verts = pyglet.graphics.vertex_list( 3, ('v3f', (-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0)), ('c3f', (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0))) shader = ShaderLoader.compile_shader("shaders/video_11_vert.glsl", "shaders/video_11_frag.glsl") glUseProgram(shader) # vertices glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, self.verts.vertices) glEnableVertexAttribArray(0) # colors glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, self.verts.colors) glEnableVertexAttribArray(1)
def init(): shader = ShaderLoader.compile_shader("shaders/video_13_vert.glsl", "shaders/video_13_frag.glsl") glUseProgram(shader) view = matrix44.create_from_translation(Vector3( [0.0, 0.0, -2.0])).flatten().astype("float32") projection = matrix44.create_perspective_projection_matrix( 45.0, 1280 / 720, 0.1, 100.0).flatten().astype("float32") c_view = numpy.ctypeslib.as_ctypes(view) c_projection = numpy.ctypeslib.as_ctypes(projection) view_loc = glGetUniformLocation(shader, b"view") proj_loc = glGetUniformLocation(shader, b"projection") Shader.model_loc = glGetUniformLocation(shader, b"model") glUniformMatrix4fv(view_loc, 1, GL_FALSE, c_view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, c_projection)
def addProgram(self, r_mode="default", base_folder="./"): if r_mode in self.program: print("already compiled shader: " + base_folder + "shader/" + r_mode) self.curr_mode = r_mode return None self.curr_mode = r_mode if self.__extractCase() is "": print("undefined case in " + self.name + " renderer, go to child") return "" self.program[self.curr_mode] = self.Program(0, 0, 0, 0) self.program[self.curr_mode].fbo = self.__GenFBO() self.program[self.curr_mode].shader = ShaderLoader.compile_shader( base_folder + "shader/" + self.__extractCase() + "_vs.shader", base_folder + "shader/" + self.__extractCase() + "_fs.shader") self.program[self.curr_mode].MVP_loc = glGetUniformLocation( self.program[self.curr_mode].shader, "MVP") self.program[self.curr_mode].MV_loc = glGetUniformLocation( self.program[self.curr_mode].shader, "MV") # self.program[name].cam_pos_loc = glGetUniformLocation(self.program[name].shader, "cam_pos") glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(100.0) return None
def main(): # initialize glfw if not glfw.init(): return w_width, w_height = 1920, 1080 aspect_ratio = w_width / w_height window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) glfw.set_key_callback(window, key_callback) glfw.set_cursor_pos_callback(window, mouse_callback) glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) cube = ObjLoader() cube.load_model("res/cube/cube.obj") cube_shader = ShaderLoader.compile_shader("shaders/video_09_cube.vs", "shaders/video_09_cube.fs") cube_tex = TextureLoader.load_texture("res/cube/cube_texture.jpg") cube_texture_offset = len(cube.vertex_index) * 12 monkey = ObjLoader() monkey.load_model("res/monkey/monkey.obj") monkey_shader = ShaderLoader.compile_shader("shaders/video_09_monkey.vs", "shaders/video_09_monkey.fs") monkey_tex = TextureLoader.load_texture("res/monkey/monkey.jpg") monkey_texture_offset = len(monkey.vertex_index) * 12 monster = ObjLoader() monster.load_model("res/monster/monster.obj") monster_shader = ShaderLoader.compile_shader( "shaders/video_09_monster.vs", "shaders/video_09_monster.fs") monster_tex = TextureLoader.load_texture("res/monster/monster.jpg") monster_texture_offset = len(monster.vertex_index) * 12 cube_vao = glGenVertexArrays(1) glBindVertexArray(cube_vao) cube_vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, cube_vbo) glBufferData(GL_ARRAY_BUFFER, cube.model.itemsize * len(cube.model), cube.model, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.model.itemsize * 2, ctypes.c_void_p(cube_texture_offset)) glEnableVertexAttribArray(1) glBindVertexArray(0) monkey_vao = glGenVertexArrays(1) glBindVertexArray(monkey_vao) monkey_vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, monkey_vbo) glBufferData(GL_ARRAY_BUFFER, monkey.model.itemsize * len(monkey.model), monkey.model, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monkey.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monkey.model.itemsize * 2, ctypes.c_void_p(monkey_texture_offset)) glEnableVertexAttribArray(1) glBindVertexArray(0) monster_vao = glGenVertexArrays(1) glBindVertexArray(monster_vao) monster_vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, monster_vbo) glBufferData(GL_ARRAY_BUFFER, monster.model.itemsize * len(monster.model), monster.model, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monster.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monster.model.itemsize * 2, ctypes.c_void_p(monster_texture_offset)) glEnableVertexAttribArray(1) glBindVertexArray(0) glClearColor(0.13, 0.2, 0.15, 1.0) glEnable(GL_DEPTH_TEST) projection = matrix44.create_perspective_projection_matrix( 45.0, aspect_ratio, 0.1, 100.0) cube_model = matrix44.create_from_translation(Vector3([-4.0, 0.0, -3.0])) monkey_model = matrix44.create_from_translation(Vector3([0.0, 0.0, -3.0])) monster_model = matrix44.create_from_translation(Vector3([0.0, 0.0, -10.0])) glUseProgram(cube_shader) cube_model_loc = glGetUniformLocation(cube_shader, "model") cube_view_loc = glGetUniformLocation(cube_shader, "view") cube_proj_loc = glGetUniformLocation(cube_shader, "proj") glUniformMatrix4fv(cube_model_loc, 1, GL_FALSE, cube_model) glUniformMatrix4fv(cube_proj_loc, 1, GL_FALSE, projection) glUseProgram(0) glUseProgram(monkey_shader) monkey_model_loc = glGetUniformLocation(monkey_shader, "model") monkey_view_loc = glGetUniformLocation(monkey_shader, "view") monkey_proj_loc = glGetUniformLocation(monkey_shader, "proj") glUniformMatrix4fv(monkey_model_loc, 1, GL_FALSE, monkey_model) glUniformMatrix4fv(monkey_proj_loc, 1, GL_FALSE, projection) glUseProgram(0) glUseProgram(monster_shader) monster_model_loc = glGetUniformLocation(monster_shader, "model") monster_view_loc = glGetUniformLocation(monster_shader, "view") monster_proj_loc = glGetUniformLocation(monster_shader, "proj") glUniformMatrix4fv(monster_model_loc, 1, GL_FALSE, monster_model) glUniformMatrix4fv(monster_proj_loc, 1, GL_FALSE, projection) glUseProgram(0) while not glfw.window_should_close(window): glfw.poll_events() do_movement() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) view = cam.get_view_matrix() glBindVertexArray(cube_vao) glUseProgram(cube_shader) glBindTexture(GL_TEXTURE_2D, cube_tex) glUniformMatrix4fv(cube_view_loc, 1, GL_FALSE, view) glDrawArrays(GL_TRIANGLES, 0, len(cube.vertex_index)) glUseProgram(0) glBindVertexArray(0) glBindVertexArray(monkey_vao) glBindTexture(GL_TEXTURE_2D, monkey_tex) glUseProgram(monkey_shader) glUniformMatrix4fv(monkey_view_loc, 1, GL_FALSE, view) glDrawArrays(GL_TRIANGLES, 0, len(monkey.vertex_index)) glUseProgram(0) glBindVertexArray(0) glBindVertexArray(monster_vao) glBindTexture(GL_TEXTURE_2D, monster_tex) glUseProgram(monster_shader) glUniformMatrix4fv(monster_view_loc, 1, GL_FALSE, view) glDrawArrays(GL_TRIANGLES, 0, len(monster.vertex_index)) glUseProgram(0) glBindVertexArray(0) glfw.swap_buffers(window) glfw.terminate()
def main(): # initialize glfw if not glfw.init(): return w_width, w_height = 800, 600 #glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) obj = ObjLoad() obj.load_model("naxramas.obj") texture_offset = len(obj.vertex_index) * 12 normal_offset = (texture_offset + len(obj.texture_index) * 8) shader = ShaderLoader.compile_shader("shaders/vertex_shader.txt", "shaders/fragment_shader.txt") VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #texture glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset)) glEnableVertexAttribArray(1) #normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image image = Image.open("DefaultMaterial_Base_Color.png") flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) angledist = [0, 5] camera = [0, 0, 5] eye = [0, 0, 0] currentColor = [1] color1 = [0.8, 0.8, 0.8] color2 = [(229 / 255), (9 / 255), (127 / 255)] projection = pyrr.matrix44.create_perspective_projection_matrix( 65.0, w_width / w_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation( pyrr.Vector3([0.0, -2.0, 0.0])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") light_loc = glGetUniformLocation(shader, "light") color_loc = glGetUniformLocation(shader, "colorr") def keyGetting(): if ((glfw.get_key(window, glfw.KEY_D) == glfw.PRESS) or (glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS)): angledist[0] += 5 updateCamera() elif ((glfw.get_key(window, glfw.KEY_A) == glfw.PRESS) or (glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS)): angledist[0] -= 5 updateCamera() if ((glfw.get_key(window, glfw.KEY_W) == glfw.PRESS) or (glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS)): cameraUp() if ((glfw.get_key(window, glfw.KEY_S) == glfw.PRESS) or (glfw.get_key(window, glfw.KEY_DOWN) == glfw.PRESS)): cameraDown() if ((glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS)): if (angledist[1] < 10): angledist[1] += 0.5 updateCamera() if ((glfw.get_key(window, glfw.KEY_E) == glfw.PRESS)): if (angledist[1] > 2): angledist[1] -= 0.5 updateCamera() if ((glfw.get_key(window, glfw.KEY_R) == glfw.PRESS)): reset() if ((glfw.get_key(window, glfw.KEY_1) == glfw.PRESS)): changeColor1() if ((glfw.get_key(window, glfw.KEY_2) == glfw.PRESS)): changeColor2() def updateCamera(): datos = get_camera_x_z(angledist[0], angledist[1]) camera[0] = datos[1] camera[2] = datos[0] def reset(): camera[0] -= camera[0] camera[1] -= camera[1] camera[2] -= (camera[2] - 5) angledist[0] -= angledist[0] angledist[1] -= (angledist[1] - 5) eye[1] -= eye[1] def cameraUp(): if camera[1] < 2: camera[1] += 0.2 eye[1] += .2 def cameraDown(): if camera[1] > -2: eye[1] -= .2 camera[1] -= 0.2 def changeColor1(): currentColor[0] -= (currentColor[0] - 1) def changeColor2(): currentColor[0] -= (currentColor[0] - 2) while not glfw.window_should_close(window): glfw.poll_events() if (currentColor[0] == 1): glUniform3f(color_loc, 0.8, 0.8, 0.8) else: glUniform3f(color_loc, (229 / 255), (9 / 255), (127 / 255)) view = pyrr.matrix44.create_look_at(pyrr.Vector3(camera), pyrr.Vector3(eye), pyrr.Vector3([0, 1, 0])) glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glUniform3f(light_loc, 0, 0, 10) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glfw.swap_buffers(window) keyGetting() sleep(0.1) glfw.terminate()
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) ##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)
def main(): # initialize glfw if not glfw.init(): return w_width, w_height = 800, 600 glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) shader = ShaderLoader.compile_shader("shaders/video_18_vert.vs", "shaders/video_18_frag.fs") obj_fn = "res/monkey/monkey_smooth.obj" # obj_fn = "res/cube.obj" # obj = obj.Obj(obj) obj = init_vbo(obj_fn) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image # image = Image.open("res/cube_texture.jpg") # image = Image.open("res/old_stone_floor_texture_01-512x512.png") # image = Image.open("res/seamless_animal_fur-texture.jpg") image = Image.open("res/BrickES1_128.jpg") flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -3.0])) projection = pyrr.matrix44.create_perspective_projection_matrix(65.0, w_width / w_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0.0])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") transform_loc = glGetUniformLocation(shader, "transform") light_loc = glGetUniformLocation(shader, "light") glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time() ) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time()) glUniformMatrix4fv(transform_loc, 1, GL_FALSE, rot_y) glUniformMatrix4fv(light_loc, 1, GL_FALSE, rot_y) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glfw.swap_buffers(window) glfw.terminate()
def main(): # initialize glfw if not glfw.glfwInit(): return w_width, w_height = 800, 600 #glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.glfwCreateWindow(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.glfwTerminate() return glfw.glfwMakeContextCurrent(window) # glfw.set_window_size_callback(window, window_resize) obj = ObjLoader() obj.load_model("res/monkey/monkey_smooth.obj") texture_offset = len(obj.vertex_index)*12 normal_offset = (texture_offset + len(obj.texture_index)*8) shader = ShaderLoader.compile_shader("shaders/video_18_vert.vs", "shaders/video_18_frag.fs") VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) #positions glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset)) glEnableVertexAttribArray(1) #normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image image = Image.open("res/monkey/monkey.jpg") flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -3.0])) projection = pyrr.matrix44.create_perspective_projection_matrix(65.0, w_width / w_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0.0])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") transform_loc = glGetUniformLocation(shader, "transform") light_loc = glGetUniformLocation(shader, "light") glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) while not glfw.glfwWindowShouldClose(window): glfw.glfwPollEvents() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time() ) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.glfwGetTime() ) glUniformMatrix4fv(transform_loc, 1, GL_FALSE, rot_y) glUniformMatrix4fv(light_loc, 1, GL_FALSE, rot_y) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glfw.glfwSwapBuffers(window) glfw.glfwTerminate()
def main(): global make_samples_count # initialize glfw if not glfw.init(): return w_width, w_height = 800, 800 #glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) glfw.set_key_callback(window, key_callback) glfw.set_cursor_pos_callback(window, cursor_pos_callback) glfw.set_mouse_button_callback(window, mouse_button_callback) glfw.set_scroll_callback(window, scroll_callback) obj = ObjLoader() obj.load_model("res/Rabbit.obj", scale=0.7) texture_offset = len(obj.vertex_index) * 12 normal_offset = (texture_offset + len(obj.texture_index) * 8) shader = ShaderLoader.compile_shader("shaders/video_18_vert.vs", "shaders/video_18_frag.fs") VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) #positions glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset)) glEnableVertexAttribArray(1) #normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image image = Image.open("res/Rabbit_D.tga") flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) focus_distance = 1.9 view = pyrr.matrix44.create_from_translation( pyrr.Vector3([0.0, 0.0, -focus_distance])) projection = pyrr.matrix44.create_perspective_projection_matrix( 65.0, w_width / w_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0.0])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") transform_loc = glGetUniformLocation(shader, "transform") light_loc = glGetUniformLocation(shader, "light") glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if make_samples_count > 0: rabbit_transform = pyrr.Matrix44.identity(float) rabbit_transform[3, 0] = pos_x rabbit_transform[3, 1] = pos_y rabbit_transform[3, 2] = 0 rot = pyrr.Matrix44.from_x_rotation(random.random() * 2.0 * math.pi) rot *= pyrr.Matrix44.from_y_rotation(random.random() * 2.0 * math.pi) #rot *= pyrr.Matrix44.from_z_rotation(random.random()*2.0*math.pi) trans = pyrr.Matrix44.identity(float) trans[3, 0] = random.random() * 1.0 - 0.5 trans[3, 1] = random.random() * 0.7 - 0.35 trans[3, 2] = 0. #random.random()*0.2 trans[3, 3] = 1. #1./(0.7 + random.random() * 0.3) # scale eye_angle = eye_distance / focus_distance #q = pyrr.Quaternion.from_matrix(rot) mat = trans * rot * rabbit_transform #stereo_filename = str(trans[3,0]) + '_' + str(trans[3,1]) + '_' + str(trans[3,2]) + '_' + str(q[0]) + '_' + str(q[1]) + '_' + str(q[2]) + '_' + str(q[3]) + '_' + str(trans[3,3]) + '_' + str(x_rot) + '_' + str(y_rot) + '_' + str(z_rot) stereo_filename = str(rot[0, 0]) + '_' + str( rot[0, 1]) + '_' + str(rot[0, 2]) + '_' + str( rot[1, 0]) + '_' + str(rot[1, 1]) + '_' + str( rot[1, 2]) + '_' + str(trans[3, 0]) + '_' + str( trans[3, 1]) + '_' + str(trans[3, 2]) + '_' + str( trans[3, 3]) """ right_eye_transform = pyrr.Matrix44.from_y_rotation(eye_angle/2) left_eye_transform = pyrr.Matrix44.from_y_rotation(-eye_angle/2) """ glUniformMatrix4fv(transform_loc, 1, GL_FALSE, mat) #right_eye_transform*mat) glUniformMatrix4fv(light_loc, 1, GL_FALSE, mat) #right_eye_transform*mat) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glfw.swap_buffers(window) right_array = snapToNumpy() """ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUniformMatrix4fv(transform_loc, 1, GL_FALSE, left_eye_transform*mat) glUniformMatrix4fv(light_loc, 1, GL_FALSE, left_eye_transform*mat) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glfw.swap_buffers(window) left_array = snapToNumpy() tmp_array = np.stack([left_array,right_array], axis=2).reshape(left_array.shape[0],left_array.shape[1],6) array = np.concatenate((tmp_array[:,:,:3],tmp_array[:,:,3:]), axis=1) """ array = right_array[:, :, :3] if make_samples_count < 201: save_to_jpg("./test/" + stereo_filename + '.png', array) else: save_to_jpg("./train/" + stereo_filename + '.png', array) #render_to_jpg(stereo_filename+'_l.png') #time.sleep(2.0) make_samples_count -= 1 else: rot = pyrr.Matrix44.from_x_rotation(rot_b) rot *= pyrr.Matrix44.from_y_rotation(rot_a) trans = pyrr.Matrix44.identity(float) trans[3, 0] = pos_x trans[3, 1] = pos_y mat = rot * trans #print(trans[3,0],trans[3,1],trans[3,2]) glUniformMatrix4fv(transform_loc, 1, GL_FALSE, mat) glUniformMatrix4fv(light_loc, 1, GL_FALSE, mat) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glfw.swap_buffers(window) glfw.terminate()
def main(): # initialize glfw if not glfw.init(): return w_width, w_height = 1920, 1080 aspect_ratio = w_width / w_height window = glfw.create_window(w_width, w_height, "3D HOUSE", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) glfw.set_key_callback(window, key_callback) glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) obj = ObjLoader() obj.load_model("resources/houseobj.obj") obj_shader = ShaderLoader.compile_shader("shaders/video_09_vert.vs", "shaders/video_09_monster.fs") obj_tex = textureloader.load_texture("resources/Lightmap.jpg") obj_texture_offset = len(obj.vertex_index) * 12 obj_vao = glGenVertexArrays(1) glBindVertexArray(obj_vao) obj_vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, obj_vbo) glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(obj_texture_offset)) glEnableVertexAttribArray(1) glBindVertexArray(0) glClearColor(0.0, 0.0, 0.0, 1.0) glEnable(GL_DEPTH_TEST) projection = matrix44.create_perspective_projection_matrix( 90.0, aspect_ratio, 1.0, 100.0) obj_model = matrix44.create_from_translation(Vector3([0.0, 0.0, -5.0])) glUseProgram(obj_shader) obj_model_loc = glGetUniformLocation(obj_shader, "model") obj_view_loc = glGetUniformLocation(obj_shader, "view") obj_proj_loc = glGetUniformLocation(obj_shader, "proj") glUniformMatrix4fv(obj_model_loc, 1, GL_FALSE, obj_model) glUniformMatrix4fv(obj_proj_loc, 1, GL_FALSE, projection) glUseProgram(0) while not glfw.window_should_close(window): glfw.poll_events() do_movement() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) view = cam.get_view_matrix() glBindVertexArray(obj_vao) glBindTexture(GL_TEXTURE_2D, obj_tex) glUseProgram(obj_shader) glUniformMatrix4fv(obj_view_loc, 1, GL_FALSE, view) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glUseProgram(0) glBindVertexArray(0) glfw.swap_buffers(window) glfw.terminate()
def main(): # initialize glfw if not glfw.init(): return glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) w_width, w_height = 800, 600 #glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) obj = ObjLoader() obj.load_model("res/cube.obj") texture_offset = len(obj.vertex_index) * 12 VAO = glGenVertexArrays(1) glBindVertexArray(VAO) shader = ShaderLoader.compile_shader("shaders/video_17_vert.vs", "shaders/video_17_frag.fs") VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #texture glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset)) glEnableVertexAttribArray(1) glBindVertexArray(0) glBindBuffer(GL_ARRAY_BUFFER, 0) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image image = Image.open("res/cube_texture.jpg") flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -3.0])) projection = pyrr.matrix44.create_perspective_projection_matrix( 65.0, w_width / w_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0.0])) glUseProgram(shader) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glUseProgram(0) while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time()) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time()) glUseProgram(shader) transformLoc = glGetUniformLocation(shader, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y) glBindVertexArray(VAO) glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glBindVertexArray(0) glUseProgram(0) glfw.swap_buffers(window) glDeleteProgram(shader) glDeleteVertexArrays(1, [VAO]) glDeleteBuffers(1, [VBO]) glfw.terminate()
def main(): # initialize glfw if not glfw.init(): return w_width, w_height = 800, 600 aspect_ratio = w_width / w_height window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) glfw.set_key_callback(window, key_callback) glfw.set_cursor_pos_callback(window, mouse_callback) obj = ObjLoader() obj.load_model("objects/sphere.obj") texture_offset = len(obj.vertex_index) * 12 normal_offset = (texture_offset + len(obj.texture_index) * 8) shader = ShaderLoader.compile_shader("shaders/main_vert.vs", "shaders/main_frag.fs") VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) #positions position = glGetAttribLocation(shader, "position") glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(position) #textures texCoords = glGetAttribLocation(shader, "inTexCoords") glVertexAttribPointer(texCoords, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset)) glEnableVertexAttribArray(texCoords) #normals normals = glGetAttribLocation(shader, "vertNormal") glVertexAttribPointer(normals, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset)) glEnableVertexAttribArray(normals) for planet in planets: texture = TextureLoader.load_texture(planets[planet]['image_path']) planets[planet]['texture'] = texture #initial position if planet == 'sun' or planet == 'stars': distance_from_sun = planets[planet]['distance_from_sun'] planets[planet]['initial_position'] = [ distance_from_sun, 0.0, distance_from_sun ] else: distance_from_sun = 30.0 + planets[planet]['distance_from_sun'] x = round(random.uniform(-distance_from_sun, distance_from_sun), 3) z = round(math.sqrt((distance_from_sun**2) - (x**2)), 3) planets[planet]['initial_position'] = [x, 0.0, z] glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.0, 0.2, 0.2, 1.0) glEnable(GL_DEPTH_TEST) projection = pyrr.matrix44.create_perspective_projection_matrix( 65.0, w_width / w_height, 0.1, 10000.0) scale = pyrr.matrix44.create_from_scale(pyrr.Vector3([0.1, 0.1, 0.1])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") normal_loc = glGetUniformLocation(shader, "normalMatrix") scale_loc = glGetUniformLocation(shader, "scale") scale_planet_loc = glGetUniformLocation(shader, "scale_planet") glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(scale_loc, 1, GL_FALSE, scale) Global_ambient_loc = glGetUniformLocation(shader, "Global_ambient") Light_ambient_loc = glGetUniformLocation(shader, "Light_ambient") Light_diffuse_loc = glGetUniformLocation(shader, "Light_diffuse") Light_specular_loc = glGetUniformLocation(shader, "Light_specular") Light_location_loc = glGetUniformLocation(shader, "Light_location") Material_ambient_loc = glGetUniformLocation(shader, "Material_ambient") Material_diffuse_loc = glGetUniformLocation(shader, "Material_diffuse") Material_specular_loc = glGetUniformLocation(shader, "Material_specular") Material_shininess_loc = glGetUniformLocation(shader, "Material_shininess") glUniform4f(Global_ambient_loc, 0.8, 0.8, 0.9, 0.1) glUniform4f(Light_ambient_loc, 0.3, 0.3, 0.3, 1.0) glUniform4f(Light_diffuse_loc, 0.25, 0.25, 0.25, 1.0) glUniform4f(Light_specular_loc, 0.9, 0.9, 0.9, 1.0) glUniform3f(Light_location_loc, 0, 0, 0) # ******************************************************************************************* # obj2 = ObjLoader() # obj2.load_model("objects/cruiser.obj") # # shader2 = ShaderLoader.compile_shader("shaders/vert.vs", "shaders/frag.fs") # # VBO2 = glGenBuffers(1) # glBindBuffer(GL_ARRAY_BUFFER, VBO2) # glBufferData(GL_ARRAY_BUFFER, obj2.model.itemsize * len(obj2.model), obj2.model, GL_STATIC_DRAW) # # # positions # position2 = glGetAttribLocation(shader, "position") # glVertexAttribPointer(position2, 3, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 3, ctypes.c_void_p(0)) # glEnableVertexAttribArray(position2) # # textures # texCoords2 = glGetAttribLocation(shader2, "inTexCoords") # glVertexAttribPointer(texCoords2, 2, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 2, ctypes.c_void_p(texture_offset)) # glEnableVertexAttribArray(texCoords2) # # normals # normals2 = glGetAttribLocation(shader2, "vertNormal") # glVertexAttribPointer(normals2, 3, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 3, ctypes.c_void_p(normal_offset)) # glEnableVertexAttribArray(normals2) # # # texture2 = TextureLoader.load_texture(planets['cruiser']['image_path']) # distance_from_sun = planets['cruiser']['distance_from_sun'] # planets['cruiser']['initial_position'] = [distance_from_sun, 0.0, distance_from_sun] # # glUseProgram(shader2) # # # Global_ambient_loc = glGetUniformLocation(shader, "Global_ambient") # Light_ambient_loc = glGetUniformLocation(shader, "Light_ambient") # Light_diffuse_loc = glGetUniformLocation(shader, "Light_diffuse") # Light_specular_loc = glGetUniformLocation(shader, "Light_specular") # Light_location_loc = glGetUniformLocation(shader, "Light_location") # Material_ambient_loc = glGetUniformLocation(shader, "Material_ambient") # Material_diffuse_loc = glGetUniformLocation(shader, "Material_diffuse") # Material_specular_loc = glGetUniformLocation(shader, "Material_specular") # Material_shininess_loc = glGetUniformLocation(shader, "Material_shininess") # # glUniform4f(Global_ambient_loc, 0.8, 0.8, 0.9, 0.1) # glUniform4f(Light_ambient_loc, 0.3, 0.3, 0.3, 1.0) # glUniform4f(Light_diffuse_loc, 0.25, 0.25, 0.25, 1.0) # glUniform4f(Light_specular_loc, 0.9, 0.9, 0.9, 1.0) # glUniform3f(Light_location_loc, 0, 0, 0) # glUniform4f(Material_ambient_loc, 0.4, 0.4, 0.4, 1.0) # glUniform4f(Material_diffuse_loc, 0.15, 0.15, 0.15, 1.0) # glUniform4f(Material_specular_loc, 1.0, 1.0, 1.0, 1.0) # glUniform1f(Material_shininess_loc, .95) # ****************************************************************************************** while not glfw.window_should_close(window): glfw.poll_events() do_movement() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) time = glfw.get_time() view = cam.get_view_matrix() glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) # **********************************planets**************************************** for planet in planets: if planet == 'cruiser': glBindTexture(GL_TEXTURE_2D, texture2) else: glBindTexture(GL_TEXTURE_2D, planets[planet]['texture']) revolution_speed = time * planets[planet][ 'revolution_ratio_relative_to_earth'] rotation_speed = time * planets[planet][ 'rotation_ratio_relative_to_earth'] # scale planet scale_factor = planets[planet]['size_ratio_relative_to_earth'] scale_planet = matrix44.create_from_scale( pyrr.Vector3([scale_factor, scale_factor, scale_factor])) glUniformMatrix4fv(scale_planet_loc, 1, GL_FALSE, scale_planet) # translation model = matrix44.create_from_translation( pyrr.Vector3(planets[planet]['initial_position'])) revolution = matrix44.create_from_y_rotation(revolution_speed) rotation = matrix44.create_from_y_rotation(rotation_speed) # revolution about z axis model = matrix44.multiply(model, revolution) # rotation about own axis model = matrix44.multiply(rotation, model) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) # ----create normalMatrix-- modelView = numpy.matmul(view, model) # modelView = numpy.matmul(modelView, scale_planet) # modelView = numpy.matmul(modelView, scale) modelView33 = modelView[0:-1, 0:-1] normalMatrix = numpy.transpose(numpy.linalg.inv(modelView33)) # ----------------- glUniformMatrix3fv(normal_loc, 1, GL_FALSE, normalMatrix) a, b, c, d = planets[planet]['Material_ambient'] glUniform4f(Material_ambient_loc, a, b, c, d) a, b, c, d = planets[planet]['Material_diffuse'] glUniform4f(Material_diffuse_loc, a, b, c, d) a, b, c, d = planets[planet]['Material_specular'] glUniform4f(Material_specular_loc, a, b, c, d) s = planets[planet]['Material_shininess'][0] glUniform1f(Material_shininess_loc, s) if planet == 'cruiser': glDrawArrays(GL_TRIANGLES, 0, len(obj2.vertex_index)) else: glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) # ******************************************************************************* glfw.swap_buffers(window) glfw.terminate()
def __init__(self): mesh = ObjLoader() mesh.load_model("newCar.obj") num_verts = len(mesh.model_vertices) // 3 self.verts = pyglet.graphics.vertex_list(num_verts, ('v3f', mesh.model_vertices), ('t2f', mesh.model_textures), ('n3f', mesh.model_normals)) shader = ShaderLoader.compile_shader("shaders/vert.glsl", "shaders/frag.glsl") glUseProgram(shader) # vertices glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, self.verts.vertices) glEnableVertexAttribArray(0) # textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, self.verts.tex_coords) glEnableVertexAttribArray(1) # normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, self.verts.normals) glEnableVertexAttribArray(2) projection = matrix44.create_perspective_projection_matrix( 45.0, 1280 / 720, 0.1, 100.0).flatten().astype("float32") view = matrix44.create_from_translation(Vector3( [0.0, 0.0, -6.0])).flatten().astype("float32") model = matrix44.create_from_translation(Vector3( [0.0, 0.0, -1.0])).flatten().astype("float32") c_projection = numpy.ctypeslib.as_ctypes(projection) c_view = numpy.ctypeslib.as_ctypes(view) c_model = numpy.ctypeslib.as_ctypes(model) view_loc = glGetUniformLocation(shader, b"view") proj_loc = glGetUniformLocation(shader, b"projection") model_loc = glGetUniformLocation(shader, b"model") self.rotate_loc = glGetUniformLocation(shader, b'rotate') self.light_loc = glGetUniformLocation(shader, b"light") glUniformMatrix4fv(view_loc, 1, GL_FALSE, c_view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, c_projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, c_model) # texture settings and loading texture = GLuint(0) glGenTextures(1, texture) glBindTexture(GL_TEXTURE_2D, texture) # set the texture wrapping glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # set the texture filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) xmas = pyglet.image.load('car.png') image_data = xmas.get_data('RGB', xmas.pitch) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xmas.width, xmas.height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data)
##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) ##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") shader = ShaderLoader.compile_shader("shaders/shadow_mapping_depth.vs", "shaders/shadow_mapping_depth.fs") ##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, ctypes.c_void_p(0)) ##textures glEnableVertexAttribArray(1)
def main(): # initialize glfw if not glfw.glfwInit(): return w_width, w_height = 800, 600 #glfw.window_hint(glfw.RESIZABLE, GL_FALSE) window = glfw.glfwCreateWindow(w_width, w_height, "My OpenGL window", None, None) if not window: glfw.glfwTerminate() return glfw.glfwMakeContextCurrent(window) # glfw.set_window_size_callback(window, window_resize) # positions colors texture coords cube = [ -0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, -0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, -0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, -0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0, 0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0 ] cube = numpy.array(cube, dtype=numpy.float32) indices = [ 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8, 12, 13, 14, 14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20 ] indices = numpy.array(indices, dtype=numpy.uint32) shader = ShaderLoader.compile_shader("shaders/video_14_vert.vs", "shaders/video_14_frag.fs") VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube, GL_STATIC_DRAW) EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices), indices, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #color glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(12)) glEnableVertexAttribArray(1) #texture glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 8, ctypes.c_void_p(24)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image image = Image.open("res/egg.jpg") img_data = numpy.array(list(image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -3.0])) projection = pyrr.matrix44.create_perspective_projection_matrix( 45.0, w_width / w_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0.0])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) while not glfw.glfwWindowShouldClose(window): glfw.glfwPollEvents() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.glfwGetTime()) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.glfwGetTime()) transformLoc = glGetUniformLocation(shader, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y) glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None) glfw.glfwSwapBuffers(window) glfw.glfwTerminate()
glfw.set_key_callback(window, key_input_clb) ##capture the mouse cursor 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 ##DHARAHARA dh_indices, dh_buffer = ObjLoader.load_model("meshes/final.obj") ##dh_indices, dh_buffer=ObjLoader.load_model("meshes/cube.obj") ##LOADING SHADERS shader = ShaderLoader.compile_shader("shaders/shadow_mapping.vs", "shaders/shadow_mapping.fs") simpleDepthShader = ShaderLoader.compile_shader( "shaders/shadow_mapping_depth.vs", "shaders/shadow_mapping_depth.fs") debugDepthQuad = ShaderLoader.compile_shader("shaders/debug_quad.vs", "shaders/debug_quad_depth.fs") ##VAO AND VBO VAO = glGenVertexArrays(2) VBO = glGenBuffers(2) EBO = glGenBuffers(1) ##DHARAHARA VAO glBindVertexArray(VAO[0]) ##Vertex Buffer Object glBindBuffer(GL_ARRAY_BUFFER, VBO[0]) glBufferData(GL_ARRAY_BUFFER, dh_buffer.nbytes, dh_buffer, GL_STATIC_DRAW)
def main(): if not glfw.init(): return glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) w_width, w_height = 1440, 900 window = glfw.create_window(w_width, w_height, "My OpenGL Window", None, None) #window = glfw.create_window(w_width,w_height,"My OpenGL Window", glfw.get_primary_monitor(), None) if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_key_callback(window, key_callback) glfw.set_cursor_pos_callback(window, mouse_callback) glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) monkey_obj = ObjLoader() monkey_obj.load_model('monkey.obj') monkey_tex = TextureLoader.load_texture("res/monkey.png") monkey_tex_offset = len(monkey_obj.vertex_index) * 12 monkey_norm_offset = (monkey_tex_offset + len(monkey_obj.texture_index) * 8) sphere_obj = ObjLoader() sphere_obj.load_model('sphere2.obj') sphere_tex = TextureLoader.load_texture("res/yellow.png") sphere_tex_offset = len(sphere_obj.vertex_index) * 12 sphere_norm_offset = (sphere_tex_offset + len(sphere_obj.texture_index) * 8) shader = ShaderLoader.compile_shader("Shaders/vert.vs", "Shaders/frag.fs") sphere_vao = glGenVertexArrays(1) glBindVertexArray(sphere_vao) sphere_VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, sphere_VBO) glBufferData(GL_ARRAY_BUFFER, sphere_obj.model.itemsize * len(sphere_obj.model), sphere_obj.model, GL_STATIC_DRAW) #Position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sphere_obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #TextureCoords glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sphere_obj.model.itemsize * 2, ctypes.c_void_p(sphere_tex_offset)) glEnableVertexAttribArray(1) #Normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sphere_obj.model.itemsize * 3, ctypes.c_void_p(sphere_norm_offset)) glEnableVertexAttribArray(2) glBindVertexArray(0) monkey_vao = glGenVertexArrays(1) glBindVertexArray(monkey_vao) monkey_VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, monkey_VBO) glBufferData(GL_ARRAY_BUFFER, monkey_obj.model.itemsize * len(monkey_obj.model), monkey_obj.model, GL_STATIC_DRAW) #Position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monkey_obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #TextureCoords glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monkey_obj.model.itemsize * 2, ctypes.c_void_p(monkey_tex_offset)) glEnableVertexAttribArray(1) #Normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, monkey_obj.model.itemsize * 3, ctypes.c_void_p(monkey_norm_offset)) glEnableVertexAttribArray(2) glBindVertexArray(0) glClearColor(0.2, 0.3, 0.2, 1.0) glEnable(GL_DEPTH_TEST) projection = pyrr.matrix44.create_perspective_projection_matrix( 60.0, w_width / w_height, 0.1, 100.0) sphere_model = matrix44.create_from_translation(Vector3([-4.0, 0.0, -3.0])) monkey_model = matrix44.create_from_translation(Vector3([0.0, 0.0, -3.0])) glUseProgram(shader) model_loc = glGetUniformLocation(shader, "model") view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "proj") light_loc = glGetUniformLocation(shader, "light") glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) while not glfw.window_should_close(window): glfw.poll_events() do_movement() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) view = cam.get_view_matrix() glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) light_y = Matrix44.from_y_rotation(0.5) rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5) glUniformMatrix4fv(light_loc, 1, GL_FALSE, light_y) glBindVertexArray(sphere_vao) glBindTexture(GL_TEXTURE_2D, sphere_tex) glUniformMatrix4fv(model_loc, 1, GL_FALSE, Matrix44(sphere_model) * rot_y) glDrawArrays(GL_TRIANGLES, 0, len(sphere_obj.vertex_index)) glBindVertexArray(0) glBindVertexArray(monkey_vao) glBindTexture(GL_TEXTURE_2D, monkey_tex) glUniformMatrix4fv(model_loc, 1, GL_FALSE, monkey_model) glDrawArrays(GL_TRIANGLES, 0, len(monkey_obj.vertex_index)) glBindVertexArray(0) glfw.swap_buffers(window) glfw.terminate()
def load_texture(self): text_offset = len(self.deer_id.vertex_index) * 12 norm_offset = (text_offset + len(self.deer_id.texture_index) * 8) # create new shader program to render the 3D self.object shader = loader.compile_shader("res/shaders/vert.vs", "res/shaders/frag.fs") # Create new Vertex Buffer self.object to hold the vertices of the loaded model vbo = glGenBuffers(1) # Bind the current VBO glBindBuffer(GL_ARRAY_BUFFER, vbo) # Load the vertices to the vertex self.object; Static draw - we won't make changes to the self.object glBufferData(GL_ARRAY_BUFFER, self.deer_id.model.itemsize * len(self.deer_id.model), self.deer_id.model, GL_STATIC_DRAW) # set the position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FLOAT, self.deer_id.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) # set texture glVertexAttribPointer(1, 2, GL_FLOAT, GL_FLOAT, self.deer_id.model.itemsize * 2, ctypes.c_void_p(text_offset)) glEnableVertexAttribArray(1) # normals glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, self.deer_id.model.itemsize * 3, ctypes.c_void_p(norm_offset)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set texture wrapping params glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Set texture filtering params glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # image = Image.open("res/deer/texture.png") image = Image.open("res/deer/mat.png") flipped = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) # glEnable(GL_TEXTURE_2D) glUseProgram(shader) view = pyrr.matrix44.create_from_translation( pyrr.Vector3([0.0, 0.0, -3.0])) proj = pyrr.matrix44.create_perspective_projection_matrix( 60.0, 640 / 480, 0.1, 200.0) model = pyrr.matrix44.create_from_translation( pyrr.Vector3([0.0, 0.0, 0.0])) view_loc = glGetUniformLocation(shader, "view") proj_loc = glGetUniformLocation(shader, "projection") model_loc = glGetUniformLocation(shader, "model") glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, proj) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) y_rot_90 = [0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1] transform_loc = glGetUniformLocation(shader, "transform") glUniformMatrix4fv(transform_loc, 1, GL_FALSE, y_rot_90) glUseProgram(0) return texture
def create_shader(): InitShader.shader = ShaderLoader.compile_shader("shaders/video_14_vert.glsl", "shaders/video_14_frag.glsl") glUseProgram(InitShader.shader)
def main(): # Инициализация if not glfw.init(): return window_width, window_height = 800, 600 window = glfw.create_window( window_width, window_height, "[email protected] // Python 3.6.2", None, None) # если не удалось создать окно if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) obj = ObjLoader() obj.load_model('resource/face.obj') # Смещение текстуры и нормалей texture_offset = len(obj.vertex_index) * 12 normal_offset = (texture_offset + len(obj.texture_index) * 8) shader = sl.compile_shader('shaders/vertex.vs', 'shaders/fragment.fs') VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) # Позиции glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) # Текстуры glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset)) glEnableVertexAttribArray(1) # Нормали glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset)) glEnableVertexAttribArray(2) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Параметры обертки текстуры glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # Пармаетры фильтрации текстуры glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # Загрузка image = Image.open('resource/african_head_diffuse.tga') flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) glEnable(GL_TEXTURE_2D) glUseProgram(shader) # Фон glClearColor(0.0, 0.0, 0.1, 1.0) glEnable(GL_DEPTH_TEST) # Показать только ребра полигонов без текстур # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Настройки "камеры" view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -3.0])) projection = pyrr.matrix44.create_perspective_projection_matrix( 50.0, window_width / window_height, 0.1, 100.0) model = pyrr.matrix44.create_from_translation( pyrr.Vector3([0.0, 0.0, -1.0])) # x y z "отступы" # Работа с шейдером view_loc = glGetUniformLocation(shader, 'view') proj_loc = glGetUniformLocation(shader, 'projection') model_loc = glGetUniformLocation(shader, 'model') transform_loc = glGetUniformLocation(shader, 'transform') light_loc = glGetUniformLocation(shader, 'light') glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) # Главный цикл while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Матрицы вращения rot_x = pyrr.Matrix44.from_x_rotation(0.0 * glfw.get_time()) rot_y = pyrr.Matrix44.from_y_rotation(0.15 * glfw.get_time()) rot_z = pyrr.Matrix44.from_z_rotation(0.0 * glfw.get_time()) # Вращение модели по xyz glUniformMatrix4fv(transform_loc, 1, GL_FALSE, rot_x * rot_y * rot_z) glUniformMatrix4fv(light_loc, 1, GL_FALSE, rot_x * rot_y * rot_z * 5) # 5 == "сила света" glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index)) glfw.swap_buffers(window) # Выход glfw.terminate()
def main(): # comenzar glfw if not glfw.init(): return aspect_ratio = wwidth / wheight #creamos la ventana window = glfw.create_window(wwidth, wheight, "ProyectoGraficas", None, None) #terminar ventana if not window: glfw.terminate() return glfw.make_context_current(window) glfw.set_window_size_callback(window, window_resize) glfw.set_key_callback(window, key_callback) glfw.set_cursor_pos_callback(window, mouse_callback) glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) #carbamos el primer ObjLoader casa = ObjLoader() #buscamos el objeto en nuestras carpetas casa.load_model("obj/casa2.obj") #buscamos la textura en nuestras carpetas casa_tex = TextureLoader.load_texture("obj/pared.jpg") #calculamos casa_texture_offset = len(casa.vertex_index) * 12 #cargamos el segundo objeto monster = ObjLoader() #buscamos el obj en nuestras carpetas monster.load_model("obj/monster.obj") #buscamos la textura en nuestras carpetas monster_tex = TextureLoader.load_texture("obj/monster.jpg") #calculamos monster_texture_offset = len(monster.vertex_index) * 12 #obtenemos los shaders de nuestras carpetas. generic_shader = ShaderLoader.compile_shader( "shaders/generic_vertex_shader.vs", "shaders/generic_fragment_shader.fs") #------------------------------------casa--------------------------------------------------------------------------# #generamos nestras variaml casavao = glGenVertexArrays(1) glBindVertexArray(casavao) casavbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, casavbo) glBufferData(GL_ARRAY_BUFFER, casa.model.itemsize * len(casa.model), casa.model, GL_STATIC_DRAW) #posicion glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, casa.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #Texturas glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, casa.model.itemsize * 2, ctypes.c_void_p(casa_texture_offset)) glEnableVertexAttribArray(1) glBindVertexArray(0) #-------------------------------------------------------------------------------------------------------------------- #-------------------------------------------------------monstruo------------------------------------------------------# monster_vao = glGenVertexArrays(1) glBindVertexArray(monster_vao) monster_vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, monster_vbo) glBufferData(GL_ARRAY_BUFFER, monster.model.itemsize * len(monster.model), monster.model, GL_STATIC_DRAW) #position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, monster.model.itemsize * 3, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, monster.model.itemsize * 2, ctypes.c_void_p(monster_texture_offset)) glEnableVertexAttribArray(1) glBindVertexArray(0) #-----------------------------------------------------------------------------------------------------------------------# #colocamos el color negro glClearColor(0, 0, 0, 0) glEnable(GL_DEPTH_TEST) projection = matrix44.create_perspective_projection_matrix( 45.0, aspect_ratio, 0.1, 100.0) #traslacion del vector a la casa2 #colocar en el lugar casaModelo = matrix44.create_from_translation(Vector3([0.0, 0.0, -3.0])) #traslacion en el vectro3 al monstruo #colocar en el lugar monster_model = matrix44.create_from_translation(Vector3([0.0, 0.0, -10.0])) #shaders glUseProgram(generic_shader) model_loc = glGetUniformLocation(generic_shader, "model") view_loc = glGetUniformLocation(generic_shader, "view") proj_loc = glGetUniformLocation(generic_shader, "proj") glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection) #mientras la ventana while not glfw.window_should_close(window): glfw.poll_events() do_movement() #colocar la ventada de un color especifico glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) view = cam.get_view_matrix() glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) #rotaciones rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5) #--------------------------------------casa--------------------------------------# glBindVertexArray(casavao) glBindTexture(GL_TEXTURE_2D, casa_tex) glUniformMatrix4fv(model_loc, 1, GL_FALSE, casaModelo) glDrawArrays(GL_TRIANGLES, 0, len(casa.vertex_index)) glBindVertexArray(0) #--------------------------------------------------------------------------------# #---------------------------------------monstruo----------------------------------# glBindVertexArray(monster_vao) glBindTexture(GL_TEXTURE_2D, monster_tex) glUniformMatrix4fv(model_loc, 1, GL_FALSE, monster_model) glDrawArrays(GL_TRIANGLES, 0, len(monster.vertex_index)) glBindVertexArray(0) #-------------------------------------------------------------------------------------- #buffer de la ventana glfw.swap_buffers(window) #finalizamos glfw.terminate()
def __init__(self, VS, FS): self.Shader = ShaderLoader.compile_shader(str(VS), str(FS))