www.quadsoftware.com
FAQ  FAQ   Search  Search   Memberlist  Memberlist   Usergroups  Usergroups
Register  ::  Log in Log in to check your private messages


Post new topic  Reply to topic
 About tex_matrix in sTextureMapping structure. « View previous topic :: View next topic » 
Author Message
takuhiro
PostPosted: Sat Oct 09, 2010 5:21 am    Post subject: About tex_matrix in sTextureMapping structure. Reply with quote



Joined: 02 Dec 2009
Posts: 29

Hi.

There is a variable named 'tex_matrix' in sTextureMapping structure.

Please teach the computational method of this matrix.
Is it computable by the use of scale, rotation, offset and texgen_planes ?
Is there a function to calculate this matrix ?
Back to top
View user's profile Send private message
ALicu
PostPosted: Sat Oct 09, 2010 7:47 am    Post subject: Reply with quote



Joined: 12 Feb 2007
Posts: 1326

Hi,

The tex_matrix is the matrix Grome (which is OpenGL based) is using internally. It is made from texture spinning (rotation around up vector, not vertical rotation on horizontal and vertical axes), texture offset and texture scaling (tiling). When you have vertical mapping (texture rotation for U and V other than 0, 0) texgen_planes (texture generation planes in OpenGL terminology) are also used to generate (project) the texture coordinates on a certain orientation and after that tex_matrix is applied.

In DirectX you need to recompute this matrix since it has a different coordinate system, plus it doesn't have texture generation planes.

Here is another thread with more details how to compute this matrix (for both DirectX (left hand) and OpenGL (right hand)):

http://www.quadsoftware.com/forum/viewtopic.php?t=505

And another:

http://www.quadsoftware.com/forum/viewtopic.php?t=150

Let me know if you have any problems with it.

Best Regards,
Adrian L.
Back to top
View user's profile Send private message
takuhiro
PostPosted: Tue Oct 12, 2010 12:22 pm    Post subject: Reply with quote



Joined: 02 Dec 2009
Posts: 29

Thank you for your fast answer.
It seems to have been able to calculate same texture matrix by sample.
However, I do not understand the method of actually using it.
How should I do to use it on Direct3D11 application ?
SetTextureStageState method seems not to exist in Direct3D11.
It was not correctly displayed in the following shader programs.

Code:

struct VS_INPUT
{
   float4 Pos : POSITION ;
   float2 Tex : TEXCOORD0 ;
};

struct PS_INPUT
{
   float4 Pos : SV_POSITION ;
   float2 Tex : TEXCOORD0 ;
};

PS_INPUT VS( VS_INPUT input )
{
   PS_INPUT   output = (PS_INPUT)0 ;
   float4      f4Tex ;

   f4Tex.x = input.Tex.x ;
   f4Tex.y = 0.0f ;
   f4Tex.z = input.Tex.y ;
   f4Tex.w = 0.0f ;

   f4Tex = mul( f4Tex, matTex ) ;

   output.Pos      = mul( input.Pos, matWorld ) ;
   output.Pos      = mul( output.Pos, matView ) ;
   output.Pos      = mul( output.Pos, matProjection ) ;
   output.Tex.xy   = f4Tex.xz ;

   return output ;
}


How should I do to display it correctly ?
Back to top
View user's profile Send private message
ALicu
PostPosted: Tue Oct 12, 2010 12:42 pm    Post subject: Reply with quote



Joined: 12 Feb 2007
Posts: 1326

Yes, DirectX11 (and 10) doesn't have SetTextureStageState since it doesn't have any fixed pipeline functionality. And indeed you need to apply the texture matrix in the shaders.

By looking at your shaders, I see immediately one error. Instead of:

Code:

   f4Tex.x = input.Tex.x ;
   f4Tex.y = 0.0f ;
   f4Tex.z = input.Tex.y ;
   f4Tex.w = 0.0f ;


you need to use:

Code:

   f4Tex.x = input.Tex.x ;
   f4Tex.y = input.Tex.y ;
   f4Tex.z = 0.0f ;
   f4Tex.w = 1.0f ;


Since the UV texture coordinates are the first two in the vector. They have nothing to do with the way texture is mapped in 3D (so the second one is Z, while up, OY, is not used). They are just x and y when multiplying with the texture matrix.

Also to be noted that the original terrain texture coordinates don't need to be provided as a vertex attribute (in TEXCOORD0), they can be easily compiled on the fly by using an unitary mapping on the terrain zone plane. Something like this:

Code:


float4 UnitTexMap;
UnitTexMap.x = input.Pos.x * RecipAreaSize + 0.5f;
UnitTexMap.y = input.Pos.z * RecipAreaSize + 0.5f;
UnitTexMap.z = 0.0f;
UnitTexMap.w = 1.0f;

Out.Tex.xy = mul(UnitTexMap, matTex ).xy;



Where RecipAreaSize is the inverse size of a terrain zone = 1.0 / (TileSize * TilesNo).

Regards,
Adrian L.
Back to top
View user's profile Send private message
takuhiro
PostPosted: Wed Oct 13, 2010 7:09 am    Post subject: Reply with quote



Joined: 02 Dec 2009
Posts: 29

By the way, texture matrix acquired from Grome2 plugin program is seen for Left-handed coordinate system.

Scale U = 5
Scale V = 5
Offset U = 0
Offset V = 0
Rotation Spin = 45 deg
Rotation H = 0 deg
Rotation V = 0 deg


matTex._11 = 3.535534 ; matTex._12 = 0.707106 ; matTex._13 = -0.000000 ; matTex._14 = 0.000000 ;
matTex._21 = -3.535533 ; matTex._22 = 0.707106 ; matTex._23 = 0.000000 ; matTex._24 = 0.000000 ;
matTex._31 = 0.000000 ; matTex._32 = 0.000000 ; matTex._33 = 1.000000 ; matTex._34 = 0.000000 ;
matTex._41 = 0.000000 ; matTex._42 = 0.000000 ; matTex._43 = 0.000000 ; matTex._44 = 1.000000 ;

It is different when calculating by the sample program for Right-handed coordinate system.

matTex._11 = 3.535534 ; matTex._12 = 0.707107 ; matTex._13 = -0.000000 ; matTex._14 = 0.000000 ;
matTex._21 = -3.535534 ; matTex._22 = 0.707107 ; matTex._23 = 0.000000 ; matTex._24 = 0.000000 ;
matTex._31 = 0.000000 ; matTex._32 = 0.000000 ; matTex._33 = 1.000000 ; matTex._34 = 0.000000 ;
matTex._41 = 3.535534 ; matTex._42 = 0.292893 ; matTex._43 = 0.000000 ; matTex._44 = 1.000000 ;

Isn't there means to acquire for Right-handed coordinate system ?
Back to top
View user's profile Send private message
takuhiro
PostPosted: Wed Oct 13, 2010 9:20 am    Post subject: Reply with quote



Joined: 02 Dec 2009
Posts: 29

Could you see because it sends the program that I made?
Or, could you provide Direct3D11 program that operates for Right-handed coordinate system because programming is unskilled.
Back to top
View user's profile Send private message
takuhiro
PostPosted: Thu Oct 14, 2010 7:01 am    Post subject: Reply with quote



Joined: 02 Dec 2009
Posts: 29

I do not understand the usage of texture generation planes.
I think that I become the following when the sample is used as it is.

Code:

matTexGen.m[0][0] = f4TexPlaneS.x ;   // texgen_planes 0。
matTexGen.m[1][0] = f4TexPlaneS.y ;
matTexGen.m[2][0] = f4TexPlaneS.z ;
matTexGen.m[3][0] = f4TexPlaneS.w ;

matTexGen.m[0][1] = f4TexPlaneT.x ;   // texgen_planes 1。
matTexGen.m[1][1] = f4TexPlaneT.y ;
matTexGen.m[2][1] = f4TexPlaneT.z ;
matTexGen.m[3][1] = f4TexPlaneT.w ;

matWorldView   = XMMatrixMultiply( matWorld, matView ) ;
matInvWorldView = XMMatrixInverse( &vecTemp, matWorldView ) ;
matTemp      = XMMatrixMultiply( matInvWorldView, matTexGen ) ;
matTex      = XMMatrixMultiply( matTemp, matTex ) ;


Shader is as follows.

Code:

struct VS_INPUT
{
    float4 Pos : POSITION ;
    float2 Tex : TEXCOORD0 ;
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION ;
    float2 Tex : TEXCOORD0 ;
};

PS_INPUT VS( VS_INPUT input )
{
   PS_INPUT   output = (PS_INPUT)0 ;
   float4      f4Tex ;

   f4Tex.x = input.Tex.x ;
   f4Tex.y = input.Tex.y ;
   f4Tex.z = 0.0f ;
   f4Tex.w = 1.0f ;

   f4Tex = mul( f4Tex, matTex ) ;

   output.Pos      = mul( input.Pos, World ) ;
   output.Pos      = mul( output.Pos, View ) ;
   output.Pos      = mul( output.Pos, Projection ) ;
   output.Tex.xy   = f4Tex.xy ;

   return output ;
}


However, it is not possible to display it correctly by this program.
How should I do?
Back to top
View user's profile Send private message
ALicu
PostPosted: Thu Oct 14, 2010 11:55 am    Post subject: Reply with quote



Joined: 12 Feb 2007
Posts: 1326

Hi,

Your code seems to be correct.

Are you sure XMMatrixMultiply is identical in functionality with gMultMat which is:

Code:

inline void gMultMat(t_float4x4 &mat, const t_float4x4& m1, const t_float4x4& m2)
{
   // Fill local variables first, this will ensure compiler that m1 members are different than m2 members

   float m1m00 = m1.m[0][0], m1m01 = m1.m[0][1], m1m02 = m1.m[0][2], m1m03 = m1.m[0][3];
   float m1m10 = m1.m[1][0], m1m11 = m1.m[1][1], m1m12 = m1.m[1][2], m1m13 = m1.m[1][3];
   float m1m20 = m1.m[2][0], m1m21 = m1.m[2][1], m1m22 = m1.m[2][2], m1m23 = m1.m[2][3];
   float m1m30 = m1.m[3][0], m1m31 = m1.m[3][1], m1m32 = m1.m[3][2], m1m33 = m1.m[3][3];

   float m2m00 = m2.m[0][0], m2m01 = m2.m[0][1], m2m02 = m2.m[0][2], m2m03 = m2.m[0][3];
   float m2m10 = m2.m[1][0], m2m11 = m2.m[1][1], m2m12 = m2.m[1][2], m2m13 = m2.m[1][3];
   float m2m20 = m2.m[2][0], m2m21 = m2.m[2][1], m2m22 = m2.m[2][2], m2m23 = m2.m[2][3];
   float m2m30 = m2.m[3][0], m2m31 = m2.m[3][1], m2m32 = m2.m[3][2], m2m33 = m2.m[3][3];
   
   mat.m[0][0] = m1m00*m2m00 + m1m01*m2m10
         + m1m02*m2m20 + m1m03*m2m30;
   mat.m[0][1] = m1m00*m2m01 + m1m01*m2m11
         + m1m02*m2m21 + m1m03*m2m31;
   mat.m[0][2] = m1m00*m2m02 + m1m01*m2m12
         + m1m02*m2m22 + m1m03*m2m32;
   mat.m[0][3] = m1m00*m2m03 + m1m01*m2m13
         + m1m02*m2m23 + m1m03*m2m33;

   mat.m[1][0] = m1m10*m2m00 + m1m11*m2m10
         + m1m12*m2m20 + m1m13*m2m30;
   mat.m[1][1] = m1m10*m2m01 + m1m11*m2m11
         + m1m12*m2m21 + m1m13*m2m31;
   mat.m[1][2] = m1m10*m2m02 + m1m11*m2m12
         + m1m12*m2m22 + m1m13*m2m32;
   mat.m[1][3] = m1m10*m2m03 + m1m11*m2m13
         + m1m12*m2m23 + m1m13*m2m33;

   mat.m[2][0] = m1m20*m2m00 + m1m21*m2m10
         + m1m22*m2m20 + m1m23*m2m30;
   mat.m[2][1] = m1m20*m2m01 + m1m21*m2m11
         + m1m22*m2m21 + m1m23*m2m31;
   mat.m[2][2] = m1m20*m2m02 + m1m21*m2m12
         + m1m22*m2m22 + m1m23*m2m32;
   mat.m[2][3] = m1m20*m2m03 + m1m21*m2m13
         + m1m22*m2m23 + m1m23*m2m33;

   mat.m[3][0] = m1m30*m2m00 + m1m31*m2m10
         + m1m32*m2m20 + m1m33*m2m30;
   mat.m[3][1] = m1m30*m2m01 + m1m31*m2m11
         + m1m32*m2m21 + m1m33*m2m31;
   mat.m[3][2] = m1m30*m2m02 + m1m31*m2m12
         + m1m32*m2m22 + m1m33*m2m32;
   mat.m[3][3] = m1m30*m2m03 + m1m31*m2m13
         + m1m32*m2m23 + m1m33*m2m33;
}



Also, is the matrix not working only when you have vertical mapping, or is it not working for all cases (when you have only tiling for example)?

Can you post some screenshots about the texturing in Grome compared with the ones in your program?

Regards,
Adrian L.
Back to top
View user's profile Send private message
takuhiro
PostPosted: Tue Oct 19, 2010 1:20 pm    Post subject: Reply with quote



Joined: 02 Dec 2009
Posts: 29

I cannot reproduce texture mapping that uses texgen planes in Direct3D10 application.
Please make a simple Direct3D10 program.

Crying or Very sad
Back to top
View user's profile Send private message
ALicu
PostPosted: Wed Oct 20, 2010 12:34 pm    Post subject: Reply with quote



Joined: 12 Feb 2007
Posts: 1326

Hi,

Making a "simple" Direct3D 10 program to load, update and render a terrain is not a very easy task. Graphite (our renderer) supports only DX9 and OpenGL for now so I cannot use the code directly from there.

Can you send me (you can use my address licu at quadsoftware.com) what you have made so far and I will try to help you?

Regards,
Adrian L.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Based on a template by Dustin Baccetti
Powered by phpBB © 2001, 2005 phpBB Group