参考 opengl编程指南 P17
直接上代码:
<span style="font-size:18px;">// doubleBuffer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <gl/glut.h>
static GLint spin = 0;
void init(){
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);//使用单一着色
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPushMatrix();
glRotated(spin,1.0,0.0,1.0);
//glRectf(25.0,25.0,100.0,100.0);
glRectf(0,0,1,1);
//glRectf(-0.25,-0.25,0.25,0.25);
glPopMatrix();
glutSwapBuffers();
}
void spinDisplay(){
spin += 2;
spin = spin % 360;
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y){
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
glutIdleFunc(spinDisplay);
}
break;
case GLUT_RIGHT_BUTTON:
if(state == GLUT_DOWN){
glutIdleFunc(NULL);
}
default:
break;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
glutInit(&argc,(char**)argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("双缓冲");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
</span>1.glShadeModel(GL_FLAT); 还有 一个 参数 是 GL_SMOOTH(默认值)。他们的区别是 当你 给 一个多边形 的 顶点 指定 不同 颜色时,矩阵中 的颜色 是 单一(Gl_Flat),还是
平滑的 过度(Gl_SMOOTH).
2. glRotated 函数, 旋转 函数 ,对 当前 矩阵 进行 操作
3. glPushMatrix();
glRotated(spin,1.0,0.0,1.0);
glPopMatrix()
glPushMatrix 函数 将 栈顶 矩阵 拷贝一份,然后 将 拷贝的 栈顶 矩阵 入栈。
glPopMatrix 函数 将 栈顶 矩阵 出栈。
将 旋转 函数 放入 这两个 函数 之间 ,是 为了 在旋转后,恢复到旋转之前的 栈顶 矩阵状态。
看不懂? 这里 有一个 解释的 还算 可以的 :http://blog.sina.com.cn/s/blog_70c3d9ed010122bp.html
4.glRectf 函数,传 两组 坐标进去,一个 在 左下角,一个 在 右上角,画一个矩阵出来。
关于 矩阵的 参数 ,书上 给的 glrectf(-25.0,-25.0,25.0,25.0) 我 认为 是 错误的 。
glrectf 参数 应该 是 在 -1 ~ 1 之间的 。是 opengl 世界 坐标系坐标,原点 在 屏幕中心 。 不懂 可以 百度。
5.glutIdleFunc设置全局的回调函数,当没有窗口事件到达时,GLUT程序功能可以执行后台处理任务或连续动画。如果启用,这个idle function会被不断调用,直到有窗口事件发生。回调函数没有参数。当前的窗口和菜单在执行idle func之前不会改变。当程序依赖多窗口或菜单时最好不要依赖于当前设定。
6.
if(state == GLUT_DOWN) //callback was due to a press (of a mouse button)
//相当于“如果某个鼠标键被按下”
if(state == GLUT_UP) //callback was due to a release (of a mouse button)
//相当于“如果某个鼠标键被放开(弹起)”
if (button== GLUT_left_BUTTON) //callback was due to a release/press of the left button
//相当于“如果鼠标左键被按下或者被放开”
if (button== GLUT_right_BUTTON) //callback was due to a release/press of the right button
//相当于“如果鼠标右键被按下或者被放开”
if (button== GLUT_MIDDLE_BUTTON) //callback was due to a release/press of the MIDDLE
//button
//相当于“如果鼠标中键被按下或者被放开”
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/fuming0210sc/article/details/46845439