資源簡介
OpenGL編程示例,制作雪花飄落效果,有鍵盤和鼠標控制。
代碼片段和文件信息
//////////////////////////////////////////////
//?Double?buffered?resizeable?2D?GL?windowing?environment?using?GLUT
//////////////////////////////////////////////
#include?
#include?
#include?
#include?
//
//globale?variables
//
const?int?milliseconds_per_frame?=?500;
float?posX?posY;
float?posXA[100];
float?posYA[100];
int?sizeA[100];
float?angleA[100];
int?cnt=0;
int?size=1;
int?angle=0;
int?filled?=?1;
int?animation=1;
int?windowWidth=700;
int?windowHeight=600;
//
//?Forward?declarations
//
void?DrawScene();
void?DrawSnow();
void?DrawEdge();
void?FillEdge();
//
//?Initialisation?functions
//
static?void?DrawInit()
{
????//?OpenGL?one-time?initialisation
????glClearColor(0.65F?0.65F0.9F?1.0F);
????cnt=0;
}
//
//?Callbacks
//
void?Timer(int?id)
{
for(int?i=0;?i{
????angleA[i]+=(0.1+5/sizeA[i]);
????posXA[i]+=0.25;
????posYA[i]-=0.5;
????if(posXA[i]>(windowWidth+50))
????{
?????posXA[i]-=windowWidth;
????}
????if(posYA[i]<-50)
????{
??????posYA[i]+=windowHeight;
????}
}
glutPostRedisplay();
}
void?DisplayFunc()
{
????glShadeModel(GL_SMOOTH);
????glClearColor(0.65F?0.65F0.9F?1.0F);
????glClear(GL_COLOR_BUFFER_BIT);
????if(animation)
??????glutTimerFunc(milliseconds_per_frameTimer1);
????glMatrixMode(GL_MODELVIEW);
????glLoadIdentity();
????DrawScene();
????glutSwapBuffers();
}
void?PainSnow(int?btn?int?state?int?x?int?y)
{
if(btn==GLUT_LEFT_BUTTON?&&?state==GLUT_DOWN)
{
????size?=?rand()%5+1;
????angle?=?rand()%30;
????posX=x;
????posY?=?windowHeight-y;
????posXA[cnt]=posX;
????posYA[cnt]=posY;
????sizeA[cnt]=size;
????angleA[cnt]=angle;
????cnt++;
}
}
void?KeyboardFunc(unsigned?char?key?int?x?int?y)
{
????switch(key)
????{
????????case?‘a‘?:
????????case?‘A‘?:
????????{
??????????if(animation==0)
??????????{
????????????animation=1;
??????????}else{
????????????animation=0;
??????????}
????????}
????????case?‘x‘?:
????????case?(‘X‘):
????????{
??????????cnt=0;
??????????DrawInit();
??????????break;
????????}
????????case?‘f‘:
????????case?‘F‘:
????????{
??????????if(filled==1)
??????????{
????????????filled=0;
??????????}
???????????else
??????????{
???????????filled=1;
??????????}
??????????break;
????????}
????????case?27:?//?escape?key?pressed
????????{
????????????exit(0);
????????????break;
????????}
????}
}
void?ReshapeFunc(?int?x?int?y?)
{
????//?Orthographic?view?fills?the?window
????glMatrixMode(GL_PROJECTION);
????glLoadIdentity();
????gluOrtho2D(?0.0?x?0.0?y?);
????glViewport(?0?0?x?y?);
????windowWidth=x;
????windowHeight=y;
}
void?IdleFunc()
{
????glutPostRedisplay();
}
void?FillEdge()
{
??????glColor3f(0.70f?0.70f?1.0f);
??????glBegin(GL_POLYGON);
?????????glVertex2f(0.0f?30.0f);
????????????glVertex2f(0.0f?00.0f);
?????????glVertex2f(260.0f?00.0f);
?????????glVertex2f(250.0f?30.0f);
- 上一篇:基于模糊控制的速度跟蹤控制問題
- 下一篇:c++分屏顯示
評論
共有 條評論