Hi All,
I was wondering if I could get some help with this. I'm a noob trying to learn some Python and OpenGL with the GLM Math
lib. I'd like to eventually move back to Cpp for graphics programming but was getting a bit overwhelmed. I've given this
a good try and am either making some small mistakes or am completely missing the mark. I'm getting a green screen and no cube
with no errors. I've left this as functions instead of one class because with python at the moment I'm just starting to code
classes. I've commented out the shader compilation checks as it was killing my program. I probably have errors there I'm
about 3 days into GLSL
. Thanks hopefully this might help others with a similar issue. (Ex. Page 124 OpenGl Super Bible)
I was wondering if I could get some help with this. I'm a noob trying to learn some Python and OpenGL with the GLM Math
lib. I'd like to eventually move back to Cpp for graphics programming but was getting a bit overwhelmed. I've given this
a good try and am either making some small mistakes or am completely missing the mark. I'm getting a green screen and no cube
with no errors. I've left this as functions instead of one class because with python at the moment I'm just starting to code
classes. I've commented out the shader compilation checks as it was killing my program. I probably have errors there I'm
about 3 days into GLSL

from OpenGL.GL import shaders from OpenGL.arrays import vbo from OpenGL.GL import * from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, glBindVertexArray from datetime import datetime import pygame import glm import numpy as np import ctypes import math as m import os #GLSL def startup(): vertex_shader_source = """ #version 430 core in vec4 position; out VS_OUT { vec4 color; } vs_out; uniform mat4 mv_matrix; uniform mat4 proj_matrix; void main(void) { gl_Position = proj_matrix * mv_matrix * position; vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0); } """ fragment_shader_source = """ #version 430 core out vec4 color; in VS_OUT { vec4 color; } fs_in; void main(void) { color = fs_in.color; } """ # --- Create a program program = glCreateProgram() #vertex_shader = GLuint() vertex_shader = glCreateShader(GL_VERTEX_SHADER) glShaderSource(vertex_shader, vertex_shader_source) glCompileShader(vertex_shader) #result = GL_FALSE #glGetShaderiv(vertex_shader,GL_COMPILE_STATUS,result) #if (result!=1): # shader didn't compile # raise Exception("Couldn't compile shader\nShader compilation Log:\n"+glGetShaderInfoLog(vertex_shader)) #fragment_shader = GLuint() fragment_shader = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(fragment_shader, fragment_shader_source) glCompileShader(fragment_shader) #result_fs = glGetShaderiv(fragment_shader,GL_COMPILE_STATUS) #if (result_fs!=1): # shader didn't compile # raise Exception("Couldn't compile shader\nShader compilation Log:\n"+glGetShaderInfoLog(fragment_shader)) glAttachShader(program, vertex_shader) glAttachShader(program, fragment_shader) glLinkProgram(program) mv_location = glGetUniformLocation(program, "mv_matrix") proj_location = glGetUniformLocation(program, "proj_matrix") # Create VAO and bind it vertex_array_object = GLuint() glGenVertexArrays(1, vertex_array_object) glBindVertexArray(vertex_array_object) vertex_positions = [-0.25, 0.25, -0.25, -0.25, -0.25, -0.25, 0.25, -0.25, -0.25, 0.25, -0.25, -0.25, 0.25, 0.25, -0.25, -0.25, 0.25, -0.25, 0.25, -0.25, -0.25, 0.25, -0.25, 0.25, 0.25, 0.25, -0.25, 0.25, -0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, -0.25, 0.25, -0.25, 0.25, -0.25, -0.25, 0.25, 0.25, 0.25, 0.25, -0.25, -0.25, 0.25, -0.25, 0.25, 0.25, 0.25, 0.25, 0.25, -0.25, -0.25, 0.25, -0.25, -0.25, -0.25, -0.25, 0.25, 0.25, -0.25, -0.25, -0.25, -0.25, 0.25, -0.25, -0.25, 0.25, 0.25, -0.25, -0.25, 0.25, 0.25, -0.25, 0.25, 0.25, -0.25, -0.25, 0.25, -0.25, -0.25, -0.25, -0.25, -0.25, -0.25, -0.25, 0.25, -0.25, 0.25, -0.25, 0.25, 0.25, -0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, -0.25, 0.25, 0.25, -0.25, 0.25, -0.25] vertex_positions = np.array(vertex_positions, dtype=np.float32) # Generate buffers to hold our vertex_positions vertex_buffer_object = GLuint() glGenBuffers(1,vertex_buffer_object) glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object) glBufferData(GL_ARRAY_BUFFER, len(vertex_positions), vertex_positions, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, len(vertex_positions), ctypes.c_void_p(0)) glEnableVertexAttribArray(0); glEnable(GL_CULL_FACE) glFrontFace(GL_CW) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) # --- Clean up now that we don't need these shaders anymore. glDeleteShader(vertex_shader) glDeleteShader(fragment_shader) return program, proj_location, mv_location def render(): program, proj_location, mv_location = startup() currentTime = datetime.now().second green = [0.0, 0.25, 0.0, 1.0] #one = 1.0 glClearBufferfv(GL_COLOR, 0, green) #glClearBufferfv(GL_DEPTH, 0, one) glUseProgram(program) proj_matrix = glm.perspective(glm.radians(50.0),800/600,0.1,1000.0) proj_matrix = np.array(proj_matrix, dtype=np.float32) glUniformMatrix4fv(proj_location, 1, GL_FALSE, proj_matrix) f = currentTime * m.pi * 0.1 x = glm.mat4() vectran1 = glm.translate(x, glm.vec3(0.0, 0.0, -4.0)) vectran2 = glm.translate(x, glm.vec3(m.sin(2.1 * f) * 0.5,m.cos(1.7 * f) * 0.5, m.sin(1.3 * f) * m.cos(1.5 * f) * 2.0)) vecrot1 = glm.rotate(x,currentTime * 45.0, glm.vec3(0.0, 1.0, 0.0)) vecrot2 = glm.rotate(x,currentTime * 81.0, glm.vec3(1.0, 0.0, 0.0)) mv_matrix = vectran1 * vectran2 * vecrot1 * vecrot2 mv_matrix = np.array(mv_matrix, dtype=np.float32) mvp_matrix = mv_matrix * proj_matrix # Set the model-view and projection matrices glUniformMatrix4fv(mv_location, 1, GL_FALSE, mv_matrix) glDrawArrays(GL_TRIANGLES, 0, 36) #display(shader, vertex_array_object) def shutdown(): glDeleteVertexArrays() def main(): pygame.init() screen = pygame.display.set_mode((800,600), pygame.OPENGL | pygame.DOUBLEBUF) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: return startup() render() pygame.display.flip() main()