NeHe Tutorials OpenGL
 X4. OpenGL

30 X4 λ 07, 11, 20, 32, 34.

 

Visual Studio Professional 2010. Visual Studio 7 . , Visual Studio, . Visual Studio Professional 2010 , .

 

:

1.    Collision UFOgame ( 30).

2.    ( 34).

3.    ( 07).

4.    ( 11).

5.    .

6.    .

7.    .

8.    ( 32).

9.    ( 20).

10.  .

 

1. Collision UFOgame ( 30).

 

:

-        LessonsGLCode_2010 30 Lesson30 (, LessonXN);

-        Collision UFOgame;

-        UFOgame.vcproj, UFOgame.vcxproj, UFOgame.vcxproj.filters, UFOgame.rc, UFOgame.sln Notepad Collision UFOgame ( CTRL + H);

-        Collision UFOgame ( CTRL + SHIFT + H).

 

( (Start Without Debugging)) , UFOgame Collision. , Collision , , UFOgame. _30 _XN( CTRL + SHIFT + H), 30_UFOgameDrawInit.cpp XN_UFOgameDrawInit.cpp, 30_GLHelp.txt XN_GLHelp.txt, XN_GLHelp.txt 30 XN. XN LessonsGLCode_2010 , .

XN_UFOgameDrawInit.cpp .

 

2. ( 34).

 

: . : XN_CollisionDrawInit.cpp DrawGLScene_XN ( ) , render columns (cylinders).

 

34 :

-        -> 34_HeightMapDrawInit.cpp;

-        Data Terrain.raw 34;

-        34_HeightMapDrawInit.cpp:

#define MAP_SIZE 1024 // Size Of Our .RAW Height Map

#define STEP_SIZE 16 // Width And Height Of Each Quad

#define HEIGHT_RATIO 1.5f //Ratio That The Y Is Scaled According To The X And Z

static BYTE g_HeightMap[MAP_SIZE*MAP_SIZE]; // Holds The Height Map Data

static float scaleValue = 0.15f; // Scale Value For The Terrain

extern RECT m_viewRect;

void RenderHeightMap(BYTE pHeightMap[]); // This Renders The Height Map As Quads

void LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap);

int Height(BYTE *pHeightMap, int X, int Y); // This Returns The Height From A Height Map Index

void SetVertexColor(BYTE *pHeightMap, int x, int y);// Sets The Color Value For A Particular Index,

// Depending On The Height Index

static bool bRender = TRUE;

 

,

 

GLfloat aLen = 512; // len of wall(original 320);

 

320 aLen( CTRL + H);

 

int InitGL_XN(void) :

 

LoadRawFile("Data/Terrain.raw", MAP_SIZE * MAP_SIZE, g_HeightMap); // (Lesson 34)

int DrawGLScene_XN(GLvoid) :

 

glMatrixMode(GL_MODELVIEW);

glPushMatrix();

glTranslatef(-aLen,-aLen,-aLen);

RenderHeightMap(g_HeightMap); // Render The Height Map

glPopMatrix();

 

( (Start Without Debugging)) , UFOgame . , .

 

3. ( 07).

 

:

 

extern CWordArray m_globTexture; //

extern CDWordArray m_maskTexture; // ,

 

static CWordArray m_groundTexture; //

static CWordArray m_skyTexture; //

static GLuint m_idPortHoles; // Π

static GLuint m_idCrossHair; ; // ࠠ

static GLuint m_idSpark; //

static GLuint filterF = 0; //

static GLuint filterG = 0; //

 

void LoadGLTexturesImages(CStringArray * sArray); //

void RemoveAllTextures(void);

int LoadGLTextureImage(CString fName, BOOL bMask);

GLuint LoadGLTextureImage(CString fName);

COLORREF ColorRandom(int rMin, int rMax);

 

void LoadGLTextures_XN() :

 

void LoadGLTextures_XN()

{

CStringArray sArray; //

sArray.Add("data/boden.jpg");

sArray.Add("data/marble.jpg");

sArray.Add("data/Base.JPG");

sArray.Add("data/Bumps.JPG");

sArray.Add("data/Glass.JPG");

sArray.Add("data/Lights.JPG");

sArray.Add("data/Wand.JPG");

sArray.Add("data/Logo.bmp");

 

sArray.Add("data/zodiak.jpg");

sArray.Add("data/sky_01.jpg");

sArray.Add("data/sky_02.jpg");

sArray.Add("data/sky_03.jpg");

sArray.Add("data/sky_04.jpg");

sArray.Add("data/sky_05.jpg");

sArray.Add("data/sky_06.jpg");

sArray.Add("data/Logo.bmp");

 

LoadGLTexturesImages(&sArray);

sArray.RemoveAll();

 

for( int i = 0; i < 8; i++)

m_groundTexture.Add(m_globTexture.GetAt(i));

 

for( int i = 0; i < 8; i++)

m_skyTexture.Add(m_globTexture.GetAt(i+8));

 

 

m_idPortHoles = LoadGLTextureImage("data/PortHoles_1.bmp", TRUE);

m_idCrossHair = LoadGLTextureImage("data/Crosshair.bmp", TRUE);

m_idSpark = LoadGLTextureImage("data/Spark.bmp");

 

}

 

, LoadGLTextures_XN Data ( , ).

 

DrawGLScene_XN :

 

//render walls(planes) with texture

filterF %= m_skyTexture.GetSize();

glBindTexture(GL_TEXTURE_2D, m_skyTexture.GetAt(filterF));

 

ProcessKeyBoard_XN :

 

else if (strCmnd == "F") filterF++;

 

( (Start Without Debugging)) , UFOgame , F. , . :

 

//Top Face

glTexCoord2f(0.0f, 0.0f); glVertex3f(-aLen,aLen,-aLen);

glTexCoord2f(1.0f, 0.0f); glVertex3f(aLen,aLen,-aLen);

glTexCoord2f(1.0f, 1.0f); glVertex3f(aLen,aLen,aLen);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-aLen,aLen,aLen);

 

, X4. , :

 

glBindTexture(GL_TEXTURE_2D, m_idSpark);

 

( (Start Without Debugging)) , UFOgame , .

4. ( 11).

 

, RenderHeightMap 34 , 11:

 

void RenderHeightMap(BYTE pMountMap[], GLuint texid) //Lesson 44

{

GLuint B_LIGHTING = glIsEnabled(GL_LIGHTING);

 

if(texid > 0)

glDisable(GL_LIGHTING);

else

glEnable(GL_LIGHTING);

if(texid > 0)

{

glEnable(GL_TEXTURE_2D);

filterG %= m_groundTexture.GetSize();

glBindTexture(GL_TEXTURE_2D, texid);

}

 

if(!pMountMap) return; // ?

if(bRender) // ?

glBegin( GL_QUADS ); //

else

glBegin( GL_LINES ); //

for ( int X = 0; X < MAP_SIZE; X += STEP_SIZE )

for (int Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )

{

// (X, Y, Z)

 

int X1 = X+STEP_SIZE;

int Y1 = Y+STEP_SIZE;

float xx = X/(float)MAP_SIZE;

float yy = Y/(float)MAP_SIZE;

float xx1 = X1/(float)MAP_SIZE;

float yy1 = Y1/(float)MAP_SIZE;

 

//vertex X,Y1:

int y = Height(pMountMap, X, Y1 );

if(texid <= 0) SetVertexColor(pMountMap, X, Y1);

else glTexCoord2f(xx, yy1);

glVertex3i(X, y, MAP_SIZE - Y1);

 

//vertex X,Y:

y = Height(pMountMap, X, Y );

if(texid <= 0) SetVertexColor(pMountMap, X, Y);

else glTexCoord2f(xx, yy);

glVertex3i(X, y, MAP_SIZE - Y);

 

//vertex X1,Y1:

y = Height(pMountMap, X1, Y );

if(texid <= 0) SetVertexColor(pMountMap, X1, Y);

else glTexCoord2f(xx1, yy);

glVertex3i(X1, y, MAP_SIZE - Y);

 

//vertex X1,Y1:

y = Height(pMountMap, X1, Y1 );

if(texid <= 0) SetVertexColor(pMountMap, X1, Y1);

else glTexCoord2f(xx1, yy1);

glVertex3i(X1, y, MAP_SIZE -Y1);

}

 

glEnd();

glColor4f(1.0f, 1.0f, 1.0f, 1.0f); //

 

B_LIGHTING ? glEnable(GL_LIGHTING): glDisable;

 

}

 

ProcessKeyBoard_XN :

 

else if (strCmnd == "G") filterG++;

 

DrawGLScene_XN :

 

filterG %= m_groundTexture.GetSize();

RenderHeightMap(g_HeightMap, m_groundTexture[filterG]); // Render The Height Map

 

( (Start Without Debugging)) , UFOgame . , .

5.

 

C :

 

void DrawUFO(void)

{

glPushMatrix();

glScalef(1,.3f,1);

gluSphere(cylinder_obj,20,20,20);

glPopMatrix();

}

 

gluSphere(cylinder_obj,20,20,20):

 

DrawUFO();

 

( (Start Without Debugging)) , UFOgame .

 

6. .

 

, :

 

struct myColor

{

GLfloat clr[3];

};

 

, :

 

static CPtrArray ArrayVel; // holds velocity of balls

static CPtrArray ArrayPos; // position of balls

static CPtrArray OldPos; // old position of balls

static CPtrArray m_colorArray; // color of balls

 

, :

 

static int m_curObj = 0;

 

:

 

myColor * GetMyColor(int i) //

{

if(i < 0)

return NULL;

if(i >= m_colorArray.GetSize())

return NULL;

return (myColor *) m_colorArray.GetAt(i);

}

TVector * GetVel(int i) //

{

if(i < 0)

return NULL;

if(i >= ArrayVel.GetSize())

return NULL;

return (TVector *) ArrayVel.GetAt(i);

}

TVector * GetPos(int i) //

 

{

if(i < 0)

return NULL;

if(i >= ArrayPos.GetSize())

return NULL;

return (TVector *) ArrayPos.GetAt(i);

}

TVector * GetOldPos(int i) //

{

if(i < 0)

return NULL;

if(i >= OldPos.GetSize())

return NULL;

return (TVector *) OldPos.GetAt(i);

}

 

, :

 

static TVector m_Var(25,25,0); //

 

, :

 

static BOOL m_bRotate = FALSE; //

 

:

 

void CameraLookAt_XN(void)

{

//set camera in hookmode

if (hook_toball1)

{

TVector unit_followvector=(* GetVel(m_curObj));

unit_followvector.unit();

 

gluLookAt((* GetPos(m_curObj)).X()+m_Var.X(),(* GetPos(m_curObj)).Y()+m_Var.Y() ,(* GetPos(m_curObj)).Z()+m_Var.Z() ,

(* GetPos(m_curObj)).X()+(* GetVel(m_curObj)).X() ,(* GetPos(m_curObj)).Y()+(* GetVel(m_curObj)).Y() ,

(* GetPos(m_curObj)).Z()+(* GetVel(m_curObj)).Z() ,0,1,0);

}

else

{

gluLookAt(pos.X(),pos.Y(),pos.Z(), pos.X()+dir.X(),pos.Y()+dir.Y(),pos.Z()+dir.Z(), 0,1.0,0.0);

glRotatef(camera_rotation,0,1,0);

}

}

 

DrawGLScene_XN //set camera in hookmode.

 

:

void AppendUFO(void)

{

myColor * pM = new myColor;

m_colorArray.Add(pM);

COLORREF rgb = ColorRandom(64, 196);

pM->clr[0] = GetRValue(rgb)/255.f;

pM->clr[1] = GetGValue(rgb)/255.f;

pM->clr[2] = GetBValue(rgb)/255.f;

 

TVector * pV = new TVector;

ArrayPos.Add(pV);

pV = new TVector;

ArrayVel.Add(pV);

pV = new TVector;

OldPos.Add(pV);

}

void InitUFO(void)

{

for(int i = 0; i< NrOfBalls; i++)

AppendUFO();

}

 

InitVars :

void InitVars_XN()

{

GLfloat tLen = aLen - 20;

//create palnes

pl1._Position=TVector(0,-tLen+200,0);

pl1._Normal=TVector(0,1,0);

pl2._Position=TVector(tLen,0,0);

pl2._Normal=TVector(-1,0,0);

pl3._Position=TVector(-tLen,0,0);

pl3._Normal=TVector(1,0,0);

pl4._Position=TVector(0,0,tLen);

pl4._Normal=TVector(0,0,-1);

pl5._Position=TVector(0,0,-tLen);

pl5._Normal=TVector(0,0,1);

pl6._Position=TVector(0,tLen,0);

pl6._Normal=TVector(0,-1, 0);

 

cylinder_obj= gluNewQuadric();

gluQuadricTexture(cylinder_obj, GL_TRUE);

 

 

//Set initial positions and velocities of balls

//also initialize array which holds explosions

InitUFO();

ResetUFO();

}

 

 

void ResetUFO(void)

{

//Set initial positions and velocities of balls

//also initialize array which holds explosions

ExplosionArray[0]._Alpha=0;

ExplosionArray[0]._Scale=1;

ExplosionArray[1]._Alpha=0;

ExplosionArray[1]._Scale=1;

ExplosionArray[2]._Alpha=0;

ExplosionArray[2]._Scale=1;

GLfloat r = 20;

GLfloat bLen = aLen * 0.9f;

for (int i=0; i<NrOfBalls; i++)

{

GLfloat vv = 5;

(* GetVel(i)) =

TVector(vv *(1.0f - 2.0f *(float)(rand()%100)*0.01f),

vv *(1.0f - 2.0f *(float)(rand()%100)*0.01f),

vv *(1.0f - 2.0f *(float)(rand()%100)*0.01f));

(* GetPos(i))=TVector(-500+i*75, 300, -500+i*50);

ExplosionArray[i]._Alpha=0;

ExplosionArray[i]._Scale=1;

}

for (int i=10; i<20; i++)

{

ExplosionArray[i]._Alpha=0;

ExplosionArray[i]._Scale=1;

}

}

 

DrawGLScene_XN :

for (int i=0;i<NrOfBalls;i++)

{

myColor * pM = GetMyColor(i);

 

glPushMatrix();

glTranslated((* GetPos(i)).X(),(* GetPos(i)).Y(),(* GetPos(i)).Z());

glColor3f(pM->clr[0],pM->clr[1],pM->clr[2]);

 

glEnable(GL_DEPTH_TEST);

glEnable(GL_LIGHTING);

glEnable(GL_AUTO_NORMAL);

glEnable(GL_NORMALIZE);

glDisable(GL_TEXTURE_2D);

DrawUFO();

//ApplyMaskTextureToUFO(LOWORD(m_maskTexture.GetAt(0)),HIWORD(m_maskTexture.GetAt(0)));

 

glPopMatrix();

}

 

FindBallCol_XN :

 

int FindBallCol_XN(TVector& point, double& TimePoint, double Time2, int& BallNr1, int& BallNr2)

{

TVector RelativeV;

TRay rays;

double MyTime=0.0, Add=Time2/150.0, Timedummy=10000, Timedummy2=-1;

TVector posi;

//Test all balls against eachother in 150 small steps

for (int i=0;i<NrOfBalls-1;i++)

{

for (int j=i+1;j<NrOfBalls;j++)

{

RelativeV=(* GetVel(i))-(* GetVel(j));

rays=TRay((* GetOldPos(i)),TVector::unit(RelativeV));

MyTime=0.0;

 

if ( (rays.dist((* GetOldPos(j)))) > 40) continue;

 

while (MyTime<Time2)

{

MyTime+=Add;

posi=(* GetOldPos(i))+RelativeV*MyTime;

if (posi.dist((* GetOldPos(j)))<=40)

{

point=posi;

if (Timedummy>(MyTime-Add)) Timedummy=MyTime-Add;

BallNr1=i;

BallNr2=j;

break;

}

}

}

}

if (Timedummy!=10000) { TimePoint=Timedummy;

return 1;

}

return 0;

}

 

idle idle_XN, , X4 λ, .

 

( (Start Without Debugging)) , UFOgame . .

 

7. .

 

:

 

void IncreaseUFOnum(void)

{

if( NrOfBalls >= 50)

return;

AppendUFO();

 

GLfloat vv = 5;

(* GetVel(NrOfBalls)) =

TVector(vv *(1.0f - 2.0f *(float)(rand()%100)*0.01f),

vv *(1.0f - 2.0f *(float)(rand()%100)*0.01f),

vv *(1.0f - 2.0f *(float)(rand()%100)*0.01f));

 

GLfloat bLen = aLen - 20;

GLfloat xx = -bLen + 2*bLen*(rand()%100)*0.01f;

GLfloat yy = -bLen + 200 + (2*bLen - 200)*(rand()%100)*0.01f;

GLfloat zz = -bLen + 2*bLen*(rand()%100)*0.01f;

(* GetPos(NrOfBalls))= TVector(xx, yy,zz);

 

NrOfBalls++;

}

void DecreaseUFOnum(void)

{

if(ArrayPos.GetSize() <=1)

return;

TVector * pV = (TVector *)ArrayPos.GetAt(0);

ArrayPos.RemoveAt(0);

delete pV;

 

pV = (TVector *)ArrayVel.GetAt(0);

ArrayVel.RemoveAt(0);

delete pV;

pV = (TVector *)OldPos.GetAt(0);

OldPos.RemoveAt(0);

delete pV;

myColor * pC = (myColor *)m_colorArray.GetAt(0);

m_colorArray.RemoveAt(0);

delete pC;

NrOfBalls --;

if(m_curObj>=NrOfBalls)

m_curObj = 0;

 

}

 

ProcessKeyBoard_XN :

else if (strCmnd == "A") IncreaseUFOnum();

else if (strCmnd == "D") DecreaseUFOnum();

else if (strCmnd == "B") m_bRotate = !m_bRotate;

 

( (Start Without Debugging)) , UFOgame .

 

, , , , . , , , 8, 9, 10 X4 λ , . , , .

 

 

LessonsCode_2010.7z (5.09 Mb)


petrov@msun.ru

PMG  17 2013