第一个Shader程序

时间:2024-11-28 17:05:01

fx文件:

 float4x4 matWorld;
float Time=1.0f; struct VS_OUTPUT
{
float4 Pos :POSITION;
float4 Color :COLOR;
}; VS_OUTPUT VS(float4 Pos:POSITION,float4 Color:COLOR)
{
VS_OUTPUT Out=(VS_OUTPUT);
float4 pos1=Pos;
pos1.y+= cos( Time*2.0f)+;
Out.Pos=mul(pos1,matWorld);
Out.Color=Color;
return Out;
} float4 PS(VS_OUTPUT vsout): COLOR
{
return vsout.Color;
} technique RenderScene
{
pass P0
{
CullMode=None;
vertexShader=compile vs_1_1 VS();
pixelShader=compile ps_2_0 PS();
}
}

C#编写的托管代码,基于WW的渲染框架用托管D3D9 调用:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorldWind.Renderable;
using Utility;
using Microsoft.DirectX.Direct3D;
using System.IO;
using Microsoft.DirectX;
using System.Drawing;
using System.Windows.Forms; namespace AppScene
{
public class Tri : RenderableObject
{
static Effect m_effect = null;
VertexBuffer vertexBuffer = null;
public Tri(string name)
: base(name)
{
}
public override void Initialize(DrawArgs drawArgs)
{
if (m_effect == null)
{
string outerrors = "";
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream effectStream = assembly.GetManifestResourceStream("AppScene.Tri.fx");
string pathfx = "Tris.fx";
// string pathfx = " Default_DirectX_Effect.fx";
//string pathfx = "CreateParamModel.fx"; //string pathfx = "flag.fx";
//m_effect = Effect.FromStream(
// drawArgs.device,
// effectStream,
// null,
// null,
// ShaderFlags.None,
// null,
// out outerrors);
m_effect = Effect.FromFile(
drawArgs.device,
pathfx,
null,
null,
ShaderFlags.None,
null,
out outerrors);
if (outerrors != null && outerrors.Length > )
Log.Write(Log.Levels.Error, outerrors);
}
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), , drawArgs.device, , CustomVertex.PositionColored.Format, Pool.Default);
vertexBuffer.Created += new EventHandler(vertexBuffer_Created);
vertexBuffer_Created(vertexBuffer, null);
Matrix WorldMatrix = Matrix.Identity;
Matrix viewMatrix = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, -9.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));
Matrix projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / , 1.0f, 1.0f, 100.0f);
WorldMatrix = drawArgs.device.GetTransform(TransformType.World);
viewMatrix = drawArgs.device.GetTransform(TransformType.View);
projMatrix = drawArgs.device.GetTransform(TransformType.Projection); m_effect.SetValue("matWorld", WorldMatrix * viewMatrix * projMatrix);
// m_effect.SetValue("matViewProjection", viewMatrix * projMatrix);
isInitialized = true;
} public override void Update(DrawArgs drawArgs)
{
try
{
if (!isInitialized)
Initialize(drawArgs); }
catch (Exception ex)
{
Log.Write(ex);
}
}
public override void Render(DrawArgs drawArgs)
{
if (!isInitialized)
return; drawArgs.device.SetStreamSource(, vertexBuffer, );
drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format;
int iTime = Environment.TickCount % ;
float Angle = iTime * (2.0f * (float)Math.PI) / 1000.0f;
m_effect.SetValue("Time", Angle);
m_effect.Technique = "RenderScene";
// m_effect.Technique = "Default_DirectX_Effect";
int numPasses = m_effect.Begin(); for (int i = ; i < numPasses; i++)
{
m_effect.BeginPass(i);
drawArgs.device.DrawPrimitives( PrimitiveType.TriangleList, , );
m_effect.EndPass();
}
m_effect.End(); } void vertexBuffer_Created(object sender, EventArgs e)
{
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vertexBuffer.Lock(, );
verts[].Position = new Vector3(12.0f,11.0f,10.0f);
verts[].Color = Color.Red.ToArgb();
verts[].Position = new Vector3(30.0f, 32.0f, 10.0f);
verts[].Color = Color.Red.ToArgb();
verts[].Position = new Vector3(10.0f, 60.0f, 10.0f);
verts[].Color = Color.Yellow.ToArgb();
vertexBuffer.Unlock();
} public override void Dispose()
{
if (vertexBuffer!=null)
{
vertexBuffer.Dispose();
}
} public override bool PerformSelectionAction(DrawArgs drawArgs)
{
return false;
}
}
}

这里出现一个问题:

鼠标在窗体上移动才能够显示上下移动的三角形。

有时候启动了程序,三角形干脆不显示。开始还没有这个问题,后来出现这个问题!

原来我以为是视域体的问题,但是这个三角形肯定在视域体内部啊。

是帧率控制的问题?需要控制帧率吗,控制帧率是为了减少屏幕刷新次数。不至于刷新太频繁画面干脆不显示吧!

机器有没有问题?

这个问题真是困扰人!

WW的渲染框架本身会有问题吗,通过继承构建的渲染列表对状态机产生影响了?书上的示例程序是没有问题的,为啥我写到一个RenderObject中会出现问题?

后记:移植了NativeMethod类后就好了,应该是消息分发的问题。