Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Trouble transforming vertices in simple shader.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 programmer_tom   Members   -  Reputation: 214

Like
0Likes
Like

Posted 07 September 2013 - 10:18 PM

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?



Sponsor:

#2 bradbobak   Members   -  Reputation: 1133

Like
0Likes
Like

Posted 07 September 2013 - 11:47 PM

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



#3 Sponji   Members   -  Reputation: 1314

Like
0Likes
Like

Posted 08 September 2013 - 03:19 AM

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

#4 WhiteChocolateMocha   Members   -  Reputation: 375

Like
0Likes
Like

Posted 08 September 2013 - 05:03 AM

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 };


#5 Ohforf sake   Members   -  Reputation: 1832

Like
0Likes
Like

Posted 08 September 2013 - 11:51 AM

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.



#6 programmer_tom   Members   -  Reputation: 214

Like
0Likes
Like

Posted 09 September 2013 - 01:03 PM

Argh it wasn't any of those things :(

 

Any other ideas?



#7 BornToCode   Members   -  Reputation: 936

Like
0Likes
Like

Posted 09 September 2013 - 09:06 PM

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



#8 Ohforf sake   Members   -  Reputation: 1832

Like
0Likes
Like

Posted 10 September 2013 - 01:08 AM

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?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS