要使用C語(yǔ)言編寫(xiě)一個(gè)旋轉(zhuǎn)的動(dòng)畫(huà),可以使用圖形庫(kù)如OpenGL或者SDL來(lái)實(shí)現(xiàn)。以下是使用OpenGL庫(kù)編寫(xiě)一個(gè)旋轉(zhuǎn)的動(dòng)畫(huà)的示例代碼:
#include <GL/glut.h>
float angle = 0.0f; // 角度
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 設(shè)置視角
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
// 設(shè)置旋轉(zhuǎn)
glRotatef(angle, 0.0f, 1.0f, 0.0f);
// 繪制一個(gè)立方體
glBegin(GL_QUADS);
// 繪制前面
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// 繪制后面
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
// 繪制左面
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
// 繪制右面
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
// 繪制上面
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
// 繪制下面
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
glutSwapBuffers();
}
void update(int value) {
angle += 1.0f; // 每次更新角度增加1度
if (angle > 360) {
angle -= 360;
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Rotation