我在调整 GLUT 窗口大小时遇到问题。我想打印一个矩形,但是当我使用调整大小功能时,我似乎无法在窗口中绘制和塑造。我有以下代码,我想在调整窗口大小时改变正方形的宽度,但如果调整高度,我不希望它的高度改变。
#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
/* globals */
GLsizei wh = 500, ww = 500;
/* initial window size */
GLfloat size = 3.0;
/* half side length of square */
void myinit(void)
{
glClearColor(0.0,0.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,1,0,1.0,-1.0,1.0);
glViewport(0,0,ww,wh);
}
/* display callback -- required by GLUT 3.0 */
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,.5,.7);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();
}
void mouse(int button, int state, int x, int y) {
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,500,0.0,500,0.0,1.0);
}
}
/*app quits if q is pressed*/
void keyboard(unsigned char key, int x, int y) {
if(key == 'Q' | key == 'q')
exit(0);
}
/* The main program -- note that there is no "flow of control" -- the program merely calls functions to handle interactions */
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(ww,wh);
glutInitWindowPosition(100,100);
glutCreateWindow("Square");
myinit();
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutDisplayFunc(renderScene);
//glutReshapeFunc(changeSize);
glutMainLoop();
return 0;
}
我做了一个测试 changeSize 函数,当我使用它时,它只会显示一个空的蓝色窗口,其中没有矩形