简体   繁体   中英

GLSL compilation error when using QGLShaderProgram

I'm currently trying out some of the opengl components in Qt 5, I'm compiling on Mac OSX 10.8 with QtCreator 2.6.2, clang 4.2 I've written a very basic GLSL shader that compiles and links well in OpenGl Shader Builder, but when I try to load it using a QGLShader, it fails to compile and the log function returns no error message.

Vertex Shader:

attribute vec4 in_position;
attribute vec3 in_normal;
attribute vec3 in_color;
attribute vec2 in_tex;

uniform vec4 lightPosition;

varying vec2 texCoords;
varying vec3 normal;
varying vec3 vertToLightDir;

void main(void)
{
    gl_Position = gl_ModelViewProjectionMatrix * in_position;
    vec4 worldVert = gl_ModelViewMatrix * in_position;
    vertToLightDir = normalize(vec3(lightPosition - worldVert));
    normal = gl_NormalMatrix * in_normal;
    texCoords = in_tex;
}

Fragment Shader:

uniform sampler2D texture0;
uniform vec4 lightColor;

varying vec2 texCoords;
varying vec3 normal;
varying vec3 vertToLightDir;

void main(void)
{
    float lightIntensity = clamp(dot(normal, vertToLightDir), 0.0, 1.0);
    gl_FragColor = (texture2D(texture0, texCoords) + (lightColor * lightIntensity)) * 0.5;
}

The code that loads the shaders:

QGLShader fragShader(QGLShader::Fragment);
bool success = fragShader.compileSourceFile("Fragment.glsl");
qDebug() << fragShader.log();

I used the debugger to see that the compileSourceFile function returns false, I also used access("Fragment.glsl", F_OK) to see if the program manages to find the file and it does, the same goes for the vertex shader file, I can't seem to find the reason they won't compile. Is there something I'm doing wrong ?

After some debugging I realized I was trying to compile the shaders before a valid context was created. My bad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM