Logo

¡Bienvenido a Stratos!

Acceder

Foros



[c#.net] Directx Question

Iniciado por royteusink, 23 de Febrero de 2006, 01:37:29 PM

« anterior - próximo »

royteusink

 Haay Haddd Team,

I have a question about fx shaders :blink:, How can i apply this shader to model / texture??
Please help i can't figured it out :(
It returns a black vertex :S

The fx file works with FX Composer from Nvidia.

Bump.fx:

float Script : STANDARDSGLOBAL <
   string UIWidget = "none";
   string ScriptClass = "object";
   string ScriptOrder = "standard";
   string ScriptOutput = "color";
   string Script = "Technique=Technique?NoPixelShader;";
> = 0.8;

// World Inverse or Model Inverse matrix
float4x4 WorldIMatrix : WorldInverse;

// Model*View*Projection
float4x4 WorldViewProj : WorldViewProjection;


texture diffuseTexture : DIFFUSE
<
   string ResourceName = "rockwall.dds";
   string ResourceType = "2D";
>;

texture normalMap : NORMAL
<
   string ResourceName = "rockwall_normal.dds";
   string ResourceType = "2D";
>;


float4 BumpHeight
<
> = { 1.0, 1.0, 0.5, 0.0};

float4 LightPos : Position
<
   string Object = "PointLight";
   string Space = "World";
> = { 100.0f, 100.0f, 100.0f, 0.0f };


technique NoPixelShader <string Script = "Pass=p0;";>
{
   pass p0 <string Script = "Draw=geometry;";>
   {
       VertexShaderConstant[0] = <WorldIMatrix>;         // c0-c3 is Model matrix
       VertexShaderConstant[4] = <WorldViewProj>;            // c4-c7 is ModelViewProjection matrix
       VertexShaderConstant[8] = <WorldIMatrix>;
       VertexShaderConstant[12] = <LightPos>;
       VertexShaderConstant[13] = {0.5, 0.5, 0.5, 1.0};
       VertexShaderConstant[14] = <BumpHeight>;

       VertexShader = asm
       {
           vs.1.1                    // version number

           dcl_position v0
           dcl_normal v3
           dcl_texcoord0 v7
           dcl_tangent0 v8
           dcl_binormal0 v9
           dcl_color0 v5
           
           m4x4 oPos, v0, c4        // pos in screen space.
           mov oT0, v7                // tex coords for normal map
           mov oT1, v7                // tex coords for diffuse tex.

           mov r0, c12
           dp3 r0.w, r0, r0        // normalize light dir.
           rsq r0.w, r0.w
           mul r0, r0, r0.w    

           // Transform Light to Object space
           dp3 r2.x, c8, r0        // Tangent dot Light
           dp3 r2.y, c9, r0        // Binormal dot Light
           dp3 r2.z, c10, r0        // Normal dot Light

           // Transform Light to tangent space.
           dp3 r1.x, v8, r2        // Tangent dot Light
           dp3 r1.y, v9, r2        // Binormal dot Light
           dp3 r1.z, v3, r2        // Normal dot Light
           sge r1.w, r1.x, r1.x

           mov r0, c14
           add r0, r0, r0          // *= 2
           mul r0, r1, r0          // *= bump height

           mad oD0, r0, c13, c13   // put in diffuse
       };

        Zenable = true;
       ZWriteEnable = true;
       CullMode = None;
       
       Texture[0] = <normalMap>;
       MinFilter[0] = Linear;
       MagFilter[0] = Linear;
       MipFilter[0] = Linear;

       Texture[1] = <diffuseTexture>;
       MinFilter[1] = Linear;
       MagFilter[1] = Linear;
       MipFilter[1] = Linear;

       ColorOp[0] = DotProduct3;
       AlphaOp[0] = SelectArg1;
       ColorArg1[0] = Texture;
       ColorArg2[0] = Diffuse;
       AlphaArg1[0] = Texture;
       AlphaArg2[0] = Diffuse;

       ColorOp[1] = Modulate;
       ColorArg1[1] = Current;
       ColorArg2[1] = Texture;

       AlphaOp[1] = SelectArg1;
   }
}


Program Source:

//2005/07/11. TheZBuffer.com. Updated for June 2005 SDK
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Chapter11Code
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
       private Device device = null;
       private VertexBuffer vb = null;
       private Effect effect = null;
       private VertexDeclaration decl = null;
 private VolumeTexture diffuseTexture;
 private VolumeTexture normalMap;
       // Our matrices
       private Matrix worldMatrix;
       private Matrix viewMatrix;
       private Matrix projMatrix;

       /// <summary>
 /// Required designer variable.
 /// </summary>
 private System.ComponentModel.Container components = null;
       private float angle = 0.0f;

 public Form1()
 {
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();

           this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
 }

       /// <summary>
       /// We will initialize our graphics device here
       /// </summary>
       public bool InitializeGraphics()
       {
           // Set our presentation parameters
           PresentParameters presentParams = new PresentParameters();

           presentParams.Windowed = true;
           presentParams.SwapEffect = SwapEffect.Discard;
           presentParams.AutoDepthStencilFormat = DepthFormat.D16;
           presentParams.EnableAutoDepthStencil = true;

           bool canDoShaders = true;
           // Does a hardware device support shaders?
           Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware);
           if (hardware.VertexShaderVersion >= new Version(1, 1))
           {
               // Default to software processing
               CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
               // Use hardware if it's available
               if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
                   flags = CreateFlags.HardwareVertexProcessing;
               // Use pure if it's available
               if (hardware.DeviceCaps.SupportsPureDevice)
                   flags |= CreateFlags.PureDevice;
               // Yes, Create our device
               device = new Device(0, DeviceType.Hardware, this, flags, presentParams);
           }
           else
           {
               // No shader support
               canDoShaders = false;
               // Create a reference device
               device = new Device(0, DeviceType.Reference, this, CreateFlags.SoftwareVertexProcessing, presentParams);
           }

  //device.RenderState.Lighting = true;
  device.Lights[0].Type = LightType.Directional;
  device.Lights[0].Diffuse = Color.Red;
  device.Lights[0].Direction = new Vector3(0,0,0.0f);
  device.Lights[0].Enabled = true;


  diffuseTexture = TextureLoader.FromVolumeFile(device, "Media/rockwall.dds");
  normalMap = TextureLoader.FromVolumeFile(device, "Media/rockwall_normal.dds");

  effect = Effect.FromFile(device, @Application.StartupPath + "/Media/Bump.fx", null, "", ShaderFlags.None, null);
  effect.Technique = "NoPixelShader";

  effect.SetValue("diffuseTexture", diffuseTexture);
  effect.SetValue("normalMap", normalMap);
 

           // Create our vertex data
           vb = new VertexBuffer(typeof(CustomVertex.PositionOnly), 3, device,Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionOnly.Format, Pool.Default);
           vb.Created += new EventHandler(this.OnVertexBufferCreate);
           OnVertexBufferCreate(vb, null);

           // Store our project and view matrices
           projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4,this.Width / this.Height, 1.0f, 100.0f);
           viewMatrix = Matrix.LookAtLH(new Vector3(0,0, 5.0f), new Vector3(), new Vector3(0,1,0));

           // Create our vertex declaration
           VertexElement[] elements = new VertexElement[]{new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), VertexElement.VertexDeclarationEnd };

           decl = new VertexDeclaration(device, elements);
           return canDoShaders;
       }

       private void OnVertexBufferCreate(object sender, EventArgs e)
       {
           VertexBuffer buffer = (VertexBuffer)sender;

           CustomVertex.PositionOnly[] verts = new CustomVertex.PositionOnly[3];
  verts[0].Position=new Vector3(0.0f, 1.0f, 1.0f);
  verts[1].Position=new Vector3(-1.0f, -1.0f, 1.0f);
  verts[2].Position=new Vector3(1.0f, -1.0f, 1.0f);

           buffer.SetData(verts, 0, LockFlags.None);
       }

       private void UpdateWorld()
       {
           worldMatrix = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f),
               angle / ((float)Math.PI * 4.0f), angle / ((float)Math.PI * 6.0f)),  
               angle / (float)Math.PI);

           angle += 0.1f;

           Matrix worldViewProj = worldMatrix * viewMatrix * projMatrix;
           effect.SetValue("WorldViewProj", worldViewProj);

       }

       protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
       {
           device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);

           UpdateWorld();
           device.BeginScene();
           device.SetStreamSource(0, vb, 0);
           device.VertexDeclaration = decl;
 
           // Render our triangle using an effect
           int numPasses = effect.Begin(0);
           for (int i = 0; i < numPasses; i++)
           {
   effect.BeginPass(i);
               device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
   effect.EndPass();
           }

           effect.End();
           device.EndScene();
           device.Present();
           this.Invalidate();
       }



       /// <summary>
 /// Clean up any resources being used.
 /// </summary>
 protected override void Dispose( bool disposing )
 {
  if( disposing )
  {
   if (components != null)
   {
    components.Dispose();
   }
  }
  base.Dispose( disposing );
 }

 #region Windows Form Designer generated code
 /// <summary>
 /// Required method for Designer support - do not modify
 /// the contents of this method with the code editor.
 /// </summary>
 private void InitializeComponent()
 {
  this.components = new System.ComponentModel.Container();
  this.Size = new Size(800,600);
  this.Text = "Form1";
 }
 #endregion

 /// <summary>
 /// The main entry point for the application.
 /// </summary>
       static void Main()
       {
           using (Form1 frm = new Form1())
           {
               // Show our form and initialize our graphics engine
               frm.Show();
               if (!frm.InitializeGraphics())
               {
                   MessageBox.Show("Your card does not support shaders.  " +
                       "This application will run in ref mode instead.");
               }
               Application.Run(frm);
           }
       }
}
}


Much Thanx,
Roy Teusink - Holland

Haddd

               

effect.Commit

device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);


This could be a problem, you are not using commit. But i think this is not the forum to solve these problems, it's focused only on the engine.

royteusink

 effect.Commit Doesn't excist but okey, it still doesn't work..., is there a forum where i can ask this sort of questions?

Haddd

 CommitChanges()

This is a good forum:

MS .net forum

Sorry, but it's no so easy take your code and give you a fast response.






Stratos es un servicio gratuito, cuyos costes se cubren en parte con la publicidad.
Por favor, desactiva el bloqueador de anuncios en esta web para ayudar a que siga adelante.
Muchísimas gracias.
Stratos es un servicio gratuito, cuyos costes se cubren en parte con la publicidad.
Por favor, desactiva el bloqueador de anuncios en esta web para ayudar a que siga adelante.
Muchísimas gracias.