glut程序
8.多窗口且子窗口能够跟随reshape的变化而变化
///
#include <GL/glut.h>
#include <iostream>///
int winWidth, winHeight; // <<=======NEW!!!
int mainWinID; // <<=======NEW!!!
int subWinID0(0), subWinID1(0), subWinID2(0), subWinID3(0); // <<=======NEW!!!///
void myMainWinDraw(void);
void myMainWinReshape(int _width, int _height);void mySubWinDrawSphere(void);
void mySubWinDrawTeapot(void);
void mySubWinDrawTorus(void);
void mySubWinDrawIcos(void);///
int main(int argc,char** argv) {glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(400, 400);glutInitWindowPosition(0, 0);mainWinID = glutCreateWindow("Hello");glutDisplayFunc(myMainWinDraw);glutReshapeFunc(myMainWinReshape);glutMainLoop();return 0;
}///
void myMainWinReshape(int _width, int _height) {winWidth = _width; // <<==============NEW!!!winHeight = _height; // <<==============NEW!!!if (subWinID0) // <<==============NEW!!!glutDestroyWindow(subWinID0); // <<==============NEW!!!subWinID0 = glutCreateSubWindow(mainWinID, 0, 0, // <<=======NEW!!!winWidth>>1, winHeight>>1);glutDisplayFunc(mySubWinDrawSphere); // <<=======NEW!!!if (subWinID1) {glutDestroyWindow(subWinID1);}subWinID1 = glutCreateSubWindow(mainWinID, winWidth>>1, 0,winWidth>>1, winHeight>>1);glutDisplayFunc(mySubWinDrawTeapot);if (subWinID2)glutDestroyWindow(subWinID2);subWinID2 = glutCreateSubWindow(mainWinID, 0, winHeight>>1,winWidth>>1, winHeight>>1);glutDisplayFunc(mySubWinDrawTorus);if (subWinID3)glutDestroyWindow(subWinID3);subWinID3 = glutCreateSubWindow(mainWinID, winWidth>>1, winHeight>>1,winWidth>>1, winHeight>>1);glutDisplayFunc(mySubWinDrawIcos);
}///
void myMainWinDraw() {glClearColor(0.0, 0.0, 0.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glFlush();
}///
void mySubWinDrawSphere(void) {glClearColor(0.0, 0.0, 1.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glRotatef(30, 1.0f, 1.0f, 1.0f);glutWireSphere(0.8f, 20, 20);glFlush();
}///
void mySubWinDrawTeapot(void) {glClearColor(1.0, 1.0, 0.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glutWireTeapot(0.5f);glFlush();
}///
void mySubWinDrawTorus(void) {glClearColor(0.0, 1.0, 1.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glutWireTorus(0.3f, 0.6f, 20, 30);glFlush();
}///
void mySubWinDrawIcos(void) {glClearColor(1.0, 0.0, 1.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glRotatef(30, 1.0f, 1.0f, 1.0f);glutWireIcosahedron();glFlush();
}///
新增的知识点:
1.回调函数myMainWinReshape中:改变窗口大小时,先将原来子窗口销毁(如果存在的话),再重新产生新的子窗口。
2.glutDestroyWindow:销毁窗口
道理上是这个样子,但是我在实际运行过程中,拖动鼠标改变窗口大小时,出现了问题。
差不多是说,没有注册给窗口6的回调函数。可能和我这个freeglut有关系的。暂时没有解决。
9.可控制的子窗口显示
///
#include <GL/ogl.h>
#include <iostream>///
void myMainWinDraw(void);
void myMainWinReshape(int _width, int _height);
void myMainWinKeyboard(unsigned char _key, int _x, int _y);void mySubWinDrawSphere(void);
void mySubWinDrawTeapot(void);
void mySubWinDrawTorus(void);
void mySubWinDrawIcos(void);///
int winWidth, winHeight;
int mainWinID;
int subWinID0(0), subWinID1(0), subWinID2(0), subWinID3(0);// <<================= NEW !!!!!!
bool isShowSubWin0(1), isShowSubWin1(1), isShowSubWin2(1), isShowSubWin3(1);///
int main(int argc,char** argv) {glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(400, 400);glutInitWindowPosition(0, 0);mainWinID = glutCreateWindow("Hello");glutDisplayFunc(myMainWinDraw);glutReshapeFunc(myMainWinReshape);glutKeyboardFunc(myMainWinKeyboard);glutMainLoop();return 0;
}///
void myMainWinReshape(int _width, int _height) {winWidth = _width;winHeight = _height;if (subWinID0) {glutDestroyWindow(subWinID0);subWinID0 = 0;}if (isShowSubWin0) {subWinID0 = glutCreateSubWindow(mainWinID, 0, 0, winWidth>>1, winHeight>>1);glutDisplayFunc(mySubWinDrawSphere);}if (subWinID1) {glutDestroyWindow(subWinID1);subWinID1 = 0;}if (isShowSubWin1) {subWinID1 = glutCreateSubWindow(mainWinID, winWidth>>1, 0,winWidth>>1, winHeight>>1);glutDisplayFunc(mySubWinDrawTeapot);}if (subWinID2) {glutDestroyWindow(subWinID2);subWinID2 = 0;}if (isShowSubWin2) {subWinID2 = glutCreateSubWindow(mainWinID, 0, winHeight>>1,winWidth>>1, winHeight>>1);glutDisplayFunc(mySubWinDrawTorus);}if (subWinID3) {glutDestroyWindow(subWinID3);subWinID3 = 0;}if (isShowSubWin3) {subWinID3 = glutCreateSubWindow(mainWinID, winWidth>>1, winHeight>>1,winWidth>>1, winHeight>>1);glutDisplayFunc(mySubWinDrawIcos);}
}///
void myMainWinDraw() {glClearColor(0.0, 0.0, 0.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glFlush();
}// <<<<<===================== NEW !!!!!
#define RENEW myMainWinReshape(winWidth, winHeight);\glutPostRedisplay();\//myMainWinDraw();///
void myMainWinKeyboard(unsigned char _key, int _x, int _y) {switch (_key) {case 'a':isShowSubWin0 = !isShowSubWin0;RENEW;break;case 's':isShowSubWin1 = !isShowSubWin1;RENEW;break;case 'z':isShowSubWin2 = !isShowSubWin2;RENEW;break;case 'x':isShowSubWin3 = !isShowSubWin3;RENEW;break;case 'f':// <<================== NEW !!!glutFullScreen();break;case 'v':// <<================== NEW !!!glutSetWindow(mainWinID);glutReshapeWindow(300, 300);break;case 'w':// <<================== NEW !!!glutSetWindow(subWinID0);glutReshapeWindow(100, 100);}
}#undef RENEW // <<================== NEW !!!///
void mySubWinDrawSphere(void) {glClearColor(0.0, 0.0, 1.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glRotatef(30, 1.0f, 1.0f, 1.0f);glutWireSphere(0.8f, 20, 20);glFlush();
}///
void mySubWinDrawTeapot(void) {glClearColor(1.0, 1.0, 0.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glutWireTeapot(0.5f);glFlush();
}///
void mySubWinDrawTorus(void) {glClearColor(0.0, 1.0, 1.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glutWireTorus(0.3f, 0.6f, 20, 30);glFlush();
}///
void mySubWinDrawIcos(void) {glClearColor(1.0, 0.0, 1.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glRotatef(30, 1.0f, 1.0f, 1.0f);glutWireIcosahedron();glFlush();
}///
引入bool量isShowSubWin0, isShowSubWin1, isShowSubWin2, isShowSubWin3来判断是否应该显示该子窗口。