# viewSpace to objectSpace

## Recommended Posts

mokaschitta    124
Hi, I am currently doing some rayCasting on the GPU using GLSL and I once again stumbled across a problem. Right now I am doing the raycasting in viewSpace, down the negative -z axis. I am raycasting 3D Metaballs which are accelarated using spatialHashing via texture lookups. My hashFunction simply takes the current Position and gives me all metaballs in that bucket. Anyways now since I started doing the raycasting in viewSpace I need to convert the position back to object/ModelViewSpace to find the right bucket. Am I right that I would have to multiply the current position with the inverse of the ModelViewMatrix? Thanks!

##### Share on other sites
jpetrie    13102
Quote:
 Am I right that I would have to multiply the current position with the inverse of the ModelViewMatrix?

Yes.

##### Share on other sites
mokaschitta    124
I am kind of scared that this could ruin the performance (since I would have to make multiple matrix multiplications per fragment).- Maybe I should try raycasting in object space directly.

##### Share on other sites
mokaschitta    124
Just in case anybody wants to know. I do the raycasting in objectSpace now which saves me alot of matrix Multiplications. Basically I reconstruct the nearplane in objects space each time the camera changes like this:

	// get the current modelview matrix	glGetFloatv(GL_MODELVIEW_MATRIX , modelview);	        //right & upvector of the current camera setup	Vec3f Right(modelview[0], modelview[4], modelview[8]);	Vec3f Up(modelview[1], modelview[5], modelview[9]);	Vec3f dir = at-cpos; //camera dir	dir.normalize();	Vec3f nPos = cpos+dir*near; //find center point on the nearplane		Vec3f ntl,ntr,nbl,nbr;	        //find corners of the nearplane	ntl = nPos+(Up*halfHeight)-(Right*halfWidth);	ntr = nPos+(Up*halfHeight)+(Right*halfWidth);	nbl = nPos-(Up*halfHeight)-(Right*halfWidth);	nbr = nPos-(Up*halfHeight)+(Right*halfWidth);        //halfWidth and halfHeight as set in glFrustum

then I basically just draw a quad with the four calculated corners (which should be fullscreen since its on the nearplane) and pass the cameraPosition to my shader. The ray direction can then simply be calculated like this:

varying vec3 msPos; //vertex position of the quad in objectSpaceuniform vec3 camPos;void main(){    vec3 rayOrigin = msPos;    vec3 rayDir = normalize(rayOrigin-camPos);}

alternatively you can render the nearplane quad to a texture and do the same thing with a texture lookUp.

It took me quite some time to figgure this out so I hope this will help somebody :)