Please try this source..
/*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;
}
_____ /____ /|| | || MtY | ||_____|/Marty
GLubyte * Data;
glVertexPointer(3, GL_INT, 0, Data);
Is it OK? Im sorry when thats OK, I havent used vertex arrays yet...
glVertexPointer(3, GL_INT, 0, Data);
Is it OK? Im sorry when thats OK, I havent used vertex arrays yet...
PM Times change...
Excuse my poor english!
I changed all the GLubyte for Data to GLint. It still gives an acess violation 
Thanx anyways...
Marty

Thanx anyways...
Marty
_____ /____ /|| | || MtY | ||_____|/Marty
1. I would change the GL_INT to GL_UNSIGNED_BYTE and leave the GLubyte, but that probably isnt the problem...
2. Where does the acces violation happen?
2. Where does the acces violation happen?
PM Times change...
Excuse my poor english!
Debug or single step the program, then you will see where it happens. And it is probably just going outside on of the arrays boundarys, either because they''re allocated wrong, or because you''re using them wrong.
The violation happenes at 0xC00000005... I tried to debug but it''s a bug in the opengl routine:
glDrawElements(GL_LINE_STRIP, DataSize, GL_UNSIGNED_INT, Data);
so it gives assembly code
glDrawElements(GL_LINE_STRIP, DataSize, GL_UNSIGNED_INT, Data);
so it gives assembly code

_____ /____ /|| | || MtY | ||_____|/Marty
Try this instead:
glDrawElements(GL_LINE_STRIP, DataSize/3, GL_UNSIGNED_INT, Data);
The second parameter is the number of elements to be drawn, so its going over the boundaries of your array.
glDrawElements(GL_LINE_STRIP, DataSize/3, GL_UNSIGNED_INT, Data);
The second parameter is the number of elements to be drawn, so its going over the boundaries of your array.
I tried, but it doesn''t work. If I just put a number for the number of elements, 0 or 1 works. Maybe because it''s just one strip? Still it doesn''t show anything then...
Marty
Marty
_____ /____ /|| | || MtY | ||_____|/Marty
There are not a lot of bugs in OpenGL routines ;*)
You should make sure you use them correctly though...
The 4th parameter in glDrawElements should be an array of *indices* in Data.
In your program, you use the coordinates of your vertices as indices in your vertex array. The result is not that surprising.
If you wanna see if your vertices are correct, just use glDrawArrays.
Then, to render your heightmap, just build an array of indices (that will define the order in which the vertices have to be specified).
Anyway, you must enable GL_VERTEX_ARRAY !!! (edit : oops, just read your code again... You did this already...).
Hope it helps.
[edited by - rodzilla on February 19, 2003 6:06:47 PM]
You should make sure you use them correctly though...
The 4th parameter in glDrawElements should be an array of *indices* in Data.
In your program, you use the coordinates of your vertices as indices in your vertex array. The result is not that surprising.
If you wanna see if your vertices are correct, just use glDrawArrays.
Then, to render your heightmap, just build an array of indices (that will define the order in which the vertices have to be specified).
Anyway, you must enable GL_VERTEX_ARRAY !!! (edit : oops, just read your code again... You did this already...).
Hope it helps.
[edited by - rodzilla on February 19, 2003 6:06:47 PM]
SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)
Your problem is that you want OGL to draw more lines than u have in the array...
1. When Data is GLint, the param must be GL_INT
2. When Data is GLubyte, the param must be GL_UNSIGNED_BYTE
etc.
3. Give us the actual allocation code and the glDrawElemnts code (glVetexPointer too)...
THX
1. When Data is GLint, the param must be GL_INT
2. When Data is GLubyte, the param must be GL_UNSIGNED_BYTE
etc.
3. Give us the actual allocation code and the glDrawElemnts code (glVetexPointer too)...
THX
PM Times change...
Excuse my poor english!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement