|
I'm currently working on a 3D-engine that uses Binary Space Partitioning like Doom built from scratch. The idea is that I start with the basics, and work my way up to include color indexing, multithreading, dynamic lighting etc, but as for now I'm stuck on the perspective transformation. I dug up this useful stackexchange answer which covers the necessary steps to get the coordinates, but unfortunately I can't quite grasp the concept of homogenous coordinates. Right now I'm getting the coordinates relative to the camera's position and rotation (just along the z-axis, since you're just looking left and right for now) which works for a map-feature, but I'm unsure what to do afterwards. What is the w-component for the 3D-coordinates supposed to be? And how is the camera-coordinate system supposed to be oriented, should z be orthogonal to the projection plane? I tried following the instructions assuming w is 1 and the ground being the xy-plane, but the resulting vector ended up being infinity. Here's my code - pardon the mess, I started with Processing because it's quick but I'm planning on switching to some form of C because Processing is severely lacking. code:
|
# ¿ Jul 31, 2017 18:25 |
|
|
# ¿ Apr 29, 2024 09:37 |
|
Doc Block posted:I'm not really qualified to comment on your math code, but you should definitely be wary of allocating a new vector every projection. Also, use matrix math instead if at all possible. The sloppy code is mostly on Processing, it can't do Matrix math - hence my desire to switch languages. I suspected that Z was supposed to be the distance from the viewer, and after some adjusting, it now seems to work properly (save for the incorrect vertex order when viewing the wall from the back, but that was expected). Here's what the correct math looks like: code:
Edit: I noticed I never actually linked to the afformentioned stackexchange post. Here it is. I must admit it had me confused for a while because I've never seen column-major matrices before. horriblePencilist fucked around with this message at 14:04 on Aug 1, 2017 |
# ¿ Aug 1, 2017 14:01 |
|
^^ Thanks for the links, I'll check them out.Doc Block posted:Well, if nothing else you need the w coordinate to make the vertex have 4 elements so it can be multiplied against a 4x4 matrix. The real purpose of the w coord IIRC is that it gets used to do the perspective divide. Set it to 1.0 in your vertex data and then don't worry about it. Why not use a 3x4 matrix? Does it use up less resources or something?
|
# ¿ Aug 1, 2017 21:53 |