def Render(self, mode=0): BaseContext.Render(self, mode) self.shader.begin() try: self.set_uniform_value() glUniform1f(self.tween_loc, self.tween_fraction) glUniformMatrix4fv(self.MVP_loc, 1, GL_FALSE, glm.value_ptr(self.MVP)) glUniformMatrix4fv(self.ModelMatrix_loc, 1, GL_FALSE, glm.value_ptr(glm.mat4(1.0))) glUniformMatrix4fv(self.ViewMatrix_loc, 1, GL_FALSE, glm.value_ptr(self.ViewMatrix)) glUniform3f(self.LOCATION_OFFSET_loc, self.location[0], self.location[1], self.location[2]) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.texture.textureGLID) glUniform1i(self.diffuse_texture_loc, 0) glEnableVertexAttribArray(self.Vertex_position_loc) glBindBuffer(GL_ARRAY_BUFFER, self.vertexbuffer) glVertexAttribPointer(self.Vertex_position_loc, 3, GL_FLOAT, GL_FALSE, 0, None) glEnableVertexAttribArray(self.tweened_loc) glBindBuffer(GL_ARRAY_BUFFER, self.vertex_tweened_buffer) glVertexAttribPointer(self.tweened_loc, 3, GL_FLOAT, GL_FALSE, 0, None) glEnableVertexAttribArray(self.Vertex_normal_loc) glBindBuffer(GL_ARRAY_BUFFER, self.normalbuffer) glVertexAttribPointer(self.Vertex_normal_loc, 3, GL_FLOAT, GL_FALSE, 0, None) glEnableVertexAttribArray(self.Vertex_texture_coordinate_loc) glBindBuffer(GL_ARRAY_BUFFER, self.uvbuffer) glVertexAttribPointer(self.Vertex_texture_coordinate_loc, 2, GL_FLOAT, GL_FALSE, 0, None) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.indicesbuffer) glDrawElements( GL_TRIANGLES, # draw mode self.indices.size, # count of indices GL_UNSIGNED_SHORT, # type of indices data None ) finally: glDisableVertexAttribArray(self.Vertex_position_loc) glDisableVertexAttribArray(self.tweened_loc) glDisableVertexAttribArray(self.Vertex_normal_loc) glDisableVertexAttribArray(self.Vertex_texture_coordinate_loc) self.shader.end()
def key_callback(window, key, scancode, action, mods): if key == glfw.KEY_UP and action == glfw.PRESS: myCamera.camPos = myCamera.camPos + (myCamera.camSpeed * myCamera.camFront) view = glm.lookAt(myCamera.camPos, myCamera.camPos + myCamera.camFront, myCamera.camUp) MVP_loc = glGetUniformLocation(myShader.ID, 'MVP') MVP = projection * view * model * translate glUniformMatrix4fv(MVP_loc, 1, GL_FALSE, glm.value_ptr(MVP)) print("UP KEY PRESSED") print(myCamera.camPos) if key == glfw.KEY_DOWN and action == glfw.PRESS: myCamera.camPos = myCamera.camPos - (myCamera.camSpeed * myCamera.camFront) view = glm.lookAt(myCamera.camPos, myCamera.camPos + myCamera.camFront, myCamera.camUp) MVP_loc = glGetUniformLocation(myShader.ID, 'MVP') MVP = projection * view * model * translate glUniformMatrix4fv(MVP_loc, 1, GL_FALSE, glm.value_ptr(MVP)) print("DOWN KEY PRESSED") if key == glfw.KEY_RIGHT and action == glfw.PRESS: myCamera.camPos = myCamera.camPos + glm.normalize( glm.cross(myCamera.camFront, myCamera.camUp) * myCamera.camSpeed) view = glm.lookAt(myCamera.camPos, myCamera.camPos + myCamera.camFront, myCamera.camUp) MVP_loc = glGetUniformLocation(myShader.ID, 'MVP') MVP = projection * view * model * translate glUniformMatrix4fv(MVP_loc, 1, GL_FALSE, glm.value_ptr(MVP)) print("RIGHT KEY PRESSED") if key == glfw.KEY_LEFT and action == glfw.PRESS: myCamera.camPos = myCamera.camPos - glm.normalize( glm.cross(myCamera.camFront, myCamera.camUp) * myCamera.camSpeed) view = glm.lookAt(myCamera.camPos, myCamera.camPos + myCamera.camFront, myCamera.camUp) MVP_loc = glGetUniformLocation(myShader.ID, 'MVP') MVP = projection * view * model * translate glUniformMatrix4fv(MVP_loc, 1, GL_FALSE, glm.value_ptr(MVP)) print("LEFT KEY PRESSED")
def render_model(self, shader, model): # result = self.get_id(['program', 'model'], shader_id) # if not all(result): # return # # program, model = result program = shader.get_param('program') glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, glm.value_ptr(model))
def render_view(self, shader, view): # result = self.get_id(['program', 'view'], shader_id) # if not all(result): # return # # program, view = result program = shader.get_param('program') glUniformMatrix4fv(glGetUniformLocation(program, 'view'), 1, GL_FALSE, glm.value_ptr(view))
def render_proj(self, shader, project): # result = self.get_id(['program', 'project'], shader_id) # if not all(result): # return # # program, project = result program = shader.get_param('program') glUniformMatrix4fv(glGetUniformLocation(program, 'projection'), 1, GL_FALSE, glm.value_ptr(project))
def main(): window = glfw_initialize() fbo_width, fbo_height = 50, 50 fbo = setup_fbo(fbo_width, fbo_height) shader, mvploc = setup_shader() num_view = 40 random_rotation = [random_uniform_rotation() for _ in range(num_view)] os.makedirs('./size50/', exist_ok=True) f = open("Thingi10K/goodfile_id.csv", 'r', encoding='utf-8') rdr = csv.reader(f) filenames = ["Thingi10K/raw_meshes/" + line[0] + ".stl" for line in rdr] f.close() for idx, filename in enumerate(filenames): print(idx) if not os.path.isfile(filename): continue # read mesh m = mesh.Mesh.from_file(filename) for scale in [0.3, 0.4, 0.5, 0.6, 0.7]: for i in range(num_view): vao, vbo = setup_glBuffer(m, random_rotation[i]) mvp = get_mvp(scale) glUseProgram(shader) glUniformMatrix4fv(mvploc, 1, GL_FALSE, glm.value_ptr(mvp)) glBindFramebuffer(GL_FRAMEBUFFER, fbo) glViewport(0, 0, fbo_width, fbo_height) glClearColor(0.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_BIT) glBindVertexArray(vao) glDrawArrays(GL_TRIANGLES, 0, m.v0.shape[0] * 3) glfw.swap_buffers(window) glfw.poll_events() captured = glReadPixels(0, 0, fbo_width, fbo_height, GL_RGB, GL_FLOAT) captured = (captured * 255).astype(np.uint8) img = Image.fromarray(captured) file_id = os.path.splitext(filename)[0].split('/')[-1] img.save(f"size50/{file_id}_sc{scale}_view{i}.png") glBindFramebuffer(GL_FRAMEBUFFER, 0) glDeleteBuffers(1, [vbo]) glDeleteVertexArrays(1, [vao]) glfw.terminate()
def _set_uniform_mat4(id: int, value): """ Set a uniform to a matrix. Args: id (int): [description] value ([type]): [description] """ glUniformMatrix4fv(id, 1, GL_FALSE, glm.value_ptr(value))
def main(): window = display.create(WIDTH, HEIGHT, 'GL') shader = Shader() camera = Camera(window, glm.vec3(0.0, 0.0, 1.1), 0.0, 0.0, 0.0, 60, 0.01, -1000) shader.start() shader.putVecInUniformLocation('color', glm.vec4(0.01, 1.0, 0.01, 1.0)) proj = camera.createProjectionMatrix() shader.loadProjectionMatrix(glm.value_ptr(proj)) view = camera.createViewMatrix() shader.loadViewMatrix(glm.value_ptr(view)) vb = glGenBuffers(1) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, vb) glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) ivb = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ivb) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) while display.isNotClosed(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glClearColor(0.1, 0.0, 0.03, 1.0) shader.loadTransformationMatrix(glm.value_ptr( createTransformationMatrix( glm.vec3(0.0, 0.0, -0.1), glm.vec3(0.0, 0.1*glfw.get_time(), 0.0*glfw.get_time()), glm.vec3(1.0, 1.0, 1.0) ) )) camera.movement() shader.loadViewMatrix(glm.value_ptr(camera.createViewMatrix())) glPointSize(3.0) glDrawElements(GL_TRIANGLES, len(indices)+1, GL_UNSIGNED_INT, None) display.update() shader.stop() glDeleteBuffers(1, [vb]) display.close()
def display(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) elapsed_ms = glutGet(GLUT_ELAPSED_TIME) projection = glm.perspective(glm.radians(60), aspect, 0.1, 10.0) view = glm.lookAt(glm.vec3(0, 0, 4), glm.vec3(0, 0, 0), glm.vec3(0, 1, 0)) angle = elapsed_ms * math.pi * 2 / 5000.0 model = glm.rotate(glm.mat4(1), angle * 0.5, glm.vec3(0, 1, 0)) model = glm.rotate(model, angle, glm.vec3(1, 0, 0)) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(projection)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(view)) glUniformMatrix4fv(2, 1, GL_FALSE, glm.value_ptr(model)) glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None) glutSwapBuffers() glutPostRedisplay()
def load_light_setup(self, light_setup): # Vertex Shader gl.glUniform1ui(self.vs_light_count, light_setup.light_count) gl.glUniform3fv(self.vs_camera_position, 1, glm.value_ptr(light_setup.camera_position)) for index in range(light_setup.light_count): gl.glUniform3fv(self.vs_light_position + index, 1, glm.value_ptr(light_setup.light_positions[index])) # Fragment shader gl.glUniform1ui(self.ps_light_count, light_setup.light_count) gl.glUniform3fv(self.ps_global_ambient, 1, glm.value_ptr(light_setup.global_ambient)) for index in range(light_setup.light_count): gl.glUniform3fv(self.ps_light_color + index, 1, glm.value_ptr(light_setup.lights[index].color)) gl.glUniform3fv( self.ps_light_attenuation + index, 1, glm.value_ptr(light_setup.lights[index].attenuation))
def __mainloop(self): proj, view, model = glm.mat4(1), glm.mat4(1), glm.mat4(1) aspect = self.__vp_size[0]/self.__vp_size[1] proj = glm.perspective(glm.radians(90.0), aspect, 0.1, 10.0) view = glm.lookAt(glm.vec3(0,-3,0), glm.vec3(0, 0, 0), glm.vec3(0,0,1)) angle1 = self.elapsed_ms() * math.pi * 2 / 5000.0 angle2 = self.elapsed_ms() * math.pi * 2 / 7333.0 model = glm.rotate(model, angle1, glm.vec3(1, 0, 0)) model = glm.rotate(model, angle2, glm.vec3(0, 1, 0)) glUniformMatrix4fv(self.___uniform['u_proj'], 1, GL_FALSE, glm.value_ptr(proj) ) glUniformMatrix4fv(self.___uniform['u_view'], 1, GL_FALSE, glm.value_ptr(view) ) glUniformMatrix4fv(self.___uniform['u_model'], 1, GL_FALSE, glm.value_ptr(model) ) glClearColor(0.2, 0.3, 0.3, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glDrawElements(GL_TRIANGLES, self.__no_indices, GL_UNSIGNED_INT, None)
def render(self): glClearColor(0.2, 0.2, 0.2, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) i = glm.mat4(1) #como las matrices vistas en Static rendering # Model/Object matrix: translate * rotate * scale translate = glm.translate(i, self.cubePos) pitch = glm.rotate(i, glm.radians(0), glm.vec3(1, 0, 0)) yaw = glm.rotate(i, glm.radians(0), glm.vec3(0, 1, 0)) roll = glm.rotate(i, glm.radians(0), glm.vec3(0, 0, 1)) rotate = pitch * yaw * roll scale = glm.scale(i, glm.vec3(1, 1, 1)) model = translate * rotate * scale # View Matrix # glm.lookAt( eye, center, up) camTranslate = glm.translate(i, glm.vec3(0, 0, 3)) camPitch = glm.rotate(i, glm.radians(0), glm.vec3(1, 0, 0)) camYaw = glm.rotate(i, glm.radians(0), glm.vec3(0, 1, 0)) camRoll = glm.rotate(i, glm.radians(0), glm.vec3(0, 0, 1)) camRotate = camPitch * camYaw * camRoll view = glm.inverse(camTranslate * camRotate) if self.active_shader: glUniformMatrix4fv( glGetUniformLocation(self.active_shader, "model"), 1, GL_FALSE, glm.value_ptr(model)) glUniformMatrix4fv( glGetUniformLocation(self.active_shader, "view"), 1, GL_FALSE, glm.value_ptr(view)) glUniformMatrix4fv( glGetUniformLocation(self.active_shader, "projection"), 1, GL_FALSE, glm.value_ptr(self.projection)) glBindVertexArray(self.VAO) glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None) glBindVertexArray(0)
def render(time): glClearBufferfv(GL_COLOR, 0, [0.0, 0.0, 0.0, 1.0]) glClearBufferfi(GL_DEPTH_STENCIL, 0, 1.0, 0) M_matrix = glm.rotate(glm.mat4(1.0), time, glm.vec3(1.0, 1.0, 0.0)) V_matrix = glm.lookAt(glm.vec3(0.0, 0.0, 15.0), glm.vec3(4.0, 4.0, 0.0), glm.vec3(0.0, 1.0, 0.0)) glUseProgram(rendering_program) M_location = glGetUniformLocation(rendering_program, "M_matrix") V_location = glGetUniformLocation(rendering_program, "V_matrix") P_location = glGetUniformLocation(rendering_program, "P_matrix") glUniformMatrix4fv(M_location, 1, GL_FALSE, glm.value_ptr(M_matrix)) glUniformMatrix4fv(V_location, 1, GL_FALSE, glm.value_ptr(V_matrix)) glUniformMatrix4fv(P_location, 1, GL_FALSE, glm.value_ptr(P_matrix)) glDrawArraysInstanced(GL_TRIANGLES, 0, 36, 100)
def draw(self, opengl_frame): self.status_text.set( f"fps {opengl_frame.fps:.03f} frame: {opengl_frame.number_of_frames:05d}" ) vp_valid, vp_size, view, projection = self.__tkinter_navigation.update( ) if not vp_valid: glViewport(0, 0, *vp_size) if self.cull_mode == 0: glDisable(GL_CULL_FACE) else: glEnable(GL_CULL_FACE) glCullFace(GL_FRONT if self.cull_mode == 2 else GL_BACK) glPolygonMode( GL_FRONT_AND_BACK, GL_POINT if self.polygon_mode == 0 else GL_LINE if self.polygon_mode == 1 else GL_FILL) glUniformMatrix4fv(self.___uniform['u_proj'], 1, GL_FALSE, glm.value_ptr(projection)) glUniformMatrix4fv(self.___uniform['u_view'], 1, GL_FALSE, glm.value_ptr(view)) glClearColor(0.2, 0.3, 0.3, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) angle1 = 0 # self.elapsed_ms() * math.pi * 2 / 5000.0 angle2 = 0 # self.elapsed_ms() * math.pi * 2 / 7333.0 model = glm.mat4(1) model = glm.rotate(model, angle1, glm.vec3(0, 0, 1)) model = glm.rotate(model, angle2, glm.vec3(0, 1, 0)) multi_mesh = True index = self.shape_index if index < len(self.__meshes): glBindBuffer(GL_SHADER_STORAGE_BUFFER, self.__ssbo) glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, glm.sizeof(glm.mat4), glm.value_ptr(model)) if multi_mesh: self.__multimesh.draw_range(index, index + 1) else: self.__meshes[index].draw()
def __init__(self): VERTEX_SHADER = """ #version 330 layout (location=0) in vec4 position; layout (location=1) in vec4 color; uniform bool use_uniform_color; uniform vec4 uniform_color; uniform mat4 mvpMatrix; out vec4 frag_color; void main() { gl_Position = mvpMatrix * position; if(use_uniform_color) frag_color = uniform_color; else frag_color = color; } """ FRAGMENT_SHADER = """ #version 330 in vec4 frag_color; out vec4 output_color; void main() { output_color = frag_color; } """ self.__shaderProgram = ShaderProgram(VERTEX_SHADER, FRAGMENT_SHADER) self.__shaderProgram.bind() self.__useUniformColorLoc = gl.glGetUniformLocation(self.__shaderProgram.getProgramID(), "use_uniform_color"); self.__uniformColorLoc = gl.glGetUniformLocation(self.__shaderProgram.getProgramID(), "uniform_color"); self.__mvpMatrixLoc = gl.glGetUniformLocation(self.__shaderProgram.getProgramID(), "mvpMatrix"); identity = glm.mat4() gl.glUniformMatrix4fv(self.__mvpMatrixLoc, 1, gl.GL_FALSE, glm.value_ptr(identity)) gl.glUniform1i(self.__useUniformColorLoc, 0) color = np.array([1.0, 1.0, 1.0, 1.0], dtype=np.float32) gl.glUniform4fv(self.__uniformColorLoc, 1, color); self.__shaderProgram.release()
def render(self, camera): self.pv = self.p * self._calcVMat(camera) glEnableVertexAttribArray(2) glEnableVertexAttribArray(3) glEnableVertexAttribArray(4) glEnableVertexAttribArray(5) if self._useProgram(self.colorIns, self.colorInsProgram): glUniformMatrix4fv(VPINDEX, 1, GL_FALSE, glm.value_ptr(self.pv)) self._flush(self.colorIns, self._renderColorInstanced) if self._useProgram(self.texIns, self.textureInsProgram): glUniformMatrix4fv(VPINDEX, 1, GL_FALSE, glm.value_ptr(self.pv)) self._flush(self.texIns, self._renderTextureInstanced) glDisableVertexAttribArray(2) glDisableVertexAttribArray(3) glDisableVertexAttribArray(4) glDisableVertexAttribArray(5) if self._useProgram(self.colors, self.colorProgram): self._flush(self.colors, self._renderColor) if self._useProgram(self.textures, self.textureProgram): self._flush(self.textures, self._renderTexture) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if self._useProgram(self.texBlending, self.textureProgram): self._flush(self.texBlending, self._renderTexture) glEnableVertexAttribArray(2) glEnableVertexAttribArray(3) glEnableVertexAttribArray(4) glEnableVertexAttribArray(5) if self._useProgram(self.texInsBlending, self.textureInsProgram): glUniformMatrix4fv(VPINDEX, 1, GL_FALSE, glm.value_ptr(self.pv)) self._flush(self.texInsBlending, self._renderTextureInstanced) glDisable(GL_BLEND)
def render(self): glClearColor(0.2, 0.2, 0.2, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) i = glm.mat4(1) translate = glm.translate(i, self.cube_position) pitch = glm.rotate(i, glm.radians(0), glm.vec3(1, 0, 0)) yaw = glm.rotate(i, glm.radians(0), glm.vec3(0, 1, 0)) roll = glm.rotate(i, glm.radians(0), glm.vec3(0, 0, 1)) rotate = pitch * yaw * roll scale = glm.scale(i, glm.vec3(1, 1, 1)) model = translate * rotate * scale camera_translate = glm.translate(i, self.camera_position) camera_pitch = glm.rotate(i, glm.radians(self.camera_pitch_degrees), glm.vec3(1, 0, 0)) camera_yaw = glm.rotate(i, glm.radians(self.camera_yaw_degrees), glm.vec3(0, 1, 0)) camera_roll = glm.rotate(i, glm.radians(self.camera_roll_degrees), glm.vec3(0, 0, 1)) camera_rotate = camera_pitch * camera_yaw * camera_roll view = glm.inverse(camera_translate * camera_rotate) if self.active_shader: glUniformMatrix4fv( glGetUniformLocation(self.active_shader, 'model'), 1, GL_FALSE, glm.value_ptr(model)) glUniformMatrix4fv( glGetUniformLocation(self.active_shader, 'view'), 1, GL_FALSE, glm.value_ptr(view)) glUniformMatrix4fv( glGetUniformLocation(self.active_shader, 'projection'), 1, GL_FALSE, glm.value_ptr(self.projection)) glBindVertexArray(self.VAO) glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None) glBindVertexArray(0)
def _Draw(self, time): self.__model = glm.mat4(1) angle1 = time * 2 * math.pi / 13 angle2 = time * 2 * math.pi / 17 #self.__model = glm.rotate(self.__model, angle1, glm.vec3(1, 0, 0) ) #self.__model = glm.rotate(self.__model, angle2, glm.vec3(0, 0, 1) ) glBindBuffer(GL_SHADER_STORAGE_BUFFER, self.__mvp_ssbo) glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, glm.sizeof(glm.mat4), glm.value_ptr(self.__model)) if self.__update_view: glBufferSubData(GL_SHADER_STORAGE_BUFFER, 1 * glm.sizeof(glm.mat4), glm.sizeof(glm.mat4), glm.value_ptr(self.__view)) glClearColor(0.3, 0.3, 0.3, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.__cube.Draw()
def display(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) projection = glm.ortho(-aspect, aspect, -1, 1, -1, 1) if aspect < 1: projection = glm.ortho(-1, 1, 1 / -aspect, 1 / aspect, -1, 1) elapsed_ms = glutGet(GLUT_ELAPSED_TIME) angle = elapsed_ms * math.pi * 2 / 5000.0 model_view = glm.translate( glm.mat4(1), glm.vec3(math.cos(angle) * 0.5, math.sin(angle) * 0.5, 0)) model_view = glm.scale(model_view, glm.vec3(0.2, 0.2, 1)) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(projection)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(model_view)) glDrawArrays(GL_TRIANGLE_FAN, 0, 4) glutSwapBuffers() glutPostRedisplay()
def Keyboard(key, x, y): global trans, program, counter5 # Exits program if key is 27 or key is b'q' or key is b'Q': sys.exit(0) # Perform manual transformations if key is b't': transformation_code = int(input("Next transformation:")) if transformation_code is 1: print("Rotation! ") transformation_angle = int(input("Angle:")) trans = glm.rotate(trans, glm.radians(transformation_angle), glm.vec3(0.0, 0.0, 1.0)) elif transformation_code is 2: print("Rotation! ") transformation_angle = int(input("Angle:")) trans = glm.rotate(trans, glm.radians(transformation_angle), glm.vec3(0.0, 1.0, 0.0)) elif transformation_code is 3: print("Rotation! ") transformation_angle = int(input("Angle:")) trans = glm.rotate(trans, glm.radians(transformation_angle), glm.vec3(1.0, 0.0, 0.0)) elif transformation_code is 4: print("Translation! ") d_x = float(input("dx:")) d_y = float(input("dy:")) d_z = float(input("dz:")) trans = glm.translate(trans, glm.vec3(d_x, d_y, d_z)) elif transformation_code is 5: print("Scale! ") s_x = float(input("sx:")) s_y = float(input("sy:")) s_z = float(input("sz:")) trans = glm.scale(trans, glm.vec3(s_x, s_y, s_z)) transformLoc = glGetUniformLocation(program.program_id, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm.value_ptr(trans)) Display()
def render(self): glClearColor(0.25, 0.5, 0.5, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if self.active_shader: glUniformMatrix4fv( glGetUniformLocation(self.active_shader, "view"), 1, GL_FALSE, glm.value_ptr(self.viewMatrix)) glUniformMatrix4fv( glGetUniformLocation(self.active_shader, "projection"), 1, GL_FALSE, glm.value_ptr(self.projection)) glUniform4f(glGetUniformLocation(self.active_shader, "light"), self.pointLight.x, self.pointLight.y, self.pointLight.z, self.pointLight.w) glUniform4f(glGetUniformLocation(self.active_shader, "color"), 1, 1, 1, 1) glUniformMatrix4fv( glGetUniformLocation(self.active_shader, "model"), 1, GL_FALSE, glm.value_ptr(self.modelList[self.activeModelIndex].getMatrix( self.camPosition, self.projection, self.camRotation))) self.modelList[self.activeModelIndex].renderInScene()
def render(self) -> None: """Render proxy objects to canvas with a diffuse shader.""" if not self.init(): return proj = self.parent.projection_matrix view = self.parent.modelview_matrix glUseProgram(self.parent.shaders['diffuse']) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(proj)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(view)) for mesh in self._meshes: glBindVertexArray(mesh.vao) glUniform4fv(2, 1, glm.value_ptr(mesh.color)) glUniform1i(3, int(mesh.selected)) glDrawElements(GL_TRIANGLES, mesh.count, GL_UNSIGNED_INT, ctypes.c_void_p(0)) glBindVertexArray(0) glUseProgram(0)
def idle(): global theta loc = gl.glGetUniformLocation(program, "model") model = glm.mat4(1.0) model = glm.rotate(model, theta, (1.0, 0.0, 0.0)) model = glm.rotate(model, theta, (0.0, 1.0, 0.0)) gl.glUniformMatrix4fv(loc, 1, gl.GL_FALSE, glm.value_ptr(model)) theta += 0.01 if theta > 2 * math.pi: theta -= 2 * math.pi draw()
def __mainloop(self): if not self.__vp_valid: self.width = glutGet(GLUT_WINDOW_WIDTH) self.height = glutGet(GLUT_WINDOW_HEIGHT) self.__vp_size = [self.width, self.height] self.__vp_valid = True aspect = self.width / self.height self.proj = glm.perspective(glm.radians(90.0), aspect, self.near, self.far) glUniformMatrix4fv(self.___uniform['u_proj'], 1, GL_FALSE, glm.value_ptr(self.proj) ) glUniformMatrix4fv(self.___uniform['u_view'], 1, GL_FALSE, glm.value_ptr(self.view) ) glUniformMatrix4fv(self.___uniform['u_model'], 1, GL_FALSE, glm.value_ptr(self.model) ) glClearColor(0.2, 0.3, 0.3, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glDrawArrays(GL_TRIANGLES, 0, self.__no_vert) glutSwapBuffers() glutPostRedisplay()
def rendering(self, MVP, View, Projection): self.shader.begin() glUniformMatrix4fv(self.MVP_ID, 1, GL_FALSE, glm.value_ptr(MVP)) glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, self.linebuffer) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) glDrawArrays(GL_LINES, 0, len( self.lines)) # 12*3 indices starting at 0 -> 12 triangles glDisableVertexAttribArray(0) self.shader.end()
def Keyboard(self, key, x, y): global loop, mode # Exits program if key is 27 or key is b'q' or key is b'Q': sys.exit(0) if key is b'l': loop = not loop if key is b'v': if self.visualization_mode is GL_LINES: self.visualization_mode = GL_POINTS else: self.visualization_mode = GL_LINES if key is b't': mode = TRANSLATION_MODE elif key is b'r': mode = ROTATION_MODE elif key is b'e': mode = SCALE_MODE if key is b'a': if mode is TRANSLATION_MODE: self.translate_object('CLOSE') elif mode is ROTATION_MODE: self.rotate_object('INCREASE_Z') elif mode is SCALE_MODE: self.scale_object('INCREASE_Z') print(self.matrix) if key is b'd': if mode is TRANSLATION_MODE: self.translate_object('FAR') elif mode is ROTATION_MODE: self.rotate_object('LOWER_Z') elif mode is SCALE_MODE: self.scale_object('LOWER_Z') print(self.matrix) transformLoc = glGetUniformLocation(self.program.program_id, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm.value_ptr(self.matrix)) glutPostRedisplay()
def draw_objects(shader): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Create perspective matrix pers = glm.perspective(0.5,1.0,0.1,50.0) # Create rotation matrix rot_x = glm.rotate(glm.mat4(1.0),0.5 * glfw.get_time(),glm.vec3(1.0,0.0,0.0)) rot_y = glm.rotate(glm.mat4(1.0),0.5 * glfw.get_time(),glm.vec3(0.0,1.0,0.0)) # Create normal transformation matrix normal_matrix = glm.mat4(1.0) normalMatrixLoc = glGetUniformLocation(shader, "normal_matrix") glUniformMatrix4fv(normalMatrixLoc, 1, GL_FALSE, glm.value_ptr(normal_matrix*rot_x*rot_y)) # Create tranformation matrix for sphere trans = glm.translate(glm.mat4(1.0), glm.vec3(-0.8,0.8,-5.5)) modelViewLoc = glGetUniformLocation(shader, "model_view_matrix") glUniformMatrix4fv(modelViewLoc, 1, GL_FALSE, glm.value_ptr(trans*rot_x*rot_y)) modelViewProjLoc = glGetUniformLocation(shader, "model_view_projection_matrix") glUniformMatrix4fv(modelViewProjLoc, 1, GL_FALSE, glm.value_ptr(pers*trans*rot_x*rot_y)) # Draw Sphere sphere_quadric = gluNewQuadric() gluQuadricNormals(sphere_quadric, GLU_SMOOTH) gluSphere(sphere_quadric, 0.35, 20, 20) # Create tranformation matrix for teapot trans = glm.translate(glm.mat4(1.0), glm.vec3(0.0,0.0,-20.5)) scale = glm.scale(glm.mat4(1.0), glm.vec3(0.1, 0.1, 0.1)) modelViewLoc = glGetUniformLocation(shader, "model_view_matrix") glUniformMatrix4fv(modelViewLoc, 1, GL_FALSE, glm.value_ptr(trans*scale*rot_x*rot_y)) modelViewProjLoc = glGetUniformLocation(shader, "model_view_projection_matrix") glUniformMatrix4fv(modelViewProjLoc, 1, GL_FALSE, glm.value_ptr(pers*trans*scale*rot_x*rot_y)) draw_faces(teapot_faces) # Create tranformation matrix for cylinder trans = glm.translate(glm.mat4(1.0), glm.vec3(0.8,-0.8,-5.5)) modelViewLoc = glGetUniformLocation(shader, "model_view_matrix") glUniformMatrix4fv(modelViewLoc, 1, GL_FALSE, glm.value_ptr(trans*rot_x*rot_y)) modelViewProjLoc = glGetUniformLocation(shader, "model_view_projection_matrix") glUniformMatrix4fv(modelViewProjLoc, 1, GL_FALSE, glm.value_ptr(pers*trans*rot_x*rot_y)) # Draw cylinder cylinder_quad = gluNewQuadric() gluQuadricNormals(cylinder_quad, GLU_SMOOTH) gluCylinder(cylinder_quad, 0.25, 0.25, 0.5, 10, 10)
def catchKey(self, key, x, y): global loop if key == GLUT_KEY_LEFT: if mode is TRANSLATION_MODE: self.translate_object('LEFT') elif mode is ROTATION_MODE: self.rotate_object('LOWER_X') elif mode is SCALE_MODE: self.scale_object('LOWER_X') if key == GLUT_KEY_RIGHT: if mode is TRANSLATION_MODE: self.translate_object('RIGHT') elif mode is ROTATION_MODE: self.rotate_object('INCREASE_X') elif mode is SCALE_MODE: self.scale_object('INCREASE_X') if key == GLUT_KEY_UP: if mode is TRANSLATION_MODE: self.translate_object('UP') elif mode is ROTATION_MODE: self.rotate_object('INCREASE_Y') elif mode is SCALE_MODE: self.scale_object('INCREASE_Y') if key == GLUT_KEY_DOWN: if mode is TRANSLATION_MODE: # self.translate_object([0.0, (-1) * translation, 0.0]) self.translate_object('DOWN') elif mode is ROTATION_MODE: self.rotate_object('LOWER_Y') elif mode is SCALE_MODE: self.scale_object('LOWER_Y') print(self.matrix) transformLoc = glGetUniformLocation(self.program.program_id, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm.value_ptr(self.matrix)) glutPostRedisplay()
def display(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if aspect >= 1: projection = glm.ortho(-aspect, aspect, -1, 1, -1, 1) else: projection = glm.ortho(-1, 1, -1/aspect, 1/aspect, -1, 1) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(projection)) glDrawArrays(GL_TRIANGLES, 0, 3) glutSwapBuffers() glutPostRedisplay()
def Display(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) transform = self.perspective_matrix * self.view_matrix * self.matrix transformLoc = glGetUniformLocation(self.program.program_id, "transform") glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm.value_ptr(transform)) glBindVertexArray(vao) glDrawArrays(GL_TRIANGLES, 0, 12 * 3) glutSwapBuffers()