VAO data not being received by shader

Started by
0 comments, last by Kaptein 9 years, 11 months ago

VAO data not being received by shader is only my suspicion because i don't know what's the problem.

I'm simply trying to pass position and color vertex attributes to the shader.

Global vars:


GLuint vao[3];
GLuint vertexLoc, colorLoc;
float vertices1[] = {   -3.0f, 0.0f, -5.0f, 1.0f,
            -1.0f, 0.0f, -5.0f, 1.0f,
            -2.0f, 2.0f, -5.0f, 1.0f};
float colors1[] = { 0.0f, 0.0f, 1.0f, 1.0f,
            0.0f, 1.0f, 0.0f, 1.0f,
            1.0f,0.0f, 0.0f, 1.0f};

Init:


void initRendering() {
 
std::string vertexInputString="textureVS.txt";
std::string fragmentInputString="textureFS.txt";
 
//create new shader
InitializeProgram(shaderOne, readFiletoString(vertexInputString), readFiletoString(fragmentInputString));
 
    GLuint buffers[2];
    glGenVertexArrays(3, vao);
    //
    // VAO for first triangle
    //
    glBindVertexArray(vao[0]);
    // Generate two slots for the vertex and color buffers
    glGenBuffers(1, buffers);
    // bind buffer for vertices and copy data into buffer
    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);
    glEnableVertexAttribArray(vertexLoc);
    glVertexAttribPointer(vertexLoc, 4, GL_FLOAT, 0, 0, 0);
 
// bind buffer for colors and copy data into buffer
    glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors1), colors1, GL_STATIC_DRAW);
    glEnableVertexAttribArray(colorLoc);
    glVertexAttribPointer(colorLoc, 4, GL_FLOAT, 0, 0, 0);
 
//enables/disables
glEnable(GL_DEPTH_TEST);
glEnable (GL_BLEND);
 
//load textures
image = SOIL_load_OGL_texture(
        "resources/blank.png",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y
    );
if( 0 == image ){printf( "SOIL loading error: '%s'\n", SOIL_last_result());}
 
//start shader
glUseProgram(shaderOne);
 
 
vertexLoc = glGetAttribLocation(shaderOne,"position");
colorLoc = glGetAttribLocation(shaderOne,"color");
 
}

Draw:


void drawPerspectiveScene(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, windowWidth, windowHeight);
glClearColor(0.5,5.5,0.25,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float)windowWidth / (float)windowHeight, 0.4, 9000.0);
glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
 
 
//set camera
gluLookAt(cam.posX,cam.posY,cam.posZ,cam.viewX,cam.viewY,cam.viewZ,cam.upX,cam.upY,cam.upZ);
 
glColor3f(0.0,0.0,1.0);
 
glBindVertexArray(vao[0]);
glDrawArrays(GL_TRIANGLES, 0, 3);
 

//swap buffers
glutPostRedisplay();
glutSwapBuffers();
 
}

GLSL:

Vert:


attribute vec4 position;
attribute vec4 color;
varying vec4 Color;
 
void main() {
Color=color; 
gl_Position = gl_ModelViewProjectionMatrix * position;
}

Frag:


varying vec4 color;
 
void main()
{
    gl_FragColor = color;
}
 

I know im using deprecated function, this is my first step towards modern opengl.

It renders without error, however it uses the color data as vertex position data, so the triangle looks distorted.

Advertisement

Uh, besides the fact that you are creating the VAO with the variables before they are set,

I can't actually help you here because it looks like you are mixing old and new, and it might simply not work at all.

I think attributes and varyings was when you used glVertexPointer() and friends, but nowadays we use glBindAttribLocation before glLinkProgram, or define order in shader using layout location.

So, before you link your shader program, do this:


glBindAttribLocation(shader, 0, "in_vertex");
glBindAttribLocation(shader, 1, "in_color");
glLinkProgram(shader);

And in the vertex shader:


#version 130
in vec4 in_vertex;
in vec4 in_color;

out vec4 v_color;

void main(void)
{
    v_color = in_color;
    gl_Position = gl_ModelViewProjectionMatrix * in_vertex;
}

Finally, fragment shader:


#version 130

in vec4 v_color;

void main(void)
{
    gl_FragColor = v_color;
}

This isn't a very modern solution, but it will work for you, and you won't have to make so many changes.

This topic is closed to new replies.

Advertisement