0
\$\begingroup\$

I'm trying to move away from using glVertex() in my game code because it doesn't seem to work well with a large quantity of vertices. At the moment I'm trying to make code that uses Shaders, but when compiling with shaders.compileProgram, it gives me the error shown in the title, with no explanation as to what is wrong.

Code so far:

import OpenGL
from OpenGL.GL import *
from OpenGL.GL import shaders
from OpenGL.arrays import vbo

import math
import pygame
from pygame.locals import *

VERTEX_SHADER = shaders.compileShader("""
#version 120 
 void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
 }
""", GL_VERTEX_SHADER)

FRAGMENT_SHADER = shaders.compileShader("""
#version 120 
void main() { 
    gl_FragColor = vec4( 0, 1, 0, 1 ); 
}
""", GL_FRAGMENT_SHADER)

self.shader = shaders.compileProgram(VERTEX_SHADER,FRAGMENT_SHADER)

The full error message:

Traceback (most recent call last):
  File "PyOpenGl shader test from scratch.py", line 24, in <module>
    self.shader = shaders.compileProgram(VERTEX_SHADER,FRAGMENT_SHADER)
  File "/home/[my name]/.local/lib/python2.7/site-packages/OpenGL    /GL/shaders.py", line 196, in compileProgram
    program.check_validate()
  File "/home/[my name]/.local/lib/python2.7/site-packages/OpenGL/GL/shaders.py", line 108, in check_validate
    glGetProgramInfoLog( self ),
RuntimeError: Validation failure (0): 

Is there a line I need to add for it to tell me what is wrong?

\$\endgroup\$
  • \$\begingroup\$ What is the result of the call to glGetProgramInfoLog()? Does anything change if you change the values in the fragment shader to (0.0, 1.0, 0.0, 1.0)? I've sometimes had issues with implicit casts in glsl. \$\endgroup\$ – user1118321 Jul 18 '18 at 2:36
  • \$\begingroup\$ Don't use gl_ModelViewProjectionMatrix if you want to move away from GL1 stuff \$\endgroup\$ – Bálint Jul 18 '18 at 2:45
  • \$\begingroup\$ Usually calling glGetShaderInfoLog(yourShader), glGetProgramInfoLog(yourProgram) or glGetError() solves your problem \$\endgroup\$ – Bálint Jul 18 '18 at 2:48
  • \$\begingroup\$ gl_ModelViewProjectionMatrix is compatible with Glsl 120. There's a bad habit in OpenGL circles of focusing on use of old stuff rather than answering the question. It might have been helpful to say what to use instead. \$\endgroup\$ – Maximus Minimus Jul 18 '18 at 3:05
  • \$\begingroup\$ @MaximusMinimus Thank you for that, for the record I get errors trying to use GLSL 330 in PyOpenGL. (But not C++ OpenGL for some reason?) \$\endgroup\$ – C1ff Jul 18 '18 at 16:12
1
\$\begingroup\$

Well, it turns out that I've just completely missed the fact that this tutorial uses a special library specifically for the tutorial. (I hate when a tutorial does this... How am I supposed to do anything without the tutorial module?)

This is the tutorial in question.

It doesn't even appear that the problem was with the shaders, I was just missing functions from an inherited class.

\$\endgroup\$
1
\$\begingroup\$

This often happens also if you do not specify the context before compiling the shaders. For example, for QT Widgets (pytqt) you should have your shaders compiled when the gl context is enabled, for example in the initializeGL function. So in the case of using QT you can do something like:

class MyQtWidget(QOpenGLWidget, QWidget):
    def __init__(self, directory):
        super(MyQtWidget, self).__init__()
...

def initializeGL(self):
    """
    Initializes openGL context and scenery
    """
    self.shader = compileProgram(
            compileShader(my_vert_shader, gl.GL_VERTEX_SHADER),
            compileShader(my_frag_shader, gl.GL_FRAGMENT_SHADER)
        )

As for your case, please see the example from here:
https://github.com/tartley/opengl-tutorials/blob/master/pyopengl/05-lighting.py
Shaders are compiled after the context has been enabled (via the inheritance of BaseContext class).

Another example with the use of glfw also initializes the context beforehand:
https://github.com/totex/PyOpenGL_tutorials/blob/master/main.py
So in here we have this glfw.make_context_current function.

However, even if you do the above, this does not mean that your shaders are correct, but if you do intialize the context, you should get a verbose error message on what is wrong with your shaders.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.