abcdef
abcdef

Reputation: 246

OpenGL subwindows don't respond simultaneously

I have a problem, i'm using glut (the openGL utility toolkit). I'm making a subwindow in a mainwindow. The mainwindow displays a simple figure and the subwindow displays the figure from another view. The figure rotates so the mainwindow and the subwindow should always be redisplayed.

But only one of the two displays the rotating figure. So when I start the program the figure in the mainwindow rotates but in the subwindow it don't rotate, it just stands still.

When I move my mouse in the subwindow and press any key the roles changes so the figure rotates in the subwindow and stands still in the mainwindow.

How can I let them both display simultaneous. I followed the tutorial from Lighthouse, but it didn't give me an answer. Do I have to do something with my viewport?

 * GLUT Shapes Demo
 *
 * Written by Nigel Stewart November 2003
 *
 * This program is test harness for the sphere, cone
 * and torus shapes in GLUT.
 *
 * Spinning wireframe and smooth shaded shapes are
 * displayed until the ESC or q key is pressed.  The
 * number of geometry stacks and slices can be adjusted
 * using the + and - keys.
 */
#include <windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <math.h>
#include <stdlib.h>

static int slices = 16;
static int stacks = 16;
int m=0;


int mainWindow,SubWindow, SubWindow2;

/* GLUT callback Handlers */

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10,10,-10,10,-10,10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

static void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27 :
        case 'q':
            exit(0);
            break;

        case '+':
            slices++;
            stacks++;
            break;

        case '-':
            if (slices>3 && stacks>3)
            {
                slices--;
                stacks--;
            }
            break;
    }


    //glutPostRedisplay();
}
void keyp(int key, int xx, int yy) {


    glutSetWindow(mainWindow);
    glutPostRedisplay();

}

void displaysub()
{const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    const double a = t*90.0;

    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
     glClearColor(20,1,1,1);
     glLoadIdentity();
     glOrtho(-5,5,-5,5,-5,5);
    glColor3f(0,0,0);
glRotated(a,0,0,10);
    glPushMatrix();
        glTranslated(0,0,0);
        glutSolidSphere(2,10,10);
    glPopMatrix();

    glutSwapBuffers();
}

void display()
{const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    const double a = t*90.0;
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
     glClearColor(3,0,0,1);
glLoadIdentity();
     glOrtho(-10,10,-10,10,-10,10);
     glRotated(a,10,10,0);


displaysub();


}

static void idle(void)
{
    glutPostRedisplay();
}

/* Program entry point */
void init()
{


    glClearColor(3,0,0,1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    // register callbacks
    glutIgnoreKeyRepeat(1);
    glutKeyboardFunc(key);
    glutSpecialFunc(keyp);

}



int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);


    mainWindow = glutCreateWindow("GLUT Shapes");
        glutSetWindow(mainWindow);
        glClearColor(3,0,0,1);
        glutDisplayFunc(display);
        init();


    SubWindow = glutCreateSubWindow(mainWindow,0,0,50,50);
        glutSetWindow(SubWindow);
        glClearColor(3,0,0,1);
        glutDisplayFunc(displaysub);
        init();



  glutIdleFunc(idle);
    glutMainLoop();

    return 1;
}

Upvotes: 0

Views: 1050

Answers (1)

Harald Scheirich
Harald Scheirich

Reputation: 9764

The documentation on glutPostRedisplay specifies that only the display func of the current window will be called. In this case there are two windows. I am not an expert using glut but I would suggest two changes

Remove displaysub() from the display() function and rewrite idle()

static void idle()
{
  int currentWindow = glutGetWindow();
  glutSetWindw(mainWindow);
  glutPostRedisplay();
  glutSetWindw(subWindow);
  glutPostRedisplay();
  glutSetWindow(currentWindow);
}

glutPostRedisplay just marks the window for update in the main loop, which I guess is the one with mouse focus. By doing a post for each window independent of the current window all windows will receive their respective display calls

Upvotes: 6

Related Questions