OpenGL显示窗口重定形函数

    我们在建立初始显示窗口后,需要在其后改变位置与形状。窗口尺寸的改变可能改变其纵横比并引起对象形状的改变。所以GLUT库提供glutReshapeFunc(winReshapeFcn)函数。和其他GLUT函数一起放在程序的主过程中,不过该函数是在窗口尺寸输入后激活。其参数是接受新窗口高度的过程名。还可以接着使用新尺寸去重新设置投影参数并完成任何其他操作,包括改变显示窗口颜色。另外,可以保存宽度和高度供其它子程序使用。

void glutReshapeFunc(void (*func)(int width, int height));

    官方文档在这里

 1 #include <GL/glut.h>
 2 #include <cmath>
 3 #include <cstdlib>
 4 
 5 const double TWO_PI = 6.2831853;
 6 
 7 GLsizei winWidth = 400, winHeight = 400;
 8 GLuint regHex;
 9 
10 class screenPt {
11 private:
12     GLint x, y;
13 
14 public:
15     screenPt() {
16         x = y = 0;
17     }
18     void setCoords(GLint xCoord, GLint yCoord) {
19         x = xCoord;
20         y = yCoord;
21     }
22     GLint getx() const {
23         return x;
24     }
25     GLint gety() const {
26         return y;
27     }
28 };
29 
30 static void init(void)
31 {
32     screenPt hexVertex, circCtr;
33     GLdouble theta;
34     GLint k;
35 
36     circCtr.setCoords(winWidth/2, winHeight/2);
37 
38     glClearColor(1.0, 1.0, 1.0, 0.0);
39 
40     regHex = glGenLists(1);
41     glNewList(regHex, GL_COMPILE);
42     glColor3f(1.0, 0.0, 0.0);
43     glBegin(GL_POLYGON);
44     for (k = 0; k < 6; ++k) {
45         theta = TWO_PI * k / 6.0;
46         hexVertex.setCoords(circCtr.getx() + 150 * cos(theta),
47             circCtr.gety() + 150 * sin(theta));
48         glVertex2i(hexVertex.getx(), hexVertex.gety());
49     }
50     glEnd();
51     glEndList();
52 }
53 
54 void regHexagon()
55 {
56     glClear(GL_COLOR_BUFFER_BIT);
57 
58     glCallList(regHex);
59 
60     glFlush();
61 }
62 
63 void winReshapeFcn(int newWidth, int newHeight)
64 {
65     glMatrixMode(GL_PROJECTION);
66     glLoadIdentity();
67     gluOrtho2D(0.0, (GLdouble)newWidth, 0.0, (GLdouble)newHeight);
68 
69     glClear(GL_COLOR_BUFFER_BIT);
70 }
71 
72 int main(int argc, char* argv[])
73 {
74     glutInit(&argc, argv);
75     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
76     glutInitWindowPosition(100, 100);
77 
78     glutInitWindowSize(winWidth, winHeight);
79     glutCreateWindow("Reshape-Function & Display-List Example");
80 
81     init();
82     glutDisplayFunc(regHexagon);
83     glutReshapeFunc(winReshapeFcn);
84 
85     glutMainLoop();
86     return 0;
87 }
原文地址:https://www.cnblogs.com/clairvoyant/p/5563590.html