Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Modern OpenGL2D Ortho Textured Rectangle

  1. #1
    Intern Newbie
    Join Date
    Jul 2013
    Posts
    35

    Modern OpenGL2D Ortho Textured Rectangle

    In old OpenGL, once the projection matrix has been set to GLOrtho2D, you could simply use glTranslatef to position a 2D rectangle and then draw it like so:

    Code :
    void drawTexturedSurface(int width, int height) {
    	glBegin(GL_QUADS);
    		glTexCoord2d(0.0f, 0.0f); glVertex2f(0, 0);
    		glTexCoord2d(1.0f, 0.0f); glVertex2f(width, 0);
    		glTexCoord2d(1.0f, 1.0f); glVertex2f(width, height);
    		glTexCoord2d(0.0f, 1.0f); glVertex2f(0, height);
    	glEnd();
    }

    However, modern OpenGL doesn't support glTranslatef. What is the equilivant?

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    Quote Originally Posted by Quaver View Post
    However, modern OpenGL doesn't support glTranslatef. What is the equilivant?
    The equivalent for any sequence of matrix operations is to construct the equivalent matrix then pass it using glUniform() or a uniform block.

    All of the legacy matrix functions specify the actual matrix in their manual pages. Other than that, you just need matrix multiplication (and inversion for the normal matrix, in the event that the model-view matrix isn't orthonormal).

  3. #3
    Intern Newbie
    Join Date
    Jul 2013
    Posts
    35
    Thanks for your help. I forgot to mention that modern OpenGL doesn't support glBegin or glEnd functions. Do I have to replace these with VBOs, or is there a simpler method?

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    Quote Originally Posted by Quaver View Post
    Thanks for your help. I forgot to mention that modern OpenGL doesn't support glBegin or glEnd functions. Do I have to replace these with VBOs, or is there a simpler method?
    If you want to use OpenGL 3 core profile, you have to use VBOs.

  5. #5
    Intern Newbie
    Join Date
    Jul 2013
    Posts
    35
    Is there anyway to just create one square VBO and then stretch it to be the shape I want. Or do I have to make a new one for each different size EG: 42 x 30, 32 x 27, etc...

  6. #6
    Intern Newbie
    Join Date
    Jul 2013
    Posts
    35
    I'm having a bit of trouble with this, I created two functions, one to create the VBO, and the other to load it:

    Code :
    GLuint createSquareVBO(int width, int height) {
    	float vertices[4][3] = {	{0, 0, 0},
    								{width, 0, 0},
    								{width, height, 0},
    								{0, height, 0}		};
     
    	GLuint VBOIndex;
     
    	glGenBuffers(1, &VBOIndex);
     
    	glBindBuffer(GL_ARRAY_BUFFER, VBOIndex);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4, vertices, GL_STATIC_DRAW);
     
    	return VBOIndex;
    }
     
    void drawVBO(GLuint VBOIndex, int vertices) {
    	glEnableClientState(GL_VERTEX_ARRAY);
    	glEnableClientState(GL_NORMAL_ARRAY);
     
    	glBindBuffer(GL_ARRAY_BUFFER, VBOIndex);
    	glVertexPointer(3, GL_FLOAT, 0, NULL);
    	glDrawArrays(GL_TRIANGLES, 0, sizeof(float) * vertices);
     
    	glDisableClientState(GL_VERTEX_ARRAY);
    	glDisableClientState(GL_NORMAL_ARRAY);
    }

    And then I use them like so:

    Code :
    GLuint VBOIndex;
     
    void initVBOs(void) {
    	VBOIndex = createSquareVBO(32, 32);
    }
     
    void display(void) {
    	drawVBO(VBOIndex, 4);
    }

    However my application crashes.

  7. #7
    Intern Newbie
    Join Date
    Jul 2013
    Posts
    35
    I've narrowed down the crash the the glDrawArrays call.

  8. #8
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    785
    Code :
    glDrawArrays(GL_TRIANGLES, 0, sizeof(float) * vertices);

    glDrawArrays takes the number of vertices not the number of bytes as last parameter.

  9. #9
    Intern Newbie
    Join Date
    Jul 2013
    Posts
    35
    Thanks for your help, I removed the "* sizeof(float)" however it still crashes.

  10. #10
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    Quote Originally Posted by Quaver View Post
    Is there anyway to just create one square VBO and then stretch it to be the shape I want. Or do I have to make a new one for each different size EG: 42 x 30, 32 x 27, etc...
    Transform it in the vertex shader.

Posting Permissions

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