zoukankan      html  css  js  c++  java
  • 第一个Shader程序

    fx文件:

     1 float4x4 matWorld;
     2 float Time=1.0f;
     3 
     4 struct VS_OUTPUT
     5 {
     6     float4 Pos :POSITION;
     7     float4 Color :COLOR;
     8 };
     9 
    10 VS_OUTPUT VS(float4 Pos:POSITION,float4 Color:COLOR)
    11 {
    12     VS_OUTPUT Out=(VS_OUTPUT)0;
    13     float4 pos1=Pos;
    14     pos1.y+= cos( Time*2.0f)+5;
    15     Out.Pos=mul(pos1,matWorld);
    16     Out.Color=Color;
    17     return Out;
    18 }
    19 
    20 float4 PS(VS_OUTPUT vsout): COLOR
    21 {
    22     return vsout.Color;
    23 }
    24 
    25 technique RenderScene
    26 {
    27     pass P0
    28     {
    29         CullMode=None;
    30         vertexShader=compile vs_1_1 VS();
    31         pixelShader=compile ps_2_0 PS();
    32     }
    33 }
    View Code

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

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using WorldWind.Renderable;
      6 using Utility;
      7 using Microsoft.DirectX.Direct3D;
      8 using System.IO;
      9 using Microsoft.DirectX;
     10 using System.Drawing;
     11 using System.Windows.Forms;
     12 
     13 namespace AppScene
     14 {
     15     public class Tri : RenderableObject
     16     {
     17         static Effect m_effect = null;
     18         VertexBuffer vertexBuffer = null;
     19         public Tri(string name)
     20             : base(name)
     21         {
     22         }
     23         public override void Initialize(DrawArgs drawArgs)
     24         {
     25             if (m_effect == null)
     26             {
     27                 string outerrors = "";
     28                 System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
     29                 Stream effectStream = assembly.GetManifestResourceStream("AppScene.Tri.fx");
     30                 string pathfx = "Tris.fx";
     31                // string pathfx = " Default_DirectX_Effect.fx";
     32                 //string pathfx = "CreateParamModel.fx";
     33 
     34                 //string pathfx = "flag.fx";
     35                 //m_effect = Effect.FromStream(
     36                 //    drawArgs.device,
     37                 //    effectStream,
     38                 //    null,
     39                 //    null,
     40                 //    ShaderFlags.None,
     41                 //    null,
     42                 //    out outerrors);
     43                 m_effect = Effect.FromFile(
     44                     drawArgs.device,
     45                     pathfx,
     46                     null,
     47                     null,
     48                     ShaderFlags.None,
     49                     null,
     50                     out outerrors);
     51                 if (outerrors != null && outerrors.Length > 0)
     52                     Log.Write(Log.Levels.Error, outerrors);
     53             }
     54             vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, drawArgs.device, 0, CustomVertex.PositionColored.Format, Pool.Default);
     55             vertexBuffer.Created += new EventHandler(vertexBuffer_Created);
     56             vertexBuffer_Created(vertexBuffer, null);
     57             Matrix WorldMatrix = Matrix.Identity;
     58             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));
     59             Matrix projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);
     60             WorldMatrix = drawArgs.device.GetTransform(TransformType.World);
     61             viewMatrix = drawArgs.device.GetTransform(TransformType.View);
     62             projMatrix = drawArgs.device.GetTransform(TransformType.Projection);
     63             
     64             m_effect.SetValue("matWorld", WorldMatrix * viewMatrix * projMatrix);
     65            // m_effect.SetValue("matViewProjection", viewMatrix * projMatrix); 
     66             isInitialized = true;
     67         }
     68 
     69         public override void Update(DrawArgs drawArgs)
     70         {
     71             try
     72             {
     73                 if (!isInitialized)
     74                     Initialize(drawArgs);
     75                 
     76             }
     77             catch (Exception ex)
     78             {
     79                 Log.Write(ex);
     80             }
     81         }
     82         public override void Render(DrawArgs drawArgs)
     83         {
     84             if (!isInitialized)
     85                 return;
     86 
     87             drawArgs.device.SetStreamSource(0, vertexBuffer, 0);
     88             drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format;
     89             int iTime = Environment.TickCount % 1000;
     90             float Angle = iTime * (2.0f * (float)Math.PI) / 1000.0f;
     91             m_effect.SetValue("Time", Angle);
     92             m_effect.Technique = "RenderScene";
     93            // m_effect.Technique = "Default_DirectX_Effect";
     94             int numPasses = m_effect.Begin(0);
     95         
     96             for (int i = 0; i < numPasses; i++)
     97             {
     98                 m_effect.BeginPass(i);
     99                 drawArgs.device.DrawPrimitives(  PrimitiveType.TriangleList, 0,1 );
    100                 m_effect.EndPass();
    101             }
    102             m_effect.End();
    103 
    104         }
    105 
    106         void vertexBuffer_Created(object sender, EventArgs e)
    107         {
    108             CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vertexBuffer.Lock(0, 0);
    109             verts[0].Position = new Vector3(12.0f,11.0f,10.0f);
    110             verts[0].Color = Color.Red.ToArgb();
    111             verts[1].Position = new Vector3(30.0f, 32.0f, 10.0f);
    112             verts[1].Color = Color.Red.ToArgb();
    113             verts[2].Position = new Vector3(10.0f, 60.0f, 10.0f);
    114             verts[2].Color = Color.Yellow.ToArgb();
    115             vertexBuffer.Unlock();
    116         }
    117 
    118         public override void Dispose()
    119         {
    120             if (vertexBuffer!=null)
    121             {
    122                 vertexBuffer.Dispose();
    123             }
    124         }
    125 
    126         public override bool PerformSelectionAction(DrawArgs drawArgs)
    127         {
    128             return false;
    129         }
    130     }
    131 }
    View Code

    这里出现一个问题:

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

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

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

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

    机器有没有问题?

    这个问题真是困扰人!

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

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

  • 相关阅读:
    2018-2019-1 20165212 《信息安全系统设计基础》第九周学习总结
    12321
    实现mybash
    2018-2019-1 20165212 《信息安全系统设计基础》第八周学习总结(pwd)
    2018-2019-1 20165212 实验三-并发程序设计
    2018-2019-1 20165212 20165222 20165313 实验二 固件程序设计
    2018-2019-1 20165212 《信息安全系统设计基础》第1次实验——实验环境的熟悉
    开根号(二分法、牛顿法)
    logistic回归的损失函数是什么形式?为什么?为什么不选平方损失函数?
    西瓜书课后习题——第六章
  • 原文地址:https://www.cnblogs.com/yhlx125/p/3285350.html
Copyright © 2011-2022 走看看