程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> c說話完成多線程動畫法式示例

c說話完成多線程動畫法式示例

編輯:關於C++

c說話完成多線程動畫法式示例。本站提示廣大學習愛好者:(c說話完成多線程動畫法式示例)文章只能為提供參考,不一定能成為您想要的結果。以下是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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved