之前玩OpenGL只會使用一個視窗,然而有時候想做子母畫面,那要怎麼寫呢?其實十分簡單,一旦把子視窗創造於母視窗之上後,就可以像單一視窗那樣操作!
我在母視窗上建立四個子視窗,母視窗黑、子視窗1紅、子視窗2綠、子視窗3藍、子視窗4橘,並且在母視窗上畫天藍色的Teapot,每個子視窗上畫灰色的Cube。
子視窗可以像單一視窗那樣操作,想要位移、縮放、旋轉都沒問題。應用在遊戲上一定很有趣!
/**
Theme: Subwindow
Compiler: Dev C++ 4.9.9.2
Date: 100/06/27
Author: ShengWen
Blog: https://cg2010studio.wordpress.com/
*/
#include <GL/glut.h>
#include <iostream>
using namespace std;
#define GAP 30
int main_w, w1, w2, w3, w4;
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.5, 0.5);
glutSolidCube(1);
glutSwapBuffers();
}
void displayx(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.7, 0.7);
glutSolidTeapot(1);
glutSwapBuffers();
}
void visibility(int visState){
cout<<"win: "<<glutGetWindow()<<' '<<"visible: "<<visState<<endl;
}
void reshape(int w, int h){
int width = 50;
int height = 50;
//依目前視窗大小調整子視窗的寬與高
glViewport(0, 0, w, h);
if (w > 50) {
width = (w - 3 * GAP) / 2;
} else {
width = 10;
}
if (h > 50) {
height = (h - 3 * GAP) / 2;
} else {
height = 10;
}
//重繪各子視窗於新的位置
glutSetWindow(w1);
glutPositionWindow(GAP, GAP);
glutReshapeWindow(width, height);
glutSetWindow(w2);
glutPositionWindow(GAP + width + GAP, GAP);
glutReshapeWindow(width, height);
glutSetWindow(w3);
glutPositionWindow(GAP, GAP + height + GAP);
glutReshapeWindow(width, height);
glutSetWindow(w4);
glutPositionWindow(GAP + width + GAP, GAP + height + GAP);
glutReshapeWindow(width, height);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowSize(300, 300);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
main_w = glutCreateWindow("4 subwindows"); //主視窗
glutDisplayFunc(displayx);
glutVisibilityFunc(visibility);
glutReshapeFunc(reshape);
glClearColor(0.0, 0.0, 0.0, 0.0);
w1 = glutCreateSubWindow(main_w, 10, 10, 110, 110); //子視窗 1
glutDisplayFunc(display);
glutVisibilityFunc(visibility);
glClearColor(1.0, 0.0, 0.0, 1.0);
w2 = glutCreateSubWindow(main_w, 110, 10, 110, 110); //子視窗 2
glutDisplayFunc(display);
glutVisibilityFunc(visibility);
glClearColor(0.0, 1.0, 0.0, 1.0);
w3 = glutCreateSubWindow(main_w, 10, 110, 110, 110); //子視窗 3
glutDisplayFunc(display);
glutVisibilityFunc(visibility);
glClearColor(0.0, 0.0, 1.0, 1.0);
w4 = glutCreateSubWindow(main_w, 110, 110, 110, 110); //子視窗 4
glutDisplayFunc(display);
glutVisibilityFunc(visibility);
glClearColor(1.0, 0.5, 0.0, 1.0);
cout<<"main_w: "<<main_w<<endl;
cout<<"w1: "<<w1<<endl;
cout<<"w2: "<<w2<<endl;
cout<<"w3: "<<w3<<endl;
cout<<"w4: "<<w4<<endl;
glutMainLoop();
return EXIT_SUCCESS;
}

隨意留個言吧:)~