Sie sind auf Seite 1von 25

Firepart.

h
// double to float conv #pragma warning(disable:4244) #ifdef _DEBUG #include <assert.h> #endif #define HVCtrX 0.0 #define HVCtrY 0.0 Float FireFoc=2.0f; #define FireFocX FireFoc #define FireFocY FireFoc #define ProjEX(p) (HVCtrX+FireFocX*(p).x/(p).y) #define ProjEY(p) (HVCtrY+FireFocY*(p).z/(p).y) #ifdef NOZBUFFER #define ProjEZ(ez,p) #else #define ProjEZ(ez,p) (ez)=-((p).y*(float)(1.0/20.0)-1.0) #endif inline int hrnd (int n) { return (n*rand() )/RAND_MAX; } inline double hrnd (double n) { return (n*rand() )/RAND_MAX; } inline double abs(double n) { return (n<0)?-n:n; } inline double abs(float n) { return (n<0)?-n:n; } double nrnd(double d) // normal distribution randomizer { double r,v1,v2; do { v1=2.0*hrnd(1.0)-1.0; v2=2.0*hrnd(1.0)-1.0; r=v1*v1+v2*v2; } while (r >= 1.0 || r == 0.0); r=sqrt(-2.0*log(r)/r); //gset=v1*fac; //iset=v2*fac; return v1*d*r; } /*#define NBNOISEVAL 1024 #define NOISEDD 0.9; #define NOISESCALE 2.0 float NoiseX[NBNOISEVAL+1]; float NoiseY[NBNOISEVAL+1]; float NoiseZ[NBNOISEVAL+1]; void NoiseCalc(float *t,int n,float d) { t[n>>1]=(t[0]+t[n])*0.5+hrnd(2.0f*d)-d; if (n>1) { d*=(float)NOISEDD;

NoiseCalc(t,n>>1,d); NoiseCalc(t+(n>>1),(n+1)>>1,d); } } //#include <time.h> void NoiseInit() { //srand(time(NULL)); NoiseX[0]=NoiseX[NBNOISEVAL]=0.0; NoiseCalc(NoiseX,NBNOISEVAL,1.0); NoiseY[0]=NoiseY[NBNOISEVAL]=0.0; NoiseCalc(NoiseY,NBNOISEVAL,1.0); NoiseZ[0]=NoiseZ[NBNOISEVAL]=0.0; NoiseCalc(NoiseZ,NBNOISEVAL,1.0); } void Noise(SVector3D const &p,SVector3D *n) { double d; float f; int i; f=modf(p.x*NOISESCALE,&d); i=((int)d)&(NBNOISEVAL-1); n->x=(1.0f-f)*NoiseX[i]+f*NoiseX[i+1]; f=modf(p.y*NOISESCALE,&d); i=((int)d)&(NBNOISEVAL-1); n->y=(1.0f-f)*NoiseX[i]+f*NoiseY[i+1]; f=modf(p.z*NOISESCALE,&d); i=((int)d)&(NBNOISEVAL-1); n->z=(1.0f-f)*NoiseX[i]+f*NoiseZ[i+1]; } SVector3D Noise(SVector3D const &p) { double d; float f; int i; SVector3D n; f=modf(p.x*NOISESCALE,&d); i=((int)d+123)&(NBNOISEVAL-1); n.x=(1.0f-f)*NoiseX[i]+f*NoiseX[i+1]; f=modf(p.y*NOISESCALE,&d); i=((int)d+123)&(NBNOISEVAL-1); n.y=(1.0f-f)*NoiseX[i]+f*NoiseY[i+1]; f=modf(p.z*NOISESCALE,&d); i=((int)d+123)&(NBNOISEVAL-1); n.z=(1.0f-f)*NoiseX[i]+f*NoiseZ[i+1]; return n; }*/ inline Float operator*(const SVector3D &a,const SVector3D &b) { return a.DotProduct(b); } inline SVector3D operator*(const SVector3D &a,Float b) { return SVector3D(a.x*b,a.y*b,a.z*b); } inline SVector3D &operator*=(SVector3D &a,Float b) { a.Scale(b); return a; } /*inline SVector3D operator+(const SVector3D &a,const SVector3D &b) {

return SVector3D(a.x+b.x,a.y+b.y,a.z+b.z); }*/ inline SVector3D operator^(const SVector3D &a,const SVector3D &b) { SVector3D c; c.CrossProduct(a,b); return c; } class SMatrix3D { public: SVector3D x,y,z; SMatrix3D() :x(1,0,0),y(0,1,0),z(0,0,1) {} void FromAngle(float ax,float ay,float az) { Float cx=cos(ax); Float sx=sin(ax); Float cy=cos(ay); Float sy=sin(ay); Float cz=cos(az); Float sz=sin(az); // Calcul de la matrice de transformation 3D x.x=cy*cz; x.y=cx*sz*cy+sx*sy; x.z=sx*sz*cy-cx*sy; y.x=-sz; y.y=cx*cz; y.z=sx*cz; z.x=cz*sy; z.y=cx*sz*sy-sx*cy; z.z=sx*sz*sy+cx*cy; } void Apply(const SVector3D &v,SVector3D *out) { out->x=v*x; out->y=v*y; out->z=v*z; } void RApply(SVector3D const &v,SVector3D *out) { out->x=v.x*x.x+v.y*y.x+v.z*z.x; out->y=v.x*x.y+v.y*y.y+v.z*z.y; out->z=v.x*x.z+v.y*y.z+v.z*z.z; } void Apply(SVector3D *v) { SVector3D i(*v); Apply(i,v); } void RApply(SVector3D *v) { SVector3D i(*v); RApply(i,v); } }; #define NBPARTMAX 1024 #define PARTLIFE 3.0 #define PARTINTERV 0.01 #define FIRESIZE 1.0f #define FIREDS 0.7 #define FIREALPHA 0.35f #define FIREDA 0.2 #define FIRECYLR 0.2f struct Particle

{ SVector3D p; // position SVector3D v; // dp/dt SVector3D s; // size float a; // alpha float t; // time to death float ex,ey,ez; // screen pos float dx,dy; // screen size } TblP[NBPARTMAX]; int np; float LastPartTime; Bool FireAnim,FireRotate,FireRecalc; Bool FireStop; float FireRot; float FireAng; struct Vertex { float u,v; float ex,ey,ez; }; struct Quad { Vertex v[4]; }; Quad FGround; #define NCYL 3 struct WCyl { SVector3D p; SVector3D d; float r; } FCyl[NCYL]; // wood cylinders #define S2_2 0.7071f float Circle[9][2]= {{ 1.0f, 0.0f} ,{ S2_2, S2_2} ,{ 0.0f, 1.0f} ,{-S2_2, S2_2} ,{-1.0f, 0.0f} ,{-S2_2,-S2_2} ,{ 0.0f,-1.0f} ,{ S2_2,-S2_2} ,{ 1.0f, 0.0f} }; SVector3D FireSrc(0.0f,5.0f,-1.5f); SVector3D FireDS1(2.0f,0.0f,0.0f); SVector3D FireDS2(0.0f,2.0f,0.0f); SVector3D FireDir(0.0f,0.0f,0.25f); SVector3D FPartA(0.0f,0.0f,1.0f); #define NCYLPART 4 struct GLCyl {

Vertex vTriFan1[10]; Vertex vCyl[NCYLPART+1][9]; Vertex vTriFan2[10]; } glCyl[NCYL]; // cylinders aff data void FireInit() { //NoiseInit(); np=0; LastPartTime=0.0; FireAnim=True; FireRotate=False; FireRot=PI/5.0; FireAng=0.0; FireStop=False; FireRecalc=True; FCyl[0].r=FIRECYLR; FCyl[0].d=FireDS1*2.0f; FCyl[0].p=FireSrc-FCyl[0].d*0.5f; FCyl[0].p.z-=FIRECYLR; FCyl[1].r=FIRECYLR; FCyl[1].d=(FireDS1*0.85f+FireDS2*0.5f)*2.0f; FCyl[1].p=FireSrc-FCyl[1].d*0.5f; FCyl[1].p.z-=FIRECYLR; FCyl[2].r=FIRECYLR; FCyl[2].d=(FireDS1*-0.5f+FireDS2*0.85f)*2.0f; FCyl[2].p=FireSrc-FCyl[2].d*0.5f; FCyl[2].p.z-=FIRECYLR; } void CalcFire(float t,float dt) { int i,n; Particle *p=TblP; n=0; float ds=pow(FIREDS,dt); float da=pow(FIREDA,dt); float f; SVector3D a; if (FireAnim) { while (n<np) { if ((p->t-=dt)<=0) { // kill it *p=TblP[--np]; } else { // animate it //a=p->p; a.z-=5*t; a=FPartA; //+Noise(a)*(p->p.z-FireSrc.z)*1.0; //a.x*=4.0f; a.y*=4.0f; p->v+=a*dt; p->p+=p->v*dt; //p->s*=ds; p->s.x=0.67*FIRESIZE+0.4*FIRESIZE*sin(PI*(p->t)/PARTLIFE); p->s.z=0.05*FIRESIZE+0.768*FIRESIZE*(1.0-p->t/PARTLIFE); p->a*=da; ++n; ++p; } } if (!FireStop) while (np<NBPARTMAX && t-LastPartTime>=PARTINTERV) {

LastPartTime+=(float)PARTINTERV; p=TblP+(np++); //p->p=FireSrc+FireDS1*nrnd(1.0)+FireDS2*nrnd(1.0); do { f=nrnd(0.2); } while (abs(f)>0.45); p->p=FireSrc+FCyl[hrnd(NCYL)].d*f; p->v=FireDir; p->t=LastPartTime+PARTLIFE-t; p->a=FIREALPHA; p->s.x=FIRESIZE; p->s.y=FIRESIZE; p->s.z=FIRESIZE; p->s.x=0.67*FIRESIZE+0.4*FIRESIZE*sin(PI*(p->t)/PARTLIFE); p->s.z=0.05*FIRESIZE+0.768*FIRESIZE*(1.0-p->t/PARTLIFE); } } else LastPartTime+=dt; n=0; p=TblP; SVector3D v; SMatrix3D m; SVector3D o; if (FireRotate) { FireAng+=FireRot*dt; FireRecalc=True; } m.FromAngle(0,0,FireAng); m.Apply(FireSrc,&o); o=FireSrc-o; while (n<np) { m.Apply(p->p,&v); v+=o; p->ex=ProjEX(v); p->ey=ProjEY(v); ProjEZ(p->ez,v); p->dx=FireFocX*p->s.x/v.y; p->dy=FireFocY*p->s.z/v.y; ++n; ++p; } SVector3D vg; for (n=0;n<4;++n) { vg=FireSrc; vg.y-=2*FIRECYLR; if ((n+1)&2) vg+=FireDS1; else vg-=FireDS1; if ( n &2) vg+=FireDS2; else vg-=FireDS2; m.Apply(vg,&v); v+=o; FGround.v[n].ex=HVCtrX+FireFocX*v.x/v.y; FGround.v[n].ey=HVCtrY+FireFocY*v.z/v.y; FGround.v[n].ex=ProjEX(v); FGround.v[n].ey=ProjEY(v); ProjEZ(FGround.v[n].ez,v); FGround.v[n].u=((n+1)&2)?1.0:0.0; FGround.v[n].v=( n &2)?1.0:0.0; } if (FireRecalc) { SVector3D cp,cd,cm,cn; for (int c=0;c<NCYL;++c) { m.Apply(FCyl[c].p,&cp); cp+=o; m.Apply(FCyl[c].d,&cd); #ifdef NOZBUFFER if (cd.y>0.0) // back to forward sorting #else

if (cd.y<0.0) // forward to back sorting #endif { cp+=cd; cd*=-1.0; } v.x=0.0f; v.y=0.0f; v.z=1.0f; cm.CrossProduct(v,cd); cn.CrossProduct(cd,cm); cm*=FIRECYLR/cm.Length(); cn*=FIRECYLR/cn.Length(); cd*=1.0f/NCYLPART; glCyl[c].vTriFan1[0].ex=ProjEX(cp); glCyl[c].vTriFan1[0].ey=ProjEY(cp); ProjEZ(glCyl[c].vTriFan1[0].ez,cp); for (i=0;i<9;++i) { v=cp+cm*Circle[i][0]+cn*Circle[i][1]; glCyl[c].vTriFan1[i+1].ex=ProjEX(v); glCyl[c].vTriFan1[i+1].ey=ProjEY(v); ProjEZ(glCyl[c].vTriFan1[i+1].ez,v); } for (n=0;n<NCYLPART+1;++n) { if (n) cp+=cd; for (i=0;i<9;++i) { v=cp+cm*Circle[i][0]+cn*Circle[i][1]; glCyl[c].vCyl[n][i].ex=ProjEX(v); glCyl[c].vCyl[n][i].ey=ProjEY(v); ProjEZ(glCyl[c].vCyl[n][i].ez,v); } } glCyl[c].vTriFan2[0].ex=ProjEX(cp); glCyl[c].vTriFan2[0].ey=ProjEY(cp); ProjEZ(glCyl[c].vTriFan2[0].ez,cp); for (i=0;i<9;++i) { v=cp+cm*Circle[i][0]+cn*Circle[i][1]; glCyl[c].vTriFan2[i+1].ex=ProjEX(v); glCyl[c].vTriFan2[i+1].ey=ProjEY(v); ProjEZ(glCyl[c].vTriFan2[i+1].ez,v); } } FireRecalc=False; } }

Ptypes.h
//------------------------------------------------------------------// Defines - General definitions and classes. // By: Aaron Hilton (c) July 1997 //------------------------------------------------------------------#ifndef __Defines_ #define __Defines_

//------------------------------------------------------------------// General definitions and classes. typedef long Bool; typedef long Int; typedef float Float; typedef unsigned char typedef signed char typedef unsigned short typedef short typedef unsigned long typedef long typedef char* typedef const char * typedef UInt8* typedef Int8* typedef UInt16* typedef Int16* typedef UInt32* typedef Int32* #define True 1 #define False 0 #define Null 0 // Toast pointer based allocations in one fell swoop. #define NUKE(x) if(x != 0) {delete x; x=0;} // Platform specific debug info. #ifdef _DEBUG #define __DEBUG #endif //DEBUG // CDECL is used for functions that take variable arguments. #ifndef CDECL #define CDECL __cdecl #endif // APIDECL is used on global public functions. #ifndef APIDECL #define APIDECL __stdcall #endif UInt8; Int8; UInt16; Int16; UInt32; Int32; PStr; PConstStr; PUInt8; PInt8; PUInt16; PInt16; PUInt32; PInt32; // pointer to a string // pointer to a constant string

// Not used yet... but soon. #define EXPORT //------------------------------------------------------------------// PointI2D & PointUI2D: Copywrite 18/07/97 // TBI - Write wrapper class.

typedef tagPOINT PointI2D; typedef struct _tag_PointUI2D { UInt32 x, y; } PointUI2D; // SizeI2D: Copywrite 18/07/97 // TBI - Write wrapper class. typedef tagSIZE SizeI2D; // RectI: Copywrite 18/07/97 // TBI - Write wrapper class. typedef tagRECT RectI; //------------------------------------------#define sqr( X ) ( (X) * (X) ) // Square the X #define PI 3.14159265358979323846f #define DEG2RAD(X) (PI*(X)/180.0) // Degrees -> Radians #define RAD2DEG(X) ((X)*180.0/PI) // Radians -> Degrees #define rnd() ((Float)rand()*(1.0f /(Float)RAND_MAX)) // Floating point random number generator ( 0 -> 1) // fabs: Absolute function. //#undef abs //#define abs(a) ( (a) > 0 ? (a) : -(a) ) // Force sign clamping to (-1;0;1) #define sgn(a) ((a)<0?-1:((a)?1:0)) #ifndef max #define max( a, b ) ((a) > (b) ? (a) : (b)) #endif #ifndef min #define min( a, b ) ((a) < (b) ? (a) : (b)) #endif // Macro to release an object. #define RELEASE(x) if(x != NULL) {x->Release(); x = NULL;} // Macro to display a message box containing the given string. #define DISPLAYMSG(x) MessageBox(NULL, x, "Planet Toast Message", MB_OK); #endif

Stdafx.h
// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_) #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <stdlib.h> #include <stdio.h> #include <math.h> #include <gl\gl.h> #include <gl\glu.h> #include "PTypes.h" #include "SVector.h"

// Exclude rarely-used stuff from Windows headers

//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. //#define NOEXTENSION //#define NOZBUFFER #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

Stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes // Skeleton.pch will be the pre-compiled header // stdafx.obj will contain the pre-compiled type information #include "stdafx.h" // TODO: reference any additional headers you need in STDAFX.H // and not in this file

Svector.h
//------------------------------------------------------------------// SVector.h - A vector math library. // By: Aaron Hilton (c) 1/21/98 //------------------------------------------------------------------#ifndef __SVECTOR_H_ #define __SVECTOR_H_ //------------------------------------------------------------------// Pseudo-constantes: #define EPSILON 1e-8 #define ZERO EPSILON #define M_1_MAXINT 4.65661287524579692410E-10 #define M_1_255 (1.0 / 255.0) #define M_1_65535 (1.0 / 65535.0) #define M_1_65500 (1.0 / 65500.0)

//---> 1/(2^31-1) 1/meu maxint 32bits

// 3DFX Glide requires triangles to snap its coordinates to this precision. #define SNAP_BIAS (Float)(1<<19) //------------------------------------------// Forward declaration. // Actual Vector struct.

class SVector3D { public: SVector3D() {}; SVector3D( Float tx, Float ty, Float tz ) { x = tx; y = ty; z = tz; w = 1.f; } SVector3D( Float tx, Float ty, Float tz, Float tw ) { x = tx; y = ty; z = tz; w = tw; } union { Float x; // Coorinate Float nx;// Normal }; union { Float y; Float ny; }; union { Float z; Float nz; }; Float w; // This coordinate is only used for the final transformation process. Not used by any of the vector methods. //------------------------------------------// General utility functions. void Print(char *s = Null ); // Prints the vector's coordinates to the "stderr" stream. void Rand(); // Randomly generates a new vector. inline Float DotProduct( const SVector3D &other ) const; // DotProduct of this vector into the other vector. inline void CrossProduct( const SVector3D &a, const SVector3D &b ); // Calculates the cross product of "a" and "b", then storing it in this vector. inline Float Length(); // Returns the length of this vector. //------------------------------------------// Results of these opperations are contained in "this" vector by default. inline Float Normalize(); // Normalizes this vector, and returns the scalar value used to normalize the vector. inline void Zero( ); // Reset variables to a normal zero constant. (w = 1.f) inline void Scale( Float s ); // Scale this vector by "s". inline void Add( const SVector3D &other ); // Add the other vector to this vector. inline void Subtract( const SVector3D &other ); // Subtract the other vector from this vector. (result is contained in this vector.) inline void Combine( const SVector3D &other, Float s=1.f ); // Combine vectors with a scalar quantity. inline void Lerp( const SVector3D &a, const SVector3D &b, Float fPercent ); // Linear Interpolate inline void LerpW( const SVector3D &a, const SVector3D &b, Float fPercent );// Linear Interpolate with W inline void RotV( const SVector3D &e, const SVector3D &p ); // Rotate this vector to the normal of e. inline void GetAngles( Float &fPan, Float &fPitch ); // Retrieve the angles of this vector.

// Assignment opperator. const SVector3D& operator=( const SVector3D &point ) { x = point.x; y = point.y; z = point.z; w = point.w; return *this; } // Math opperations. // Unary. friend SVector3D operator+( const SVector3D& ptSrc1, const SVector3D& ptSrc2 ); friend SVector3D operator-( const SVector3D& ptSrc1, const SVector3D& ptSrc2 ); // Binary. const SVector3D& operator+=( const SVector3D& ptSrc ) { Add( ptSrc ); return *this; } const SVector3D& operator-=( const SVector3D& ptSrc ) { Subtract( ptSrc ); return *this; } //------------------------------------------// Matrix transformation process. void Transform( const SVector3D &v, const SMatrix &a );

// };

//-----------------------------------------------------------------------// Inline implementation. //------------------------------------------// General utility functions. // DotProduct of this vector into the other vector. Float SVector3D::DotProduct( const SVector3D &other ) const { return(x * other.x + y * other.y + z * other.z); } // Calculates the cross product of "a" and "b", then storing it in this vector. void SVector3D::CrossProduct( const SVector3D &a, const SVector3D &b ) { x = a.y * b.z - b.y * a.z; y = b.x * a.z - a.x * b.z; z = a.x * b.y - b.x * a.y; } // Returns the length of this vector. Float SVector3D::Length() { return (Float) sqrt( sqr(x) + sqr(y) + sqr(z) ); } //-----------------------------------------------------------------------// Results of these opperations are contained in "this" vector by default. //------------------------------------------// Normalizes this vector, and returns the scalar value used to normalize the vector. Float SVector3D::Normalize() { Float n, nn ;

nn = sqr(x) + sqr(y) + sqr(z); if ( nn < ZERO ) return 0.0f; nn = (Float) sqrt(nn); n = 1.0f / nn; x *= n; y *= n; z *= n; return nn; } void SVector3D::Zero( ) { x = y = z = 0.f; w = 1.f; } // Scale this vector by "s". void SVector3D::Scale( Float s ) { x *= s; y *= s; z *= s; } // Add the other vector to this vector. void SVector3D::Add( const SVector3D &other ) { x += other.x; y += other.y; z += other.z; } // Subtract the other vector from this vector. void SVector3D::Subtract( const SVector3D &other ) { x -= other.x; y -= other.y; z -= other.z; } // Combine vectors with a scalar quantity. void SVector3D::Combine( const SVector3D &other, Float s) { x += s * other.x; y += s * other.y; z += s * other.z; } // Linear Interpolate void SVector3D::Lerp( const SVector3D &a, const SVector3D &b, Float fPercent ) { x = a.x*(1.f-fPercent) + b.x*fPercent; y = a.y*(1.f-fPercent) + b.y*fPercent; z = a.z*(1.f-fPercent) + b.z*fPercent;

} // Linear Interpolate with W void SVector3D::LerpW( const SVector3D &a, const SVector3D &b, Float fPercent ) { x = a.x*(1.f-fPercent) + b.x*fPercent; y = a.y*(1.f-fPercent) + b.y*fPercent; z = a.z*(1.f-fPercent) + b.z*fPercent; w = a.w*(1.f-fPercent) + b.w*fPercent; } // Rotate a quaternion vector ("p") around a normal ("e"). void SVector3D::RotV( const SVector3D &e, const SVector3D &p ) { Float m = (Float)sqrt( sqr( p.y ) + sqr( p.z ) ) ; Float im = 1.0f / m ; x = m * e.x + p.x * e.z ; y = ( -p.x * p.y * e.x + p.z * e.y ) * im + p.y * e.z ; z = ( -p.x * p.z * e.x - p.y * e.y ) * im + p.z * e.z ; } // Retrieve the angles of this vector. void SVector3D::GetAngles( Float &fPan, Float &fPitch ) { // fPan = -(Float)atan2( x, z ); // fPitch = 0;//(Float)atan2( sin(fPan)*z + cos(fPan)*x, y); double dx, dy, dz; dx = (double)x; dy = (double)y; dz = (double)z; if(dz > 0.0001f) { fPan = (Float) -atan(dx/dz); dz /= cos((double)fPan); fPitch = (Float) atan(dy/dz); } else if(dz < -0.0001f) { fPan = (Float) (PI-atan(dx/dz)); dz /= cos((double)fPan-PI); fPitch = (Float) -atan(dy/dz); } else { fPan = 0.0; fPitch = (Float)-PI/2.f; } } // Math opperations. inline SVector3D operator+( const SVector3D& ptSrc1, const SVector3D& ptSrc2 ) { SVector3D ptTmp(ptSrc1); ptTmp.Add( ptSrc2 ); return ptTmp; }

inline SVector3D operator-( const SVector3D& ptSrc1, const SVector3D& ptSrc2 ) { SVector3D ptTmp(ptSrc1); ptTmp.Subtract( ptSrc2 ); return ptTmp; } //------------------------------------------------------------------// Extra tidbits... //------------------------------------------// Calculates the equation of "b" to the power of "e". Int IPower( Int b, Int e ); #endif // __SVECTOR_H_

Hufof.cpp

//------------------------------------------------------------------// File: YourCode.cpp // Comments: Write your visual effect here. //------------------------------------------------------------------#include "stdafx.h" //#include "Texture.h" //---------------------------------------// Usefull debugger thingy. void DebugOut(const char *szFormat, ...); //---------------------------------------// Change your App Name. char szAppName[] = "Hufo's little fire"; // Timer info. Very useful for keeping animated stuff in sync on different computers. extern float fTime, fDeltaTime; // Current window dimensions. extern GLsizei g_w, g_h; #ifndef NOEXTENSION extern HWND g_hwnd; #endif #include "FirePart.h" #ifndef NOEXTENSION bool Splash() { return MessageBox(NULL, " \" \" : rotate/stop camera\n" " ENTER : stop fire animation\n" " ESC : put water on the fire\n" "\n" "Enjoy it :-) Hufo / N.A.A.\n"

,szAppName ,MB_OKCANCEL | MB_ICONWARNING) == IDOK; return True; } #endif // Use the demonstration texture. //CTexture gTexExample; float x0; // window aspect ratio #define XSTD (4.0/3.0) float TFire; // fire time float TVit; float CylShad[9]={0.2f,0.6f,1.0f,0.6f,0.2f,0.2f,0.2f,0.2f,0.2f}; struct RGBColor { float r,g,b; }; #define SetRGBColor(c) glColor3f((c).r,(c).g,(c).b) RGBColor CylBColor={0.43f,0.25f,0.10f}; RGBColor CylColor[9]; // Startup Stuff. bool Init() // Called right after the window is created, and OpenGL is initialized. { // Reset the matrix to something we know. glMatrixMode(GL_PROJECTION); glLoadIdentity(); float x = (float)g_w/(float)g_h; // Correct the viewing ratio of the window in the X axis. if (x>XSTD) gluOrtho2D( -x, x, -1, 1 ); // Reset to a 2D screen space. else gluOrtho2D( -XSTD, XSTD, -XSTD/x, XSTD/x ); // Reset to a 2D screen space. x0=x; // Reset model view matrix stack. glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); // // // gTexExample.LoadJPG( "Demo.jpg" ); glEnable( GL_TEXTURE_2D ); gTexExample.Use(); // Tells OpenGL to use this texture. glCullFace(GL_FRONT); // reject fliped faces glDepthFunc(GL_LESS); // // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA,GL_ONE); glEnable(GL_BLEND); for (int i=0;i<9;++i) { CylColor[i].r=CylBColor.r*CylShad[i]; CylColor[i].g=CylBColor.g*CylShad[i]; CylColor[i].b=CylBColor.b*CylShad[i]; } TFire=0.0; TVit=1.0; FireInit(); // initialise fire // If everything went well, then return true.

// Returning false will cause the program to quit right away. return true; } void Shutdown() // Called right after the window is destroyed. { } #ifndef NOEXTENSION void UserKey(int k) { switch (k) { case VK_RETURN: FireAnim=!FireAnim; break; case VK_SPACE: FireRotate=!FireRotate; break; case VK_ESCAPE: // PostMessage(g_hwnd,WM_CLOSE,0,0); FireStop=True; break; } } #endif #define fSQRT_3_2 0.8660254038f void AffParticle(float ex,float ey,float dx,float dy,float a) { glBegin(GL_TRIANGLE_FAN); glColor4f(1.0f,1.0f,0.0f,a); //glColor4f(a,a,0.0f,1.0f); glVertex2f(ex,ey); glColor4f(1.0f,0.0f,0.0f,0.0f); //glColor4f(a,0.0f,0.0f,0.0f); glVertex2f(ex-dx,ey); glVertex2f(ex-0.5f*dx,ey+fSQRT_3_2*dy); glVertex2f(ex+0.5f*dx,ey+fSQRT_3_2*dy); glVertex2f(ex+1.0f*dx,ey); glVertex2f(ex+0.5f*dx,ey-fSQRT_3_2*dy); glVertex2f(ex-0.5f*dx,ey-fSQRT_3_2*dy); glVertex2f(ex-1.0f*dx,ey); glEnd(); } #ifdef NOZBUFFER #define glVertexAll(v) glVertex2f((v).ex,(v).ey) #else #define glVertexAll(v) glVertex3f((v).ex,(v).ey,(v).ez) #endif // Draw all the scene related stuff. void Render() { float x = (float)g_w/(float)g_h; // Aspect Ratio if (x!=x0) // Window was resized and need reinit

{ glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (x>XSTD) gluOrtho2D( -x, x, -1, 1 ); // Reset to a 2D screen space. else gluOrtho2D( -XSTD, XSTD, -XSTD/x, XSTD/x ); // Reset to a 2D screen space. glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); x0=x; } #ifndef NOEXTENSION static float secTime=fTime; static int nf=0; if (fTime-secTime>=1.0) { // aff framerate char buf[256]; sprintf(buf,"%s - %g fps",szAppName,nf/(fTime-secTime)); // sprintf(buf,"%s - %g fps (%d part)",szAppName,nf/(fTime-secTime),np); SetWindowText(g_hwnd,buf); secTime=fTime; nf=0; } #endif TFire+=fDeltaTime*TVit; CalcFire(TFire,fDeltaTime*TVit); // animate the fire glClear( GL_COLOR_BUFFER_BIT #ifndef NOZBUFFER | GL_DEPTH_BUFFER_BIT #endif ); if (FireStop && np==0) PostMessage(g_hwnd,WM_CLOSE,0,0); int c,i,n; #ifndef NOZBUFFER glEnable(GL_DEPTH_TEST); #endif glEnable(GL_CULL_FACE); glColor4f(1.0f,1.0f,1.0f,1.0f); #ifndef NOZBUFFER for (c=0;c<NCYL;++c) { glBegin(GL_TRIANGLE_FAN); SetRGBColor(CylColor[0]); glVertexAll(glCyl[c].vTriFan1[0]); for (i=1;i<10;++i) { SetRGBColor(CylColor[i-1]); glVertexAll(glCyl[c].vTriFan1[i]); } glEnd();

} #endif for (n=0;n<NCYLPART;++n) { for (c=0;c<NCYL;++c) { glBegin(GL_QUAD_STRIP); for (i=0;i<9;++i) { SetRGBColor(CylColor[i]); glVertexAll(glCyl[c].vCyl[n ][i]); glVertexAll(glCyl[c].vCyl[n+1][i]); } glEnd(); } } #ifdef NOZBUFFER for (c=0;c<NCYL;++c) { glBegin(GL_TRIANGLE_FAN); SetRGBColor(CylColor[0]); glVertexAll(glCyl[c].vTriFan2[0]); for (i=9;i>0;--i) { SetRGBColor(CylColor[i-1]); glVertexAll(glCyl[c].vTriFan2[i]); } glEnd(); } #endif glDisable(GL_CULL_FACE); #ifndef NOZBUFFER glDisable(GL_DEPTH_TEST); #endif glEnable(GL_BLEND); Particle *p=TblP; n=np; while (n) { AffParticle(p->ex,p->ey,p->dx,p->dy,p->a); ++p; --n; } glDisable(GL_BLEND); #ifndef NOEXTENSION ++nf; #endif }

Skeleton.cpp
// Skeleton.cpp : Defines the entry point for the application. //

#include "stdafx.h" //---------------------------------// Skeleton functions and variables. //----------------char szAppName[]; float fTime=0.f, fDeltaTime=0.f; //------// Stuff. bool Init(); void Shutdown(); #ifndef NOEXTENSION bool Splash(); void UserKey(int k); #endif // Draw all the scene related stuff. void Render(); //---------------------------------// Timer info. LARGE_INTEGER TimerFreq; // Timer Frequency. LARGE_INTEGER TimeStart; // Time of start. LARGE_INTEGER TimeCur; // Current time. // Good 'ol generic drawing stuff. LRESULT CALLBACK SkeletonProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ); // Usefull debugger thingy. const int kMaxDebugMessageLength = 1024; void DebugOut(const char *szFormat, ...) { va_list argList; char szBuffer[kMaxDebugMessageLength]; va_start(argList, szFormat); _vsnprintf(szBuffer, kMaxDebugMessageLength, szFormat, argList); va_end(argList); OutputDebugString(szBuffer); } // Select the pixel format for a given device context void SetDCPixelFormat(HDC hDC) { int nPixelFormat; static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER| #ifdef NOZBUFFER PFD_DEPTH_DONTCARE, #endif PFD_TYPE_RGBA, GetDeviceCaps(hDC, BITSPIXEL),

// Size of this structure // Version of this structure // Draw to Window (not to bitmap) // Support OpenGL calls in window // Double buffered mode

// RGBA Color mode // Want the display bit depth

0,0,0,0,0,0, 0,0, 0,0,0,0,0, #ifdef NOZBUFFER 0, #else 16, #endif 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 };

// Not used to select mode // Not used to select mode // Not used to select mode // Size of depth buffer // Size of depth buffer // Not used to select mode // Not used to select mode // Draw in main plane // Not used to select mode // Not used to select mode

// Choose a pixel format that best matches that described in pfd nPixelFormat = ChoosePixelFormat(hDC, &pfd); DebugOut( "PixelFormat Selected: %d\nBPP: %d\n", nPixelFormat, GetDeviceCaps(hDC, BITSPIXEL) ); // Set the pixel format for the device context SetPixelFormat(hDC, nPixelFormat, &pfd); } void ChangeSize( GLsizei w, GLsizei h ) { // Prevent a divide by Zero if( h == 0 ) h = 1; // Set viewport to our dimensions. glViewport( 0, 0, w, h); } GLsizei g_w, g_h; #ifndef NOEXTENSION HWND g_hwnd; #endif bool bReady; // Good ol' creation code. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { #ifndef NOEXTENSION if (!Splash()) return 0; #endif // Say we're not ready to render yet. bReady = false; // Setup the window class. WNDCLASS wc; HWND hWnd; wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = (WNDPROC)SkeletonProc;

wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = szAppName; // Announce it to the Microsloth OS. if( RegisterClass( &wc ) == 0) { MessageBox( NULL, "Failed to register the idiot window class, right before creating the window.", "Fatal Blow", MB_OK | MB_ICONERROR ); return FALSE; // Failed to create window, so just quit right away. } // Make it go....... hWnd = CreateWindow( szAppName, szAppName, WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL ); if( hWnd == NULL ) { MessageBox( NULL, "Failed to create the effect window.", "Fatal Blow", MB_OK | MB_ICONERROR ); return FALSE; // Failed to create window, so just quit right away. } #ifndef NOEXTENSION g_hwnd=hWnd; #endif // Make it visible... ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); // Reset the timer stuff. QueryPerformanceFrequency(&TimerFreq); QueryPerformanceCounter(&TimeStart);

// Prepare the scene for rendering. if( Init() == false ) { MessageBox( hWnd, "Failed to initialize the visual effect.", "Fatal Blow", MB_OK | MB_ICONERROR ); return FALSE; // Failed init, so just quit right away. } // We're now ready.

bReady = true; // Usual running around in circles bit... BOOL bGotMsg; MSG msg; PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE ); while( WM_QUIT != msg.message ) { // Use PeekMessage() if the app is active, so we can use idle time to // render the scene. Else, use GetMessage() to avoid eating CPU time. bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ); if( bGotMsg ) { // Translate and dispatch the message TranslateMessage( &msg ); DispatchMessage( &msg ); } else { InvalidateRect( hWnd, NULL, FALSE ); } } // Not ready to render anymore. bReady = false; // Nuke all applicable scene stuff. Shutdown(); return msg.wParam; } // Stupid usual callback window proc message spud gun. LRESULT CALLBACK SkeletonProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { // Keep drawing stuff around. static HGLRC hRC; static HDC hDC; switch( uMsg ) { case WM_CREATE: // Remember our mSloth drawing context. hDC = GetDC( hWnd ); // Select our precious pixel format. SetDCPixelFormat( hDC ); // Yeppers, make something that OpenGL understands. hRC = wglCreateContext( hDC ); wglMakeCurrent( hDC, hRC ); break; case WM_DESTROY: wglMakeCurrent( hDC, NULL );

wglDeleteContext( hRC ); PostQuitMessage( 0 ); break; case WM_SIZE: g_w = LOWORD(lParam); g_h = HIWORD(lParam); ChangeSize( g_w, g_h ); break; case WM_PAINT: if( bReady ) { // Get the current time, and update the time controller. QueryPerformanceCounter(&TimeCur); float fOldTime = fTime; fTime = (float)((double)(TimeCur.QuadPartTimeStart.QuadPart)/(double)TimerFreq.QuadPart); fDeltaTime = fTime - fOldTime; // Draw all the scene related stuff. Render(); // Make it all visible at once! SwapBuffers( hDC ); // We actually did something with this rect, so announce it to the mSloth OS. ValidateRect( hWnd, NULL ); } break; #ifndef NOEXTENSION case WM_CHAR: UserKey(wParam); break; #endif default: return DefWindowProc( hWnd, uMsg, wParam, lParam ); } return 0; }

Svector.cpp
//------------------------------------------------------------------// SVector.cpp - A vector math library. // By: Aaron Hilton (c) 1/21/98 //------------------------------------------------------------------#include "stdafx.h" #include "SVector.h" //------------------------------------------// General utility functions. //-------------------------------------------

// Prints the vector's coordinates to the "stderr" stream. void SVector3D::Print(char *s ) { // If we have an informative string, then print it. if(s) fprintf(stderr, "%s\n", s); // Print the vector info. fprintf(stderr,">%12g\t%12g\t%12g\t%12g\n", (double)x, (double)y, (double)z, (double)w ); } // Randomly generates a new vector. void SVector3D::Rand() { x = 1.f - 2.f * rnd(); y = 1.f - 2.f * rnd(); z = 1.f - 2.f * rnd(); } //------------------------------------------// Calculates the equation of "b" to the power of "e". Int IPower( Int b, Int e ) { Int result = 1 ; // Loop the multiplication process for each exponent. while( e-- ) result *= b ; return result; }

Das könnte Ihnen auch gefallen