資源簡介
openGL繪制的一個移動的2D小車,C++編寫,長方形加上兩個車輪,實現移動
代碼片段和文件信息
#include??
#include???
#include
using?namespace?std;
const?GLint?n?=?100;//點數
const?GLfloat?R?=?7;//半徑
const?GLfloat?PI?=?3.1415926f;
static?GLfloat?SPIN_CAR?=?0.0;????????????????//旋轉量??
static?GLfloat?MOVE_CAR?=?0.0;????????????????//平移量??
static?GLfloat?SIZE_CAR?=?1.5;????????????????//縮放量??
static?GLfloat?MOVE_WHEEL?=?2.0;
struct?Point?{
int?x?y;
Point()?{
x?=?0;
y?=?0;
}
Point(int?xi?int?yi)?{
x?=?xi;
y?=?yi;
}
};
struct?CarRectangle
{
Point?lbP?rtP;
int?xmin?ymin?xmax?ymax;
CarRectangle(Point?lbPi?Point?rtPi)?{
lbP?=?lbPi;
rtP?=?lbPi;
xmin?=?lbPi.x;
ymin?=?lbPi.y;
xmax?=?rtPi.x;
ymax?=?rtPi.y;
}
};
void?init(void)
{
glClearColor(1.0?1.0?1.0?0.0);??????//指定清除顏色(黑色),即背景顏色??
glShadeModel(GL_FLAT);
}
void?drawCar(CarRectangle?carRec)?{
glClear(GL_COLOR_BUFFER_BIT);???????//清除所有的像素??
glPushMatrix();
glTranslatef(MOVE_CAR?0?0);?????????????//移動,參數含義(x軸位移y軸位移z軸位移)??
glRotatef(SPIN_CAR?0?0?1);??????????????//旋轉,參數含義(旋轉量x軸y軸z軸)??
glScalef(SIZE_CAR?1?1);??????????????//縮放,參數含義(x軸倍數y軸倍數z軸倍數)??
glColor3f(1.0?1.0?0.0);?????????????//繪制顏色RGB??
glRectf(carRec.xmincarRec.ymincarRec.xmax?carRec.ymax);?????//繪制矩形??
glPopMatrix();
glutSwapBuffers();
}
void?drawCar()?{
glPushMatrix();
glTranslatef(MOVE_CAR?0?0);?????????????//移動,參數含義(x軸位移y軸位移z軸位移)??
glRotatef(SPIN_CAR?0?0?1);??????????????//旋轉,參數含義(旋轉量x軸y軸z軸)??
glScalef(SIZE_CAR?1?1);??????????????//縮放,參數含義(x軸倍數y軸倍數z軸倍數)??
glColor3f(1.0?1.0?0.0);?????????????//繪制顏色RGB??
glRectf(-25-25?2525);?????//繪制矩形??
glPopMatrix();
//glutSwapBuffers();
}
void?drawWheelAhead()?{
//glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(MOVE_CAR?0?0);?????????????//移動,參數含義(x軸位移y軸位移z軸位移)??
glRotatef(SPIN_CAR?0?0?1);??????????????//旋轉,參數含義(旋轉量x軸y軸z軸)??
glScalef(SIZE_CAR?SIZE_CAR?1);??????????????//縮放,參數含義(x軸倍數y軸倍數z軸倍數)??
glBegin(GL_POLYGON);
glColor3f(1.0?0.0?0.0);
for?(int?i?=?0;?i glVertex2f(R*cos(2?*?PI?/?n*i)-15?R*sin(2?*?PI?/?n*i)-15);
}
glEnd();
glFlush();
glPo
- 上一篇:組態王源代碼
- 下一篇:8052單片機溫度計程序
評論
共有 條評論