def loadModel(self, modelName) : print 'Load Model :', modelName objloader = ObjLoader() self.obj = objloader.load(modelName) texdata = self.obj.texture if texdata != None : glEnable(GL_TEXTURE_2D) texture=glGenTextures(1) glBindTexture( GL_TEXTURE_2D, texture); glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2Df(GL_TEXTURE_2D, 0, 4, 0, GL_RGBA, texdata) glBindTexture( GL_TEXTURE_2D, texture );
def init_vbo(obj_fn): """ :param obj_fn: :type obj_fn: str :return: :rtype: ObjLoader """ obj = ObjLoader() obj.load_model(obj_fn) texture_offset = len(obj.vertex_index) * 12 normal_offset = (texture_offset + len(obj.texture_index) * 8) 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) return obj
def import_renderable(self, filename): """Adds the specified renderable as a mesh section to this procedural mesh component""" # obj = ObjLoader.OBJ(filename) obj = ObjLoader.OBJ(filename) self.vertexArray = obj.vertices self.indexArray = obj.faces self.faces = [] for i in range(0, len(obj.faces), 3): self.faces.append( [obj.faces[i], obj.faces[i + 1], obj.faces[i + 2]]) # here is a trick, make sure vertex index same as normal index self.normalArray = [] for nor in obj.normals: self.normalArray.append(FVector(nor[0], nor[1], nor[2])) self.uvArray = [] self.colorArray = [] self.Tangents = [] vertexArray = [] for i in range(0, len(self.vertexArray)): vertexArray.append( FVector(self.vertexArray[i][0], self.vertexArray[i][1], self.vertexArray[i][2])) self.CreateMeshSection(self.CurrentMeshSectionIndex, vertexArray, self.indexArray, self.normalArray, UV0=self.uvArray, VertexColors=self.colorArray, bCreateCollision=True) print("*************ProceduralMesh Imported!**************")
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 load_model(): obj = ObjLoader() obj.load_model("res/deer/deer.obj") return obj
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(): if not glfw.init(): return w_width, w_height = 1280, 720 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) cube = [ -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 = numpy.array(cube, dtype=numpy.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 = numpy.array(cube_indices, dtype=numpy.uint32) monkey = ObjLoader() monkey.load_model("resources/monkey/monkey.obj") monkey_texture_offset = len(monkey.vertex_index) * 12 vertex_shader = """ #version 330 in layout(location = 0) vec3 position; in layout(location = 1) vec2 textCoords; uniform mat4 vp; uniform mat4 model; out vec2 outText; void main() { gl_Position = vp * model * vec4(position, 1.0f); outText = textCoords; } """ fragment_shader = """ #version 330 in vec2 outText; out vec4 outColor; uniform sampler2D renderedTexture; void main() { outColor = texture(renderedTexture, outText); } """ shader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER)) # cube VAO cube_vao = glGenVertexArrays(1) glBindVertexArray(cube_vao) cube_VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, cube_VBO) glBufferData(GL_ARRAY_BUFFER, cube.itemsize * len(cube), cube, GL_STATIC_DRAW) cube_EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_indices.itemsize * len(cube_indices), cube_indices, GL_STATIC_DRAW) # position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) # textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube.itemsize * 5, ctypes.c_void_p(12)) glEnableVertexAttribArray(1) glBindVertexArray(0) # monkey VAO 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) ########################################################################################### cube_texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, cube_texture) # 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, w_width, w_height, 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, w_width, w_height) FBO = glGenFramebuffers(1) glBindFramebuffer(GL_FRAMEBUFFER, FBO) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, cube_texture, 0) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buff) glBindFramebuffer(GL_FRAMEBUFFER, 0) ########################################################################################### monkey_texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, monkey_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("resources/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) glBindTexture(GL_TEXTURE_2D, 0) ########################################################################################### glEnable(GL_DEPTH_TEST) view = matrix44.create_from_translation(Vector3([0.0, 0.0, -2.0])) projection = matrix44.create_perspective_projection_matrix( 45.0, aspect_ratio, 0.1, 100.0) cube_position = matrix44.create_from_translation(Vector3([0.0, 0.0, 0.0])) monkey_position = matrix44.create_from_translation( Vector3([0.0, 0.0, -1.0])) vp = matrix44.multiply(view, projection) glUseProgram(shader) vp_loc = glGetUniformLocation(shader, "vp") model_loc = glGetUniformLocation(shader, "model") glUniformMatrix4fv(vp_loc, 1, GL_FALSE, vp) while not glfw.window_should_close(window): glfw.poll_events() glClearColor(0.2, 0.25, 0.27, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) cube_rot_y = pyrr.Matrix44.from_y_rotation(glfw.get_time() * 0.1) cube_rot_x = pyrr.Matrix44.from_x_rotation(glfw.get_time() * 0.1) cube_rot_all = pyrr.matrix44.multiply(cube_rot_y, cube_rot_x) monkey_rot_y = pyrr.Matrix44.from_y_rotation(glfw.get_time()) # draw to the custom frame buffer glBindFramebuffer(GL_FRAMEBUFFER, FBO) glViewport(0, 0, w_width, w_height) glClearColor(0.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glBindVertexArray(monkey_vao) glBindTexture(GL_TEXTURE_2D, monkey_texture) glUniformMatrix4fv(model_loc, 1, GL_FALSE, monkey_rot_y * monkey_position) glDrawArrays(GL_TRIANGLES, 0, len(monkey.vertex_index)) glBindVertexArray(0) glBindFramebuffer(GL_FRAMEBUFFER, 0) # draw to the default frame buffer glBindVertexArray(cube_vao) glViewport(0, 0, new_width, new_height) glBindTexture(GL_TEXTURE_2D, cube_texture) glUniformMatrix4fv(model_loc, 1, GL_FALSE, cube_rot_all * cube_position) glDrawElements(GL_TRIANGLES, len(cube_indices), GL_UNSIGNED_INT, None) glBindVertexArray(0) 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 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(): # Инициализация 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(): # 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()
g = rgb[i, 1] b = rgb[i, 2] hexCol = "#{:02x}{:02x}{:02x}".format(r, g, b) hexColors.append(hexCol) return hexColors def hex2rgb(hexcode): return tuple(map(ord, hexcode[1:].decode('hex'))) # check texture info pathToObjFile = projectDir + '/data/3dObjs/horses.obj' pathToTexture = projectDir + '/data/textures/horses.jpg' verts, texs, normals = ObjLoader.loadObj(pathToObjFile) textureMap = cv2.imread(pathToTexture) texMapWidth = textureMap.shape[1] texMapHeight = textureMap.shape[0] visibleVertices = [] visibleVerticesColors = [] visibleVerticesTrueColors = [] visibleVerticesIdxs = [] idxCounter = 0 for nPoint in range(cloud.shape[0]): pCam = cloud[nPoint, :] pIm = k.dot(pCam) pIm = pIm / (pIm[2])
ch_xyx = [1, 0, 1] look_at_xyz = [0, 0, -1] camera_up = [0, 1, 0] mouse_xy = [height // 2, width // 2] time_interval = 10 # angles for mouse view pitch = 0 yaw = -90 first_mouse = True # for first mouse response first_init = True # for loading textures once global texture ########################### ### loading objects ### obj = ol.ObjLoader().load_model('chibi.obj') # test sky = skybox() mp = map(20) mp.random_map(35) map = mp.get_map() ######################## def valid_xy(x, y, mx): return x >= 0 and y >= 0 and x < mx and y < mx def draw_map(): l = 5 # cube length glColor3f(1, 0, 0)