Jump to content
  • Advertisement
Sign in to follow this  
namingway

OpenGL trouble drawing a 32x32 box in 2d

This topic is 4016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
You're going to want to set up an orthographic window using either gluOrtho2D(left, right, bottom, top) or glOrtho(left, right, bottom, top, zNear, zFar). Left, right, bottom, and top can be set to anything. For example, if you call gluOrtho2D(-10,10,-10,10) then your screen would be 20 units wide and 20 units high with (0,0) in the center of the screen. If you want to work in pixel coordinates you would call something like gluOrtho2D(0,1024,0,768), substituting your monitor's width and height for 1024 and 768, of course. Then a 32x32 box will be 32x32 pixels.

Share this post


Link to post
Share on other sites
I really cant get this right...
will gluOrtho2d go in my init function as such,

void InitializeOpenGL(int width, int height)
{
Log.Write("Initializing OpenGL...\n");
Log.Flush();
g_hDC = GetDC(g_hWnd);

if (!SetupPixelFormat(g_hDC))
PostQuitMessage (0);
//glEnable(GL_TEXTURE_2D);
g_hRC = wglCreateContext(g_hDC);
wglMakeCurrent(g_hDC, g_hRC);

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glDisable(GL_DEPTH_TEST);
gluOrtho2D(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);


SizeOpenGLScreen(width, height);
}



or in my rendering code

void RenderScene()
{
int i=0;
gluOrtho2D(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();

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();
//draw_tiles();

SwapBuffers(g_hDC);
}


either way still draws a large box which takes up a 1/4 of the screen.

Share this post


Link to post
Share on other sites
DO NOT use the glOrtho directly. use the code below instead.

// select the PROJECTION matrix
// if you don't call this, the OpenGL use the MODELVIEW matrix for default
glMatrixMode(GL_PROJECTION);
// load identity matrix to the PROJECTION matrix
glLoadIdentity();
// multiply the ortho matrix to the PROJECTION matrix
glOrtho( ... );
// select the MODELVIEW matrix, so you can normally use glTranslate, glRotate, etc.
glMatrixMode(GL_MODELVIEW);

Share this post


Link to post
Share on other sites
okay i fixed that up, but its still not drawing the square how i want it...

void InitializeOpenGL(int width, int height)
{
Log.Write("Initializing OpenGL...\n");
Log.Flush();
g_hDC = GetDC(g_hWnd);

if (!SetupPixelFormat(g_hDC))
PostQuitMessage (0);
//glEnable(GL_TEXTURE_2D);
g_hRC = wglCreateContext(g_hDC);
wglMakeCurrent(g_hDC, g_hRC);

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

SizeOpenGLScreen(width, height);
}


this just draws a rectangle at the top of the screen, taking up 1/2 now

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();

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

Share this post


Link to post
Share on other sites
Well, I think you'd better change the window size first, then set the projection matrix.

void InitializeOpenGL(int width, int height)
{
// ... your code to Create OpenGL Context and Make the Context Current

// change window size here
SizeOpenGLScreen(width, height);

// select the Projection matrix and use glOrtho
// use the width, height instead of SCREEN_WIDTH, SCREEN_HEIGHT
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, -1, 1);

// select the Model View matrix
glMatrixMode(GL_MODELVIEW);

// you might need to set the OpenGL Viewport
glViewport(0, 0, width, height);
}

void RenderGLScene()
{
// reset the Model View stack
glLoadIdentity();
// do your translation here
// if you don't need a translation, leave is empty

// draw the polygon
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(0, 32);
glVertex2i(32, 32);
glVertex2i(32, 0);
glEnd();
}

make sure you don't modify the Projection matrix or the Model View matrix in other place of your program.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!