Question

I'm wondering if there is a difference between GLSL and HLSL Mathematics. I'm using a selfmade Engine which works with openTK fine. My SharpDx Implementation gets everyday a bit further. I'm currently working on the ModelviewProjection Matrix. To see if it works I use a simple project which works fine with OpenTK. So I changed the Shader code from GLSL to HLSL because the rest of the program uses the engine functions. The programm did not work I couldn't see the geometry, so I changed the Modelview Matrix and the Projections Matrix to the Identity Matrix. Aftwards it worked I saw the geometry. So I changed a bit of the GLSL becuase I wanted a similar GLSL code to the HLSL and I also changed the Matrixes to the identy too. Afterwards I did not see anything it didn't work.... So I'm stuck... Any of you have an Idea? Anyways long story short My HLSL Shader Code

public string Vs = @"cbuffer Variables : register(b0){
float4 Testfarbe;
float4x4 FUSEE_MVP;
} 
struct VS_IN
{
    float4 pos : POSITION;
    float4 tex : TEXCOORD;
    float4 normal : NORMAL;
};
struct PS_IN
{
    float4 pos : SV_POSITION;
    float4 col : COLOR;
    float4 tex : TEXCOORD;
    float4 normal : NORMAL;
};
PS_IN VS( VS_IN input )
{
    PS_IN output = (PS_IN)0;
    input.pos.w = 1.0f;
    output.pos = mul(input.pos,FUSEE_MVP);
    output.col = Testfarbe;
    /*output.col = FUSEE_MV._m00_m01_m02_m03;*/
/*    output.normal = input.normal;
    output.tex = input.tex;*/
 /*   if (FUSEE_MV._m00 == 4.0f)
        output.col = float4(1,0,0,1);
    else
        output.col = float4(0,0,1,1);*/

    return output;
}
";
string Ps = @"
SamplerState pictureSampler;
Texture2D imageFG;
struct PS_IN
{
    float4 pos : SV_POSITION;
    float4 col : COLOR;
    float4 tex : TEXCOORD;
    float4 normal : NORMAL;
};

float4 PS( PS_IN input ) : SV_Target
{
    return input.col;
    /*return  imageFG.Sample(pictureSampler,input.tex);*/
    }";

So I changed my old working OpenTk project to see where the difference ist between openTK and SharpDx relating to the math calculations.

The HLSL code

 public string Vs = @"
            /* Copies incoming vertex color without change.
             * Applies the transformation matrix to vertex position.
             */

            attribute vec4 fuColor;
            attribute vec3 fuVertex;
            attribute vec3 fuNormal;
            attribute vec2 fuUV;

            varying vec4 vColor;
            varying vec3 vNormal;
            varying vec2 vUV;

            uniform mat4 FUSEE_MVP;
            uniform mat4 FUSEE_ITMV;

            void main()
            {
                gl_Position = FUSEE_MVP * vec4(fuVertex, 1.0);
                /*vNormal = mat3(FUSEE_ITMV[0].xyz, FUSEE_ITMV[1].xyz, FUSEE_ITMV[2].xyz) * fuNormal;*/
                vUV = fuUV;
            }";

        public string Ps = @"
            /* Copies incoming fragment color without change. */
            #ifdef GL_ES
                precision highp float;
            #endif

            uniform vec4 vColor;
            varying vec3 vNormal;

            void main()
            {
                gl_FragColor = vColor * dot(vNormal, vec3(0, 0, 1));
            }";

In the main code itself I only read an Obj file and set the Identity matrix

    public override void Init()
        {
        Mesh = MeshReader.LoadMesh(@"Assets/Teapot.obj.model");



        //ShaderProgram sp = RC.CreateShader(Vs, Ps);
        sp = RC.CreateShader(Vs, Ps);
        _vTextureParam = sp.GetShaderParam("Testfarbe");//vColor
}
    public override void RenderAFrame()
        {

... var mtxRot = float4x4.CreateRotationY(_angleHorz) * float4x4.CreateRotationX(_angleVert); var mtxCam = float4x4.LookAt(0, 200, 500, 0, 0, 0, 0, 1, 0);

        // first mesh
        RC.ModelView = float4x4.CreateTranslation(0, -50, 0) * mtxRot * float4x4.CreateTranslation(-150, 0, 0) * mtxCam;

        RC.SetShader(sp);

        //mapping
        RC.SetShaderParam(_vTextureParam, new float4(0.0f, 1.0f, 0.0f, 1.0f));
        RC.Render(Mesh);


        Present();
}
    public override void Resize()
        {
            RC.Viewport(0, 0, Width, Height);

            float aspectRatio = Width / (float)Height;
           RC.Projection = float4x4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1, 5000);
        }

The both programs side by side SideBySide

What I should also add is as soon as the values of my ModelView identity are bigger than 1.5 I don't see anything in my window ? anyone knows that might causing this?

I edited the Post and the Image so you see a bigger difference. I had earlier in this post the identity Matrix. If I use the Identity Matrix with this Obj-File

v 0.0 0.5 0.5
v 0.5 0.0 0.5
v -0.5 0.0 0.5

vt 1 0 0
vt 0 1 0
vt 0 0 0

f 1/2 2/3 3/1

I saw in my SharpDX project the triangle and in my openTK not. But I tink the Teapot thing is a bit better to show the difference within the to project where only the Shadercode is different! I mean I could've done something wrong in the SharpDX Implementation for this Enginge but lets assume their is everything right. At least I hope so if you guys tell my the ShaderCode is just wrong ;)

I hope I could describe my problem clear so you understand it.

Was it helpful?

Solution

OK So you've to Transpose the Matrix...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top