www.pudn.com > ÈýάÌì¿ÕÓëµØÐÎ.zip > Terrain.cpp, change:2001-10-15,size:3134b
// SkyDome Demo - October 2001
//
// Luis R. Sempé
// visual@spheregames.com
// Sphere Games (http://www.spheregames.com)
//
// You may use, copy, distribute or create derivative software
// for any purpose. If you do, I'd appreciate it if you write me
// and let me know what you do with it. Thanks!
// Have fun!
////////////////////////////////////////////////////
#include <math.h>
#include "globals.h"
#include "Terrain.h"
// Terrain vertices
VERTEX *Terrain;
int NumTerrainVertices;
// Terrain indices
WORD *TerrainIndices;
int NumTerrainIndices;
int size = 1024;
int res = 8;
int hTile = 4;
int vTile = 4;
// This is a slightly modfied version of the skyplane code
// used for rendering the terrain
void GenerateTerrain()
{
if (Terrain)
{
delete Terrain;
Terrain = NULL;
}
int divs = res;
if (divs < 1)
divs = 1;
if (divs > 256)
divs = 256;
NumTerrainVertices = (divs + 1) * (divs + 1); // 1 division would give 4 verts
NumTerrainIndices = divs * divs * 2 * 3; // 1 division would give 6 indices for 2 tris
Terrain = new VERTEX[NumTerrainVertices];
ZeroMemory(Terrain, sizeof(VERTEX));
TerrainIndices = new WORD[NumTerrainIndices];
int num_divisions = (int)sqrt(NumTerrainIndices/6);
float plane_size = (float)size;
float delta = plane_size/(float)num_divisions;
float tex_delta = 2.0f/(float)num_divisions;
float x_dist = 0.0f;
float z_dist = 0.0f;
// Calculate the vertices for the terrain
int count = 0;
VERTEX SV;
for (int i=0;i<=num_divisions;i++)
{
for (int j=0;j<=num_divisions;j++)
{
x_dist = (-0.5f * plane_size) + ((float)j*delta);
z_dist = (-0.5f * plane_size) + ((float)i*delta);
SV.x = x_dist;
SV.y = (float)(5*((cosf(x_dist)*1.5f) + (sinf(z_dist) * 1.5f) + rand()%10));
SV.z = z_dist;
SV.u = hTile*((float)j * tex_delta*0.5f);
SV.v = vTile*(1.0f - (float)i * tex_delta*0.5f);
Terrain[i*(num_divisions+1)+j] = SV;
}
}
// Calculate indices for terrain plane
int index = 0;
for (i=0;i<num_divisions;i++)
{
for (int j=0;j<num_divisions;j++)
{
int startvert = (i*(num_divisions+1) + j);
// tri 1
TerrainIndices[index++] = startvert;
TerrainIndices[index++] = startvert+1;
TerrainIndices[index++] = startvert+num_divisions+1;
// tri 2
TerrainIndices[index++] = startvert+1;
TerrainIndices[index++] = startvert+num_divisions+2;
TerrainIndices[index++] = startvert+num_divisions+1;
}
}
}
int RenderTerrain()
{
glTranslatef(0.0f,0.0f, 0.0f);
glRotatef(0,0,0,0);
glBegin(GL_TRIANGLES);
for (int i=0; i < NumTerrainIndices; i++)
{
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(Terrain[TerrainIndices[i]].u, Terrain[TerrainIndices[i]].v);
glVertex3f(Terrain[TerrainIndices[i]].x, Terrain[TerrainIndices[i]].y, Terrain[TerrainIndices[i]].z);
}
glEnd();
return 1;
}
void ReleaseTerrain()
{
if (Terrain)
{
delete Terrain;
Terrain = NULL;
}
if (TerrainIndices)
{
delete TerrainIndices;
TerrainIndices = NULL;
}
}