def main(): window_width = 512 window_height = 512 pygame.init() window_resize(window_width, window_height) init_gl() shader = compile_shader(Path('shaders', 'obj_vertex.glsl'), Path('shaders', 'obj_fragment.glsl')) vertex_array_object, obj = create_object(shader) glUseProgram(shader) clock = pygame.time.Clock() looping = True while looping: clock.tick(100) for event in pygame.event.get(): if event.type == pygame.QUIT: looping = False elif event.type == pygame.VIDEORESIZE: window_width, window_height = event.size window_resize(window_width, window_height) if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: looping = False display(shader, vertex_array_object, window_width / window_height, obj) pygame.display.set_caption("FPS: %.2f" % clock.get_fps()) pygame.display.flip() glUseProgram(0)
def create_object(): obj = ObjLoader() obj.load_model(Path('res', 'cube.obj')) texture_offset = len(obj.vertex_index) * len( obj.vert_coords[0]) * obj.model.itemsize normal_offset = texture_offset + len(obj.texture_index) * len( obj.text_coords[0]) * obj.model.itemsize # Create a new VAO (Vertex Array Object) and bind it vertex_array_object = glGenVertexArrays(1) glBindVertexArray(vertex_array_object) shader = compile_shader(Path('shaders', 'mvp_vertex.glsl'), Path('shaders', 'light_fragment.glsl')) # Generate buffers to hold our vertices vertex_buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer) # Generate buffers to hold our texture texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the position of the 'position' in parameter of our shader and bind it. position = 0 glBindAttribLocation(shader, position, 'position') glEnableVertexAttribArray(position) # Describe the position data layout in the buffer glVertexAttribPointer(position, 3, GL_FLOAT, False, obj.model.itemsize * 3, ctypes.c_void_p(0)) # Set the position of the 'inTexCoords' in parameter of our shader and bind it. texture_coords = 1 glBindAttribLocation(shader, texture_coords, 'inTexCoords') glEnableVertexAttribArray(texture_coords) # Describe the texture data layout in the buffer glVertexAttribPointer(texture_coords, 2, GL_FLOAT, False, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset)) # Set the position of the 'inTexCoords' in parameter of our shader and bind it. normals = 2 glBindAttribLocation(shader, normals, 'inNormals') glEnableVertexAttribArray(normals) # Describe the texture data layout in the buffer glVertexAttribPointer(normals, 3, GL_FLOAT, False, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset)) # 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) # Load and convert Texture image = Image.open(Path('res', 'cube_texture.jpg')) flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM) img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8) # Send the data over to the buffers glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW) # Vertices array glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) # Unbind the VAO first (Important) glBindVertexArray(0) # Unbind other stuff # glDisableVertexAttribArray(position) glBindBuffer(GL_ARRAY_BUFFER, 0) return shader, vertex_array_object, obj
def create_object(): # Create a new VAO (Vertex Array Object) and bind it vertex_array_object = glGenVertexArrays(1) glBindVertexArray(vertex_array_object) shader = compile_shader(Path('shaders', 'perspective_vertex.glsl'), Path('shaders', 'perspective_fragment.glsl')) # Generate buffers to hold our vertices vertex_buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer) # Set the position of the 'position' in parameter of our shader and bind it. position = 0 glBindAttribLocation(shader, position, 'position') glEnableVertexAttribArray(position) # Describe the position data layout in the buffer glVertexAttribPointer(position, 4, GL_FLOAT, False, vertices.itemsize * vertices_row_length, ctypes.c_void_p(0)) # Send the data over to the buffers glBufferData(GL_ARRAY_BUFFER, vertices.itemsize * len(vertices), vertices, GL_STATIC_DRAW) # Vertices array # Generate buffers to hold buffer indices element_buffer = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.itemsize * len(indices), indices, GL_STATIC_DRAW) # Indices array # Set the position of the 'colour' in parameter of our shader and bind it. colour = 1 glBindAttribLocation(shader, colour, 'colour') glEnableVertexAttribArray(colour) # Describe the position data layout in the buffer glVertexAttribPointer(colour, 4, GL_FLOAT, False, vertices.itemsize * vertices_row_length, ctypes.c_void_p(16)) # Generate buffers to hold our texture texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) # Set the position of the 'inTexCoords' in parameter of our shader and bind it. texture_coords = 2 glBindAttribLocation(shader, texture_coords, 'inTexCoords') glEnableVertexAttribArray(texture_coords) # Describe the texture data layout in the buffer glVertexAttribPointer(texture_coords, 2, GL_FLOAT, False, vertices.itemsize * vertices_row_length, ctypes.c_void_p(32)) # 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) # Load and convert Texture image = Image.open(Path('res', 'crate.jpg')) img_data = image.convert('RGB').tobytes() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data) # Unbind the VAO first (Important) glBindVertexArray(0) # Unbind other stuff # glDisableVertexAttribArray(position) glBindBuffer(GL_ARRAY_BUFFER, 0) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) return shader, vertex_array_object