Results 1 to 10 of 10

Thread: Resizing windows

  1. #1
    Guest

    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.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Nov 2002
    Location
    USA
    Posts
    254

    Re: Resizing windows

    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.

  3. #3
    Guest

    Re: Resizing windows

    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.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Nov 2002
    Location
    USA
    Posts
    254

    Re: Resizing windows

    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

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: Resizing windows

    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.

  6. #6
    Guest

    Re: Resizing windows

    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.

  7. #7
    Senior Member Regular Contributor
    Join Date
    Nov 2002
    Location
    USA
    Posts
    254

    Re: Resizing windows

    Make sure you are using double buffering.

    Ex: glutInitDisplayMode( GLUT_DOUBLE );

    - VC6-OGL

  8. #8
    Guest

    Re: Resizing windows

    Yep, I'm double buffered.

  9. #9
    Guest

    Re: Resizing windows

    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);
    }

  10. #10
    Senior Member Regular Contributor
    Join Date
    Nov 2002
    Location
    USA
    Posts
    254

    Re: Resizing windows

    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

Similar Threads

  1. help: problems resizing GLX windows
    By bootstrap in forum OpenGL: Linux
    Replies: 1
    Last Post: 03-23-2008, 09:32 AM
  2. resizing the windows
    By prashantgp in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 02-25-2003, 01:16 AM
  3. Odd behavior when resizing windows
    By jpummill in forum OpenGL: General
    Replies: 2
    Last Post: 02-21-2002, 11:29 AM
  4. Resizing Problems in Splitter Windows (MFC)
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-28-2002, 02:18 AM
  5. windows resizing problem
    By Jeeeez in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 05-17-2001, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean