Drawing in opengl 2d

Started by
6 comments, last by namingway 16 years, 6 months ago
How can i setup the drawing so that i can assign values in pixels. e.g. 32x32 tiles Because this code just draws a large square in the top left corner.

glTranslatef(-1.0f, 0.0f, 0.0f);
	glColor3f(0.0f, 0.0f, 1.0f);
	glBegin(GL_QUADS);
		glVertex2i(0, 0);
		glVertex2i(1, 0);
		glVertex2i(1, 1);
		glVertex2i(0, 1);
	glEnd();

Advertisement
Since you seem to be wanting to do 2D, set an off-center orthographic projection from (0,0) to (width,height).

To make it is hell. To fail is divine.

im pretty sure 2d is setup i have glOrtho called and 3d things like depth testing turned off, I just don't know how to draw things like you can in SDL and GDI where you specify pixel coordinates.
I don't know SDL, but I guess your question is how to draw your 2d map in opengl. You need to have a nested loop to draw your 2d map. If your tile size is 32 by 32, assign tilewidth and tileheight to 32. The code should be something like this:

for (x = 0 to mapwidth){	for (y = 0 to mapheight)	{		glBegin(GL_QUADS);			glVertex2i(x, y);			glVertex2i(x+tilewidth, y);			glVertex2i(x+tilewidth, y+tileheight);			glVertex2i(x, y+tileheight);		glEnd();	}}
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
no no the map loading code isnt a problem, well it is but its more related to this one...
For now all I want is to draw a 32x32 square onto the screen. How can I do that?
I believe you mean to texture map your bitmap tile onto the square you created on the screen. Try to google for some texture mapping tutorials. Check out NeHe tutorial which has a very good sample that shows you how to do texture mapping in opengl. Good luck!
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
As have been said before your solution is to put this at your init when you setup your 2d projection :
glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0,resX,resY,0,0,10);glMatrixMode(GL_MODELVIEW);glLoadIdentity();
this has been solved, i just needed to draw a plain untextured 32x32 square to the screen, which i have now done. However now i am having trouble with the textures as they arent displaying:(

This topic is closed to new replies.

Advertisement