Qbasicnews.com

Full Version: Texture Mapping Theory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've been playing with ray casting lately in both QB and c++. its very new to me but i can get a working engine without much trouble. The problem is that i would like to use texture mapping for the walls (and maybe even floors :wink: ). Ive googled for info on it but havnt been very successful on understanding any of it.

Any help, links to tuts, or info on it would be awesome...
For the walls, when you're casting the ray and it intercepts with a solid area, you simply use the decimal part of the ray's X or Y coordinate (depending on the orientation of the wall you've hit) to find which position along the wall you're at. Translate that into a usable texture coordinate then replace your line drawing function with something that interpolates a vertical strip of texture at the given screen coordinates.

Something like that anyway.
Direct X (sorry for the short one liner but it was a useful one :wink: )
Equation of a ray:

Origin + t*Direction

Let's say we wan't to ray trace a tunnel:

Equation of the cylindrical coordinate system:

x=cos(angle)
y=sin(angle)
z=z

Which means:


(x-a)^2 + (y-b)^2 = r^2
can be used to get the intersection of a ray with a cylinder.

To shoot a ray to a pixel:

Direction.x = Pixel.X;
Direction.y = Pixel.Y;
Direction.z = 128;

direction is a vector and you have to "normalize" it before using it in any of the equations:

ie. NormalizedV=v/|v|

So if you take...

a = Direction.x^2 + Direction.y^2
b = 2*(Origin.x*Direction.x + Origin.y*Direction.y)
c = Origin.x^2 + Origin.y^2 - r^2

You could use the Quadratic equation to get the intersection(I believe its could also be used in spheres or any quadric shapes)

a*t^2 + b*t + c = 0

Equation(quadratic formula)


t=-b +/- (b^2 - 4*a*c) / 2*a


Now there are always to solutions to a quadratic equation. Hence to (+/-) after the T=-b.

So you need to chack for 2 values. I'll call it t1 and t2 (t3's chick tx is cool)

t1=-b + (b^2 - 4*a*c) / 2*a

t2=-b - (b^2 - 4*a*c) / 2*a

We are only interested in the nearer of the two values so...

if T1<t2 then T=t1 else T=t2

Now for the intersection:

Intersection, origin and direction are vectors.

Intersection = Origin + t*Direction

To map the texture of the object:

u = int(abs(Intersection.z)*.4)
v = int(abs(atn(Intersection.y, Intersection.x)*256/PI))

Clr=Texture(u,v)

Pset(x,y),clr
WOW :o .Ill have a look through that very detailed post rel. I found some tuts by joe king that look quite useful too.

mech: im using dial up. directX is huge isnt it...
Some games have DirectX installed in it. Even Windows 98 intalls DX for you. DX 6.