//下面是控制台程序
//创建一个空的控制台程序,在菜单的项目\属性\链接\输入\附加依赖项,添加glut32.lib
//在解决方案源文件中新建main.cpp,将下面的代码复制粘贴到main.cpp中
#include <iostream>
#include <cmath>
#include <GL/glut.h>
using namespace std;
static void
key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 : //按ESC
case 'q': //按q键程序都会退出
exit(0);
break;
}
glutPostRedisplay(); //产生重绘回调
}
static void display(void)
{
static long counter = 0;
//动画实现是用双缓存,现在用背景色擦除后缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cout<<counter<<": clear color buffer"<<endl;
//设置MODELVIEW矩阵,先设为单位阵,再乘上观察矩阵,即从z轴正向100处向 (0,0,0)
//看,上方向(0,1,0)
glLoadIdentity();
gluLookAt(0,0,100,0,0,0,0,1,0);
//在后缓存绘制图形,就一个点
glPointSize(2.0f);//缺省是1
glBegin(GL_POINTS);
glColor3f(1,1,1);
glVertex3f(0,0,0);
glEnd();
cout<<counter<<": draw point"<<endl;
counter++;
//然后前后缓存交换
glutSwapBuffers();
//可以在控制台窗口看到dispaly被连续的调用,一直在不断的在后缓存擦了画,然后交换前后缓存
//由于每次点的位置大小和颜色没变,看起来点是静止的
}
static void idle(void)
{
glutPostRedisplay(); //空闲时强制重绘,这样不断的调用display
}
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double ratio = 0.1;
//glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glOrtho(-width*0.5*ratio, width*0.5*ratio, -height*0.5*ratio, height*0.5*ratio, 2.0, 100.0); //这里使用正视投影
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize (800, 600);
glutInitWindowPosition (60, 60);
glutCreateWindow ("draw a point");
glClearColor(0.2,0.2,0.4,1); //openscenegraph背景的蓝色
glShadeModel (GL_FLAT);
glutKeyboardFunc(key); //键盘按下去时
glutIdleFunc(idle); //空闲时
glutReshapeFunc(resize); //改变窗口大小时
glutDisplayFunc(display); //绘制窗口显示时
glutMainLoop();
return 0;
}