Original Post
/*Here is a small source for a program that generates a landscape, it doesn''t work well... please help.
*/
#include "gl\glut.h"
#include "stdio.h"
#include "math.h"
class Landscape
{
public:
Landscape(char* filename);
~Landscape();
void Draw(int step);
private:
GLint DataSize;
GLubyte * Data;
};
// Global variables:
GLint windW = 800, windH = 800;
const GLint FOV = 45;
Landscape * Scape;
/******** Landscape Constructor & Destructor ********/
/**************** & Draw Function *******************/
Landscape::Landscape(char* filename)
{
Data = NULL;
DataSize = 3*64; //8*8 heightmap x,y,z coordinates
Data = new GLubyte[DataSize];
int i, j;
for(i=0; i<8; i++) // all rows
{
for(j=0; j<8; j++) // all columns
Data[(i*8+j)*3] = j; // 8 per row index + column index times 3 bytes + 0 for x
}
for(i=1; i<8; i++) //all rows
{
for(j=0; j<8; j++) //all columns
Data[(i*8+j)*3 + 1] = j; // 8 per row index + column index times 3 bytes + 1 for y
}
for(i=3; i<8; i++) //all rows
{
for(j=0; j<8; j++) //all columns
Data[(i*8+j)*3 + 2] = sin(i+j); // 8 per row index + column index times 3 bytes + 2 for z
}
}
Landscape::~Landscape() //Destructor
{if (Data != NULL) {delete Data;}} //If there''s data delete it from mem.
void Landscape::Draw(int step)
{
if (Data != NULL)
{
glVertexPointer(3, GL_INT, 0, Data);
// glDrawElements(GL_LINE_STRIP, DataSize, GL_UNSIGNED_INT, Data);
}
}
/******** Initialisation ********/
void Init()
{
glClearColor(0,0,0,0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnableClientState(GL_VERTEX_ARRAY); //Needed for the VertexPointer and DrawElements (Landscape::Draw)
gluPerspective(FOV, 1.0, 1.0, 100.0);
gluLookAt( 0.0, 10.0, 10.0, // eye is at (0,10,10)
0.0, 0.0, 0.0, // center is at (0,0,0)
0.0, 1.0, 0.); // up is in positive Y direction
}
/******** Glut procedures ********/
void Key(unsigned char key, int x, int y)
{
switch (key)
{ case 27: exit(0); }
}
void Idle() {glutPostRedisplay();}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Scape->Draw(3);
glutSwapBuffers();
}
/******** Main ********/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(windW, windH); // windW & windH for possible reshape func (not implemented)
glutCreateWindow("LandScape");
Scape = new Landscape("NoFileName");
Init();
glutKeyboardFunc(Key);
glutIdleFunc(Idle);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}