Jump to content

  • Log In with Google      Sign In   
  • Create Account


Manual Picking Ray


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.

  • You cannot reply to this topic
4 replies to this topic

#1 caibbor   Members   -  Reputation: 116

Like
0Likes
Like

Posted 10 July 2012 - 10:58 PM

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;
}


Edited by caibbor, 12 July 2012 - 12:58 PM.


Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 6952

Like
0Likes
Like

Posted 10 July 2012 - 11:55 PM

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.

#3 caibbor   Members   -  Reputation: 116

Like
0Likes
Like

Posted 11 July 2012 - 09:45 AM

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.

Edited by caibbor, 11 July 2012 - 10:41 AM.


#4 caibbor   Members   -  Reputation: 116

Like
0Likes
Like

Posted 11 July 2012 - 11:11 AM

I'm very close, but not quite there yet.

Edited by caibbor, 12 July 2012 - 12:48 PM.


#5 caibbor   Members   -  Reputation: 116

Like
0Likes
Like

Posted 12 July 2012 - 12:50 PM

Updated top post with current code. Still not there yet, need another pair of eyes on this thing.




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.



PARTNERS