Skip to main content
GameDev.net gamedev.net
🔒 Locked

Perspective correct barycentric coordinates

Started by Hedanito Jan 23, 2011 at 12:30 AM 4 replies 14.9k views
Original Post
Hedanito
Hedanito
I am writing a software rasterizer as a school assignment, and I trouble getting my textures correct.
I found this tutorial about half space triangle filling, which seems more interesting than the scanline algorithm.
But then I was like: "Why don't you just calculate the barycentric coordinates right away?"
So I did, and the triangle looks fine.
Except for the texture.
So now my problems is how to use the barycentric coordinates to get perspective correct uv's (or any interpolation for that matter).
My pipeline gives the triangle in canonical space, so all I get is 3 vertices with their x,y,z and w value.
I tried googling it but they all keep talking about the scanline algorithm.
Vilem Otte
Vilem Otte
Huh, I actually never did a software rasterizer, but I made several raytracers (be it on GPU or on CPU), the principle of barycentric coordinates though stays the same. Can you write how do you calculate them - I think it would be much easier to tell you (or guess), where the bug is.
knarkowicz
knarkowicz
It doesn't matter if You use scanline or half spaces - the idea behind perspective interpolation is the same. You need just to calculate per pixel perspective correct U and Z values from linearly interpolated 1/W, 1/U and 1/V values. Read for example this thread for more info: http://www.gamedev.net/topic/592675-perspective-interpolation/.


Hedanito
Hedanito

Huh, I actually never did a software rasterizer, but I made several raytracers (be it on GPU or on CPU), the principle of barycentric coordinates though stays the same. Can you write how do you calculate them - I think it would be much easier to tell you (or guess), where the bug is.


That works because the barycentric coordinates are calculated in world space during ray/triangle intersection, not in screen space. I've written raytracers myself as well and it indeed is a lot easier there.



It doesn't matter if You use scanline or half spaces - the idea behind perspective interpolation is the same. You need just to calculate per pixel perspective correct U and Z values from linearly interpolated 1/W, 1/U and 1/V values. Read for example this thread for more info: http://www.gamedev.n...-interpolation/.


I still find it very vague. Also it interpolates 2 vertices instead of 3.

Right now I just take the barycentric coordinates, multiply each vertex uv by them, and I then add those together.
This gives the standard dent in your texture effect.
So what do I multiply, divide, whatever it is, the uv's with before I multiply them with the barycentric coordinates, and also very important, how do I get the correct value back afterwards?


cubet.png

Pictures always help
knarkowicz
knarkowicz

I still find it very vague. Also it interpolates 2 vertices instead of 3.

Right now I just take the barycentric coordinates, multiply each vertex uv by them, and I then add those together.
This gives the standard dent in your texture effect.
So what do I multiply, divide, whatever it is, the uv's with before I multiply them with the barycentric coordinates, and also very important, how do I get the correct value back afterwards?



v1,v2,v3 - 3 triangle vertices
b1,b2,b3 - barycentric coordinates for appropriate vertices


w' = ( 1 / v1.w ) * b1 + ( 1 / v2.w ) * b2 + ( 1 / v3.w ) * b3
u' = ( v1.u / v1.w ) * b1 + ( v2.u / v2.w ) * b2 + ( v3.u / v3.w ) * b3
v' = ...
perspCorrU = u' / w'
perspCorrV = v' / w'


Keep in mind, that this is a very slow approach. It can be optimized by using deltas and it's enough to interpolate between two points. Everything should be explained in any paper/tutorial about half space triangle rasterization.
Hedanito
Hedanito

[quote name='Hedanito' timestamp='1295793888' post='4763425']
I still find it very vague. Also it interpolates 2 vertices instead of 3.

Right now I just take the barycentric coordinates, multiply each vertex uv by them, and I then add those together.
This gives the standard dent in your texture effect.
So what do I multiply, divide, whatever it is, the uv's with before I multiply them with the barycentric coordinates, and also very important, how do I get the correct value back afterwards?



v1,v2,v3 - 3 triangle vertices
b1,b2,b3 - barycentric coordinates for appropriate vertices


w' = ( 1 / v1.w ) * b1 + ( 1 / v2.w ) * b2 + ( 1 / v3.w ) * b3
u' = ( v1.u / v1.w ) * b1 + ( v2.u / v2.w ) * b2 + ( v3.u / v3.w ) * b3
v' = ...
perspCorrU = u' / w'
perspCorrV = v' / w'


Keep in mind, that this is a very slow approach. It can be optimized by using deltas and it's enough to interpolate between two points. Everything should be explained in any paper/tutorial about half space triangle rasterization.
[/quote]

Thank you, that was a very clear and simple explaination.
Fixed it in like 30 sec and it worked right away =D

cube2l.png

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.