Exemple #1
0
        static void gpu_OnSetConstants(MaterialRendererServices services, int userData)
        {
            VideoDriver driver = services.VideoDriver;

            if (useHighLevelShaders && shaderFirstUpdate)
            {
                shaderWorldViewProjId = services.GetVertexShaderConstantID("mWorldViewProj");
                shaderTransWorldId    = services.GetVertexShaderConstantID("mTransWorld");
                shaderInvWorldId      = services.GetVertexShaderConstantID("mInvWorld");
                shaderLightPosId      = services.GetVertexShaderConstantID("mLightPos");
                shaderLightColorId    = services.GetVertexShaderConstantID("mLightColor");

                // textures id are important only for OpenGL interface
                if (driver.DriverType == DriverType.OpenGL)
                {
                    shaderTextureId = services.GetVertexShaderConstantID("myTexture");
                }

                shaderFirstUpdate = false;
            }

            // set inverted world matrix
            // if we are using highlevel shaders (the user can select this when
            // starting the program), we must set the constants by name

            Matrix invWorld = driver.GetTransform(TransformationState.World);

            invWorld.MakeInverse();

            if (useHighLevelShaders)
            {
                services.SetVertexShaderConstant(shaderInvWorldId, invWorld.ToArray());
            }
            else
            {
                services.SetVertexShaderConstant(0, invWorld.ToArray());
            }

            // set clip matrix

            Matrix worldViewProj = driver.GetTransform(TransformationState.Projection);

            worldViewProj *= driver.GetTransform(TransformationState.View);
            worldViewProj *= driver.GetTransform(TransformationState.World);

            if (useHighLevelShaders)
            {
                services.SetVertexShaderConstant(shaderWorldViewProjId, worldViewProj.ToArray());
            }
            else
            {
                services.SetVertexShaderConstant(4, worldViewProj.ToArray());
            }

            // set camera position

            Vector3Df pos = device.SceneManager.ActiveCamera.AbsolutePosition;

            if (useHighLevelShaders)
            {
                services.SetVertexShaderConstant(shaderLightPosId, pos.ToArray());
            }
            else
            {
                services.SetVertexShaderConstant(8, pos.ToArray());
            }

            // set light color

            Colorf col = new Colorf(0, 1, 1, 0);

            if (useHighLevelShaders)
            {
                services.SetVertexShaderConstant(shaderLightColorId, col.ToArray());
            }
            else
            {
                services.SetVertexShaderConstant(9, col.ToArray());
            }

            // set transposed world matrix

            Matrix transpWorld = driver.GetTransform(TransformationState.World).Transposed;

            if (useHighLevelShaders)
            {
                services.SetVertexShaderConstant(shaderTransWorldId, transpWorld.ToArray());
                services.SetPixelShaderConstant(shaderTextureId, new int[] { 0 });
            }
            else
            {
                services.SetVertexShaderConstant(10, transpWorld.ToArray());
            }
        }