C language implementation of Multithreaded Animation program Sample _c language

Source: Internet
Author: User

The program is a simple 3d animation program written using OpenGL graphics library and Fmod Audio library. This program works well under vs. if the associated DLL file is missing, please confirm that the FMOD and OpenGL libraries have been prepared.

Mixmodel.cpp

Copy Code code as follows:

Mixmodel.cpp: Defines the entry point for a console application.
//

#include "stdafx.h"

Define a thread
DWORD WINAPI Soundproc (
LPVOID lpvideoparameters);
Illumination variable
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 variables
Gluint Totextures[4];
char* Szfiles[4] = {"Bcb.bmp", "Sun.bmp", "Earth.bmp", "moon.bmp"};


Sound engine error-checking function
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); Enable depth Testing
Glfrontface (GL_CCW); Specifies that the clockwise wrap method represents 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); 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 the Red Sun model
Glshademodel (Gl_smooth);
glcolor3f (1.0f, 0.0f, 0.0f);
Glutsolidsphere (12.0f, 100, 100);
Glenable (gl_lighting);
The current paint color turns blue
Glshademodel (Gl_flat);
Glbindtexture (gl_texture_2d,totextures[2]);
glcolor3f (0.0f, 0.0f, 1.0f);

Draw the Earth
Save the current model view matrix
Glpushmatrix ();
GLLIGHTFV (Gl_light0,gl_position,lightpos);
Glrotatef (FElect1, 0.0f, 1.0f, 0.0f);//rotate a certain angle around the y axis
Gltranslatef (90.0f, 0.0f, 0.0f);/Translate a 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);
Recovery matrix
Glpopmatrix ();
Increase rotation step
FElect1 + 5.0f;
if (FElect1 > 360.0f) fElect1 = 5.0f;
}

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

Set Viewport size
Glviewport (0, 0, W, h);

Glmatrixmode (gl_projection);
Glloadidentity ();

Set clipping 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)
{
Angle of rotation

Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);

Resetting 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 ();
To move a graphic negatively along the z axis
Drawsphere ();

Glutswapbuffers ();
}

void Timerfunc (int value)
{
Glutpostredisplay ();
Gluttimerfunc (Timerfunc, 1);
}

The dispatch function of background music
void Bcsound ()
{
Fmod_result result;
Fmod::system *system;
Fmod::channel *channel;
result = Fmod::system_create (&system); Create a FMOD system kernel object
Errcheck (result);

result = System->setspeakermode (FMOD_SPEAKERMODE_5POINT1); Set 5.1-Channel mode
Errcheck (result);

result = System->setsoftwarechannels (100); Adjust software Mix
Errcheck (result);

result = System->sethardwarechannels (32); Adjust hardware blending
Errcheck (result);

result = System->init (fmod_init_normal, 0); Initialize Fmod with a volume size of 200
Errcheck (result);

Fmod::sound *sound;
result = System->createsound ("P115.ogg", Fmod_default, 0, &sound); Load the disk file into memory, (return after all load)
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 the disk file into memory, (return after all load)
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 ()/release
}

Animation function function
void graph (int argc, char* argv[])
{
Glutinit (&ARGC, argv);
Glutinitdisplaymode (glut_double | Glut_rgb | Glut_depth);
Glutinitwindowsize (600,350);
Glutcreatewindow ("Example of the Sun-moon model");
Glutreshapefunc (changesize);
Glutdisplayfunc (Renderscene);
Gluttimerfunc (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;
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.