Проблема с перерисовкой в шейдерах
От: SedRick  
Дата: 01.02.06 20:17
Оценка:
hi all

Люди написал свой первый шейдер, но эта тварь выдает какую-то гнусную перерисовку.
Эффект такой — выдается форма без всякого заполнения и лишь когда я гачинаю перемещать мышой окно тогда на секунды плявляется все что я хотел и мелькает с изображения на пустое окно.
Вот код программы (извините за объем, но как сделать это иначе нинаю ) :

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace VertexShaderTriang
{
    public class Form1 : Form
    {
        #region Global vars
        private Device device = null;
        private Mesh mesh = null;
        private Effect effect = null;
        private VertexDeclaration decl = null;
        private Matrix worldMatrix, viewMatrix, projMatrix;
        private float angle = 0;
        string errors = "";
        #endregion

        #region Init

        public Form1()
        {
            this.Size = new Size(1024, 768);
            this.Text = "Vertex shader triangle";
            Directory.SetCurrentDirectory(Application.StartupPath);
        }
        public bool InitDX()
        {
            PresentParameters pp = new PresentParameters();
            pp.Windowed = true;
            pp.SwapEffect = SwapEffect.Discard;
            pp.AutoDepthStencilFormat = DepthFormat.D16;
            pp.EnableAutoDepthStencil = true;
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp);
            effect = Effect.FromFile(device, @"..\..\simple.fx", null, ShaderFlags.None, null, out errors);
            if(errors.Length > 0)
                MessageBox.Show("ERROR !\n" + errors);
            effect.Technique = "TransformDiffuse";
            projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4, 4 / 3, 1, 100);
            viewMatrix = Matrix.LookAtLH(
                new Vector3(0, 0, 5),
                new Vector3(),
                new Vector3(0, 1, 0));
            VertexElement[] elements = new VertexElement[]{                
                new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                VertexElement.VertexDeclarationEnd
            };
            decl = new VertexDeclaration(device, elements);
            mesh = Mesh.Cylinder(device, 1.5f, 1.5f, 1.5f, 36, 36);
            return true;
        }
        #endregion
        #region Render procs

        private void UpdateWorld()
        {
            angle += 0.4f;
            if(angle >= 360)
                angle = 0;
            worldMatrix = Matrix.RotationAxis(new Vector3(angle/2, angle/4, angle/6), (float)Math.PI / 4);
            Matrix worldViewProj = worldMatrix * viewMatrix * projMatrix;
            effect.SetValue("WorldViewProj", worldViewProj);
        }
        #endregion
        #region Overrides

        protected override void OnPaint(PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Red, 1, 0);
            UpdateWorld();
            device.BeginScene();
            device.VertexDeclaration = decl;
            int numPasses = effect.Begin(0);
            for(int i = 0; i < numPasses; i++)
            {
                effect.Pass(i);
                mesh.DrawSubset(0);
            }
            effect.End();
            device.EndScene();
            device.Present();
            this.Invalidate();
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if((byte)e.KeyChar == (byte)Keys.Escape)
                this.Close();
        }

        #endregion

        static void Main() 
        {    
            using(Form1 frm = new Form1())
            {
                frm.Show();
                frm.InitDX();                    
                Application.Run(frm);
            }
        }
    }
}


и текст шейдера :

struct VS_OUTPUT
{
    float4 pos : POSITION;
    float4 diff : COLOR0;
};

float4x4 WorldViewProj : WORLDVIEWPROJECTION;
float3 LightDir = {0, 0, -1};

VS_OUTPUT Transform(float4 pos : POSITION, float3 normal : NORMAL)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
    float4 tranceformedNormal = mul(normal, WorldViewProj);
    Out.diff.r = 1;
    Out.diff.b = 1;
    Out.diff.ga = 1;
    Out.diff *= dot(tranceformedNormal, LightDir);
    Out.pos = mul(pos, WorldViewProj);
    
    return Out;
}

technique TransformDiffuse
{
    pass P0
    {
        CullMode = None;
        VertexShader = compile vs_1_1 Transform();
        PixelShader = NULL;
    }
}


ПАМАГИТЕ ПЛЗ !!!!!
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.