C language to implement multithreaded animation program example
- 2020-04-02 02:19:35
- OfStack
The program is the use of opengl graphics library and fmod audio library to write a simple 3d animation program. This program runs well under vs, if missing related DLL files please confirm that you have configured fmod and opengl libraries.
Mixmodel. CPP
//Mixmodel.cpp: defines the entry point for the console application.
//
#include "stdafx.h"
//Define a thread
DWORD WINAPI SoundProc(
LPVOID LPVIDEOPARAMETERS);
//Variable illumination
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 };
//Map variable
GLuint toTextures[4];
char* szFiles[4] = {"bcb.bmp","sun.bmp","earth.bmp","moon.bmp"};
//Sound engine error check function
void ERRCHECK(FMOD_RESULT result)
{
if(result != FMOD_OK)
{
printf("FMOD error!(%d) %sn",result,FMOD_ErrorString(result));
//exit(-1);
}
}
void Initial()
{
AUX_RGBImageRec* Image[4];
int i;
glEnable(GL_DEPTH_TEST); //Enable depth testing
glFrontFace(GL_CCW); //Specifies the counterclockwise winding method to represent the front of the polygon
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 ); //The background is black
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);
//Draw a red model of the sun
//glShadeModel(GL_SMOOTH);
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidSphere(12.0f, 100, 100);
glEnable(GL_LIGHTING);
//The current draw color changes to blue
//glShadeModel(GL_FLAT);
glBindTexture(GL_TEXTURE_2D,toTextures[2]);
glColor3f(0.0f, 0.0f, 1.0f);
//Mapping the
//Save the current model view matrix
//glPushMatrix();
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);//I'm going to rotate the y axis by some Angle
glTranslatef(90.0f, 0.0f, 0.0f);//I'm going to shift it by some distance
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);
//Restore the matrix
glPopMatrix();
//Increase the rotation step size
fElect1 += 5.0f;
if(fElect1 > 360.0f) fElect1 = 5.0f;
}
void ChangeSize(int w, int h)
{
if(h == 0) h = 1;
//Set the viewport size
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Set trim space
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)
{
//Rotation Angle
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Reset the model view matrix
//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();
//Move the graph negatively along the z axis
drawsphere();
glutSwapBuffers();
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(100, TimerFunc, 1);
}
//Background music scheduling function
void bcsound()
{
FMOD_RESULT result;
FMOD::System *system;
FMOD::Channel *channel;
result = FMOD::System_Create(&system); //Create the system kernel object for FMOD
ERRCHECK(result);
result = system->setSpeakerMode(FMOD_SPEAKERMODE_5POINT1); //Set 5.1 channel mode
ERRCHECK(result);
result = system->setSoftwareChannels(100); //Adjust the software mix
ERRCHECK(result);
result = system->setHardwareChannels(32); //Adjust the hardware mix
ERRCHECK(result);
result = system->init(200, FMOD_INIT_NORMAL, 0); //Initialize FMOD with volume 200
ERRCHECK(result);
FMOD::Sound *sound;
result = system->createSound("P115.ogg", FMOD_DEFAULT, 0, &sound); //Load disk files into memory, (all loaded and returned)
ERRCHECK(result);
unsigned int lenms;
result = sound->getLength(&lenms,FMOD_TIMEUNIT_MS);
ERRCHECK(result);
result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);//play
ERRCHECK(result);
Sleep(lenms);
result = system->createSound("End Theme.mp3", FMOD_DEFAULT, 0, &sound); //Load disk files into memory, (all loaded and returned)
ERRCHECK(result);
result = sound->getLength(&lenms,FMOD_TIMEUNIT_MS);
ERRCHECK(result);
result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);//play
ERRCHECK(result);
Sleep(lenms);
system->release();//The release of
}
//Animation function
void graph(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600,350);
glutCreateWindow(" Sun, moon and earth model example ");
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;
}
< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201404/20140414072848.jpg? 20143147312 ">