Heightmap help

Started by
2 comments, last by Sordith 18 years, 10 months ago
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
Advertisement
try Using
glDrawArrays(GL_QUADS, 0, numverts/4);
that cleared up the garbage but it still only make half the image, that heighmap is 50 X 50 but it come out to 25 x 50
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.

This topic is closed to new replies.

Advertisement