Question

I'm trying my hand at opengl programming, loaded up a default example in codeblocks, and after doing a whole lot of function pointer loading, I got myself an opengl 4.3 context in a window which can be cleared with a certain color(YES!), so I went ahead and try to render a triangle...well its not working, not sure where I went wrong I've been around the dark corners of google and my debugger to see if VBO and shader creation went wrong...no luck

here's relevent gl code:

GLuint IndiceArray[3] = {
0, 1, 2
};

const float vertexPositions[] = {
    0.75f, 0.75f,
    0.75f, -0.75f,
    -0.75f, -0.75f,
};

const float vertexColors[] = {
    1.0f, 0.0f, 0.0f, 1.0f,
    0.0f, 1.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f, 1.0f,
};

//Prepare GPU program
vertexID = glCreateShader( GL_VERTEX_SHADER );
fragmentID = glCreateShader( GL_FRAGMENT_SHADER );

glShaderSource(vertexID, 1, &vsSource, NULL) ;
glShaderSource(fragmentID, 1, &fsSource, NULL) ;

glCompileShader( vertexID );
glCompileShader( fragmentID );

programID = glCreateProgram();
glAttachShader( programID, vertexID );
glAttachShader( programID, fragmentID );
glLinkProgram( programID );
glUseProgram( programID );

//Send to OpenGL
glGenBuffers(3, vertexAttribBufferID);

glBindBuffer( GL_ARRAY_BUFFER, vertexAttribBufferID[0] );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW );

glBindBuffer( GL_ARRAY_BUFFER, vertexAttribBufferID[1] );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertexColors), vertexColors, GL_STATIC_DRAW );

glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vertexAttribBufferID[2] );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(IndiceArray), IndiceArray, GL_STATIC_DRAW );

colorAttribID = glGetAttribLocation( programID, "colorIn" );
positionAttribID = glGetAttribLocation( programID, "vertexPos" );

/* program main loop */
while (!bQuit)
{
    /* check for messages */
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        /* handle or dispatch messages */
        if (msg.message == WM_QUIT)
        {
            bQuit = TRUE;
        }
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    else
    {
        /* OpenGL animation code goes here */

        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        //Render!!
        glBindBuffer( GL_ARRAY_BUFFER, vertexAttribBufferID[0] );
        glEnableVertexAttribArray( positionAttribID );
        glVertexAttribPointer( positionAttribID, 2, GL_FLOAT, GL_FALSE, 0, 0 );

        glBindBuffer( GL_ARRAY_BUFFER, vertexAttribBufferID[1] );
        glEnableVertexAttribArray( colorAttribID );
        glVertexAttribPointer( colorAttribID, 4, GL_FLOAT, GL_FALSE, 0, 0 );

        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vertexAttribBufferID[2] );

        glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0 );
        glDisableVertexAttribArray( colorAttribID );
        glDisableVertexAttribArray( positionAttribID );

        SwapBuffers(hDC);
        Sleep (1);
    } 

And shaders :

const char vertexShader[] =
"#version 150 core\n"

"in vec2 vertexPos;\n"
"in vec4 colorIn;\n"
"smooth out vec4 outColor;\n"

"void main()\n"
"{\n"
"   gl_Position = vec4(vertexPos, 0.0f, 1.0f);\n"
"   outColor = colorIn;\n"
"}";

const char fragShader[] =
"#version 150 core\n"

"smooth in vec4 inColor;\n"
"out vec4 f_color;\n"

"void main()\n"
"{\n"
"   f_color = inColor;\n"
"}";

Needless to say, I would be ever thankful for any help, might save me from being frustrated from OpenGL!!!

edit: I only see the cleared background in case you're wondering...

Was it helpful?

Solution

OK, figured it out: apparently data that you pass between shaders must have the same name as variables on both sides, so my code should be:

const char vertexShader[] =
"#version 150 core\n"

"in vec2 vertexPos;\n"
"in vec4 colorIn;\n"
"smooth out vec4 outColor;\n"

"void main()\n"
"{\n"
"   gl_Position = vec4(vertexPos, 0.0f, 1.0f);\n"
"   outColor = colorIn;\n"
"}";

const char fragShader[] =
"#version 150 core\n"

"smooth in vec4 outColor;\n"
"out vec4 f_color;\n"

"void main()\n"
"{\n"
"   f_color = inColor;\n"
"}";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top