def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(800, 600, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() window.makeContextCurrent() window.setKeyCallback(key_callback) gl.init() err = gl.getError() print(gl) if err: print("WINDOW OPEN ERROR:", err) vertexShader = gl.createShader(gl.VERTEX_SHADER) gl.shaderSource(vertexShader, [VERTEX_SHADER_SOURCE]); gl.compileShader(vertexShader) success = gl.getShaderiv(vertexShader, gl.COMPILE_STATUS) print("VERTEX SHADER COMPILE STATUS:",success) if not success: infoLog = gl.getShaderInfoLog(vertexShader) print("ERROR COMPILING SHADER:", infoLog) sys.exit() fragmentShader = gl.createShader(gl.FRAGMENT_SHADER) gl.shaderSource(fragmentShader, [FRAGMENT_SHADER_SOURCE]); gl.compileShader(fragmentShader) success = gl.getShaderiv(fragmentShader, gl.COMPILE_STATUS) print("FRAGMENT SHADER COMPILE STATUS:",success) if not success: infoLog = gl.getShaderInfoLog(fragmentShader) print("ERROR COMPILING SHADER:", infoLog) sys.exit() shaderProgram = gl.createProgram() gl.attachShader(shaderProgram, vertexShader) gl.attachShader(shaderProgram, fragmentShader) gl.linkProgram(shaderProgram) gl.getProgramiv(shaderProgram, gl.LINK_STATUS) print("SHADER PROGRAM LINK STATUS:",success) if not success: infoLog = gl.getProgramInfoLog(shaderProgram) print("ERROR LINKING SHADER PROGRAM:", infoLog) sys.exit() assert(gl.isProgram(shaderProgram)) gl.useProgram(shaderProgram) #gl.deleteShader(vertexShader) #gl.deleteShader(fragmentShader) vao = gl.genVertexArrays(1)[0] vbo, ebo = gl.genBuffers(2) print(vao, vbo, ebo) # 1. Bind Vertex Array Object gl.bindVertexArray(vao) # 2. Copy our vertices array in a buffer for OpenGL to use gl.bindBuffer(gl.ARRAY_BUFFER, vbo) gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) # 2. Copy our index array in a buffer for OpenGL to use gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo) gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW) # 3. Then set our vertex attributes pointers gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * sizeof(gl.GLfloat), None) gl.enableVertexAttribArray(0) # 4. unbind the vbo gl.bindBuffer(gl.ARRAY_BUFFER, 0) # 4. Unbind the VAO gl.bindVertexArray(0) while not window.shouldClose(): glfw.pollEvents() framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT) gl.useProgram(shaderProgram) gl.bindVertexArray(vao) gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) # gl.drawArrays(gl.TRIANGLES, 0, 3); gl.bindVertexArray(0) window.swapBuffers() gl.deleteVertexArrays([vao]) gl.deleteBuffers([vbo, ebo])
def main(): glfw.setErrorCallback(error_callback) glfw.init() glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # glfw.windowHint(glfw.RESIZABLE, gl.FALSE) glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1) window = glfw.createWindow(WIDTH, HEIGHT, "LearnOpenGL") if window is None: print('could not open window.') glfw.terminate() sys.exit() inputMap = InputMap() window.makeContextCurrent() window.setKeyCallback(inputMap.key_callback) window.setCursorPosCallback(inputMap.mouse_callback) #window.setScrollCallback(inputMap.scroll_callback) window.setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED) gl.init() err = gl.getError() if err: print("WINDOW OPEN ERROR:", err) camera = Camera(position=Vec3(0.0, 0.0, 3.0)) lastX = 400 lastY = 300 firstMouse = True deltaTime = 0.0 lastFrame = 0.0 gl.enable(gl.DEPTH_TEST) lampShaderProgram = Shader(LAMP_VERTEX_SHADER_SOURCE, LAMP_FRAGMENT_SHADER_SOURCE) lightingShaderProgram = Shader(LIGHTING_VERTEX_SHADER_SOURCE, LIGHTING_FRAGMENT_SHADER_SOURCE) lightingShaderProgram.use() lightVAO, containerVAO = gl.genVertexArrays(2) vbo = gl.genBuffers(1)[0] # 1. Bind Vertex Array Object gl.bindVertexArray(containerVAO) # 2. Copy our vertices array in a buffer for OpenGL to use gl.bindBuffer(gl.ARRAY_BUFFER, vbo) gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # Normal attribute gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.normal.offset) gl.enableVertexAttribArray(1) # 4. Unbind the VAO gl.bindVertexArray(0) # 1. Bind Vertex Array Object gl.bindVertexArray(lightVAO) # Position attribute gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset) gl.enableVertexAttribArray(0) # 4. Unbind the VAO gl.bindVertexArray(0) # 4. unbind the vbo gl.bindBuffer(gl.ARRAY_BUFFER, 0) lightPos = Vec3(1.2, 1.0, 2.0) while not window.shouldClose(): inputMap.begin_frame() glfw.pollEvents() input = inputMap.get_input() camera.processInput(input, 1.0/30.0) framebuffer_width, framebuffer_height = window.getFramebufferSize() gl.viewport(0, 0, framebuffer_width, framebuffer_height) gl.clearColor(0.2, 0.3, 0.3, 1.0) gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT) lightingShaderProgram.use() lightingShaderProgram.uniforms.objectColor = Vec3(1.0, 0.5, 0.31) lightingShaderProgram.uniforms.lightColor = Vec3(1.0, 0.5, 1.0) lightingShaderProgram.uniforms.lightPos = lightPos lightingShaderProgram.uniforms.viewPos = camera.position # create transformations view = camera.getViewMatrix() projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0) # assign the transformations lightingShaderProgram.uniforms.view = view lightingShaderProgram.uniforms.projection = projection # draw the container gl.bindVertexArray(containerVAO) model = Mat4.Identity() lightingShaderProgram.uniforms.model = model gl.drawArrays(gl.TRIANGLES, 0, 36) gl.bindVertexArray(0) # draw the lamp lampShaderProgram.use() lampShaderProgram.uniforms.view = view lampShaderProgram.uniforms.projection = projection model = Mat4.Translation(lightPos) * Mat4.Scale(0.2) lampShaderProgram.uniforms.model = model gl.bindVertexArray(lightVAO) gl.drawArrays(gl.TRIANGLES, 0, 36) gl.bindVertexArray(0) window.swapBuffers() lampShaderProgram.delete() lightingShaderProgram.delete() gl.deleteVertexArrays([containerVAO, lightVAO]) gl.deleteBuffers([vbo])