Click to See Complete Forum and Search --> : Help with heighmap


Azrul
January 21st, 2007, 01:17 AM
i'm trying to learn to generate terrain using raw file and an openGL function called GL_TRIANGLE_STRIPS. i had compile it but i could not run. it just hang there for a moment and becam not responding..

i also include the attachment..

http://www.codeguru.com/forum/attachment.php?attachmentid=18455&stc=1

can someone have a look and try to comment on the problem...

Zachm
January 21st, 2007, 03:45 PM
I tried to fix your code a bit. There were some, as I believe to be, a typo errors, like putting '=' instead of '+' in the DisplayHeightMap() function.
I've added a gluLookAt() function for the camera, but besides that I haven't done anything. It's probably still not the way you want it, but at least now the DisplayHeightMap() function doesn't hang because of a '=' instead of a '+' :)

The fixed code:


#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <iostream.h>

int mapX;
int mapY;
int mapZ;

const int MapHeight = 384;
const int MapWidth = 384;
GLuint texture[10];

BYTE HeightMap[MapWidth][MapHeight];

GLuint LoadTexture( const char* filename, int width, int height)
{
GLuint texture;
unsigned char* data;
FILE * file;

file = fopen (filename, "rb");
if (file ==NULL) return 0;
data = (unsigned char *)malloc(width * height * 3);
fread (data, width * height * 3, 1, file);
fclose (file);

glGenTextures( 1, &texture );
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D( GL_TEXTURE_2D, 0, 3,
width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, (GLubyte*)data);

gluBuild2DMipmaps(GL_TEXTURE_2D,3,width,height,GL_RGB, GL_UNSIGNED_BYTE,data);
free(data);
return texture;
}

void FreeTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}

void LoadHeightMap (char * Filename, int Width, int Height)
{
int smoothen;

FILE * File;

File = fopen (Filename, "rb");

fread (HeightMap,1, Width * Height * 3, File);

fclose (File);

for (smoothen = 0; smoothen < 3; smoothen ++)
{
for (mapX = 1; mapX < Width; mapX ++)
{
for (mapZ = 1; mapZ < Height * 3; mapZ++)
{
mapY = HeightMap[mapX][mapZ];
mapY += HeightMap[mapX - 1][mapZ];
mapY += HeightMap[mapX + 1][mapZ];
mapY += HeightMap[mapX][mapZ - 1];
mapY += HeightMap[mapX][mapZ + 1];
mapY += HeightMap[mapX - 1][mapZ - 1];
mapY += HeightMap[mapX - 1][mapZ + 1];
mapY += HeightMap[mapX + 1][mapZ - 1];
mapY += HeightMap[mapX + 1][mapZ + 1];
mapY = mapY/9;
HeightMap[mapX][mapZ] = mapY;
}
}
}
}

void DisplayHeightMap (void)
{
int Height;

for (mapX = 0; mapX < MapWidth; mapX +=4)
{
for (mapZ = 0; mapZ < MapHeight; mapZ +=4)
{
glBegin(GL_TRIANGLE_STRIP);
Height = HeightMap[mapX][mapZ];
glTexCoord2f(0,0);
glVertex3f(float(mapX), Height, float (mapZ));

Height = HeightMap[mapX][mapZ + 4];
glTexCoord2f(0,1);
glVertex3f(float(mapX), Height, float(mapZ + 4));

Height = HeightMap[mapX + 4][mapZ];
glTexCoord2f(1,0);
glVertex3f(float(mapX + 4), Height, float(mapZ));

Height = HeightMap[mapX + 4][mapZ + 4];
glTexCoord2f(1,1);
glVertex3f(float(mapX + 4), Height, float (mapZ + 4));
glEnd();
}
}
}

void enable (void)
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
}

void display (void)
{
glClearColor(0.0,0.0,0.0,0.1);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(150,200,-400,150,50,0,0,1,0);
enable();
glColor3f(1,1,1);
glBindTexture(GL_TEXTURE_2D,texture[0]);
DisplayHeightMap();
glutSwapBuffers();
}

void init (void)
{

texture[0] = LoadTexture("height.raw",256,256);
LoadHeightMap("height.raw",128,128);
}

void reshape (int w, int h)
{
glViewport (0,0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective (45, (GLfloat)h/(GLfloat)w, 0.01, 5500.0);
glMatrixMode(GL_MODELVIEW);
}

int main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition (100,100);
glutCreateWindow ("Trial");

init();

glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();

return 0;
}

Azrul
January 22nd, 2007, 07:49 AM
thanx for the help.. at least it get me started with something!!!