PDA

View Full Version : Why it doesnt works?? - multiple height maps



lsdi
01-21-2002, 12:57 PM
Why it doesnt works?

I load a .raw file, them I put everything in a matrix, the basecode its correct, I think its some problem in vertices loader, sorry about bad english.

here is the code:





#include "globals.h"
#define ELEVATION 0.01f
#define MAP_SIZE 256
#define ID 256

extern GLuint texture[256];


BYTE g_HeightMap[MAP_SIZE*MAP_SIZE];

GLfloat vertices_xx[ID][5][MAP_SIZE*MAP_SIZE];
GLfloat vertices_xy[ID][5][MAP_SIZE*MAP_SIZE];
GLfloat vertices_xz[ID][5][MAP_SIZE*MAP_SIZE];

// ID ITS THE ID OF THE HEIGHT MAP
// 5 ITS THE 0..6 VERTICES TO MAKE TO MAKE TWO TRIANGLES

GLfloat Height(BYTE *pHeightMap, int X, int Y)
{
int x = X % MAP_SIZE;
int y = Y % MAP_SIZE;
if(!pHeightMap) return 0;
return pHeightMap[x + (y * MAP_SIZE)];
} // THIS WORKS 100%

void LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap)
{
FILE *pFile = NULL;
pFile = fopen( strName, "rb" );
if ( pFile == NULL )
{
MessageBox(NULL, "Mapa nao encontrado.", "Erro", MB_OK);
return;
}
fread( pHeightMap, 1, nSize, pFile );
int result = ferror( pFile );
if (result)
{
MessageBox(NULL, "Erro ao carregar Mapa de terreno.", "Erro", MB_OK);
}
fclose(pFile);
} // THIS WORKS 100%


/* PROBLEM STARTS HERE, I GUESS...*/
void buildVertex(BYTE pHeightMap[],GLint STEP_SIZE,int QUAD_ID)
{
int X=0,Y=0;
int looper;
if(!pHeightMap) return;
for ( X = 0; X < MAP_SIZE; X += STEP_SIZE ) {
for ( Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )
{
vertices_x[QUAD_ID][0][looper]=X;
vertices_y[QUAD_ID][0][looper]=Height(pHeightMap, X, Y )*ELEVATION;
vertices_z[QUAD_ID][0][looper]=Y;

vertices_x[QUAD_ID][1][looper]=X;
vertices_y[QUAD_ID][1][looper]=Height(pHeightMap, X, Y + STEP_SIZE)*ELEVATION;
vertices_z[QUAD_ID][1][looper]=Y + STEP_SIZE;

vertices_x[QUAD_ID][2][looper]=X + STEP_SIZE;
vertices_y[QUAD_ID][2][looper]=Height(pHeightMap, X + STEP_SIZE, Y+ STEP_SIZE) *ELEVATION;
vertices_z[QUAD_ID][2][looper]=Y + STEP_SIZE;

vertices_x[QUAD_ID][3][looper]=X + STEP_SIZE;
vertices_y[QUAD_ID][3][looper]=Height(pHeightMap, X + STEP_SIZE, Y )*ELEVATION;
vertices_z[QUAD_ID][3][looper]=Y;

vertices_x[QUAD_ID][4][looper]=X;
vertices_y[QUAD_ID][4][looper]=Height(pHeightMap, X, Y )*ELEVATION;
vertices_z[QUAD_ID][4][looper]=Y;

vertices_x[QUAD_ID][5][looper]=X + STEP_SIZE;
vertices_y[QUAD_ID][5][looper]=Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE) *ELEVATION;
vertices_z[QUAD_ID][5][looper]=Y + STEP_SIZE;

looper++;
}
}
}

void drawQuad(int QUAD_ID,int TEX_ID,int AUTO_GEN,int START_POSIx,int START_POSIz)
{
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texture[TEX_ID]);
if(AUTO_GEN==1) {
GLfloat sPlane[] = {1.0/MAP_SIZE, 0, 0, 0};
GLfloat tPlane[] = {0, 0, 1.0/MAP_SIZE, 0};
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, sPlane);
glTexGenfv(GL_T, GL_OBJECT_PLANE, tPlane);
}
glTranslatef(START_POSIx,0,START_POSIz);
glBegin(GL_TRIANGLES);
int i;
for(i=0;i<=MAP_SIZE*MAP_SIZE;i++) {
glTexCoord2f(0,0); glVertex3f(vertices_x[QUAD_ID][0][i],vertices_y[QUAD_ID][0][i],vertices_z[QUAD_ID][0][i]);
glTexCoord2f(1,0); glVertex3f(vertices_x[QUAD_ID][1][i],vertices_y[QUAD_ID][1][i],vertices_z[QUAD_ID][1][i]);
glTexCoord2f(1,1); glVertex3f(vertices_x[QUAD_ID][2][i],vertices_y[QUAD_ID][2][i],vertices_z[QUAD_ID][2][i]);
glTexCoord2f(0,1); glVertex3f(vertices_x[QUAD_ID][3][i],vertices_y[QUAD_ID][3][i],vertices_z[QUAD_ID][3][i]);
glTexCoord2f(0,0); glVertex3f(vertices_x[QUAD_ID][4][i],vertices_y[QUAD_ID][4][i],vertices_z[QUAD_ID][4][i]);
glTexCoord2f(1,1); glVertex3f(vertices_x[QUAD_ID][5][i],vertices_y[QUAD_ID][5][i],vertices_z[QUAD_ID][5][i]);
}
glEnd();
glPopMatrix();
}

void loadQuad(LPSTR strName,int QUAD_ID)
{
LoadRawFile(strName, MAP_SIZE * MAP_SIZE, g_HeightMap);
buildVertex(g_HeightMap,15,QUAD_ID);
}

void loadQuads(void)
{
loadQuad("hmap/Terrain.raw",1);
}

void drawQuads(void)
{
drawQuad(1,3,1,0,0);
}

Lyr
01-21-2002, 02:21 PM
GLfloat vertices_xx[ID][5][MAP_SIZE*MAP_SIZE];
GLfloat vertices_xy[ID][5][MAP_SIZE*MAP_SIZE];
GLfloat vertices_xz[ID][5][MAP_SIZE*MAP_SIZE];
// ID ITS THE ID OF THE HEIGHT MAP
// 5 ITS THE 0..6 VERTICES TO MAKE TO MAKE TWO TRIANGLES


If you want an array where you can use array element 0,1,2,3,4 and 5 you have to set the array size to 6, not to 5.




GLfloat vertices_xx[ID][6][MAP_SIZE*MAP_SIZE];
GLfloat vertices_xy[ID][6][MAP_SIZE*MAP_SIZE];
GLfloat vertices_xz[ID][6][MAP_SIZE*MAP_SIZE];


btw I would suggest to use a triangle strip.


hope this helps

[This message has been edited by Mexx (edited 01-21-2002).]