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

Thread: Modern OpenGL2D Ortho Textured Rectangle

  1. #1
    Member 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
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,104
    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
    Member 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
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,104
    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
    Member 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
    Member 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
    Member Newbie
    Join Date
    Jul 2013
    Posts
    35
    I've narrowed down the crash the the glDrawArrays call.

  8. #8
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    Code :
    glDrawArrays(GL_TRIANGLES, 0, sizeof(float) * vertices);

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

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

  10. #10
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,104
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-06-2015, 07:35 AM
  2. Replies: 2
    Last Post: 05-14-2014, 03:01 AM
  3. Issues mixing textured and non-textured objects
    By deegee in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-12-2011, 08:35 AM
  4. ortho + perspective + ortho
    By mithun_daa in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 06-09-2004, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean