Jump to content
  • Advertisement
Sign in to follow this  

Ray tracing "camera" vector...

This topic is 5180 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, On my recent quest to write a ray tracer I've run into a little problem. In a ray tracer you find the ray coming out of the lense by normalizing a vector. I also have the vector in which the camera is pointing. What I need to do is find the vector of the ray by rotating the original vector by the camera vector. Is there a way to do this using vectors? The only way I can think of is using trig to get the rotation of each vector, then adding the two together, and converting them back to the ray vector. (If so some help with this would be nice, or a tutorial, I'm still rusty on my trig) Thanks for your help ~zix~

Share this post

Link to post
Share on other sites
If I understand you correctly basicly you want to rotate the lense ray vector so that its z-component is facing the camera direction. I'm not sure if this is a good way to do it but you can create a rotation matrix from the camera direction vector and apply the rotation to the ray vector. Here is how you create the rotation matrix from a z-axis vector (camera direction in your case):

public static Matrix3 FromZAxis(Vector3 zAxis)
Vector3 yAxis;
if(zAxis.X == 0 && zAxis.Z == 0)
yAxis = new Vector3(-zAxis.Y, 0, 0);
yAxis = new Vector3(0, 1, 0);

Vector3 xAxis = yAxis.Cross(zAxis);

yAxis = xAxis.Cross(zAxis);
yAxis = -yAxis;

return new Matrix3(xAxis, yAxis, zAxis);

My answer may be way off though :).

Share this post

Link to post
Share on other sites
its a simple basis transformation. You define a basis for your camera. The first vector is the direction of the camera vector that you have, the second vector is the camera's up vector and the third you can get by taking the cross product of the two. You can arrange these three vectors to form a 3x3 transformation matrix which you will use to transform any point or vector that you want.

Share this post

Link to post
Share on other sites
Thanks guys, I'm trying the transformation matrix first, although I'm not having much luck with that.

See any errors here (Excuse the fact that the prototype is in VB)

Private Function RayCamera(ray As Vector3) As Vector3
Dim m As Matrix33
Dim cross As Vector3
Dim up As Vector3
up.X = 0
up.Y = 0
up.Z = 1

cross = Vector3CrossProduct(up, CamVect)

m.m11 = CamVect.X
m.m12 = CamVect.Y
m.m13 = CamVect.Z

m.m21 = up.X
m.m22 = up.Y
m.m23 = up.Z

m.m31 = cross.X
m.m32 = cross.Y
m.m33 = cross.Z

RayCamera = MatrixVectorMultiply3(m, ray)

End Function

Is the ordering of the matrix wrong (probably), or should I look into my multiplying and cross product functions for errors?

Thanks again

Share this post

Link to post
Share on other sites
your code looks correct. For the cross product order, it really depends on what handed coordinate system you are using. Also, what I have told you is basically to accomplish the rotation that you were asking about. Looking at the first part of your post, it seems you are trying to find the ray through each pixel (which you would then trace in your scene). If that is what you are doing, then you on the wrong track. It is not a simple vector rotation. For that, you must find the 3D world coordinates of each pixel and then create a ray originating t the camera center and going through the pixel. If you do it this way, then the rotation will be implicit and you don't have to do it separately.

Share this post

Link to post
Share on other sites
The ray tracing coming out of the camera-point is working fine (I can get it looking into the world orginating at point x,y,z and it works), It's just the rotation of the camera that's difficult. I'm looking at rotation matricies right now on google, hopefully that will fix it (If I can ever find it, I'm not having much luck with google right now)

Right now (fooling around with the matrix) I'm either getting an image that's all twisted up or black.

The source for ray thru. pixel.

Public Sub RenderPixel(hdc As Long, X As Long, Y As Long)
Dim ax As Long, ay As Long
ax = X - (Width / 2)
ay = Y - (Height / 2)
Dim c As RGBA128

'trace ray
Dim ray As Vector3
Dim pos As Vector3
pos.X = Cam.X
pos.Y = Cam.Y
pos.Z = Cam.Z

ray.X = ax
ray.Y = FOV
ray.Z = ay
ray = NormalizeVector(ray)
ray = RayCamera(ray)

c = TraceRay(ray, pos, True) 'goes to previous part of code

'set pixel
Dim fc As RGBA32
fc = RGBA128toRGBA32(c)
SetPixel hdc, X, Height - Y - 1, RGB(fc.R, fc.G, fc.b)
End Sub

So, all I need is a rotation matrix from a vector, thanks for any smaple code, tutorial, etc (I cant seem to find anything, although I'm sure it's out there)


[Edited by - zix99 on August 10, 2005 4:48:12 PM]

Share this post

Link to post
Share on other sites
Thanks everyone for the responses.. it seems to be working except for the fact that everything is rotated upside down at what seems to be a 50 degree angle. I know it's just a matter of contructing or multiplying the matrix wrong, yet I havent found anything that is causing the problem and it's starting to get to me.

I'd appreciate it if someone could spot the problem: (This is just rotation code, ray tracing is someplace else)

Public Function MatrixVectorMultiply3(m1 As Matrix33, m2 As Vector3) As Vector3
MatrixVectorMultiply3.X = (m1.m11 * m2.X) + (m1.m12 * m2.Y) + (m1.m13 * m2.Z)
MatrixVectorMultiply3.Y = (m1.m21 * m2.X) + (m1.m22 * m2.Y) + (m1.m23 * m2.Z)
MatrixVectorMultiply3.Z = (m1.m31 * m2.X) + (m1.m32 * m2.Y) + (m1.m33 * m2.Z)
End Function

Private Function RayCamera(ray As Vector3) As Vector3
Dim m As Matrix33

Dim yaxis As Vector3, xaxis As Vector3 'zaxis=camvect
yaxis.X = 0
yaxis.Y = 0
yaxis.Z = 1

xaxis = Vector3CrossProduct(yaxis, CamVect)
xaxis = NormalizeVector(xaxis)

yaxis = Vector3CrossProduct(xaxis, CamVect)
yaxis = NormalizeVector(yaxis)
yaxis.X = -yaxis.X
yaxis.Y = -yaxis.Y
yaxis.Z = -yaxis.Z

'create matrix
m.m11 = xaxis.X: m.m12 = xaxis.Y: m.m13 = xaxis.Z
m.m21 = yaxis.X: m.m22 = yaxis.Y: m.m23 = yaxis.Z
m.m31 = CamVect.X: m.m32 = CamVect.Y: m.m33 = CamVect.Z

RayCamera = MatrixVectorMultiply3(m, ray)
RayCamera = NormalizeVector(RayCamera)

End Function

I'm almost certain it's the contruction of the matrix, but I've tried everything in constructing it, and it's either no image, or an upsidedown one.

Thanks for helping

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!