Jump to content

How to render a multi-color triangle


Richard Baker

Recommended Posts

I have a collection of triangles on the screen and the color at each point on each triangle is determined by an algorithm and therefore cannot be approximated by barycentric interpolation.  I want to render this triangle quickly and accurately.  Here is my attempt at a solution:  I have the program calculate the screen area of the tringle using a cross-product. Then it calculates the color difference between each point using the Pythagorean theorem treating rgb as coordinates i.e. square root of (red-red1)2 +(green2-green1)2+ (blue2-blue1)2

What I need to figure out next is what function I should use as a function of color difference and area. 

I want the user to define a specific threshold with units.  I want the user to define the threshold.  The user-defined threshold should be in units of number of color changes per area. If the function is below a certain threshold it will perform barycentric interpolation.  If it is above the threshold it will subdivide the triangle in two more triangles and do the test again. It performs this recursively.

Thank you for any help you can provide.

Link to comment
Share on other sites

52 minutes ago, Richard Baker said:

I want to render this triangle quickly and accurately. 

How are you rendering triangle?

1) OpenGL/DirectX?

2) Drawing pixel by pixel in GDI?

Edited by Sensei
Link to comment
Share on other sites

After tinkering with the numbers I realized the barycentric interpolation is inefficient. I divide the surface into quads and then I interpolated the lighting function along the gradient of the lighting function.  Gradient in this case means the sum of the first order partial derivatives, not the gradient in paint terminology, and I am using Phong-Blinn as my lighting function.  But the question remains:  What is a good measure of color change per distance or area?

Link to comment
Share on other sites

On 8/2/2021 at 7:28 PM, Richard Baker said:

I have a collection of triangles on the screen and the color at each point on each triangle is determined by an algorithm

I would start from benchmarking how long it takes to render:

1) completely flat solid color triangle by drawing each pixel by pixel manually.

2) triangle which has every pixel color taken from your algorithm as-is without any attempt to optimize it.

So you will know how much CPU spends time on drawing pixels, and how much it spends on your algorithm (the difference between 2 and 1).

Search for alternative functions instead of drawing pixel by pixel.

e.g. Windows GDI has SetPixel() for drawing pixel by pixel:

https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-setpixel

which is the most inefficient way of drawing anything on screen.

But there is very fast SetDIBits()

https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-setdibits

and even faster OpenGL and DirectX, if you will generate texture and use it on triangle..

 

Place function getting current system time at the beginning. Typically you will get it in milliseconds.

And place yet another instance of it at the end. Subtract both and you have time in milliseconds.

If it is a few milliseconds, make a loop for inner code, 10, 100, 1000 etc. repetitions, to get more accurate result.

 

Did you try using UseBuffer and PaintBuffer? They seem to create off-screen double-buffer, which is copied at once when you call PaintBuffer:

https://education.ti.com/html/webhelp/EG_TINspireCode/EN/Subsystems/RG_Nspire_EN/Content/M_RefGuide/RG_DrawCommand.htm#UseBuffer

https://education.ti.com/html/webhelp/EG_TINspireCode/EN/Subsystems/RG_Nspire_EN/Content/M_RefGuide/RG_DrawCommand.htm#PaintBuffer

 

Quote

What is a good measure of color change per distance or area?

If you will calculate sqrt(( r1-r0)^2+(g1-g0)^2+(b1-b0)^2) you will compare change of brightness.

e.g. r0=g0=b0 r1=1,g1=0,b1=0 will give exactly the same result as r1=0,g1=1,b1=0.

To measure color difference, instead of brightness, you could convert RGB->HSB/HSV color space, and check Hue variation (+- some threshold).

 

Edited by Sensei
Link to comment
Share on other sites

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
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.