我使用最新的 freeglut 成功编译了此代码,但我不断得到一个空白窗口,其中填充了glClearColor函数中指定的颜色。这个简单的程序来自我大学提供的交互式计算机图形学教科书。我一个字一个字地抄了下来,还是不行。它应该旋转 2 个正方形。
我在 Windows 7 机器上使用 gcc:
$ gcc -Iinlude -Llib -o sample.exe sample.cpp -lfreeglut -lopengl32
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
void rashape(int width, int height);
void display();
void renderScene();
void animate(int value);
void drawSquare();
int angle = 0;
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(600, 300);
glutInitWindowPosition(0, 0);
glutCreateWindow("Primjer animacije");
glutDisplayFunc(display);
glutTimerFunc(20, animate, 0);
glutMainLoop();
}
void animate(int value) {
angle++;
if(angle >= 360) kut = 0;
glutPostRedisplay();
glutTimerFunc(20, animate, 0);
}
void display() {
glClearColor(1.0f, 0.2f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
renderScene();
glutSwapBuffers();
}
void reshape(int width, int height) {
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width-1, 0, height-1, 0, 1);
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_MODELVIEW);
}
void drawSquare() {
glBegin(GL_QUADS);
glVertex2f( 0.0f, 0.0f);
glVertex2f(100.0f, 0.0f);
glVertex2f(100.0f, 100.0f);
glVertex2f( 0.0f, 100.0f);
glEnd();
}
void renderScene() {
glPointSize(6);
glColor3f(0.3f, 1.0f, 0.3f);
glPushMatrix();
glTranslatef( 150.0f, 160.0f, 0.0f);
glScalef(1.5f, 1.5f, 1.0f);
glRotatef((float)kut, 0.0f, 0.0f, 1.0f);
glTranslatef(-50.0f, -50.0f, 0.0f);
drawSquare();
glPopMatrix();
glPushMatrix();
glTranslatef( 400.0f, 160.0f, 0.0f);
glScalef(1.5f, 1.5f, 1.0f);
glRotatef(-(float)kut, 0.0f, 0.0f, 1.0f);
glTranslatef(-50.0f, -50.0f, 0.0f);
drawSquare();
glPopMatrix();
}