[gameprogrammer] Re: terrain geometry calculation problem
- From: Roger D Vargas <luo_hei@xxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Wed, 18 Oct 2006 15:24:52 -0400
The error was in the rendering code. Each line should have a 0, 1 and 2.
The last 3 have 1,1 and 2. Big stupid mistake I made.
Sami Näätänen escribió:
On Tuesday 17 October 2006 18:12, Roger D Vargas wrote:
It is the same. Here is an screenshot of the problem:
http://www.granhotelstgo.cu/x/terrain.jpg
As I said it will only draw the over the edge ones.
You have also a face culling problem.
When the array goes over the edge it will use the next rows first
points.
These are the vertex points in the grid
a,b,c is the first triangle.
The second triangle is b,c,d which it seams to draw on the screen.
When correct shape is triangles e,f,g and f,g,h
from vertices e,f,g,h.
0 1 2 3 4 5 6 7 8 9
0 a
1b c
2d
3
4ef
5gh
6
7
8
9
10
Try to exchange the drawing of vertex 1 and vertex 2 and see what it
gives to you.
Sami Näätänen escribió:
On Monday 16 October 2006 17:02, Roger D Vargas wrote:
Im trying to implement a basic terrain renderer following the one
explained in chapter 7 of Opengl game programming book. But the
result is a weird mesh far from looking like a terrain. Can
somebody see if Im missing something in the following code? Iti
copied from the book with very few modifications.
I use this map for heights
float hm[MAP_Z][MAP_X]={
{0,0,2.0,0,0,0,5.0,0,0,0},
{0,2.0,0,0,0,0,0,0,0,0},
{0,0,0,1.0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{2,0,0,0,0,4.1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{2,0,0,0,0,0,0,0,2.0,0},
{0,0,0,0,2.0,0,0,0,0,0},
{0,0,3.0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,4.0,0,0,0}
};
And here is the code to calculate the geometry
void create_geometry()
{
int z,x;
for (z=0;z<MAP_Z;z++) {
for (x=0;x<MAP_X;x++) {
terrain[x][z][0]=x*ts;
terrain[x][z][1]=hm[z][x];
terrain[x][z][2]=z*ts;
}//for x
}//for z
}
And this is the rendering code
glBindTexture(GL_TEXTURE_2D,grass);
for (z=0;z<=9;z++) {
glBegin(GL_TRIANGLE_STRIP);
for (x=0;x<=9;x++) {
The loops are going too far.
'vertex 1' will use x+1 as x index, which is 10 when the x is 9.
So this will overflow the 0..9 index of the terrain array.
Same happens to the z index in vertices 2 and 3 and to the x index
in vertex 3.
//vertex 0
glColor3f(terrain[x][z][0]/255.0,terrain[x][z][1]/255.0,terrain[x]
[z] [2]/255.0); glTexCoord2f(0.0,0.0);
glVertex3f(terrain[x][z][0],terrain[x][z][1],terrain[x][z][2]);
//vertex 1
glColor3f(terrain[x+1][z][1]/255.0,terrain[x+1][z][1]/255.0,terrai
n[x +1][z][2]/255.0f); glTexCoord2f(1.0,0.0);
glVertex3f(terrain[x+1][z][1],terrain[x+1][z][1],terrain[x+1][z][2
]); //vertex 2
glColor3f(terrain[x][z+1][1]/255.0,terrain[x][z+1][1]/255.0,terrai
n[x ][z+1][2]/255.0); glTexCoord2f(0.0,1.0);
glVertex3f(terrain[x][z+1][1],terrain[x][z+1][1],terrain[x][z+1][2
]); //vertex 3
glColor3f(terrain[x+1][z+1][1]/255.0,terrain[x+1][z+1][1]/255.0,te
rra in[x+1][z+1][2]/255.0); glTexCoord2f(1.0,1.0);
glVertex3f(terrain[x+1][z+1][1],terrain[x+1][z+1][1],terrain[x+1][
z+1 ][2]);
}
glEnd();
}
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
--
http://dsgp.blogspot.com | Linux, programación, juegos
Have no place I can be since I found Serenity
But you can’t take the sky from me
______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: terrain geometry calculation problem
- From: Alan Wolfe
- References:
- [gameprogrammer] terrain geometry calculation problem
- From: Roger D Vargas
- [gameprogrammer] Re: terrain geometry calculation problem
- From: Sami Näätänen
- [gameprogrammer] Re: terrain geometry calculation problem
- From: Roger D Vargas
- [gameprogrammer] Re: terrain geometry calculation problem
- From: Sami Näätänen
Other related posts:
- » [gameprogrammer] terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
- » [gameprogrammer] Re: terrain geometry calculation problem
It is the same. Here is an screenshot of the problem: http://www.granhotelstgo.cu/x/terrain.jpg
As I said it will only draw the over the edge ones. You have also a face culling problem.
When the array goes over the edge it will use the next rows first points.
These are the vertex points in the grid a,b,c is the first triangle. The second triangle is b,c,d which it seams to draw on the screen.
When correct shape is triangles e,f,g and f,g,h from vertices e,f,g,h.
0 1 2 3 4 5 6 7 8 9 0 a 1b c 2d 3 4ef 5gh 6 7 8 9 10
Try to exchange the drawing of vertex 1 and vertex 2 and see what it gives to you.
Sami Näätänen escribió:On Monday 16 October 2006 17:02, Roger D Vargas wrote:Im trying to implement a basic terrain renderer following the one explained in chapter 7 of Opengl game programming book. But the result is a weird mesh far from looking like a terrain. Can somebody see if Im missing something in the following code? Iti copied from the book with very few modifications.
I use this map for heights
float hm[MAP_Z][MAP_X]={ {0,0,2.0,0,0,0,5.0,0,0,0}, {0,2.0,0,0,0,0,0,0,0,0}, {0,0,0,1.0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {2,0,0,0,0,4.1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {2,0,0,0,0,0,0,0,2.0,0}, {0,0,0,0,2.0,0,0,0,0,0}, {0,0,3.0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,4.0,0,0,0} };
And here is the code to calculate the geometry
void create_geometry() { int z,x; for (z=0;z<MAP_Z;z++) { for (x=0;x<MAP_X;x++) { terrain[x][z][0]=x*ts; terrain[x][z][1]=hm[z][x]; terrain[x][z][2]=z*ts; }//for x }//for z }
And this is the rendering code
glBindTexture(GL_TEXTURE_2D,grass); for (z=0;z<=9;z++) { glBegin(GL_TRIANGLE_STRIP); for (x=0;x<=9;x++) {The loops are going too far.
'vertex 1' will use x+1 as x index, which is 10 when the x is 9. So this will overflow the 0..9 index of the terrain array. Same happens to the z index in vertices 2 and 3 and to the x index in vertex 3.
//vertex 0
glColor3f(terrain[x][z][0]/255.0,terrain[x][z][1]/255.0,terrain[x]
[z] [2]/255.0); glTexCoord2f(0.0,0.0);
glVertex3f(terrain[x][z][0],terrain[x][z][1],terrain[x][z][2]);
//vertex 1
glColor3f(terrain[x+1][z][1]/255.0,terrain[x+1][z][1]/255.0,terrai
n[x +1][z][2]/255.0f); glTexCoord2f(1.0,0.0);
glVertex3f(terrain[x+1][z][1],terrain[x+1][z][1],terrain[x+1][z][2 ]); //vertex 2 glColor3f(terrain[x][z+1][1]/255.0,terrain[x][z+1][1]/255.0,terrai n[x ][z+1][2]/255.0); glTexCoord2f(0.0,1.0);
glVertex3f(terrain[x][z+1][1],terrain[x][z+1][1],terrain[x][z+1][2 ]); //vertex 3 glColor3f(terrain[x+1][z+1][1]/255.0,terrain[x+1][z+1][1]/255.0,te rra in[x+1][z+1][2]/255.0); glTexCoord2f(1.0,1.0); glVertex3f(terrain[x+1][z+1][1],terrain[x+1][z+1][1],terrain[x+1][ z+1 ][2]);
} glEnd(); }--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html
--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html
-- http://dsgp.blogspot.com | Linux, programación, juegos Have no place I can be since I found Serenity But you can’t take the sky from me
- [gameprogrammer] Re: terrain geometry calculation problem
- From: Alan Wolfe
- [gameprogrammer] terrain geometry calculation problem
- From: Roger D Vargas
- [gameprogrammer] Re: terrain geometry calculation problem
- From: Sami Näätänen
- [gameprogrammer] Re: terrain geometry calculation problem
- From: Roger D Vargas
- [gameprogrammer] Re: terrain geometry calculation problem
- From: Sami Näätänen