Manual Picking Ray

Started by
3 comments, last by caibbor 11 years, 9 months ago
For some reason I cannot figure out how to get manual picking working. I've most closely followed Nomagon's post from here: http://stackoverflow...ing-ray-picking

My code does look a bit simple compared to most implementations, but I think it does everything it needs to. I've seen other implementations use things like tan() and some other fluff, but it seems like useless fluff.



Line3f GetPickLine( void ) {
const int* vp = game->renderer.viewport;

// map UI mouse pos to [-1,1]
Vec3f pos( game->input.GetUIMousePos() );

pos.x = 2 * pos.x / vp[GL_VP_W] - 1; // todo * aspectRatio
pos.y = 1 - 2 * pos.y / vp[GL_VP_H]; // invert y axis
pos.z = -1;

// get the inverted modelViewProjection matrix
Mat4f mv( game->renderer.modelMatrix );
Mat4f mvpi = Mat4f( game->renderer.projectionMatrix ) * mv;
mvpi.InvertRotationOrtho();
mvpi.InvertTranspositionOrtho();

// multiply mouse pos and modelViewProjection matrix to get a point on that ray
pos = mvpi * pos;

Vec3f cam( -mv.Column3f(3) ); // camera position is last column of modelView matrix
return Line3f( cam, pos );
}

//matrices are column-major
void Mat4f::InvertRotationOrtho( void ) {
swap( data[0][1], data[1][0] );
swap( data[0][2], data[2][0] );
swap( data[1][2], data[2][1] );
}

void Mat4f::InvertTranspositionOrtho( void ) {
data[3][0] = -data[3][0];
data[3][1] = -data[3][1];
data[3][2] = -data[3][2];
}

Vec3f Mat4f::Column3f( const int c ) const {
ASSERT( c >=0 && c <= 3 , "Mat4f::Column3f: Bad argument\n." )
return Vec3f( data[c][0], data[c][1], data[c][2] );
}

void swap( float& one, float& two ) {
float tmp(one);
one = two;
two = tmp;
}

Advertisement
You have
[source lang="java"]
mat = game->renderer.modelMatrix * game->renderer.projectionMatrix
[/source]

whereas the link you have posted described it as
[source lang="java"] mat = game->renderer.projectionMatrix * game->renderer.modelMatrix[/source]
As long as you have the same multiplication rules, you must use the same order.
Edit:

Matrix multiplication isn't commutative, I figured it should have been....
Seems the multiplication fucntion is fine, but I had got some bugs elsewhere in the matrix stuff

Still trying to get this to work, it still isn't.
I'm very close, but not quite there yet.
Updated top post with current code. Still not there yet, need another pair of eyes on this thing.

This topic is closed to new replies.

Advertisement