glut是opengl輔助庫.
現(xiàn)通過它實現(xiàn)一個位于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;


/**//*
*顯示回調函數(shù)
*/
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();
//雙緩存 顯示一幀,下一幀已經在緩沖區(qū)畫好,交換出來就好
//本身帶有一個強制的glFlush();
glutSwapBuffers();
}

/**//*
*空閑時間回調函數(shù)
*/
void idlefunc()


{
//角度自增 實現(xiàn)正方形的旋轉
angle+=1.0;
if(angle>=360.0f)
angle=0.0;
//使得函數(shù)執(zhí)行完成后,調用重繪函數(shù)
glutPostRedisplay();
}

/**//*
*win32 main函數(shù)
*/
void main(int argc, char **argv)


{
//初始化 glut
glutInit(&argc, argv);
//初始化顯示模式為 RGB 和雙緩沖
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
//設置窗口左上角位置
glutInitWindowPosition(100,100);
//設置窗口的初始化大小
glutInitWindowSize(512,512);
//創(chuàng)建一個窗口標題為 "字符串"
glutCreateWindow("3D Tech- GLUT Tutorial");
//注冊顯示回調函數(shù)
glutDisplayFunc(display);
//注冊空閑時間函數(shù)
glutIdleFunc(idlefunc);
//設置多邊形繪制模式為正面,線框式
glPolygonMode(GL_FRONT,GL_LINE);
//進入仿真循環(huán)
glutMainLoop();
}





posted on 2009-07-04 18:36
米游 閱讀(400)
評論(0) 編輯 收藏 引用 所屬分類:
OpenGL/OSG