Rendering external objects
by Jiri Schmitt · in Torque 3D Professional · 08/29/2010 (9:56 pm) · 4 replies
Hello,
I'm trying to integrate SpeedTree into Torque 3D. I have a problem with project and model view matrices settings.
SpeedTree has following function for view parameters settings:
When I set following parameters from Torque engine it does not render anything:
vcameraPos - state->getCameraPosition ()
mProjection - GFX->getProjectionMatrix ()
mModelview - GFX->getViewMatrix ()
fNearClip - state->getFrustum().getNearDist ()
fFarClip - state->getFrustum().getFarDist ()
I do not know which coordinate system Torque uses. I tried to set following coordinate systems which SpeedTree supports as default.

So I tried to create projection and modelView manually (in RenderObjectExample::render function):
Perspective function in SpeedTree:
[code]
inline void Mat4x4::Perspective(st_float32 fFieldOfView, st_float32 fAspectRatio, st_float32 fNear, st_float32 fFar)
{
Mat4x4 mTemp;
st_float32 f = 1.0f / tan(0.5f * fFieldOfView);
mTemp.m_afSingle[0] = f / fAspectRatio;
mTemp.m_afSingle[5] = f;
mTemp.m_afSingle[10] = (fFar + fNear) / (fNear - fFar);
mTemp.m_afSingle[11] = -1.0f;
mTemp.m_afSingle[14] = 2.0f * fFar * fNear / (fNear - fFar);
mTemp.m_afSingle[15] = 0.0f;
*this = *this * mTemp;
}
[code]
Now trees are rendered but there are errors. If I'm walking, trees are being moved from their position. Also when I look at their top or bottom, they are too high and stretched.



Could someone help me?
Thank you
I'm trying to integrate SpeedTree into Torque 3D. I have a problem with project and model view matrices settings.
SpeedTree has following function for view parameters settings:
st_bool CView::Set(const Vec3& vCameraPos,
const Mat4x4& mProjection,
const Mat4x4& mModelview,
st_float32 fNearClip,
st_float32 fFarClip);When I set following parameters from Torque engine it does not render anything:
vcameraPos - state->getCameraPosition ()
mProjection - GFX->getProjectionMatrix ()
mModelview - GFX->getViewMatrix ()
fNearClip - state->getFrustum().getNearDist ()
fFarClip - state->getFrustum().getFarDist ()
I do not know which coordinate system Torque uses. I tried to set following coordinate systems which SpeedTree supports as default.

So I tried to create projection and modelView manually (in RenderObjectExample::render function):
const Vec3 vOrigin; Vec3 vEye = state->getVectorEye(); Vec3 vCameraPos = state->getCameraPosition (); Mat4x4 mModelviewMatrix; if(IsReflectPass()) mModelviewMatrix.LookAt(vOrigin, Vec3(vEye.x, -vEye.y, vEye.z), CCoordSys::ConvertFromStd(0.0, 0.0, -1.0)); else mModelviewMatrix.LookAt(vOrigin, vEye, CCoordSys::ConvertFromStd(0.0, 0.0, 1.0)); mModelviewMatrix.Translate(-vCameraPos.x, -vCameraPos.y, -vCameraPos.z); Mat4x4 mProjectionMatrix; mProjectionMatrix.Perspective(state->getFrustum().getFov (), state->getFrustum().getAspectRatio (), nearClip, farClip); SpeedTree::CView.Set(vCameraPos, mProjectionMatrix, mModelviewMatrix, nearClip, farClip);
Perspective function in SpeedTree:
[code]
inline void Mat4x4::Perspective(st_float32 fFieldOfView, st_float32 fAspectRatio, st_float32 fNear, st_float32 fFar)
{
Mat4x4 mTemp;
st_float32 f = 1.0f / tan(0.5f * fFieldOfView);
mTemp.m_afSingle[0] = f / fAspectRatio;
mTemp.m_afSingle[5] = f;
mTemp.m_afSingle[10] = (fFar + fNear) / (fNear - fFar);
mTemp.m_afSingle[11] = -1.0f;
mTemp.m_afSingle[14] = 2.0f * fFar * fNear / (fNear - fFar);
mTemp.m_afSingle[15] = 0.0f;
*this = *this * mTemp;
}
[code]
Now trees are rendered but there are errors. If I'm walking, trees are being moved from their position. Also when I look at their top or bottom, they are too high and stretched.



Could someone help me?
Thank you
#2
If I'm using eye vector instead of modelview matrix it works, but for azimuth only:
08/30/2010 (7:44 am)
Thank you, Chris. I used it, but it does not work with Torque matrixes. Maybe I'm getting project (GFX->getProjectionMatrix ()) and model view matrixes (GFX->getViewMatrix ()) incorrectly. If I look how SpeedTree computes azimuth and pitch it does not work with this view matrix.Mat4x4 modelView = GFX->getViewMatrix (); Vec3 cameraDir(modelView[2], modelView[6], modelView[10]); float m_fAzimuth = atan2f(cameraDir.y, cameraDir.x); float m_fPitch = asinf(cameraDir.z);
If I'm using eye vector instead of modelview matrix it works, but for azimuth only:
Vec3 cameraDir = GFX->getVectorEye (); float m_fAzimuth = atan2f(cameraDir.y, cameraDir.x); float m_fPitch = asinf(cameraDir.z);
#3
Projecttion:
Torque SpeedTree
---------------------------
[ 0] 0.977419 0.977419
[ 1] 0.000000 0.000000
[ 2] 0.000000 0.000000
[ 3] 0.000000 0.000000
[ 4] 0.000000 0.000000
[ 5] -0.000000 1.303225
[ 6] 1.303225 0.000000
[ 7] 0.000000 0.000000
[ 8] 0.000000 0.000000
[ 9] 1.000100 0.000000
[10] 0.000000 -1.000200
[11] -0.100010 -1.000000
[12] 0.000000 0.000000
[13] 1.000000 0.000000
[14] 0.000000 -0.200020
[15] 0.000000 0.000000
Model View:
Torque SpeedTree
---------------------------
[ 0] 1.000000 0.000000
[ 1] 0.000000 -0.049979
[ 2] 0.000000 -0.998750
[ 3] 0.000000 0.000000
[ 4] 0.000000 -1.000000
[ 5] 1.000000 0.000000
[ 6] 0.000000 0.000000
[ 7] 0.000000 0.000000
[ 8] 0.000000 0.000000
[ 9] 0.000000 0.998750
[10] 1.000000 -0.049979
[11] 0.000000 0.000000
[12] 0.000000 0.004331
[13] 0.000000 -1.700378
[14] 0.000000 0.092607
[15] 1.000000 1.000000
08/30/2010 (8:57 am)
I compared matrixes from Torque and SpeedTree:Projecttion:
Torque SpeedTree
---------------------------
[ 0] 0.977419 0.977419
[ 1] 0.000000 0.000000
[ 2] 0.000000 0.000000
[ 3] 0.000000 0.000000
[ 4] 0.000000 0.000000
[ 5] -0.000000 1.303225
[ 6] 1.303225 0.000000
[ 7] 0.000000 0.000000
[ 8] 0.000000 0.000000
[ 9] 1.000100 0.000000
[10] 0.000000 -1.000200
[11] -0.100010 -1.000000
[12] 0.000000 0.000000
[13] 1.000000 0.000000
[14] 0.000000 -0.200020
[15] 0.000000 0.000000
Model View:
Torque SpeedTree
---------------------------
[ 0] 1.000000 0.000000
[ 1] 0.000000 -0.049979
[ 2] 0.000000 -0.998750
[ 3] 0.000000 0.000000
[ 4] 0.000000 -1.000000
[ 5] 1.000000 0.000000
[ 6] 0.000000 0.000000
[ 7] 0.000000 0.000000
[ 8] 0.000000 0.000000
[ 9] 0.000000 0.998750
[10] 1.000000 -0.049979
[11] 0.000000 0.000000
[12] 0.000000 0.004331
[13] 0.000000 -1.700378
[14] 0.000000 0.092607
[15] 1.000000 1.000000
#4
We are also looking at speedtree with Beta2.
My email address is chris@iceisfun.com
Thanks!
08/30/2010 (12:37 pm)
Jiri -- Could we talk via email about your SpeedTree implementation?We are also looking at speedtree with Beta2.
My email address is chris@iceisfun.com
Thanks!
Associate Chris Robertson