Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

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

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2006
    Location
    Downey, CA
    Posts
    20

    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:
    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
    Code :
    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.
    Visit Lazy Foo' Productions home of my SDL tutorials

    Want to transition from OpenGL 2.1 to 3.0+? Check out my OpenGL Tutorial

  2. #2
    Junior Member Newbie
    Join Date
    Jan 2009
    Location
    Poland
    Posts
    26

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

    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)

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2006
    Location
    Downey, CA
    Posts
    20

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

    Quote Originally Posted by Jacek Nowak
    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.
    Visit Lazy Foo' Productions home of my SDL tutorials

    Want to transition from OpenGL 2.1 to 3.0+? Check out my OpenGL Tutorial

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •