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

Terrain rendering

Started by fredrik90 Aug 24, 2005 at 9:08 AM 6 replies 1.5k views
Original Post
fredrik90
fredrik90
Hi Can anyone see any errors in this code?
	
Terrain_List = glGenLists(1);

	glEnable(GL_TEXTURE_2D);
 
	glNewList(Terrain_List, GL_COMPILE_AND_EXECUTE);
		glBegin(GL_TRIANGLE_STRIP);	

			glEnable(GL_TEXTURE_2D); 

			glBindTexture(GL_TEXTURE_2D, texture1->ID);

			for(int x = 0; x > SizeX -1; x++)
			{
				for(int y = 0; y > SizeY - 1; y++)
				{
					glColor3d(0.2, 0.3, 0.5);
					glNormal3f((GLfloat) normal[x][y].x,(GLfloat) normal[x][y].y, (GLfloat) normal[x][y].z);
					glTexCoord2d((GLdouble) x / SizeX * x, y / SizeY * y);
					glVertex3f((GLfloat) Vertex[x][y].x * Scale, (GLfloat) Vertex[x][y].y * Scale,(GLfloat) Vertex[x][y].z * Scale);
				}
			}
		glEnd();
	glEndList();

Thanks, Fredrik
Flight-Real leader
Madhed
Madhed
Hi there, it would be helpful to us if you posted what specific error you get.
tags for source code are "source" not "code"

*edit*

I'm not used to OpenGL but it seems that you're drawing triangle strips, so you can't just throw every vertex at the gfx card in the order of the array.

You have to make sure that the order is correct for a tri-strip.
Hm... don't know how to explain...

this link is for direct3D but i'm sure the information also counts for OpenGL

cheers
Basiror
Basiror
you bind the texture within a glbegin glend call you should do this before the glbegin call
http://www.8ung.at/basiror/theironcross.html
fredrik90
fredrik90
Ok, thanks. I will test it, Im sorry I did not explain my problem, that is: I dont see the terrain that should have been drawn.
Flight-Real leader
screwtape
screwtape
your vertex specifciation is incorrect (as Madhed tried to explain).

you need to specify multiple vertices per inner loop .. as it is you specify lines, not triangles .. so it renders nothing because you have no triangles in your drawing code.

try somehting more like this (note it may vary -slightly- depending on how you have your terrain data set up), but this should give you a good head-start towards the solution. alternatively you can try drawing each triangle ... using three 3 glVertex3f calls per inner loop.

 Terrain_List = glGenLists(1); glEnable(GL_TEXTURE_2D); glNewList(Terrain_List, GL_COMPILE_AND_EXECUTE); glBegin(GL_TRIANGLE_STRIP);	 glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture1->ID); for(int x = 0; x < SizeX -1; x++) {  for(int y = 0; y < SizeY - 2; y++) {       // spec vertex <x, y>    glColor3d(0.2, 0.3, 0.5);    glNormal3f((GLfloat) normal[x][y].x,(GLfloat) normal[x][y].y, (GLfloat) normal[x][y].z);    glTexCoord2d((GLdouble) x / SizeX * x, y / SizeY * y);    glVertex3f((GLfloat) Vertex[x][y].x * Scale, (GLfloat) Vertex[x][y].y * Scale,(GLfloat) Vertex[x][y].z * Scale);      int y1 = y+1; //this is y 'plus' 1 .. d@mn filter    // spec vertex <x, y+1>    glColor3d(0.2, 0.3, 0.5);    glNormal3f((GLfloat) normal[x][y1].x,(GLfloat) normal[x][y1].y, (GLfloat) normal[x][y1].z);    glTexCoord2d((GLdouble) x / SizeX * x, y1 / SizeY * y1);    glVertex3f((GLfloat) Vertex[x][y1].x * Scale, (GLfloat) Vertex[x][y1].y * Scale,(GLfloat) Vertex[x][y1].z * Scale);    // this may really be incorrect depending on your dataset-setup    if(y1 == (SizeY - 2))    {      // add a degenerate triangle(s)      // last vert of this row      glVertex3f((GLfloat) Vertex[x][y1].x * Scale, (GLfloat) Vertex[x][y1].y * Scale,(GLfloat) Vertex[x][y1].z * Scale);      // first vert of next row      glVertex3f((GLfloat) Vertex[x+1][0].x * Scale, (GLfloat) Vertex[x+1][0].y * Scale,(GLfloat) Vertex[x+1][0].z * Scale);    }  } } glEnd(); glEndList();


sorry if i made any typos here.


i would also recommend once you figure this out that you try packing all of this info into vertex arrays and vbos ... it is the preferred method for static terrains (or so I have been told).
fredrik90
fredrik90
ok thanks, I thought it just added all in the list.
will test that, thank you.
Flight-Real leader
fredrik90
fredrik90
Check out this code.

	calculate_normal();	Terrain_List = glGenLists(1);	glEnable(GL_TEXTURE_2D); 	glNewList(Terrain_List, GL_COMPILE_AND_EXECUTE);		glBindTexture(GL_TEXTURE_2D, texture1->ID);		glBegin(GL_QUADS);				glEnable(GL_TEXTURE_2D); 			for(int x = 0; x > SizeX -1; x = x+4)			{				for(int y = 0; y > SizeY - 1; y = y+4)				{					glNormal3f((GLfloat) normal[x][y].x,(GLfloat) normal[x][y].y, (GLfloat) normal[x][y].z);					glTexCoord2d((GLdouble) x / SizeX * x, y / SizeY * y);					glVertex3f((GLfloat) Vertex[x][y].x * Scale, (GLfloat) Vertex[x][y].y * Scale,(GLfloat) Vertex[x][y].z * Scale);										glNormal3f((GLfloat) normal[x+1][y+1].x,(GLfloat) normal[x+1][y+1].y, (GLfloat) normal[x+1][y+1].z);					glTexCoord2d((GLdouble) x+1 / SizeX * x+1, y+1 / SizeY * y+1);					glVertex3f((GLfloat) Vertex[x+1][y+1].x * Scale, (GLfloat) Vertex[x+1][y+1].y * Scale,(GLfloat) Vertex[x+1][y+1].z * Scale);										glNormal3f((GLfloat) normal[x+2][y+2].x,(GLfloat) normal[x+2][y+2].y, (GLfloat) normal[x+2][y+2].z);					glTexCoord2d((GLdouble) x+2 / SizeX * x+2, y+2 / SizeY * y+2);					glVertex3f((GLfloat) Vertex[x+2][y+2].x * Scale, (GLfloat) Vertex[x+2][y+2].y * Scale,(GLfloat) Vertex[x+2][y+2].z * Scale);										glNormal3f((GLfloat) normal[x+3][y+3].x,(GLfloat) normal[x+3][y+3].y, (GLfloat) normal[x+3][y+3].z);					glTexCoord2d((GLdouble) x+3 / SizeX * x+3, y+3 / SizeY * y+3);					glVertex3f((GLfloat) Vertex[x+3][y+3].x * Scale, (GLfloat) Vertex[x+3][y+3].y * Scale,(GLfloat) Vertex[x+3][y+3].z * Scale);				}			}		glEnd();	glEndList();


thanks for reply.
Flight-Real leader
RichardS
RichardS
Remove the glEnable from the glBegin/glEnd pair. That will likely cause problems.

Secondly, just to make sure that your source data is good, disable texturing, set the color to something bright, like red, and draw GL_POINTS instead of GL_TRIANGLE_STRIP. You should see your hightmap in point form. While this isn't a final solution, it's an excellent debugging step.

Topic Locked

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

Sign in to reply to this topic.