0

I am not using glutMainLoop() in my program but I am using glutMainLoopEvent() in a simple while loop. This is fine but the reshape callback is never called. I register my reshape event in the main(). Here's my source code:

#include <iostream>
#include "GL/freeglut.h"


float angle = 0.0f;


void ResizeEvent(int w, int h)
{
    std::cout << "Resizing... << w << " " << h << std::Endl;

    if(h == 0)
        h = 1;

    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLdouble)w / h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void InitializeGL()
{
    glClearColor(0.0, 0.0, 0.0, 1.0f);
    glViewport(0, 0, 640, 480);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, 640.0 / 480.0, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Update()
{
    angle += 0.05f;
}

void Display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(0.0f, 0.0f, -3.0f);
    glRotatef(angle, 0.5f, 1.0f, 0.75f);
    glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);
            glVertex3f(-1.0f, -1.0f, 0.0f);
        glColor3f(0.0f, 1.0f, 0.0f);
            glVertex3f(1.0f, -1.0f, 0.0f);
        glColor3f(0.0f, 0.0f, 1.0f);
            glVertex3f(0.0f, 1.0f, 0.0f);
    glEnd();

    glutSwapBuffers();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Learning freeglut");

    InitializeGL();

    glutReshapeFunc(ResizeEvent);

    while(true)
    {
        Update();

        Display();

        glutMainLoopEvent();
    }

    return 0;
}

Thanks in advance!

Edit: Anyone?

1 Answers1

0

glutReshapeFunc requires glutDisplayFunc to be set to something. This can even be an empty function as long as you don't need the window to update while resizing.

Possible solution

    ...

    glutReshapeFunc(ResizeEvent);

    glutDisplayFunc(Display);

    while(true)
    {
        Update();

        Display();

        glutMainLoopEvent();
    }

    ...
Zurg
  • 1