Sign in to follow this  
chad_420

Heightmap help

Recommended Posts

chad_420    290
Im working on a simple heightmapped terrain. Cant seem to get it to read more than a 3rd of my image. Also it seems a bunxh of garbage is being drawn. Maybe someone can spot the problem? Here is a screenshot and some code :D initialization
			numverts = (int) (((image->w - 1) * (image->h - 1))* 4 );	
						
			tverts = new vert3f[numverts];
			
			
			int ix = 0, iz = 0;

			int vplace = 0;
			while(vplace < numverts)
			{
				tverts[vplace].x = ix;
				tverts[vplace].y = (float)(getpixel(image, (int)ix, (int)iz) * heightscale);
				tverts[vplace].z = iz;
				vplace++;

				tverts[vplace].x = ix;
				tverts[vplace].y = (float)(getpixel(image, (int)ix, (int)(iz +1)) * heightscale);
				tverts[vplace].z = iz + detail;
				vplace++;

				tverts[vplace].x = ix + detail;
				tverts[vplace].y = (float)(getpixel(image, (int)(ix + 1), (int)(iz + 1)) * heightscale);
				tverts[vplace].z = iz + detail;
				vplace++;

				tverts[vplace].x = ix + detail;
				tverts[vplace].y = (float)(getpixel(image, (int)(ix + 1), (int)iz) * heightscale);
				tverts[vplace].z = iz;
				vplace++;
				
				iz++;
				if(iz > image->w) 
				{
					ix++;
					iz = 0;
				}
			}
			glGenBuffersARB( 1, &VBOVertices );					// Get A Valid Name
				
			glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBOVertices );	
			glBufferDataARB( GL_ARRAY_BUFFER_ARB, numverts *sizeof(float), tverts, GL_STATIC_DRAW_ARB );



per frame
				glClear(GL_COLOR_BUFFER_BIT);
				glClear(GL_DEPTH_BUFFER_BIT);
				glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
				
				glEnableClientState( GL_VERTEX_ARRAY );					// Enable Vertex Arrays		
				glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBOVertices );
				glVertexPointer( scalars_per_vertice, GL_FLOAT, 0, (char *)NULL);			// Set The Vertex Pointer To The Vertex Buffer
			
				glDrawArrays( GL_QUADS, 0, numverts);

				glDisableClientState(GL_VERTEX_ARRAY);
				glPopMatrix();



image of problems

Share this post


Link to post
Share on other sites
Sordith    181
Array indicies go from 0 to length-1. Also, you're drawing a quad at every point along your bitmap's width excluding the last point. Since this is the case, the code:

iz++;
if(iz > image->w)
{
ix++;
iz = 0;
}




is going outside your bounds by two quads. This may work better:

iz++;
if(iz >= image->w-detail)
{
ix++;
iz = 0;
}




You also may want to add detail to ix rather than 1, and check to see if iz+detail will be outside of the bounds of the image.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this