Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Help me strangle this bug...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 TerranFury   Members   -  Reputation: 142

Like
Likes
Like

Posted 08 August 2001 - 03:36 PM

I am trying to triangulate a heightmap in a uniform grid using multiple vertex lists of triangle strips. It was working, but then I tried to make the code better by eliminating my global variables. Now, I'm geting incomprehensible triangle "junk" on the screen (I don't THINK there's a memory leak; otherwise I'd be getting Access Violation errors, wouldn't I?). Most of it is blue and green (my color list only specifies shades of blue and green) but some some is magenta or white (how'd THAT get there?), and when I draw GL_LINE_STRIPs and disable color lists instead of colored GL_TRIANGLE_STRIPs, whole areas of the screen are pure white - filled with lines. I know it's a bit much to ask to post all this code up here, but I, for some reason, just can't catch my mistake. If anyone could help me, I would be very appreciative: To begin, I do the following:
      
terrain = maketerrain(10, 10, 0.75, 1.25)
  
The associated code is below
  
struct terr_struct
{
	GLfloat *** verts;
	GLfloat *** cols;
	unsigned long width;
	unsigned long height;
	unsigned long vert_num;
};
terr_struct terrain;



double cos_interpol(float a, float b, float x)
{
	//Why is this here?  So I can triangulate my mesh

	//in multiple resolutions, eventually


	float ft = x * pi;
	float f = (1 - cos(ft)) * .5;
	
	return  a * (1-f) + b * f;
}



void genmap(unsigned char ** maparry, unsigned long width, unsigned long height)
{
	for(int y = 0; y < height + 3; y++)
	{
		for(int x = 0; x < width + 3; x++)
		{
			maparry[x][y] = rand() % 256;
		}
	}
}

float map(double x, double y, unsigned char ** maparry)
{
	float ans;
	float x1, x2;
	float xfactor = (double)x - (double)((long)x);
	float yfactor = (double)y - (double)((long)y);

	x1 = cos_interpol(maparry[(long)x][(long)y], maparry[(long)x][(long)y + 1], xfactor);
	x1 = cos_interpol(maparry[(long)x + 1][(long)y], maparry[(long)x +1][(long)y + 1], xfactor);
	ans = x1 * (1 - yfactor) + x2 * yfactor;

	return (float)ans / (float)255;
}


void makecols(unsigned char ** maparry, GLfloat *** a_cols)
{
	char a = 0;

	for(int y = 0; y < 10 - 1; y++)
	{
		for(int x = 0; x < 20; x++)
		{
			if(x%2 == 0){a=0;}else{a=1;}

			//Dumb place-filler function that looks sort of like

			//blue water and green hills.

			a_cols[y][x][0] = 0;
			a_cols[y][x][1] = pow((map(x/2, y + a, maparry))/(float)2, 2) * 2;//a_verts[y][x][1];

			a_cols[y][x][2] = pow((1 - map(x/2, y + a, maparry))/(float)2, 2) * 2;//a_verts[y][x][1];

		}
	}
}

unsigned long makegrid(float tilesize, float yscale, unsigned char ** maparry, GLfloat *** a_verts, unsigned long width, unsigned long height)
{
	unsigned long vnum = 0;
	unsigned long total_vnum = 0;

	for(long y = 0; y < height - 1; y++)
	{
		for(long x = 0; x < width; x++)
		{
			a_verts[y][vnum][0] = (x - (float)(width / (float)2)) * (float)tilesize;
			a_verts[y][vnum][2] = (y - (float)(height / (float)2)) * (float)tilesize;
			a_verts[y][vnum][1] = map(x, y, maparry) * yscale;
			vnum++;
			total_vnum++;

			a_verts[y][vnum][0] = (x - (float)(width / (float)2)) * (float)tilesize;
			a_verts[y][vnum][2] = ((y + 1) - (float)(height / (float)2)) * (float)tilesize;
			a_verts[y][vnum][1] = map(x, (y+1), maparry) * yscale;
			vnum++;
			total_vnum++;
		}
		vnum = 0;
	}

	return total_vnum;
}

terr_struct maketerrain(unsigned long width, unsigned long height, float tilesize, float yscale)
{
	unsigned char ** terr_map;
	GLfloat *** verts;
	GLfloat *** cols;
	terr_struct ret_val;
	
	terr_map = new unsigned char * [width + 3];
	for(unsigned long i = 0; i < width + 3; i++)
	{
		terr_map[i] = new unsigned char[height + 3];
	}

	verts = new GLfloat ** [height - 1];
	for(unsigned long y = 0; y < height - 1; y++)
	{
		verts[y] = new GLfloat * [width * 2];
		for(unsigned long x = 0; x < width * 2; x++)
		{
			verts[y][x] = new GLfloat[3];
		}
	}

	cols = new GLfloat ** [height - 1];
	for(y = 0; y < height - 1; y++)
	{
		cols[y] = new GLfloat * [width * 2];
		for(unsigned long x = 0; x < width * 2; x++)
		{
			cols[y][x] = new GLfloat[3];
		}
	}

	genmap(terr_map, width, height);
	ret_val.vert_num = makegrid(tilesize, yscale, terr_map, verts, width, height);
	makecols(terr_map, cols);

	for(i = 0; i < height + 3; i++)
	{
		delete [] terr_map[i];
	}
	delete [] terr_map;

	ret_val.cols = cols;
	ret_val.verts = verts;
	ret_val.width = width;
	ret_val.height = height;

	return ret_val;
}
  
Then I try to draw it each frame as follows:
  
for(unsigned long i = 0; i < terrain.height - 1; i++)
{
		glVertexPointer(3 , GL_FLOAT, 0, terrain.verts[i] );
		glColorPointer(3, GL_FLOAT, 0, terrain.cols[i]); 
		glDrawArrays(GL_TRIANGLE_STRIP, 0, terrain.vert_num);
}
  
So there you have it. Thanks in advance for any help. Edited by - TerranFury on August 8, 2001 10:59:36 PM

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS