Sign in to follow this  

Rects, bounding boxes and filling

This topic is 1521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!

 

So I've gotten this assignement to combine an array of trianglesinto a image of a teapot. 

I also have to translate, scale, calculate bounding box of the triangle and fill it with a color.

 

I have gotten so far that the translation of the coords and scale is ok. The teapot is being drawn but im getting some difficulty while calculating the bounding boxes and also filling the triangle with a color. The program window is made with SDL.

 

The filling of the triangles. 

The triangles are a struct, and a total of 1092 are drawn on the screen, each with its specific color.

Now im getting to understand that i need to travers the pixels on the screen, and then looking for their wireframe color which has a rgb value of 187. 

 

What i have done is 

SDL_Color color;
Uint32 pixel = 0;
int x = 0, y = 0;
	
        pixel = GetPixel(screen,x,y);
	SDL_GetRGB(pixel,screen->format,&color.r,&color.g,&color.b);

I have made a for loop which traverses the entire screen in x and y and looking for pixels with the wireframe color, but im doing something wrong since my program just freezes. Think its running out of memory. I must be doing something wrong in my for loop. 

 

What I think i need to do:

for(x = 0; x < screen->w; x++){
 
   if(color.r == 187){ // if i find a pixel with the wireframe color
     while(color.r == 187){ //do this until i get 1 pixel after the red, so im inside the //triangle.
   
    x++; // increase x coord.
    pixel = GetPixel(screen,x,y);
    SDL_GetRGB(pixel,screen->format,&color.r,&color.g,&color.b);//get color from new pixel
}
  SetPixel(screen,x,y,triangle->fillcolor);
  if(x == screen->w){
   x = 0;
   y++;
}
}
}

Im seeing that this is a infinite loop though. Still it doestn color anything properly. 

 

Any help would be appreciated : ) ps. Its C.

Edited by nichos

Share this post


Link to post
Share on other sites

If I understand it correctly, you already have the wireframe rectanbles on screen and you want to fill them with colors? Are they screen aligned?
Few issues with your second code block:
1. You don't check that y<height.
2. You don't handle corner cases correctly, you will sometime draw pixels out of the wireframe bounds:
    a. Imagine a screen aligned rectangle Your code fails for the top and bottom lines. By the end of the while loop, you are one pixel after the top-right corner of the rect.
    b. For non-screen-aligned-rects, procssing lines with corners of rects will result in OOB behavior as well, since those lines contain a single pixel.
 

Can you give better explanation of what you are trying to do?

Share this post


Link to post
Share on other sites

Sorry i mixed up rectangles and triangles. I need to fill triangles and not rectangles. 

Yes I have a wireframe of the triangles drawn on screen and want to fill them with color.  I changed my code around a little bit by something i found online and altered it to match my code.

int x = 0, y = 0;

	
	int minx = minimum(triangle->sx1,triangle->sx2,triangle->sx3);
	int maxx = maximum(triangle->sx1,triangle->sx2,triangle->sx3);
	int miny = minimum(triangle->sy1,triangle->sy2,triangle->sy3);
	int maxy = maximum(triangle->sy1,triangle->sy2,triangle->sy3);
				
	for(y = miny;y < maxy; y++){
		for(x = minx; x < maxx; x++){
			if((triangle->sx1 - triangle->sx2) * (y - triangle->sy1) -                               (triangle->sy1 - triangle->sy2) * (x - triangle->sx1) > 0 &&
			 (triangle->sx2 - triangle->sx3) * (y - triangle->sy2) -                                  (triangle->sy2 - triangle->sy3) * (x - triangle->sx2) > 0 &&
			 (triangle->sx3 - triangle->sx1) * (y - triangle->sy3) -                                  (triangle->sy3 - triangle->sy1) * (x - triangle->sx3) > 0)
			{
				SetPixel(screen,x,y,triangle->fillcolor);
			}
		}
	}

However this does not fill ALL the triangles, there are some glitches. 10y271y.jpg

Share this post


Link to post
Share on other sites

This topic is 1521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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