Problem with Reshape calback

Started by
0 comments, last by toogreat4u 15 years, 2 months ago
I am trying to have my reshape callback preserve all aspects when the screen is being adjusted and I am having troubles. When resized I get all sorts of views/clipping views that I do not intend to get. Any help would be great.

#include <gl/glut.h>
#include <cmath>

// this is the initial gluOrtho2D area
GLdouble left = 0.0;
GLdouble right = 700.0;
GLdouble bottom = 0.0;
GLdouble top = 700.0;

void scene()
{
        // all these functions work correctly
	grass_area();
	sky_area();
	mountains();
	houses();
	trees();
	fox_hole();
	ug_hideout();
	tent();
}

void display()
{
	/* clear window */
	glClear(GL_COLOR_BUFFER_BIT);

	/* draw scene */
	scene();

	/* for single buffer us glFlush
	 * technically can use for double as well
	 * just pointless
	 */
	glutSwapBuffers();
}

// problem area here
void myreshape(GLsizei w, GLsizei h)
{
	/* adjust clipping window */
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if(w <= h)
		gluOrtho2D(left,right, bottom * (GLfloat) h / (GLfloat) w, top * (GLfloat) h / (GLfloat) w);
	else
		gluOrtho2D(left * (GLfloat) w / (GLfloat) h, right * (GLfloat) w / (GLfloat) h, bottom, top);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	/* adjust viewport */
	glViewport(0,0,w,h);

	right = w;
	top = h;
}

void init()
{
	/* set clear color */
	glClearColor(0.0,0.0,0.0,1.0);
}

int main(int argc, char** argv)
{
	/* intialize mode and open a window in upper-left corner of
	 * screen
	 *
	 * window titles is name of program (arg[0])
	 */
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(700,700);
	glutInitWindowPosition(0,0);
	glutCreateWindow("Game Title");
	glutDisplayFunc(display);
	glutReshapeFunc(myreshape);
	init();
	glutMainLoop();
}

I am thinking I am missing something easy but I cant figure it out.
Advertisement
Ok I figured out what I was doing wrong even though its pretty much identical to the code above here is my solution:

void myreshape(GLsizei w, GLsizei h){  // adjust clipping window  glMatrixMode(GL_PROJECTION);  glLoadIdentity();  if(w <= h)    {      gluOrtho2D(left, right, bottom * (GLfloat) h / (GLfloat) w, top * (GLfloat) h / (GLfloat) w);    }  else    {      gluOrtho2D(left * (GLfloat) w / (GLfloat) h, right * (GLfloat) w / (GLfloat) h, bottom , top);    }  glMatrixMode(GL_MODELVIEW);    // adjust viewport  glViewport(0, 0, w, h);}

This topic is closed to new replies.

Advertisement