Resizing windows

Sorry to ask stoopid questions but could someone help me here? I have an assignment for Thursday and I’ve just noticed that when I resize my window the objects seems to move further away or something and since the lecturer gave me the code I really don’t have a clue what to do with it. I’d ask him but he has gone away until the assignment is due in so I had to come here:

void Resize(int width, int height)
{
Width = width;
Height = height;

// Reset the viewport
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(22.5, (float)width / (float)height, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
}

I’m assuming it’s something to do with gluPerspective but that’s it. Any help would be muchly appreciated.

This should work:

void reshape( int w, int h ) {
if( h == 0 )
h = 1;

float ratio = 1.0 * w / h;

glViewport( 0, 0, w, h );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 25, ratio, 1, 1000 );
glMatrixMode( GL_MODELVIEW );
}

Hope this helps.

Thanks for the quick response but it still screws up, just in a different way, maybe it’s somewhere else in my code. It just seemed to rotate out of the viewpoint.

Here’s a sample program:

#include <gl/glut.h>

void init( void ) {
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_SMOOTH );
glEnable( GL_DEPTH_TEST );
}

void reshape( int w, int h ) {
if( h == 0 )
h = 1;

float ratio = 1.0 * w / h;

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glViewport( 0, 0, w, h );

gluPerspective( 25, ratio, 1, 1000 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, -1.0, 0.0f, 1.0f, 0.0f );
}

void display( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glPushMatrix();

glBegin( GL_POLYGON );
glVertex3f( -1.0, 1.0, -1.0 );
glVertex3f( 1.0, 1.0, -1.0 );
glVertex3f( 1.0, -1.0, -1.0 );
glVertex3f( -1.0, -1.0, -1.0 );
glEnd();

glPopMatrix();

glutSwapBuffers();
}

void keyboard( unsigned char key, int x, int y ) {
switch ( key ) {
case 27:
exit( 0 );
break;
}
}

void main( int argc, char **argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );
glutEnterGameMode();
init();
glutDisplayFunc( display );
glutIdleFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );

glutMainLoop();
}

Maybe this can help solve your problem.

  • VC6-OGL

Post the code, not enough info to say.

Sounds like you are not resetting the matrix each time you draw.

void display()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Originally posted by Hulley:
Thanks for the quick response but it still screws up, just in a different way, maybe it’s somewhere else in my code. It just seemed to rotate out of the viewpoint.

Cheers dudes, it’s mostly working now, you were right about that matrix business. But I’m still having this problem where it’s not refreshing the contents of the window when I’m resizing, I am probably expected to do it since I had it doing it in Uni.

Make sure you are using double buffering.

Ex: glutInitDisplayMode( GLUT_DOUBLE );

  • VC6-OGL

Yep, I’m double buffered.

Sorry to kinda bump, dunno if it’s allowed, but I have two days to do this, but I decided to post all the code except stupid irrelevant functions:

//globals
int Width; //width of window
int Height; //height of window

//functions
void Redraw(void);
void Resize(int width, int height);
void DrawTree1();
void DrawGravestone();
void DrawCross();
void DrawScenery();

//redraws frame
void Redraw(void)
{

static GLfloat sun_light[4]  = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat sun_pos[4]    = { 0.0, 100.0, 50.0, 0.0 };
//static GLfloat fogcolor[4]   = { 0.0, 0.0, 0.0, 0.0 };

/* Clear the window to black */
glClearColor(0.2, 0.7, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/*fog
glFogfv(GL_FOG_COLOR, fogcolor);
glFogi(GL_FOG_MODE, GL_LINEAR);



glFogf(GL_FOG_START, 10.0);
glFogf(GL_FOG_END, 19.0);*/

//enable drawing features
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
//glEnable(GL_FOG);

glShadeModel(GL_SMOOTH);

//lights
glLightfv(GL_LIGHT0, GL_DIFFUSE, sun_light);
glLightfv(GL_LIGHT0, GL_POSITION, sun_pos);

glEnable(GL_COLOR_MATERIAL);
DrawScenery();

glEnd();

glutSwapBuffers();

}

/*

  • ‘Resize()’ - Resize the window…
    */

void Resize(int width, int height)
{
Width = width;
Height = height;

/* Reset the viewport... */
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(22.5, (float)width / (float)height, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(792, 573);
glutCreateWindow(“Charlie Chaplin”);
glutReshapeFunc(Resize);
glutDisplayFunc(Redraw);
glutMainLoop();
return (0);
}

Try this code for your resize function:

void Resize(int width, int height)
{
Width = width;
Height = height;

if(height == 0)
height = 1;

float ratio = 1.0 * width / height;

/* Reset the viewport… */
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 25, ratio, 1, 1000 );
glMatrixMode(GL_MODELVIEW);
}

  • VC6-OGL