计算机图形学第四版4.4节 opengl代码错误(至少在windows在运行错误)
glPointSize()方法不能用于glbegin和glend之间
正确代码如下:
#include<GL/freeglut.h>
#include<GL/glut.h>
GLsizei winWidth = 400, winHeight = 300;
void init(void) {
glClearColor(1.0, 1.0,10.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void DrawPoint() {
glColor3f(1.0, 0.0, 0.0);
glPointSize(10.0);
glBegin(GL_POINTS);
glVertex2i(50, 100);
glEnd();
glPointSize(20.0);
glBegin(GL_POINTS);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(100, 200);
glEnd();
glPointSize(30.0);
glBegin(GL_POINTS);
glColor3f(0.0, 0.0, 1.0);
glVertex2i(250, 150);
glEnd();
glFlush();
}
void winReshapeFcn(GLint newWidth, GLint newHeight) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT);
winWidth = newHeight;
winHeight = newHeight;
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(winWidth, winHeight);
glutCreateWindow("Point");
init();
glutDisplayFunc(DrawPoint); //绘制窗口
glutReshapeFunc(winReshapeFcn); //
glutMainLoop();
}
运行结果