• Advertisement
Sign in to follow this  

Trouble transforming vertices in simple shader.

This topic is 1596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement