Skip to main content
gamedev.net
🔒 Locked

Whats wrong with this code...

Started by BradDaBug May 31, 2003 at 6:41 PM 10 replies 1.8k views
Original Post
BradDaBug
BradDaBug
I hate doing this, but I cannot figure out why this code is messing up. It looks ok in release mode, but turn on debug mode and it draws all sorts of crazy triangles everywhere. Here's what I mean: The terrain 256 X 256 verticies split up into blocks of 16 X 16. Here's the block setup:
int CTerrain::SetupTerrainBlocks(void)
{
	unsigned int currentVertex, numBlockPolys;
	
	numBlockPolys = BLOCK_SIZE - 1;	// Always 1 less polygon for n vertices

	
	// Loop across each terrain block

	for (int z = 0; z < mapheight / numBlockPolys; z++)
		for (int x = 0; x < mapwidth / numBlockPolys; x++)
		{
			mapblocks[x][z] = new TerrainBlock;
			int index = 0;

			// Setup the indexing info for this map block

			for (int z2 = 0; z2 < BLOCK_SIZE -1; z2++)
				for (int x2 = 0; x2 < BLOCK_SIZE; x2++)
				{
					// Move to the correct block

					currentVertex = z * numBlockPolys * mapwidth + x * numBlockPolys;
					// Move to the correct offset within the block

					currentVertex += z2 * mapwidth + x2;
			
					mapblocks[x][z]->indexArray[0][index++] = currentVertex + mapwidth;
					mapblocks[x][z]->indexArray[0][index++] = currentVertex;
				}
		}
	
	return(OK);
}
Here's the rendering code:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, terrain);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
glColorPointer(3, GL_FLOAT, 0, colors);

for (int z = 0; z < mapheight / (BLOCK_SIZE - 1); z++)
for (int x = 0; x < mapwidth / (BLOCK_SIZE -1); x++)
DrawTerrainBlock(mapblocks[x][z]);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
And here's the DrawTerrainBlock() function:
int CTerrain::DrawTerrainBlock(TerrainBlock *block)
{
	// Do frustum culling!

	if (!CheckBlockVisibility(block))
		return(OK);

	int mipmaplevel = 0;
	int mipmapHeight, mipmapWidth;
	
	// Set the size of the terrain block depending on the mipmap level

	mipmapHeight = (BLOCK_SIZE -1) / pow(2, mipmaplevel) + 1;
	mipmapWidth = mipmapHeight;
	
	glEnable(GL_CULL_FACE);
	
	glPolygonMode(GL_FRONT, GL_LINE);
	
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
	
	unsigned int tex = GetEngine()->GetGraphics()->GetTextureManager()->GetSurface(TEX_GRASS_1);
	glBindTexture(GL_TEXTURE_2D, tex);
	
	for (int z = 0; z < mipmapHeight - 1; z++)
		glDrawElements(GL_TRIANGLE_STRIP, mipmapWidth * 2, GL_UNSIGNED_INT, 
						&block->indexArray[mipmaplevel][z * mipmapWidth * 2]);
	
	glPolygonMode(GL_FRONT, GL_FILL);
		
	return(0);
}
I'd REALLY appreciate any insight into this. EDIT: Changed some of the code to make it more human friendly. BTW, why wont the source tags STAY PUT during each edit? It gets rid of all but the first opening tag and the last closing tag! Edit some more: I think there are a few bugs in the Edit thingy. It deleted some of my code when I tried to edit! [edited by - BradDaBug on May 31, 2003 12:52:11 AM]
I like the DARK layout!
Alan Kemp
Alan Kemp
The first thing that looks out of place is in your block setup code:


for (int x2 = 0; x2 < 17; x2++)


17? Shouldn''t that be 16?

Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Peon
Peon
And easier to change, definitely. Edit->Find/Replace is really a brute force method to do things, and it won''t always work. I always try and use variables for "magic numbers"
Peon
BradDaBug
BradDaBug
OK, I''ve edited my original code to be a little bit more readable. And BLOCK_SIZE is defined as 17, btw. I forgot to add it but I don''t wanna edit the original post AGAIN because of all the problems I''m having editing it.
I like the DARK layout!
Basiror
Basiror
quote:
Original post by Peon
And easier to change, definitely. Edit->Find/Replace is really a brute force method to do things, and it won''t always work. I always try and use variables for "magic numbers"


using variables for everything isn t good either


use macros
#define BLAHBLAH
http://www.8ung.at/basiror/theironcross.html
AikonIV
AikonIV
No don''t use macros. Use consted variables.

I''m afraid I can''t really help with your problem though...
BradDaBug
BradDaBug
What''s wrong with #define''s?

After some more debugging, I''ve figured out that the problem is that the index arrays of last edge of the last row of terrain blocks have invalid data. It''s looking outside the vertex array! But now I''ve got to figure out how to fix it. It works fine for EVERY other terrain block, so I don''t get why it''d mess up on the last edge.
I like the DARK layout!
31337
31337
How did you bust out that sweet looking text? Is this GL?
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
Beer Hunter
Beer Hunter
quote:
Original post by BradDaBug
What''s wrong with #define''s?
This.
valles
valles
I don''t use open gl, but I have questions.

for (int x2 = 0; x2 < BLOCK_SIZE; x2++) // Should be BLOCK_SIZE-1

Better documentation would help explain currentVertex, I''m guessing you''re enumerating each of the vertexes, though I can''t figure the purpose.

currentVertex = x * pow(BLOCKSIZE, 3) + x2 * pow(BLOCKSIZE,2) + z * BLOCK_SIZE + z2;

For the rendering I would suggest fixing the -1 and adding parentheses:
for (int z = 0; z < mapheight / BLOCK_SIZE; z++)
{
for (int x = 0; x < mapwidth / BLOCK_SIZE; x++)
{
DrawTerrainBlock(mapblocks[x][z]);
}
}

As for your DrawTerrainBlock, I don''t understand why you''re implementing mipmapping at this point when you can''t get the basic code to work. Also, for my code I''m still plotting verticies manually, so I really don''t understand:

glDrawElements(GL_TRIANGLE_STRIP, mipmapWidth * 2, GL_UNSIGNED_INT, &block->indexArray[mipmaplevel][z * mipmapWidth * 2]);

Back in :

mapblocks[x][z]->indexArray[0][index++] = currentVertex;

We should have been assigning each of these blocks a set of at least four verticies, or two verticies and a size setting.

I could forsee you setting:
mapblocks[x][z]->indexArray[0][index++] = the x coordinate
mapblocks[x][z]->indexArray[0][index++] = the y coordinate

Also note: when you begin the first loop of this index++ you skip index == 0 and go straight to index == 1;

Anyway, that''s the only way I could figure it.

My best guess from the image would say that whichever corner that is, your indexes are screwed, it would be important to note wether that corner is the final square or the first square, because a final square could point to you drawing lines past the end of your stack and conversion errors between INT and FLOATs.

- Valles
BradDaBug
BradDaBug
quote:
Original post by 31337
How did you bust out that sweet looking text? Is this GL?

Yah, its OpenGL. Each letter is a textured quad. I print the text using a PrintText() function that then uses a blt() function I wrote) for each letter. then blt() translates those screen coords into 3D coords using gluUnProject().

BTW, my code is based on the landscape rendering code from OpenGL Game Programming.

The terrain for the WHOLE landscape is stored in a vertex array. Then I''m splitting up the terrain into blocks (later to be used for geomipmapping, and currently for frustum culling) and each block has an index array (used for those calls to glDrawElements()) that lists the indexes of the verticies in that block. currentVertex is the index of the vertex in the vertex array.
I like the DARK layout!

Topic Locked

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

Sign in to reply to this topic.