Skip to main content
GameDev.net gamedev.net
🔒 Locked

Please try this source..

Started by Marty666 Feb 19, 2003 at 9:23 AM 9 replies 400+ views
Original Post
Marty666
Marty666
/*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
PM
PM
GLubyte * Data;
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!
Marty666
Marty666
I changed all the GLubyte for Data to GLint. It still gives an acess violation

Thanx anyways...


Marty
_ /__ /|| | || MtY | ||_____|/Marty
PM
PM
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?
PM Times change... Excuse my poor english!
James Trotter
James Trotter
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.
Marty666
Marty666
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
_ /__ /|| | || MtY | ||_____|/Marty
Annon
Annon
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.
Marty666
Marty666
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
_ /__ /|| | || MtY | ||_____|/Marty
rodzilla
rodzilla
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]
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)
PM
PM
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
PM Times change... Excuse my poor english!
Annon
Annon
Ah, I think i found it: The last parameter of glDrawElements isnt the data to render itself, but an array of indices of the data-array-elements that are to be drawn.

Try this:

glDrawArray(GL_LINE_STRIP,0,Datasize/3);

I hope that works now ;-).

Edit: Too late...

[edited by - Annon on February 19, 2003 1:48:18 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.