XNA Game Development Forums
2012/05/18 06:13:14 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Shaders and rotating models....  (Read 2827 times)
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« on: 2007/02/27 13:46:42 »

I have a problem with one of my shaders, I took it from the Microsoft shader tutorial and the first issue I had with it was that when I placed an object in the scene that was rendered with it that it flipped the objects position. For example if I have the object a position of (0,0,-8) this would normally put the object directly in front of my camera, but with this shader it placed it behind me. I figured this shader was for a left handed system where XNA is right (maybe it's the other way around) so I decided to flip the variables in the shader to fix this, so setting the World *= -1 etc.. and this worked a treat.

Now the issue I have is that when I rotate the model; it's position in the world is also rotated, for example, if I rotate the object with object.Rotate(new Vector3(0,1,0),MathHelper.PiOver2) the object then has the same position value but in the rendered world it appears to the right of my camera. I have fixed this issue but I would like a better solution. The fix is in the SetParameters of the shader class and all it does is swap the way the World matrix is calculated from scale * rotation * position to scale * position * rotation

Here is my fix:
Code:
            Matrix World;
            if (!myObject.AlwaysFacingCamera)
            {
                if (myObject.UseLeftHandedWorldCalc)
                {
                    World = Matrix.CreateScale(myObject.Scaling) *
                            Matrix.CreateTranslation(myObject.Position) *
                            Matrix.CreateFromQuaternion(myObject.Rotation);                   
                }
                else
                {
                    World = Matrix.CreateScale(myObject.Scaling) *
                            Matrix.CreateFromQuaternion(myObject.Rotation) *
                            Matrix.CreateTranslation(myObject.Position);
                }
            }
            else
            {
                World = Matrix.CreateScale(myObject.Scaling) *
                            Matrix.CreateFromQuaternion(RCCameraManager.ActiveCamera.Rotation*-1) *
                            Matrix.CreateTranslation(myObject.Position);
            }

This works a treat now but I would like a better solution to this, but my weak shader knowledge has stopped me from being able to convert the shader. If any of you guys could help that would be great  Wink If there is a better way of doing this in code I would not be adverse to that either, but a fix to the shader or a guide on how to do this would be best.

Here is the shader after my first fix
Code:
float3 DirFromLight < string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};
float3 DirFromSky < string UIDirectional = "Direction from Sky"; > = { 0.0f, -1.0f, 0.0f };           

// light intensity
float4 LightAmbientIntensity    = { 0.8f, 0.8f, 0.8f, 1.0f };    // ambient
float4 LightSpecularColor       = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular

// material reflectivity
float4 MaterialAmbientIntensity = { 0.5f, 0.5f, 0.5f, 1.0f };    // ambient
float4 MaterialSpecularColor    = { 0.2f, 0.2f, 0.2f, 1.0f };    // specular
int    MaterialSpecularPower    = 32;                            // power

// transformations
float4x3 World  : WORLD;
float4x3 View   : VIEW;
float4x4 ViewProjection : VIEWPROJECTION;
float3 CameraPos : CAMERAPOSITION;   

texture EnvironmentMap
<
    string type = "CUBE";
    string name = "default_reflection.dds";
>;


samplerCUBE EnvironmentSampler = sampler_state
{
    Texture = (EnvironmentMap);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};


float4 CubeMapLookup(float3 CubeTexcoord)
{   
    return texCUBE(EnvironmentSampler, CubeTexcoord);
}

float3 CalcReflectionVector(float3 ViewToPos, float3 Normal)
{
   return reflect(ViewToPos, Normal);
}


float4 CalcAmbient()
{
    return LightAmbientIntensity * MaterialAmbientIntensity;
}
float4 CalcSpecular(float3 Normal, float3 DirFromLight, float3 EyeToVertex)
{
    float3 R = normalize(reflect(DirFromLight, Normal));
    return MaterialSpecularColor * LightSpecularColor * pow(max(0, dot(R, -EyeToVertex)), MaterialSpecularPower/4);
}

// vertex shader output structure
struct VS_OUTPUT
{
    float4 Pos  : POSITION;
    float4 Diff : COLOR0;
    float4 Spec : COLOR1;
    float3 CubeTexcoord : TEXCOORD0;
};

VS_OUTPUT VS(
    float3 InPos  : POSITION,   // Vertex position in model space
    float3 InNormal : NORMAL,   // Vertex normal in model space
    float  Occ  : TEXCOORD0)    // Occlusion factor
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    // Fix as this was from a left handed system.
    World = World * -1;
    InPos = InPos * -1;

    // transform the position and normal
    float3 Pos = mul(float4(InPos, 1), (float4x3)World);         // position (view space)   
    float3 Normal = normalize(mul(InNormal, (float3x3)World));   // normal (view space)
    float3 EyeToVertex = normalize(Pos - CameraPos);             // vector from vertex towards eye


    Out.Pos  = mul(float4(Pos, 1), ViewProjection);              // position (projected)   
   
    Out.Diff += CalcAmbient();
   
    Out.Spec += CalcSpecular(Normal, DirFromLight, EyeToVertex);

    Out.CubeTexcoord = CalcReflectionVector(EyeToVertex, Normal);
   
    return Out;
}

float4 PS ( VS_OUTPUT In) : COLOR
{
    float4 OutColor;
   
    float4 Environment = CubeMapLookup(In.CubeTexcoord);
   
    OutColor = lerp(In.Diff, Environment, 0.25);

    OutColor += In.Spec;

    return OutColor;
}


technique Go
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS();
        PixelShader  = compile ps_2_0 PS();
    }
}

Thanks.
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #1 on: 2007/02/28 08:06:54 »

Translate * Rotate <> Rotate * Translate

If you Translate and then Rotate a model, it will rotate around the world origin, not around it's current point,  hence why you're getting unexpected results.

So Rotate the model first, and then Translate it to it's position in 3D worldspace.

Of course, doing it the other way can be useful, for example doing orbitals.
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #2 on: 2007/02/28 13:43:39 »

OK,

This is probably me not understanding the Matrix objects, but, if that was the case then one of my world calculations would work for ALL my shaders. Yet for one set of shaders I need one World calcualtion and for the others I need another World calcualtion.

I guess you will have to explain this one face to face when you buy me that pint  Wink
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #3 on: 2007/03/03 17:42:12 »

Can any of you guys let me know if I am barking up the right tree with this? I just want to know if the behavior I am getting is due the the "handedness" of the shader and XNA....

I am happy with my current solution to the issue, but would like a confirmation on what is actually happening here.

Mike, Chr0n1x

Any ideas?

Thanks.
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Tutorial 3 :: More Rendering Complexity - Shaders and Cameras Tutorial Discussion « 1 2 3 4 » mikeschuld 52 17433 Last post 2009/12/12 17:04:40
by Mikeske
Moving the models aroung Hazy Mind XNA Engine Squeezle42 6 2476 Last post 2007/03/07 14:40:48
by Squeezle42
How can I speed up shaders?? General Discussion EclipsE 7 2807 Last post 2007/04/27 00:26:21
by Chr0n1x
Multiple postscreen shaders Hazy Mind XNA Engine EclipsE 2 1670 Last post 2007/04/29 21:50:58
by Chr0n1x
Switching shaders. Motion Blur. Shadows. Hazy Mind XNA Engine amartinez1660 0 977 Last post 2007/05/07 00:01:25
by amartinez1660
Nice models and free models, new site! General Discussion 3d4ya 2 3054 Last post 2010/08/03 03:05:01
by ThackeG67
Texturing Models Hazy Mind XNA Engine Peregrinati 4 2106 Last post 2008/12/15 12:14:41
by Peregrinati
xsi models not displaying correctly Hazy Mind XNA Engine crazyhor77 2 1474 Last post 2009/09/21 22:48:16
by crazyhor77
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.69 seconds with 18 queries.