着色器和效果——1.1编写HLSL着色器

1.1编写HLSL着色器

阅读此文表明您已同意文末的声明

我们可以直接把HLSL着色器代码作为一长串字符串编写进我们的应用程序源文件中。但是,更加方便和模块化的方法是把着色器的代码从应用程序代码中分离出来。因此,我们可以在记事本里编写我们的着色器代码,并将之保存进一般的ASCII文本文件中。然后再使用D3DXCompileShaderFromFile函数(1.2.2)编译我们的着色器。

作为介绍,下面给出一个简单的用HLSL编写的顶点着色器,它的源文件是从文本文件Transform.txt生成而来。完整的项目可以在Transform标题下找到。顶点着色器通过一个组合视图和投影矩阵变换(transform)顶点,并且设置顶点颜色的漫反射成分(the diffuse color component of the vertex)为蓝色。

注意:本例使用顶点着色器作为示例,但是别急管顶点着色器做了什么,它们会在下一章中讲述。至于现在,你要做的就是熟悉HLSL程序的语法和格式。

/
//
// File: transform.txt
//
// Author: Frank D. Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP,
//         MSVC++ 7.0
//
// Desc: Vertex shader that transforms a vertex by the view and
//       projection transformation, and sets the vertex color to blue.
//
/

  
  
   
    
  
  
//
// Globals
//

  
  
   
    
  
  
// Global variable to store a combined view and projection
// transformation matrix.  We initialize this variable
// from the application.
matrix ViewProjMatrix;

  
  
   
    
  
  
// Initialize a global blue color vector.
vector Blue = {0.0f, 0.0f, 1.0f, 1.0f};

  
  
   
    
  
  
//
// Structures
//

  
  
   
    
  
  
// Input structure describes the vertex that is input
// into the shader.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值