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:

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?

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).

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.

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…

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.

I’ve narrowed down the crash the the glDrawArrays call.


glDrawArrays(GL_TRIANGLES, 0, sizeof(float) * vertices);

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

Thanks for your help, I removed the “* sizeof(float)” however it still crashes.

Transform it in the vertex shader.

Thanks for the information, I still can’t seem to stop the crash though.