随笔-38  评论-23  文章-0  trackbacks-0

glut是opengl辅助库.

现通过它实现一个位于z=0平面的正方形 并绕z轴旋转.

/**  

   One workaround to this issue is requiring users to always link with
   the same CRT as GLUT is compiled with.  That requires users supply a
   non-standard option.  GLUT 3.7 has its own built-in workaround where
   the executable's "exit" function pointer is covertly passed to GLUT.
   GLUT then calls the executable's exit function pointer to ensure that
   any "atexit" calls registered by the application are called if GLUT
   needs to exit.

   Note that the __glut*WithExit routines should NEVER be called directly.
   To avoid the atexit workaround, #define GLUT_DISABLE_ATEXIT_HACK.

**
*/

/**
** That for why define below
*
*/

#define GLUT_DISABLE_ATEXIT_HACK 


#ifdef WIN32
  #include 
<windows.h>
#endif

#include
<gl/gl.h>
#include
<GL/glut.h>

GLfloat angle
=0.0;

/*
*显示回调函数
*/

void display()
{
    
//clear the color buffer 即每帧清屏
    glClear(GL_COLOR_BUFFER_BIT);

    
//设置模型视图矩阵
    glMatrixMode(GL_MODELVIEW);
    
//设置当前矩阵为单位矩阵
    glLoadIdentity();
    
//将正方形按z轴旋转 angle角度
    glRotatef(angle,0.0,0.0,1.0);

    
//画一个正方形
    glBegin(GL_QUADS);
        glVertex3f(
-0.5,-0.5,0.0);
        glVertex3f(
0.5,-0.5,0.0);
        glVertex3f(
0.5,0.5,0.0);
        glVertex3f(
-0.5,0.5,0.0);
    glEnd();
    
//glFlush();
    
//双缓存 显示一帧,下一帧已经在缓冲区画好,交换出来就好
    
//本身带有一个强制的glFlush();
    glutSwapBuffers();
}

/*
*空闲时间回调函数
*/

void idlefunc()
{
    
//角度自增 实现正方形的旋转
    angle+=1.0;
    
if(angle>=360.0f
        angle
=0.0;
    
//使得函数执行完成后,调用重绘函数
    glutPostRedisplay();
}

/*
*win32 main函数
*/

void main(int argc, char **argv)
{
    
//初始化 glut
    glutInit(&argc, argv);
    
//初始化显示模式为 RGB 和双缓冲
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    
//设置窗口左上角位置
    glutInitWindowPosition(100,100);
    
//设置窗口的初始化大小
    glutInitWindowSize(512,512);
    
//创建一个窗口标题为  "字符串"
    glutCreateWindow("3D Tech- GLUT Tutorial");
    
//注册显示回调函数
    glutDisplayFunc(display);
    
//注册空闲时间函数
    glutIdleFunc(idlefunc);
    
//设置多边形绘制模式为正面,线框式
    glPolygonMode(GL_FRONT,GL_LINE);
    
//进入仿真循环
    glutMainLoop();
}








posted on 2009-07-04 18:36 米游 阅读(343) 评论(0)  编辑 收藏 引用 所属分类: OpenGL/OSG

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理