Reputation: 55
I am experimenting with python, OpenGl (pyopengl) and shaders (MacOS) and I am getting this weird message: OpenGL.GL.shaders.ShaderValidationError: Validation failure (0): b'Validation Failed: No vertex array object bound.\n' (on '''OpenGL.GL.shaders.compileProgram''' line)
What am I doing wrong? Here is the full code (slightly modified from the code by Attila Toth), sorry for the indentation
from OpenGL.GL import *
import OpenGL.GL.shaders
import glfw
import numpy
def main():
if not glfw.glfwInit():
return
glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 3);
glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 3);
glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE, glfw.GLFW_OPENGL_CORE_PROFILE);
glfw.glfwWindowHint(glfw.GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
window = glfw.glfwCreateWindow(800, 600, "My Window", None, None)
#print(glGetString(GL_VERSION))
if not window:
glfw.glfwTerminate()
return
glfw.glfwMakeContextCurrent(window)
triangle = [-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0
]
triangle = numpy.array(triangle, dtype=numpy.float32)
vertex_shader = """
#version 330
in vec3 position;
in vec3 color;
out vec3 newColor;
void main()
{
gl_Position = vec4(position, 1.0f);
newColor = color;
}
"""
fragment_shader = """
#version 330
in vec3 newColor;
out vec4 outColor;
void main()
{
outColor = vec4(newColor, 1.0f);
}
"""
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, 72, triangle, GL_STATIC_DRAW)
shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
position = glGetAttribLocation(shader, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
color = glGetAttribLocation(shader, "color")
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(color)
glUseProgram(shader)
glClearColor(.1, .3, .5, 1.0)
while not glfw.glfwWindowShouldClose(window):
glfw.glfwPollEvents()
glClear(GL_COLOR_BUFFER_BIT)
glMultiDrawArrays(GL_TRIANGLES, 0, 3)
glfw.glfwSwapBuffers(window)
glfw.glfwTerminate()
if __name__ == "__main__":
main()
Upvotes: 3
Views: 948
Reputation: 211135
When you use a cor profile OpenGL context (GLFW_OPENGL_CORE_PROFILE
), then you have to create a named Vertex Array Object
for the specification of the arrays of generic vertex attributes, because the default VAO (0) is not valid.
For instance:
vao = glGenVertexArrays(1)
glBindVertexArray(vao)
position = glGetAttribLocation(shader, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
color = glGetAttribLocation(shader, "color")
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(color)
Upvotes: 4