Search code examples
cglslglutfreeglut

GLSL Uniform float not correctly passing


I'm trying to create a shader for a sceen-filling quad. But i can't seem to pass a uniform float to the shader.

In the following example i'm initializing glut, creating/compiling & linking the shaders, passing a uniform int & float to the shader, and getting them back to check. the int asdf works fine, the float qwer is behaving weirdly.

If i set the value of qwer to 1.3, the uniform is set to -2.0, if i set it to 1.2, the uniform is set to 2.0.

#include <stdio.h>
#include <string.h>
#include <GL/glut.h>
#include <unistd.h>

int gw = 640, gh = 360;

void drawScene(){
    //creating a screen filling quad
    glBegin(GL_QUADS);

    glTexCoord2f(0.0, 0.0f); glVertex2i(-1, 1);
    glTexCoord2f(1.0, 0.0f); glVertex2i(1, 1);
    glTexCoord2f(1.0, 1.0f); glVertex2i(1, -1);
    glTexCoord2f(0.0, 1.0f); glVertex2i(-1, -1);

    glEnd();

    glutSwapBuffers();
}

void update(int value){
    glutPostRedisplay();
    glutTimerFunc(1000 / 30, update, 0);
}

int main(int argc, char** argv){

    //shader source code

    char *fraShdrStr = "\n\
uniform int asdf;\
uniform float qwer;\
void main(){\n\
    vec2 p = gl_TexCoord[0].xy;\n\
    gl_FragColor=vec4(p.x,qwer,float(asdf),1.0);\n\
}";
    char *verShdrStr = "\n\
void main(){\n\
    gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;\n\
    gl_TexCoord[0]=gl_MultiTexCoord0;\n\
}";

    size_t verShdrLen, fraShdrLen;
    char errorBuffer[1024];
    int errorLength;
    int program, verShdr, fraShdr;
    verShdrLen = strlen(verShdrStr);
    fraShdrLen = strlen(fraShdrStr);

    //initializing glut
    glutInit(&argc, argv);
    glutInitWindowSize(gw, gh);

    glutCreateWindow("");

    //creating, compiling and linking shaders
    verShdr = glCreateShader(GL_VERTEX_SHADER);
    fraShdr = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(verShdr, 1, &verShdrStr, &verShdrLen);
    glShaderSource(fraShdr, 1, &fraShdrStr, &fraShdrLen);

    glCompileShader(verShdr);
    glGetShaderInfoLog(verShdr, 1024, &errorLength, errorBuffer);
    if(errorLength) printf("Vertex Shader Error:\n%s\n", errorBuffer);

    glCompileShader(fraShdr);
    glGetShaderInfoLog(fraShdr, 1024, &errorLength, errorBuffer);
    if(errorLength) printf("Fragmen Shader Error:\n%s\n", errorBuffer);

    program = glCreateProgram();

    glAttachShader(program, verShdr);
    glAttachShader(program, fraShdr);

    glLinkProgram(program);
    glGetProgramInfoLog(program, 1024, &errorLength, errorBuffer);
    if(errorLength) printf("Linking Error:\n%s\n", errorBuffer);



    glUseProgram(program);

    //initializing variables to pass as uniform
    int asdf = 9;
    int asdf2;
    float qwer = 1.0;
    float qwer2;

    //setting the uniform values
    glUniform1i(glGetUniformLocation(program, "asdf"), asdf);
    glGetUniformiv(program, glGetUniformLocation(program, "asdf"), &asdf2);
    printf("%d\n", asdf2);

    glUniform1f(glGetUniformLocation(program, "qwer"), qwer);
    glGetUniformfv(program, glGetUniformLocation(program, "qwer"), &qwer2);
    printf("%f\n", qwer2);


    glutDisplayFunc(drawScene);
    glutTimerFunc(1000/30, update, 0);

    glutMainLoop();
}

Solution

  • You are misunderstanding the whole picture. Since OpenGL 3.0 using glBegin/glEnd in order to draw stuff is deprecated. Instead you should use an approach, based on using so-called Vertex Arrays. Check out this question to get an example code snippet.