Advertisement

Drawing a shaded polgon.

Started by March 14, 2003 06:24 PM
3 comments, last by MattS423 21 years, 8 months ago
Ok, each vert has a color, i need to draw the polygon. It needs to be shaded, (i think i''m using gouraud?) but anyway...this source almost works...but for some reason it doesn''t.. The main bugs:
  • Completely horisonial lines (in that curcumstance, there is a divide by 0)
  • color phase outs aren''t working properly (there can be more of a color, but not less) (the color deltas won''t become negitive)
  • drawing stops after the y value reaches LocalVerts[NumVerts].y
any help would be greatly appreciated. Thank you. Here''s the source:
  
int CPolygon::DrawSolid(UINT* Buffer,int Pitch)
{
	
	//re-order the localverts

	//so that LocalVerts[0] is the topmost vert


	//first, find out which vert is the topmost and

	//which is the lowest

	int index = 0;
	int TopVert = 0;
	for(index=0; index <= NumVerts; index++)
	{
		if(Verts[TopVert].y > Verts[index].y)
		{
			//the new top vert is LocalVerts[index]

			TopVert = index;
		}
		

	}

	//ok, now adjust the verts so that TopVert is the top vert


	//create a buffer that will hold the verts for this methon only

	//to keep from messing up the global verts.

	VERTEX2D* LocalVerts = new VERTEX2D[NumVerts];
	
	//copy ''em in!

	for(index = 0; index <= (NumVerts - TopVert - 1); index++)
	{
		//set all the ones after the TopVert

		LocalVerts[index] = Verts[TopVert+index];
	}

	//set all the ones before the TopVert

	for( ; index < NumVerts; index++)
	{
		LocalVerts[index] = Verts[abs(NumVerts - index - TopVert)];
	}

	//find the lowest vert

	int LowVert = 0;
	for(index = 0; index <= NumVerts; index++);
	{
		if(LocalVerts[LowVert].y < LocalVerts[index].y)
		{
			LowVert = index;
		}
	}
	//verts are now in the correct order.


	//ok, so now LocalVerts[0] is the topmost vert.

	
	//draw this sucker

	
	int Height = abs((int)((LocalVerts[0].y - LocalVerts[LowVert].y) +0.5 )) ;

	//both are origionating at LocalVerts[0]

	//Left is aiming for LocalVerts[NumVerts-1]

	//right is aiming for LocalVerts[1]


	//set the verts to which we are aiming.

	int AimToLeft = NumVerts-1;
	int AimToRight = 1;

	int CurrentVertLeft = 0;
	int CurrentVertRight = 0;

	//all legnths are verticle

	double LeftLength = fabs(LocalVerts[CurrentVertLeft].y - LocalVerts[AimToLeft].y);
	double RightLength = fabs(LocalVerts[CurrentVertRight].y - LocalVerts[AimToRight].y);

	//the left deltas

	double dLeftAlpha = (((LocalVerts[AimToLeft].Color - LocalVerts[CurrentVertLeft].Color) >> 24) & 0xFF) / LeftLength;
	double dLeftRed = (((LocalVerts[AimToLeft].Color - LocalVerts[CurrentVertLeft].Color) >> 16) & 0xFF) / LeftLength;
	double dLeftGreen = (((LocalVerts[AimToLeft].Color - LocalVerts[CurrentVertLeft].Color) >> 8) & 0xFF) / LeftLength;
	double dLeftBlue = ((LocalVerts[AimToLeft].Color - LocalVerts[CurrentVertLeft].Color) & 0xFF) / LeftLength;   

	
	double dRightAlpha = (((LocalVerts[AimToRight].Color - LocalVerts[CurrentVertRight].Color) >> 24) & 0xFF) / RightLength;
	double dRightRed = (((LocalVerts[AimToRight].Color - LocalVerts[CurrentVertRight].Color) >> 16) & 0xFF) / RightLength;
	double dRightGreen = (((LocalVerts[AimToRight].Color - LocalVerts[CurrentVertRight].Color) >> 8) & 0xFF) / RightLength;
	double dRightBlue = ((LocalVerts[AimToRight].Color - LocalVerts[CurrentVertRight].Color) & 0xFF) / RightLength;   

	//start ''em on the topmost vert

	double RightAlphaCurr = ((LocalVerts[CurrentVertRight].Color & (255 << 24)) >> 24 );
	double RightRedCurr = ((LocalVerts[CurrentVertRight].Color & (255 << 16)) >> 16 );
	double RightGreenCurr =((LocalVerts[CurrentVertRight].Color & (255 << 8)) >> 8 );
	double RightBlueCurr =(LocalVerts[CurrentVertRight].Color & (255));

	double LeftAlphaCurr = ((LocalVerts[CurrentVertLeft].Color & (255 << 24)) >> 24 );
	double LeftRedCurr = ((LocalVerts[CurrentVertLeft].Color & (255 << 16)) >> 16 );
	double LeftGreenCurr = ((LocalVerts[CurrentVertLeft].Color & (255 << 8)) >> 8 );
	double LeftBlueCurr =(LocalVerts[CurrentVertLeft].Color & (255));

	//ok, now start the loop


	double RightXCurr = (LocalVerts[CurrentVertRight].x + xPos);
	double LeftXCurr = (LocalVerts[CurrentVertLeft].x + xPos);

	double DeltaXYLeft = fabs(((LocalVerts[CurrentVertLeft].x - LocalVerts[AimToLeft].x)/(LocalVerts[CurrentVertLeft].y - LocalVerts[AimToLeft].y)));
	double DeltaXYRight = fabs(((LocalVerts[CurrentVertRight].x - LocalVerts[AimToRight].x)/(LocalVerts[CurrentVertRight].y - LocalVerts[AimToRight].y)));

	int y = (int)LocalVerts[0].y;
	for(; y <= Height; y++)
	{
		DrawShadedLine((int)LeftXCurr,y+yPos,_RGB32BIT((int)LeftAlphaCurr,(int)LeftRedCurr,(int)LeftGreenCurr,(int)LeftBlueCurr),
				(int)RightXCurr,y+yPos,_RGB32BIT((int)RightAlphaCurr,(int)RightRedCurr,(int)RightGreenCurr,(int)RightBlueCurr),
				Buffer,Pitch);
		
		if(LeftXCurr >= (LocalVerts[AimToLeft].x + xPos ))
		{
			
			//We hit the left vert.

			//reclaculate the deltas, and positions for

			//the left side

		
			CurrentVertLeft = AimToLeft;
			AimToLeft--;

			LeftLength = fabs(LocalVerts[CurrentVertLeft].y - LocalVerts[AimToLeft].y);
	
			LeftXCurr = (LocalVerts[CurrentVertLeft].x + xPos);

			dLeftAlpha = (((LocalVerts[AimToLeft].Color - LocalVerts[CurrentVertLeft].Color) >> 24) & 0xFF) / LeftLength;
			dLeftRed = (((LocalVerts[AimToLeft].Color - LocalVerts[CurrentVertLeft].Color) >> 16) & 0xFF) / LeftLength;
			dLeftGreen = (((LocalVerts[AimToLeft].Color - LocalVerts[CurrentVertLeft].Color) >> 8) & 0xFF) / LeftLength;
			dLeftBlue = ((LocalVerts[AimToLeft].Color - LocalVerts[CurrentVertLeft].Color) & 0xFF) / LeftLength;   

			LeftAlphaCurr = ((LocalVerts[CurrentVertLeft].Color & (255 << 24)) >> 24 );
			LeftRedCurr = ((LocalVerts[CurrentVertLeft].Color & (255 << 16)) >> 16 );
			LeftGreenCurr = ((LocalVerts[CurrentVertLeft].Color & (255 << 8)) >> 8 );
			LeftBlueCurr =(LocalVerts[CurrentVertLeft].Color & (255));

			DeltaXYLeft = fabs(((LocalVerts[CurrentVertLeft].x - LocalVerts[AimToLeft].x)/(LocalVerts[CurrentVertLeft].y - LocalVerts[AimToLeft].y)));

			
		}

		if(RightXCurr >= (LocalVerts[AimToRight].x + xPos))
		{
			//We hit the right vert.

			//recalculate the deltas and positions for 

			//the right side


			CurrentVertRight = AimToRight;
			AimToLeft++;

			RightXCurr = (LocalVerts[CurrentVertRight].x + xPos);

			RightLength = fabs(LocalVerts[CurrentVertRight].y - LocalVerts[AimToRight].y);

			dRightAlpha = (((LocalVerts[AimToRight].Color - LocalVerts[CurrentVertRight].Color) >> 24) & 0xFF) / RightLength;
			dRightRed = (((LocalVerts[AimToRight].Color - LocalVerts[CurrentVertRight].Color) >> 16) & 0xFF) / RightLength;
			dRightGreen = (((LocalVerts[AimToRight].Color - LocalVerts[CurrentVertRight].Color) >> 8) & 0xFF) / RightLength;
			dRightBlue = ((LocalVerts[AimToRight].Color - LocalVerts[CurrentVertRight].Color) & 0xFF) / RightLength;   

			RightAlphaCurr = ((LocalVerts[CurrentVertRight].Color & (255 << 24)) >> 24 );
			RightRedCurr = ((LocalVerts[CurrentVertRight].Color & (255 << 16)) >> 16 );
			RightGreenCurr =((LocalVerts[CurrentVertRight].Color & (255 << 8)) >> 8 );
			RightBlueCurr =(LocalVerts[CurrentVertRight].Color & (255));

			
			
			DeltaXYRight = fabs(((LocalVerts[CurrentVertRight].x - LocalVerts[AimToRight].x)/(LocalVerts[CurrentVertRight].y - LocalVerts[AimToRight].y)));


		}

		RightXCurr += DeltaXYRight;
		LeftXCurr += DeltaXYLeft;
	

		LeftAlphaCurr += dLeftAlpha;
		LeftRedCurr += dLeftRed;
		LeftGreenCurr += dLeftGreen;
		LeftBlueCurr += dLeftBlue;

		RightAlphaCurr += dRightAlpha;
		RightRedCurr += dRightRed;
		RightGreenCurr += dRightGreen;
		RightBlueCurr += dRightBlue;

		
		
	}


	//delete the local verts

	delete [] LocalVerts;
	return 1;
}

  
Programmers of the world, UNTIE!
hey bro guess who??!!
Advertisement
any ideas?
Programmers of the world, UNTIE!
DeltaXYLeft = fabs(((LocalVerts[CurrentVertLeft].x - LocalVerts[AimToLeft].x)/(LocalVerts[CurrentVertLeft].y - LocalVerts[AimToLeft].y)));


this will result in a divide by zero if both y's are the same (if these y's are integers). so put an if statement in to check that.

as for the rest of your errors, either post the whole source code so people can debug it, or debug it yourself. sorry, you know it's not very funny to debug someone elses code.

[edited by - morebeer on March 16, 2003 8:16:51 AM]
umm...i did post the whole source code...

but i did debug most of it...thanks for you help....
Programmers of the world, UNTIE!

This topic is closed to new replies.

Advertisement