Gourad shading problem

Started by
13 comments, last by Roaders 21 years, 10 months ago
>> is this what you meant by clipping or have I got the wrong end of the stick?

No this is not what I meant.

The issue here is clipping. Lets say you a screen that goes from (0,0) to (100,100) and you want to draw a triangle: (90,20,10) (130,50,110), (90, 80, 10). This extends out of the screen.

At y=100 this triangle would have a z-value about 35 (according to my head math).

In your code you simply change the triangle to (90,20,10) (100,50,110), (90, 80, 10), i.e. you clamp the x-value to fit inside the screen. Now a y=100 the z value is 110. You have in other words changed the value of z of those parts of the triangle within the screen. And even worse the triangle has another shape - it should be drawn as a quad when a corner is cut of.

The easy way to get around this problem is to apply scissoring (I think this is what the hardware does). To do this, change your code for PutPixel so it only draws to screen if the given coordinates are within screen coordinates (a simple check). You can optionally speed this up since there is a space coherency in what pixels need to be scissored and which do not. And then remove all your clamping of corners - the corners of each triangle should not be changed.

Do you follow me?
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Advertisement
Yes I do. There is a problem with triangles not being drawn if they are clipped at the top of the screen. I haven''t really looked into it yet but it sounds like this could be what''s causing it. Thanks for letting me know.


Giles
Giles Roadnightgiles.roadnight.name
The upper and the left edge of the screen are usually the parts where you have to take care of Z, color and u/v-information when clipping (as long as you don't scanconvert your polygons right-to-left...:-)). So if you are having problems at the top or the left, faulty clipping of any of these may be your problem. I haven't had a look into your code...sorry, but it's simply too much..:-) Do you still use z for interpolation? I would still say that 1/z would be much better, but you ignored this in the "other thread" too :-).




[edited by - EgonOlsen on June 7, 2002 7:18:50 PM]
I didn''t mean to ignore it. The thing is that perspective is working fine at the moment so I see no reason to change it. If you can tell me more about the reason why I should be using 1/z then I''ll use that instead.

Thanks for the suggestion anyway.

Giles
Giles Roadnightgiles.roadnight.name
Because z is not linear in screenspace, but 1/z is. And because you are linear interpolating, 1/z is the better choice IMHO. If you are using z, you''ll get errors (like you get when you are doing affine texture mapping). These errors are most visible on large polygons. Try how it looks in your engine if two large polygons intersect when you use z for interpolation. The errors should be noticable. If they are not...well, then be happy..until they are...:-)

This topic is closed to new replies.

Advertisement