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

Cohen-Sutherland Algorithm

Started by Nacho Oct 7, 2001 at 8:15 AM 4 replies 750+ views
Original Post
Nacho
Nacho
I´m reading Tricks of the Windows Game Programming Gurus and I´ve got a little problem with the Cohen-Sutherland Algorithm implementation. There´s a section of the code I don´t get: // try and trivially reject if((p1_code&p2_code)) return(0); I tested the algorithm on paper tracing the values of the variables and, altough I think that I´m doing that correctly when I get to that part of the code I have to return to the caller function. It´s weird because the demo works ok. By the way, what method do you use to debug DirectX application, because in VC++ there´s a Run To Cursor option but each time I try to use it for a DirectX game my system crashes. Thanks in advance!
Ruval
Ruval
That test works because if either of the points have matching bits in the outcode (and therefore both outside the same side of the viewport) then they will never be visible.

I assume you know this already, but an outcode is set by testing the point against the clipping rectangle, and setting bit 0 if ifs left of the screen, 1 if it''s above etc.

As for the run to cursor thing, are you trying to trace inside a Lock-ed area of code? e.g. line drawing in DDraw usually requires a Lock-Unlock pair.

Try using DDLOCK_NOSYSLOCK as one of the flags to the Lock function, as long as you''re not using WindowsNT/2000.
Nacho
Nacho
Thanks Ruval! I reread you answer a thousand times and there´s something I don´t get. In the book I´ve got two int variables for the clip codes, these are: p1_code and p2_code. The code for the areas outside the viewport are written as #defines in the following way:

0x0000 for center
0x0008 for north
0x0004 for south
0x0002 for east
0x0001 for west

Supposing both endpoint are north, that is, p1_code = p2_code = 0x0008. Please tell me if I´m wrong, but p1_code & p2_code will be = 1000 AND 1000 = 1000. The statement would become true so I would have to return. Why is the code written in this way:

if((p1_code & p2_code))
return(0);

instead of this one:

if(p1_code & p2_code)
return(0);

Are the extra pair of parenthesis really neccesary? I hope you can help me, I know that my question is really silly, but I want to understand the code. Thanks!
Nacho
Nacho
And now the debugging! I think that I was performing the Run to Cursor command over an unlocked part of the code. I just wanted to know because I have using Turbo C++ (DOS) for a year and you can stop the program whenever you want and toggle between the code and the screen by pressing alt+f5 even in graphics mode. I haven´t tried the flag you told me but I think it´s gonna work, thanks!
Ruval
Ruval
In this case those extra parentheses aren''t needed - some people
put them around all bitwise expressions, I''ve never been quite
sure why

Nacho
Nacho
I didn´t know about the parenthesis, ok, I´ll keep that in mind. Hey, but what about my example of a clipped line? Where am I getting wrong with my variable´s tracking?

Topic Locked

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

Sign in to reply to this topic.