How to plot a 2d tilemap with opengl?

I am in the process of learning opengl in hopes of using it for 2d rendering. I tried to write a quick test to plot a simple map of quads but i can't get the right effect. This is my code so far (please tell me what is wrong with it)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "GLWindow.h"

const int MapWidth = 12;
const int MapHeight = 12;
const int TileWidth = 40;
const int TileHeight = 40;

void InitFunc()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	RECT r;
	GetClientRect(GLWindow::Instance()->Window(), &r);

	gluOrtho2D(r.left, r.right, r.bottom, r.top);
	glViewport(0, 0, r.right, r.bottom);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void RenderFunc()
{
	glLoadIdentity();

	glPushMatrix();

	for (int y = 0; y < MapHeight; y++)
	{
		for (int x = 0; x < MapWidth; x++)
		{
			glTranslatef((float)x*TileWidth, (float)y*TileHeight, 0.0);

			glBegin(GL_QUADS);
				glVertex2f((float)TileWidth, 0.0);
				glVertex2f(0.0, 0.0);
				glVertex2f(0.0, (float)TileHeight);
				glVertex2f((float)TileWidth, (float)TileHeight);
			glEnd();
		}
	}

	glPopMatrix();
}

int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
	GLWindow::Instance()->SetInitFunc(InitFunc);
	GLWindow::Instance()->SetRenderFunc(RenderFunc);
	GLWindow::Instance()->Start();
}


GLWindow is just a class that creates a window and rendering context and runs the message pump.
Last edited on
Topic archived. No new replies allowed.