• Create Account

## Ray tracing "camera" vector...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1zix99  Members

205
Like
0Likes
Like

Posted 10 August 2005 - 06:21 AM

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~

### #2Opwiz  Members

347
Like
0Likes
Like

Posted 10 August 2005 - 06:52 AM

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){	zAxis.Normalize();	Vector3 yAxis;	if(zAxis.X == 0 && zAxis.Z == 0)	{		yAxis = new Vector3(-zAxis.Y, 0, 0);	}	else	{		yAxis = new Vector3(0, 1, 0);	}	Vector3 xAxis = yAxis.Cross(zAxis);	xAxis.Normalize();	yAxis = xAxis.Cross(zAxis);	yAxis.Normalize();	yAxis = -yAxis;	return new Matrix3(xAxis, yAxis, zAxis);}

My answer may be way off though :).

### #3musawirali  Members

199
Like
0Likes
Like

Posted 10 August 2005 - 07:29 AM

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.

### #4zix99  Members

205
Like
0Likes
Like

Posted 10 August 2005 - 08:08 AM

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 Vector3Dim m As Matrix33Dim cross As Vector3Dim up As Vector3up.X = 0up.Y = 0up.Z = 1cross = Vector3CrossProduct(up, CamVect)m.m11 = CamVect.Xm.m12 = CamVect.Ym.m13 = CamVect.Zm.m21 = up.Xm.m22 = up.Ym.m23 = up.Zm.m31 = cross.Xm.m32 = cross.Ym.m33 = cross.ZRayCamera = 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
~zix~

### #5musawirali  Members

199
Like
0Likes
Like

Posted 10 August 2005 - 09:37 AM

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.

### #6zix99  Members

205
Like
0Likes
Like

Posted 10 August 2005 - 09:48 AM

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 Longax = X - (Width / 2)ay = Y - (Height / 2)Dim c As RGBA128'trace rayDim ray As Vector3Dim pos As Vector3pos.X = Cam.Xpos.Y = Cam.Ypos.Z = Cam.Z'rayray.X = axray.Y = FOVray.Z = ayray = NormalizeVector(ray)ray = RayCamera(ray)'tracec = TraceRay(ray, pos, True) 'goes to previous part of code'set pixelDim fc As RGBA32fc = RGBA128toRGBA32©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)

Thanks
~zix~

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

### #7zix99  Members

205
Like
0Likes
Like

Posted 11 August 2005 - 06:37 AM

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 FunctionPrivate Function RayCamera(ray As Vector3) As Vector3Dim m As Matrix33Dim yaxis As Vector3, xaxis As Vector3 'zaxis=camvectyaxis.X = 0yaxis.Y = 0yaxis.Z = 1xaxis = Vector3CrossProduct(yaxis, CamVect)xaxis = NormalizeVector(xaxis)yaxis = Vector3CrossProduct(xaxis, CamVect)yaxis = NormalizeVector(yaxis)yaxis.X = -yaxis.Xyaxis.Y = -yaxis.Yyaxis.Z = -yaxis.Z'create matrixm.m11 = xaxis.X: m.m12 = xaxis.Y: m.m13 = xaxis.Zm.m21 = yaxis.X: m.m22 = yaxis.Y: m.m23 = yaxis.Zm.m31 = CamVect.X: m.m32 = CamVect.Y: m.m33 = CamVect.Z'multiplyRayCamera = 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
~zix~

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.