我有一个问题,我正在使用过剩(openGL实用工具包)。我正在主窗口制作一个子窗口。主窗口显示一个简单的图形,子窗口显示另一个视图中的图形。该图旋转,因此应始终重新显示主窗口和子窗口。
但两个中只有一个显示旋转的数字。因此,当我启动程序时,主窗口中的图形会旋转,但在子窗口中它不会旋转,它只是静止不动。
当我在子窗口中移动鼠标并按任意键时,角色会发生变化,因此图形会在子窗口中旋转并在主窗口中静止不动。
如何让它们同时显示。我按照灯塔的教程,但它没有给我一个答案。 我是否必须使用我的视口执行某些操作?
* 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;
}
答案 0 :(得分:6)
glutPostRedisplay
上的文档指定仅调用当前窗口的显示功能。在这种情况下,有两个窗口。我不是使用过剩的专家,但我会建议两个改变
从displaysub()
功能中删除display()
并重写idle()
static void idle()
{
int currentWindow = glutGetWindow();
glutSetWindw(mainWindow);
glutPostRedisplay();
glutSetWindw(subWindow);
glutPostRedisplay();
glutSetWindow(currentWindow);
}
glutPostRedisplay
只标记主循环中的更新窗口,我猜是鼠标焦点的窗口。通过为每个窗口做一个独立于当前窗口的帖子,所有窗口都将接收各自的显示调用