Drawing a 2D Quad in OpenGL 2.1/3.+

I’ve been trying to upgrade my old 2D OpenGL codebase so it’s OpenGL 2.1/3+/ES compatible. I’ve managed to get the shaders and matrices to work and I’m trying to draw a quad.

Here’s my C++ code:


    GLfloat square[] = { 0.0f,   0.0f,
                       100.0f,   0.0f,
                       100.0f, 100.0f,
                         0.0f, 100.0f };

    GLuint testBuf = NULL;
    glGenBuffers( 1, &testBuf );
    glBindBuffer( GL_ARRAY_BUFFER, testBuf );
    glBufferData( GL_ARRAY_BUFFER, 4 * 2 * sizeof(GLfloat), square, GL_STATIC_DRAW );

    GLint verts = glGetAttribLocation( LEngine::get_shader(), "lVertex" );
    glVertexAttribPointer( verts, 2, GL_FLOAT, GL_FALSE, 0, NULL );
    GLuint iData[] = { 0, 1, 2, 3 };
    glDrawElements( GL_QUADS, 4, GL_UNSIGNED_INT, iData );

I’m trying to create and load a vertex buffer, get the attribute location (it gets reported as 0) and send the attribute data, and draw 4 vertices in order.

Here’s my vertex shader


uniform mat4 LProj;
uniform mat4 LMView;

attribute vec2 lVertex;

void main()
{
	//gl_Position = LProj * LMView * gl_Vertex;
	gl_Position = LProj * LMView * vec4(lVertex.x, lVertex.y, 0.0, 1.0);
}

All I’m getting is a blank screen.

Blank screen is not very helpful. First of all, try slapping

glEnableVertexAttribArray(verts);

somewhere. Also, make sure LProj and LMView have correct values and you aren’t rendering black quad on black screen.

Also, ‘attribute’ has been replaced with ‘in’ as in “in vec2 lVertex” (in OpenGL 3+, not sure about ES)

Yeah, turns out the attribute had to be enabled.