#1




Triangle rasterization algorithmThe triangle rasterization algorithm given in the fundamentals of computer graphics by Peter Shirley,Michael Ashikhmin book is as below
Code:
I implemented this algorithm incrementally as below. However, I am getting wrong values for constatnt alpha20_const,alpha01_const which are zero most of the times for a triangle with vertices (x0,y0),(x1,y1),(x2,y2) and colors (r0,g0,b0),(r1,g1,b1),(r2,g2,b2) respectively((r,g,b) triplet gives RGB combination for the color in graphics)) Please help me out to figure out the mistakes I have made in the implementation given below. I am multiplying the pixel coordinates with the transformation matrix, M before plotting it to the screen to apply the transformation to the triangle. CPP / C++ / C Code:
Last edited by admin : 12Mar2009 at 21:28.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags


#2




Re: Triangle rasterization algorithmQuote:
Code:
Check again. Shouldn't the equations be the following? Code:
Quote:
Well, show us a particular triangle where the answers from your program are wrong. What did you expect the values to be? What were the values? Maybe consider the following. (See Footnote.) CPP / C++ / C Code:
Regards, Dave Footnote: I have not tested this, and don't expect to. Last edited by davekw7x : 12Mar2009 at 16:45.

#3




Re: Triangle rasterization algorithmThanks Dave,
I did the corrections for beta and gamma computation as you suggested. Also I changed the condition in if loop(which is after the computation of alpha12_const, alpha20_const,alpha01_const ) as follows if(alpha12_const!=0 & alpha20_const !=0 & alpha01_const!=0). This is the algorithm for triangle filling using barycentric coordinates. So a point in this coordinate system is given by (alpha,beta,gamma). For computing these values of a point, I am calculating signed distance of that point from the edge of the triangle. I am setting only those pixels that are inside the triangle i.e. pixels with alpha,beta and gamma all three values between 0 and 1.However I am not getting a single pixel for a valid triangle. I took example of triangle with vertices (100,100) ,(100,500),(500,600) as (x0,y0),(x1,y1),(x2,y2) respectively which is right angle triangle. But according to the computation I get alpha as always 1 and beta is always negative and gamma as positive with the same value as beta e.g. alpha =1 beta = 0.556 gamma = 0.556 So how is it possible that there is not a single pixel with values of alpha,beta and gamma all three between 0 and 1? 
#4




Re: Triangle rasterization algorithmQuote:
Quote:
Quote:
CPP / C++ / C Code:
Quote:
Quote:
For each value of x and y, you must calculate a new value for alpha, beta, and gamma. I'm not sure how you arrived at your loop calculations, but I claim that if they were working properly the output would be correct. In cases of OPC (Other People's Code), I just start from fundamentals and do the calculations in a way that is "obvious" to me. If they have done some work that seems valid, I may keep it. If there is any question, I rewrite it. In case of my own code, if I couldn't see anything wrong, but I am pretty sure there is something wrong, I might wipe the slate clean and try another formulation. Using your notation, I might calculate alpha, beta, and gamma with something like: CPP / C++ / C Code:
Here's how I would try testing it: 1. On a piece of graph paper, plot three points that make a triangle. Since I am going to visually inspect the output from the program, I pick a small triangle. For an example, I will choose the three points that make up a right triangle: (1,1) (6,1) (1,5). I pick a scale on the graph paper that will let me draw the triangle easily and see at a glance what pixels will be lighted. Then I create a main program that calls the function with these three values. I pick a single color for all points so that I don't have to worry about interpolation, just the part that decides what pixels will be lighted. Then I make a version of the function that doesn't actually use any plotting routines, but just prints out coordinates of the lighted pixels. The loop inside the function might look something like: CPP / C++ / C Code:
From my pencil and paper doodlings, I would expect to see printed values for points in and on the triangle. Namely for the points: (1,1) (2,1) (3,1) (4,1) (5,1) (6,1) (1,2) (2,2) (3,2) (4,2) (1,3) (2,3) (3,3) (1,4) (2,4) (1,5) Regards, Dave 
#5




Re: Triangle rasterization algorithmDave,
Thanks a lot. I figured out the mistakes in this algorithm with the hints provided by you. Also please assist me on my other post of finding normal at a triangle vertex. 
«
Previous Thread

Next Thread
»
Thread Tools  Search this Thread 
Rate This Thread  



Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
pascal's triangle element  aijazbaig1  C Programming Language  2  27Aug2007 03:18 
triangle (polygon), drawing, sizing, and rotation programme using linked lists...  promsan  C Programming Language  12  14May2007 14:03 
Help on homework!  kemmal  C Programming Language  1  06Nov2006 12:22 
Algorithm Help Please!  daking_09  C++ Forum  0  24May2006 19:12 
help.... SLR * algorithm  tay  C Programming Language  4  10Sep2004 11:48 
Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The
All times are GMT 6. The time now is 10:00.