服务器之家

服务器之家 > 正文

c语言实现多线程动画程序示例

时间:2021-01-18 10:42     来源/作者:C语言程序设计

该程序是利用opengl图形库与fmod音频库写的一个简单3d动画程序。该程序在vs下运行良好,若缺少相关dll文件请确认已配制fmod与opengl库。

mixmodel.cpp

复制代码 代码如下:


// mixmodel.cpp : 定义控制台应用程序的入口点。
//

 

#include "stdafx.h"

//定义一个线程
DWORD WINAPI SoundProc(
 LPVOID LPVIDEOPARAMETERS);
//光照变量
GLfloat  whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat  sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat  lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
//贴图变量
GLuint toTextures[4];
char* szFiles[4] = {"bcb.bmp","sun.bmp","earth.bmp","moon.bmp"};


//声音引擎查错函数
void ERRCHECK(FMOD_RESULT result)
{
 if(result != FMOD_OK)
 {
  printf("FMOD error!(%d) %s\n",result,FMOD_ErrorString(result));
  //exit(-1);
 }
}

void Initial()
{

 AUX_RGBImageRec* Image[4];
 int i;
 glEnable(GL_DEPTH_TEST); // 启用深度测试
 glFrontFace(GL_CCW);  // 指定逆时针绕法表示多边形正面

 glEnable(GL_CULL_FACE);  // Do not calculate inside of jet

 // Enable lighting
 glEnable(GL_LIGHTING);

 // Setup and enable light 0
 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
 glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
 glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
 glEnable(GL_LIGHT0);

 // Enable color tracking
 glEnable(GL_COLOR_MATERIAL);

 // Set Material properties to follow glColor values
 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
 glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); //背景为黑色

 

 glGenTextures(4, toTextures);
 for(i=0;i<4;i++)
 {
 // Load environment map
    glBindTexture(GL_TEXTURE_2D, toTextures[i]);
 Image[i] = auxDIBImageLoadA(szFiles[i]);
 glTexImage2D(GL_TEXTURE_2D, 0, 3, Image[i]->sizeX, Image[i]->sizeY, 0, GL_RGB , GL_UNSIGNED_BYTE, Image[i]->data);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
 }

    glEnable(GL_TEXTURE_2D);
 glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);
 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);

}

void drawsphere()
{
 static float fElect1 = 0.0f;
 glTranslatef(0.0f, 0.0f, -250.0f); 
 glBindTexture(GL_TEXTURE_2D,toTextures[1]);
 glDisable(GL_LIGHTING);
 // 绘制红色的太阳模型
 //glShadeModel(GL_SMOOTH);
 glColor3f(1.0f, 0.0f, 0.0f);
 glutSolidSphere(12.0f, 100, 100);
 glEnable(GL_LIGHTING);
 // 当前绘制颜色变为蓝色
 //glShadeModel(GL_FLAT);
 glBindTexture(GL_TEXTURE_2D,toTextures[2]);
 glColor3f(0.0f, 0.0f, 1.0f);

 //绘制地球
 //保存当前的模型视图矩阵
 //glPushMatrix();
 glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
 glRotatef(fElect1, 0.0f, 1.0f, 0.0f);//绕y轴旋转一定的角度
 glTranslatef(90.0f, 0.0f, 0.0f);//平移一段距离
 glutSolidSphere(9.0f, 100, 100);

 glBindTexture(GL_TEXTURE_2D,toTextures[3]);
 glColor3f(1.0f,1.0f,0.0f);
 glRotatef(fElect1*4, 0.0f, 1.0f, 0.0f);
 glTranslatef(40.0f, 0.0f, 0.0f);
 glutSolidSphere(5.0f, 100, 100);
 // 恢复矩阵
 glPopMatrix();
 // 增加旋转步长
 fElect1 += 5.0f;
 if(fElect1 > 360.0f) fElect1 = 5.0f;
}

void ChangeSize(int w, int h)
{
 if(h == 0) h = 1;

 // 设置视区尺寸
    glViewport(0, 0, w, h);

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

 // 设置修剪空间
 GLfloat fAspect;
 fAspect = (float)w/(float)h;
 gluPerspective(45.0, fAspect, 1.0, 500.0);
/*
     if (w <= h)
   glOrtho (-nRange, nRange, nRange*h/w, -nRange*h/w, -nRange*2.0f, nRange*2.0f);
  else
   glOrtho (-nRange*w/h, nRange*w/h, nRange, -nRange, -nRange*2.0f, nRange*2.0f);
*/
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}


void RenderScene(void)
{
 // 旋转的角度
 

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 // 重置模型视图矩阵
 //glMatrixMode(GL_MODELVIEW);
 glMatrixMode(GL_PROJECTION);

    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0.0f, 1.0f, 0.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
 glBindTexture(GL_TEXTURE_2D, toTextures[0]);
 glDisable(GL_TEXTURE_GEN_S);
    glDisable(GL_TEXTURE_GEN_T);
 glDepthMask(GL_FALSE);
 glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(0.0f, 0.0f);

        glTexCoord2f(1.0f, 0.0f);
        glVertex2f(1.0f, 0.0f);

        glTexCoord2f(1.0f, 1.0f);
        glVertex2f(1.0f, 1.0f);

        glTexCoord2f(0.0f, 1.0f);
        glVertex2f(0.0f, 1.0f);
    glEnd();
 glMatrixMode(GL_PROJECTION);
    glPopMatrix();
 //glLoadIdentity();
 //
    glMatrixMode(GL_MODELVIEW);
 glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);
    glDepthMask(GL_TRUE);
 //glLoadIdentity();
 glPushMatrix();
 //将图形沿z轴负向移动
 drawsphere();
 

 glutSwapBuffers();
}

void TimerFunc(int value)
{
    glutPostRedisplay();
    glutTimerFunc(100, TimerFunc, 1);
}

//背景音乐的调度函数
void bcsound()
{
FMOD_RESULT result;
FMOD::System *system;
FMOD::Channel *channel;
result = FMOD::System_Create(&system);             // 创造FMOD的系统内核对象
ERRCHECK(result);

result = system->setSpeakerMode(FMOD_SPEAKERMODE_5POINT1);     // 设置5.1声道模式
ERRCHECK(result);

result = system->setSoftwareChannels(100);         // 调整软件混合
ERRCHECK(result);

result = system->setHardwareChannels(32);     // 调整硬件混合
ERRCHECK(result);

result = system->init(200, FMOD_INIT_NORMAL, 0);         // 初始化FMOD,音量大小为200
ERRCHECK(result);

FMOD::Sound *sound;
result = system->createSound("P115.ogg", FMOD_DEFAULT, 0, &sound);         //载入磁盘文件到内存,(全部载入后返回)
ERRCHECK(result);

unsigned int lenms;
result = sound->getLength(&lenms,FMOD_TIMEUNIT_MS);
ERRCHECK(result);

result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);//播放
ERRCHECK(result);
Sleep(lenms);

result = system->createSound("End Theme.mp3", FMOD_DEFAULT, 0, &sound);         //载入磁盘文件到内存,(全部载入后返回)
ERRCHECK(result);
result = sound->getLength(&lenms,FMOD_TIMEUNIT_MS);
ERRCHECK(result);

result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);//播放
ERRCHECK(result);
Sleep(lenms);

system->release();//释放
}

//动画功能函数
void graph(int argc, char* argv[])
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize(600,350);
 glutCreateWindow("日月地模型示例");
 glutReshapeFunc(ChangeSize);
 glutDisplayFunc(RenderScene);
    glutTimerFunc(500, TimerFunc, 1);
 Initial();
 glutMainLoop();

}

int main(int argc, char* argv[])
{
 HANDLE hThread1;
 hThread1 = CreateThread(NULL,0,SoundProc,NULL,0,NULL);
 CloseHandle(hThread1);

 graph(argc,argv);
 Sleep(3000);

    return 0;
}

DWORD WINAPI SoundProc(
 LPVOID LPVIDEOPARAMETERS)
{
 bcsound();
 return 0;
}



c语言实现多线程动画程序示例

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部