Skip to main content
GameDev.net gamedev.net
🔒 Locked

Trouble transforming vertices in simple shader.

Started by programmer_tom Sep 8, 2013 at 4:18 AM 6 replies 2k views
Original Post
programmer_tom
programmer_tom

Hey all,

Having some trouble with what should be a simple thing to do...

I've successfully compiled and linked very simple vertex and fragment shaders.

Having trouble seeing my red triangle though.

This stuff all goes fine:


GLint ProgramObject = glCreateProgram();
glAttachShader ( ProgramObject, VertexShaderObject ); 
glAttachShader ( ProgramObject, PixelShaderObject );
glLinkProgram ( ProgramObject );
glUseProgram ( ProgramObject );

VBO creation:


GLuint vbo;
glGenBuffers ( 1, &vbo );
glBindBuffer ( GL_ARRAY_BUFFER, vbo );
    
GLfloat pos[] = { -0.5, -0.5, 0.5,
                           0.5, -0.5, 0.5,
                           0.0,  0.5, 0.5 };
                                     
glBufferData, GL_ARRAY_BUFFER, sizeof(pos), &pos[0], GL_STATIC_DRAW;

// Something is wrong here i am guessing...


int nPositionID = glGetAttribLocation ( ProgramObject, "s_vPosition" );
glEnableVertexAttribArray ( nPositionID );
glVertexAttribPointer ( nPositionID, 3, GL_FLOAT, FALSE, 0, 0 );
           
   /*
   glBegin(GL_TRIANGLES);
   glVertexAttrib3f ( nPositionID, -0.5, -0.5, 0.0 );  // If i do this, i see my red triangle (correct fragment color).
   glVertexAttrib3f ( nPositionID,  0.5, -0.5, 0.0 );  // Makes me believe context is ok.
   glVertexAttrib3f ( nPositionID,  0.0,  0.5, 0.0 );
   glEnd();
   */
   
glDrawArrays ( GL_TRIANGLES, 0, 3 );
    
SwapBuffers ( m_DeviceContext );

vertex shader looks like this:


#version 130\n
in vec4 s_vPosition;
void main()
{
gl_Position = vec4 ( s_vPosition.x, s_vPosition.y, s_vPosition.z, 1.0 );
}

Any ideas?

bradbobak
bradbobak

I just started this shader stuff, but I think your vertex shader needs to output a color to the fragment shader.

Sponji
Sponji

You are using 0.5 as z in your vbo, and in the commented code you are using 0.0. Maybe that's the reason?

Derp
WhiteChocolateMocha
WhiteChocolateMocha

OpenGL by default looks down the -z axis. Setting your triangle z coords to -1 should help


GLfloat pos[] = { -0.5, -0.5, -1.0,
                           0.5, -0.5, -1.0,
                           0.0,  0.5, -1.0 };
Ohforf sake
Ohforf sake

Actually the OpenGL clip space is from -1 to 1 so 0.5 should be ok. One problem could be, that in the shader you specify the position as a 4 component vector, whereas you only hand in 3 component vectors with glVertexAttribPointer.

Also make sure you disabled the depth test.

programmer_tom
programmer_tom

Argh it wasn't any of those things :(

Any other ideas?

BornToCode
BornToCode

It looks like your vertices are not been transform by any perspective nor ortho matrix.

Ohforf sake
Ohforf sake

It looks like your vertices are not been transform by any perspective nor ortho matrix.

You don't need to.

The following code works for me (Linux, NVidia drivers):
        glewInit();


        const char *vertexShader ="#version 130\n"
                "in vec3 s_vPosition;"
                "void main()"
                "{"
                "    gl_Position = vec4 ( s_vPosition.x, s_vPosition.y, s_vPosition.z, 1.0 );"
                "}";

        const char *pixelShader ="#version 130\n"
                "void main()"
                "{"
                "    gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);"
                "}";


        GLint VertexShaderObject = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(VertexShaderObject, 1, &vertexShader, NULL);
        glCompileShader(VertexShaderObject);

        GLint PixelShaderObject = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(PixelShaderObject, 1, &pixelShader, NULL);
        glCompileShader(PixelShaderObject);

        GLint ProgramObject = glCreateProgram();
        glAttachShader ( ProgramObject, VertexShaderObject );
        glAttachShader ( ProgramObject, PixelShaderObject );
        glLinkProgram ( ProgramObject );

        int success;
        glGetProgramiv(ProgramObject, GL_LINK_STATUS, &success);
        if (success != GL_TRUE)
            std::cout << "Linking failed!" << std::endl;


        GLuint vbo;
        glGenBuffers ( 1, &vbo );
        glBindBuffer ( GL_ARRAY_BUFFER, vbo );

        GLfloat pos[] = { -0.5, -0.5, 0.5,
                                   0.5, -0.5, 0.5,
                                   0.0,  0.5, 0.5 };

        glBufferData(GL_ARRAY_BUFFER, sizeof(pos), &pos[0], GL_STATIC_DRAW);

        while (!window.getState().shutdown) {

            glClear(GL_COLOR_BUFFER_BIT);

            glDisable(GL_DEPTH_TEST);
            glUseProgram ( ProgramObject );
            int nPositionID = glGetAttribLocation ( ProgramObject, "s_vPosition" );

            glBindBuffer ( GL_ARRAY_BUFFER, vbo );
            glEnableVertexAttribArray ( nPositionID );
            glVertexAttribPointer ( nPositionID, 3, GL_FLOAT, GL_FALSE, 0, 0 );

            glDrawArrays ( GL_TRIANGLES, 0, 3 );


            Engine::Platform::BaseWindow::Event event;
            while (window.getNextEvent(event)) {
            }
            window.getContext()->swap();
        }
Can you give a minimal not working example?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.