[source=cpp]
Rotate(float fXAmnt, float fYAmnt)
{
// UP/DOWN
D3DXMatrixRotationAxis(&gMatRotation, &vRight, D3DXToRadian(fYAmnt) );
D3DXVec3TransformCoord(&vLookatPt, &vLookatPt, &gMatRotation );
D3DXVec3TransformCoord(&vUpVec, &vUpVec, &gMatRotation );
// LEFT/RIGHT
D3DXMatrixRotationAxis(&gMatRotation, &D3DXVECTOR3(0,0,1), D3DXToRadian(-fXAmnt) );
D3DXVec3TransformCoord(&vLookatPt, &vLookatPt, &gMatRotation );
D3DXVec3TransformCoord(&vUpVec, &vUpVec, &gMatRotation );
}
Im having problems porting this(small) directX code to OpenGL
I thought porting would be easy but it aint :p
gMatRotation = matrix
vXxxxxx = vector3
fXxxxxx = float
None of these functions are provided by OpenGL (or GLU). You'll need a third party matrix/vector library, or you'll just have to end up writing them yourself. I did the latter.
Quote:hmm oke, can you show me the source of D3DXVec3TransformCoord!?
No.
DirectX isn't open source, so anyone who CAN show you the code is eitehr lying or working for microsoft (although probably not for much longer)
Do a search for "Matrix Libraries" and you'll probably find something that can provide you with very similar functionality.
The functions are straight-forward matrix multiplies. Just think of a vector as a 3x1 matrix, and do a 3x3 * 3x1 matrix multiply. (Actually you will have to do this as a 4x4 * 4x1 and then divide by w to get back to 3x1.)
[source=cpp]glPoint3f* glVec3TransformCoord(glPoint3f* pOut, const glPoint3f* pV, const GLfloat* pM){ const float x = pV->x; const float y = pV->y; const float z = pV->z; const float w = (x * pM[3] + y * pM[7] + z * pM[11] + pM[15]); /* x | 0 1 2 3 y | 4 5 6 7 z | 8 9 10 11 1 | 12 13 14 15 */ pOut->x = (x * pM[0] + y * pM[4] + z * pM[8] + pM[12]) / w; pOut->y = (x * pM[1] + y * pM[5] + z * pM[9] + pM[13]) / w; pOut->z = (x * pM[2] + y * pM[6] + z * pM[10] + pM[14]) / w; return pOut;}
Is this code correct!? I still havent fixed this and I was wondering if this fucntion could be poblem?
That looks OK, although your comment should be either one of:
Your comment shows a matrix multiplication which is not defined.
/*(x y z 1) ( 0 1 2 3) ( 4 5 6 7) ( 8 9 10 11) (12 13 14 15)*/or/*( 0 4 8 12) (x)( 1 5 9 13) (y)( 2 6 10 14) (z)( 3 7 11 15) (1)*/
Your comment shows a matrix multiplication which is not defined.
^^ yeah i was lazy to change it when i placed that comment, but i fixed it now and so did i found the problem. I had it transposed. new code below
[source=cpp]glPoint3f* glVec3TransformCoord(glPoint3f* pOut, const glPoint3f* pV, const float* pM){ const float x = pV->x; const float y = pV->y; const float z = pV->z; const float w = (x * pM[12] + y * pM[13] + z * pM[14] + pM[15]); /* 0 1 2 3 | x 4 5 6 7 | y 8 9 10 11 | z 12 13 14 15 | w */ pOut->x = (x * pM[0] + y * pM[1] + z * pM[2] + pM[3])/w; pOut->y = (x * pM[4] + y * pM[5] + z * pM[6] + pM[7])/w; pOut->z = (x * pM[8] + y * pM[9] + z * pM[10] + pM[11]/w); return pOut;}
You function is Rotate(float fXAmnt, float fYAmnt). Aren't you trying to rotate the view/object?
If you are, then why not use glRotate(Ang, X, Y, Z)?
If you are, then why not use glRotate(Ang, X, Y, Z)?
@D3DXVECTOR3: Please notice first that already your original code is wrong:
The function D3DXVec3TransformCoord does a matrix vector multiplication, yes, but for position vectors. Position vectors have a homogeneous co-ordinate w of 1 (if normalized), and so the positional part of a matrix has an effect on position vectors. Direction vectors on the other hand have a w of 0, and hence the positional part has no effect. Looking at your original code, you use D3DXVec3TransformCoord for vLookatPt, what denotes a point and hence is ok, _but_ you also use it for vUpVec, what denotes a direction vector and hence is _not_ ok. Instead, it should be D3DXVec3TransformNormal. (You may also use the most general D3DXVec3Transform if you does set-up of w yourself.)
Notice furthur that this also means that you probably makes the same mistake in your OpenGL port.
Furthurmore, in your latest posted solution you use the row major order of matrices. That _may_ be ok, depending on the remaining code, but it isn't compatible to OpenGL; OpenGL uses column major order, so you can't use your matrices for e.g. glMultMatrix w/o transposing first.
BTW: Please _don't_ name your routines and types with a gl prefix. Let that prefix be reserved for the official OpenGL consortium. You may run into naming problems in the future, and today your code becomes less readable since the originator of the routines / types is not clear to infrequent readers.
The function D3DXVec3TransformCoord does a matrix vector multiplication, yes, but for position vectors. Position vectors have a homogeneous co-ordinate w of 1 (if normalized), and so the positional part of a matrix has an effect on position vectors. Direction vectors on the other hand have a w of 0, and hence the positional part has no effect. Looking at your original code, you use D3DXVec3TransformCoord for vLookatPt, what denotes a point and hence is ok, _but_ you also use it for vUpVec, what denotes a direction vector and hence is _not_ ok. Instead, it should be D3DXVec3TransformNormal. (You may also use the most general D3DXVec3Transform if you does set-up of w yourself.)
Notice furthur that this also means that you probably makes the same mistake in your OpenGL port.
Furthurmore, in your latest posted solution you use the row major order of matrices. That _may_ be ok, depending on the remaining code, but it isn't compatible to OpenGL; OpenGL uses column major order, so you can't use your matrices for e.g. glMultMatrix w/o transposing first.
BTW: Please _don't_ name your routines and types with a gl prefix. Let that prefix be reserved for the official OpenGL consortium. You may run into naming problems in the future, and today your code becomes less readable since the originator of the routines / types is not clear to infrequent readers.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement