How do I pass vertex and color positions to OpenGL shaders?

Posted by smoth190 on Game Development See other posts from Game Development or by smoth190
Published on 2012-04-14T23:45:12Z Indexed on 2012/04/14 23:49 UTC
Read the original article Hit count: 248

Filed under:
|

I've been trying to get this to work for the past two days, telling myself I wouldn't ask for help. I think you can see where that got me...

I thought I'd try my hand at a little OpenGL, because DirectX is complex and depressing. I picked OpenGL 3.x, because even with my OpenGL 4 graphics card, all my friends don't have that, and I like to let them use my programs. There aren't really any great tutorials for OpenGL 3, most are just "type this and this will happen--the end".

I'm trying to just draw a simple triangle, and so far, all I have is a blank screen with my clear color (when I set the draw type to GL_POINTS I just get a black dot). I have no idea what the problem is, so I'll just slap down the code:

Here is the function that creates the triangle:

void CEntityRenderable::CreateBuffers()
{
    m_vertices = new Vertex3D[3];
    m_vertexCount = 3;

    m_vertices[0].x = -1.0f;
    m_vertices[0].y = -1.0f;
    m_vertices[0].z = -5.0f;
    m_vertices[0].r = 1.0f;
    m_vertices[0].g = 0.0f;
    m_vertices[0].b = 0.0f;
    m_vertices[0].a = 1.0f;

    m_vertices[1].x =  1.0f;
    m_vertices[1].y = -1.0f;
    m_vertices[1].z = -5.0f;
    m_vertices[1].r = 1.0f;
    m_vertices[1].g = 0.0f;
    m_vertices[1].b = 0.0f;
    m_vertices[1].a = 1.0f;

    m_vertices[2].x =  0.0f;
    m_vertices[2].y =  1.0f;
    m_vertices[2].z = -5.0f;
    m_vertices[2].r = 1.0f;
    m_vertices[2].g = 0.0f;
    m_vertices[2].b = 0.0f;
    m_vertices[2].a = 1.0f;

    //Create the VAO
    glGenVertexArrays(1, &m_vaoID);
    //Bind the VAO
    glBindVertexArray(m_vaoID);

    //Create a vertex buffer
    glGenBuffers(1, &m_vboID);
    //Bind the buffer
    glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
    //Set the buffers data
    glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices, GL_STATIC_DRAW);
    //Set its usage
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), 0);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_TRUE,  sizeof(Vertex3D), (void*)(3*sizeof(float)));

    //Enable
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    //Check for errors
    if(glGetError() != GL_NO_ERROR)
    {
        Error("Failed to create VBO: %s", gluErrorString(glGetError()));
    }
    //Unbind...
    glBindVertexArray(0);
}

The Vertex3D struct is as such...

struct Vertex3D
{
    Vertex3D() : x(0), y(0), z(0), r(0), g(0), b(0), a(1) {}

    float x, y, z;
    float r, g, b, a;
};

And finally the render function:

void CEntityRenderable::RenderEntity()
{
    //Render...
    glBindVertexArray(m_vaoID);
    //Use our attribs

    glDrawArrays(GL_POINTS, 0, m_vertexCount);

    glBindVertexArray(0); //unbind
    OnRender();
}

(And yes, I am binding and unbinding the shader. That is just in a different place) I think my problem is that I haven't fully wrapped my mind around this whole VertexAttribArray thing (the only thing I like better in DirectX was input layouts D:).

This is my vertex shader:

#version 330

//Matrices
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
//In values
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
//Out values
out vec3 frag_color;
//Main shader
void main(void)
{
    //Position in world
    gl_Position = vec4(position, 1.0);
    //gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
    //No color changes
    frag_color = color;
}

As you can see, I've disable the matrices, because that just makes debugging this thing so much harder. I tried to debug using glslDevil, but my program just crashes right before the shaders are created... so I gave up with that. This is my first shot at OpenGL since the good old days of LWJGL, but that was when I didn't even know what a shader was. Thanks for your help :)

© Game Development or respective owner

Related posts about opengl

Related posts about shaders