Archived

This topic is now archived and is closed to further replies.

Absolution

Affine Texture Mapping and Distortion

Recommended Posts

Absolution    180
I just implemented a 3D model viewer in software. I recently added affine texture mapping (interpolated texture-mapping), but I get distortion. Specifically, if I am trying to map a rectanglar texture to a rectangular polygon (which I have split into 2 triangles) I get a slight distortion at the boundary where the two triangles meet - this is noticeable during rotations. Does anybody know why this is and how I can fix it? Abs

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Well, I have been playing around and I guess I have answered my own question. This is just a matter of perspective mapping. As I move my view-point to infinity the mapping is more ''realistic'' since I basically have an orthagonal projection. I am going to investigate some perspective projection algorithms, but if anybody can point me to some tutorials/articles (I read the 2 paragraph one on gamedev already) that would be great...

Abs

"Yeah, I am going to go ahead and have you come in on Saturday...yeah...that would be great..."

Share this post


Link to post
Share on other sites
simon_brown75    123
This is what you get with affine texture mapping, always. You can''t linearly interpolate u and v because they''re not linear in screen space. If you want to get rid of the distortion you have to use perpective correct texture mapping.

That involves calculating u/z, v/z and 1/z for each of your 3 triangle points and then linearly interpolating these values across your horizontal lines. Then at each pixel you need to work out (current u/z) / (current 1/z) to get your actual u value, and (current v/z) / (current 1/z) to get your v value.

Obviously 2 divides per pixel is very slow, so a common optimization is to work out a perspective correct u,v every 8, 16 or 32 pixels and then linearly interpolate the in-between u,v''s. This gives a minor distortion but it''s still a hundred times better than affine texture mapping and way faster than 2 divides per pixel.

I have code of mine demonstrating both techniques, but it''s badly written and poorly commented due to how old it is. I''ll send it if you want though, just reply here. If you want to see the code running, download "DirectDraw Software Texture-Mapper" from my website at http://www.sbdev.pwp.blueyonder.co.uk (DirectDraw is used for fast pixel plotting only).

Share this post


Link to post
Share on other sites
Absolution    180
Thanks, I am making a similar demo myself out of all this. I am also using DirectDraw to plot the pixels (and DirectInput). I am actually have fun (most of the time) adding as many features as possible to it. For one, it''s a great learning experience since I understand all the math and theory behind common (and a few not so common) 3D techniques. Second, it''s interesting to see how fast you can make it. So far I have added: flat, g, and phong shading; z-buffering; a custom viewing space; perspective corrected affine texture mapping ; multiple light sources ; a shadow z-buffer for each ; volume swept objects (superellipses, toroids, spheres, etc)... okay enough. Point is, I''m proud of it and I recommend it to anybody who is serious about programming in 3D. Avoid the temptation to just dive into 3D APIs.

Abs

Share this post


Link to post
Share on other sites
simon_brown75    123
I agree, I also made a point of learning the basics of software 3d programming before starting with D3D/OpenGL, and it''s given me a better understanding of the whole 3d pipeline and how to organize a 3d engine. Plus getting the code to run fast is great practice for code optimization in general.

You''d be amazed the number of people who run into problems with D3D and OpenGL through not knowing software 3D, and the number of people who write off software 3D as pointless. Sounds like you''re determined to learn properly

Share this post


Link to post
Share on other sites