import glfw import OpenGL.GL as gl def render(): gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3) if __name__ == '__main__': if not glfw.init(): raise Exception("GLFW initialization failed") window = glfw.create_window(640, 480, "My OpenGL Window", None, None) if not window: glfw.terminate() raise Exception("Window creation failed") glfw.make_context_current(window) gl.glClearColor(0.5, 0.5, 0.5, 1.0) vertices = [-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0] vao = gl.glGenVertexArrays(1) gl.glBindVertexArray(vao) vbo = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo) gl.glBufferData(gl.GL_ARRAY_BUFFER, len(vertices) * 4, (gl.GLfloat * len(vertices))(*vertices), gl.GL_STATIC_DRAW) gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, None) gl.glEnableVertexAttribArray(0) while not glfw.window_should_close(window): glfw.poll_events() render() glfw.swap_buffers(window) glfw.terminate()
import glfw import numpy as np import OpenGL.GL as gl def render(): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glUseProgram(shader_program) view = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, -2.0], [0.0, 0.0, 0.0, 1.0]]) model = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) projection = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, -1.0, -0.5], [0.0, 0.0, -1.0, 0.0]]) mv = np.dot(view, model) mvp = np.dot(projection, mv) gl.glUniformMatrix4fv(mvp_loc, 1, gl.GL_TRUE, mvp) gl.glUniform3fv(light_pos_loc, 1, np.array([0.0, 2.0, -2.0])) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glCullFace(gl.GL_BACK) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36) if __name__ == '__main__': if not glfw.init(): raise Exception("GLFW initialization failed") window = glfw.create_window(640, 480, "My OpenGL Window", None, None) if not window: glfw.terminate() raise Exception("Window creation failed") glfw.make_context_current(window) gl.glClearColor(0.5, 0.5, 0.5, 1.0) vertices = [-0.5, -0.5, 0.5, 1.0, 0.0, 0.0, -0.5, 0.5, 0.5, 1.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, -0.5, -0.5, -0.5, 1.0, 1.0, 0.0, -0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.5, 0.5, -0.5, 0.0, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, -0.5, 0.5, 0.5, 1.0, 0.0, 1.0, -0.5, 0.5, -0.5, 1.0, 1.0, 0.0, 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, -0.5, -0.5, 0.5, 1.0, 1.0, 0.0, -0.5, -0.5, -0.5, 1.0, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 1.0, 1.0, -0.5, 0.5, -0.5, 1.0, 0.0, 1.0, -0.5, 0.5, 0.5, 1.0, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, 0.0, 1.0, 0.5, 0.5, -0.5, 0.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.5, -0.5, 0.5, 1.0, 1.0, 0.0] vao = gl.glGenVertexArrays(1) gl.glBindVertexArray(vao) vbo = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo) gl.glBufferData(gl.GL_ARRAY_BUFFER, len(vertices) * 4, (gl.GLfloat * len(vertices))(*vertices), gl.GL_STATIC_DRAW) gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 24, None) gl.glEnableVertexAttribArray(0) gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 24, ctypes.c_void_p(12)) gl.glEnableVertexAttribArray(1) vertex_shader = """ #version 330 layout(location = 0) in vec3 a_position; layout(location = 1) in vec3 a_normal; uniform mat4 u_mvp; uniform vec3 u_light_pos; out vec3 v_normal; out vec3 v_light_dir; void main() { gl_Position = u_mvp * vec4(a_position, 1.0); v_normal = a_normal; v_light_dir = normalize(u_light_pos - a_position); } """ fragment_shader = """ #version 330 in vec3 v_normal; in vec3 v_light_dir; out vec4 out_color; void main() { vec3 light_color = vec3(1.0, 1.0, 1.0); vec3 object_color = vec3(0.0, 0.5, 1.0); vec3 ambient = 0.3 * object_color; vec3 norm = normalize(v_normal); vec3 light_dir = normalize(v_light_dir); vec3 diffuse = max(dot(norm, light_dir), 0.0) * light_color; vec3 view_dir = vec3(0.0, 0.0, 1.0); vec3 reflect_dir = reflect(-light_dir, norm); float specular_strength = 0.5; vec3 specular = specular_strength * light_color * pow(max(dot(reflect_dir, view_dir), 0.0), 32.0); out_color = vec4(ambient + diffuse + specular, 1.0); } """ vertex_shader_id = gl.glCreateShader(gl.GL_VERTEX_SHADER) gl.glShaderSource(vertex_shader_id, vertex_shader) gl.glCompileShader(vertex_shader_id) fragment_shader_id = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) gl.glShaderSource(fragment_shader_id, fragment_shader) gl.glCompileShader(fragment_shader_id) shader_program = gl.glCreateProgram() gl.glAttachShader(shader_program, vertex_shader_id) gl.glAttachShader(shader_program, fragment_shader_id) gl.glLinkProgram(shader_program) mvp_loc = gl.glGetUniformLocation(shader_program, 'u_mvp') light_pos_loc = gl.glGetUniformLocation(shader_program, 'u_light_pos') gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) gl.glCullFace(gl.GL_BACK) while not glfw.window_should_close(window): glfw.poll_events() render() glfw.swap_buffers(window) glfw.terminate()This example also uses the glfw package for window creation and event handling. Additionally, it uses numpy for matrix operations and ctypes for accessing buffer data.