PDA

View Full Version : Modern OpenGL2D Ortho Textured Rectangle



Quaver
08-17-2013, 07:29 AM
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:


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?

GClements
08-17-2013, 08:16 AM
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 (http://www.opengl.org/sdk/docs/man2/). 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).

Quaver
08-17-2013, 10:35 AM
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?

GClements
08-17-2013, 12:06 PM
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.

Quaver
08-17-2013, 12:50 PM
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...

Quaver
08-19-2013, 07:28 AM
I'm having a bit of trouble with this, I created two functions, one to create the VBO, and the other to load it:



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:



GLuint VBOIndex;

void initVBOs(void) {
VBOIndex = createSquareVBO(32, 32);
}

void display(void) {
drawVBO(VBOIndex, 4);
}


However my application crashes.

Quaver
08-19-2013, 09:11 AM
I've narrowed down the crash the the glDrawArrays call.

carsten neumann
08-19-2013, 09:23 AM
glDrawArrays(GL_TRIANGLES, 0, sizeof(float) * vertices);


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

Quaver
08-19-2013, 09:30 AM
Thanks for your help, I removed the "* sizeof(float)" however it still crashes.

GClements
08-20-2013, 08:28 AM
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.

Quaver
08-20-2013, 10:39 AM
Thanks for the information, I still can't seem to stop the crash though.