Jump to content
  • Advertisement
Sign in to follow this  
fredrik90

Terrain rendering

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Ok, thanks. I will test it, I`m sorry I did not explain my problem, that is: I don`t see the terrain that should have been drawn.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!