Sign in to follow this  
WiredCat

Problem with construction of two vectors from one [opengl]

Recommended Posts

hello, i am trying to create a 3d box from on screen. (from a specified modelview)

 

 

here is what i do:

 

get from modelview 3 vectors (front, right and up) (they're versors)

then i check when i clicked at the beggining and i make a vertex (the first box vertex) call it A (red dot)

after that i check where actually mouse pos is and create a second vertex (the last box front face vertex) call it B (green dot)

 

 

i make a vector from first vertex to last vertex (AB)

 

and then project it onto 2 versors (right and up)

 

then i make a depth vector (from front versor)

 

but i does not work : )

 

here is a vid:

http://www.youtube.com/watch?v=TgtNoCIx5Qs&feature=youtu.be

 

 

I am using the formula from wikipedia

 

result = lengthof(projected_vec) * cosine_between_those_2_vectors * versor_direction_of_vec_that_will_be projected_on

vec_projection.jpg

 

 

please look at my code it looks fine but maybe i can't find bug there

 

and here is code with (stage 2 - green dot and rest of box creation)

 

 





void __fastcall TModelGroupHandler::ResizeBox(int X, int Y, HWND hwnd)
{
if (n2ddistance(float(lastx), float(lasty), float(X), float(Y) ) < 10.0f) return;
if (insert_box_first_stage_completed == false) return;


double * m_PickInfo_ModelView = new double[16];
t3dpoint vX; t3dpoint vY;

glGetDoublev(GL_MODELVIEW_MATRIX, m_PickInfo_ModelView);
		/*

			AIR_MATRIX[0] = rr.x; //right
			AIR_MATRIX[1] = rr.y;
			AIR_MATRIX[2] = rr.z;
			AIR_MATRIX[3] = 0.0f;
			AIR_MATRIX[4] = ru.x; //up
			AIR_MATRIX[5] = ru.y;
			AIR_MATRIX[6] = ru.z;
			AIR_MATRIX[7] = 0.0f;
			AIR_MATRIX[8]  = -rf.x; //front
			AIR_MATRIX[9]  = -rf.y;
			AIR_MATRIX[10] = -rf.z;
			AIR_MATRIX[11] = 0.0f;
			AIR_MATRIX[12] =  0.0f;  //point
			AIR_MATRIX[13] =  0.0f;
			AIR_MATRIX[14] =  0.0f;
			AIR_MATRIX[15] = 1.0f;

        */

vX.x = float(m_PickInfo_ModelView[0]);
vX.y = float(m_PickInfo_ModelView[1]);
vX.z = float(m_PickInfo_ModelView[2]);


vY.x =	float(m_PickInfo_ModelView[4]);
vY.y =	float(m_PickInfo_ModelView[5]);
vY.z =	float(m_PickInfo_ModelView[6]);
  //vector projection onto another vector
  

  
t3dpoint nX = vX;
t3dpoint nY = vY;

vX = vector_multiple(vX, 10000.0f);
vY = vector_multiple(vY, 10000.0f);

delete m_PickInfo_ModelView;

t3dpoint res;        t3dpoint res2;  

res = GetPositionFromSpaceDepth(X, Y, hwnd); //GREEN DOT

//==================================================================================
//right vector projection
t3dpoint projected_vec;     projected_vec = vectorAB(base->p1, res);

float cos_angle = RadToDeg(   AngleBetweenVectors(vX,projected_vec)  );      //ssh

cos_angle = cos(cos_angle*imopi);                                 //ssh



float magni;
magni = magnitude( projected_vec );

float fp; /* floating point */
 fp = magni * cos_angle;

t3dpoint proper_right_vec = vector_multiple(nX, fp);
//==================================================================================
//down vector projection
//==================================================================================

projected_vec = vectorAB(base->p1, res);



cos_angle = RadToDeg(   AngleBetweenVectors(vY,projected_vec)  );      //vY should be reversed now but i does not matter now because box right vector is wrong too)




cos_angle = cos(cos_angle*imopi);                                 //ssh

fp = magni * cos_angle;

t3dpoint proper_down_vec = vector_multiple(vY, fp);

proper_down_vec = reverse_point( proper_down_vec );
 
//==================================================================================
t3dpoint depth_v = vectorAB(base->eyepos, base->eyepointingpos);    depth_v = Normalize( depth_v );

depth_v = vector_multiple(depth_v, 50.0f); //default 50 meters depth


//*************************************************
//** proper_down_vec
//** proper_right_vec
//** depth_v
//**
//**  p1    p2       p5  p6
//**
//**   front          back
//**  p4    p3       p8  p7
//**
//**
//**
//*************************************************

base->p5 = 	vectors_add(base->p1, depth_v);

base->p3 = res;
base->p7 = 	vectors_add(base->p3, depth_v);

base->p2 	=  vectors_add(base->p1, proper_right_vec);
base->p6 	=  vectors_add(base->p2, depth_v);

base->p4 	= vectors_add(base->p1, proper_down_vec);
base->p8 	= vectors_add(base->p4, depth_v);



box_can_be_drawn = true;
}

 

cheers

Edited by ___

Share this post


Link to post
Share on other sites

I realize this is a late reply, and you may have moved on from this post. In any case...

 

I would first comment that the term versor isn't that commonly used, and it can mean different things. A versor is a unit quaternion. But sometimes, for some reason, versor is used to denote a set of unit length basis vectors aligned with the axes of a coordinate system...and this is how you are using the term. I've been involved in modeling and geometric representations and coordinate space representations for some 30 years, on the practical side rather than theoretical, and I had never seen the word used before in this context. Not that this is a problem, but it confused me.

 

So, I would like to help on this problem, but your code isn't particularly intuitive. It would help to better understand the outcome that you are looking for. Can you repost your video? It does not seem to exist on YouTube. I can envision a much simpler way to create such a box, but it would help to know what you expect to happen as you move the mouse around.

 

Graham

Share this post


Link to post
Share on other sites

@grhodes_at_work: I have always seen the term versor used to represent a unit vector, not necessarily aligned with the axes. You can found this term used with this meaning in this wikipedia page on unit vectors. Since the quaternion algebra is also a vector space, the usage of the term on quaternions is in my opinion just a specialization of the term in that context.

Share this post


Link to post
Share on other sites

apatriarca,

 

Thank you for the comment. Its interesting how we come to learn different terminologies. Somehow my path just never crossed with versor. The Wikipedia page specific to versor, http://en.wikipedia.org/wiki/Versor, introduces the quaternion use case, but also mentions the unit vector use case. There has to be some origin point for this word! I think I agree with your view.

 

Graham

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this