glut是opengl輔助庫.
現(xiàn)通過它實(shí)現(xiàn)一個(gè)位于z=0平面的正方形 并繞z軸旋轉(zhuǎn).

/**//**

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;


/**//*
*顯示回調(diào)函數(shù)
*/
void display()


{
//clear the color buffer 即每幀清屏
glClear(GL_COLOR_BUFFER_BIT);

//設(shè)置模型視圖矩陣
glMatrixMode(GL_MODELVIEW);
//設(shè)置當(dāng)前矩陣為單位矩陣
glLoadIdentity();
//將正方形按z軸旋轉(zhuǎn) angle角度
glRotatef(angle,0.0,0.0,1.0);

//畫一個(gè)正方形
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();
//雙緩存 顯示一幀,下一幀已經(jīng)在緩沖區(qū)畫好,交換出來就好
//本身帶有一個(gè)強(qiáng)制的glFlush();
glutSwapBuffers();
}

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


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

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


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





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