已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
XNA中的HLSL简单应用 以前一直没搞明白HLSL,最近两天苦下功夫,终于算是学到了HLSL入门了。这里我将通过对一个矩形模型以BassicEffect贴图和通过HLSL贴图作对比,从而掌握在XNA中使用.fx效果文件,和初步了解HLSL。下面先分析一下没有使用贴图时的代码: public class Game1 : Game GraphicsDeviceManager graphics; SpriteBatch spriteBatch; /投影矩阵 Matrix projection;/观察矩阵 Matrix view;/矩形的世界矩阵 Matrix world; /矩形模型 Model cub; /定义贴图纹理和效果(先声明,暂时不使用)Texture2D text; Effect effe; /是否在暂停状态bool isPause = false;KeyboardState preks = Keyboard.GetState(); public Game1() graphics = new GraphicsDeviceManager(this); Content.RootDirectory = Content; protected override void Initialize()/初始化投影矩阵projection=Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1f, 100000f); /初始化观察矩阵view = Matrix.CreateLookAt(Vector3.Forward * 500, Vector3.Zero, Vector3.Up); /初始化矩形的世界矩阵world = Matrix.Identity; base.Initialize(); protected override void LoadContent() / Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); /载入矩形模型 cub = Content.Load(cub);/载入贴图纹理(暂时不使用)text = Content.Load(rocks); /载入效果(暂时不使用)effe = Content.Load(effe); /设置效果的Techniqueeffe.CurrentTechnique = effe.Techniquestechnique1; protected override void Update(GameTime gameTime)/当前按键状态KeyboardState ks = Keyboard.GetState(); /按S镜头拉远,W镜头拉近if (ks.IsKeyDown(Keys.S) view *= Matrix.CreateTranslation(-10 * Vector3.UnitZ); if (ks.IsKeyDown(Keys.W) view *= Matrix.CreateTranslation(10 * Vector3.UnitZ); /通过空格键控制是否暂停if (ks.IsKeyDown(Keys.Space) & preks.IsKeyUp(Keys.Space) if (isPause) isPause = false; else isPause = true; /控制FillModeif (ks.IsKeyDown(Keys.A) & preks.IsKeyUp(Keys.A) GraphicsDevice.RenderState.FillMode = FillMode.WireFrame; if (ks.IsKeyDown(Keys.Q) & preks.IsKeyUp(Keys.Q) GraphicsDevice.RenderState.FillMode = FillMode.Solid; preks = ks; /如果不是暂停状态,自动旋转矩形的世界矩阵,实现旋转效果 if (!isPause) world *= Matrix.CreateFromAxisAngle(new Vector3(1, 1, -1), gameTime.ElapsedGameTime.Ticks * 0.0000001f); base.Update(gameTime); protected override void Draw(GameTime gameTime) graphics.GraphicsDevice.Clear(Color.CornflowerBlue); foreach (ModelMesh mesh in cub.Meshes) foreach (BasicEffect effect in mesh.Effects) effect.World = world;effect.View = view; effect.Projection = projection; effect.EnableDefaultLighting(); mesh.Draw(); base.Draw(gameTime); 可以看到程序的效果如下:现在我们为例子加入贴图(即前面声名的Texture),因为是使用的Basiceffect类的方法,所以很简单,只需要在foreach (BasicEffect effect in mesh.Effects)的循环体中插入以下代码, effect.TextureEnabled = true; effect.Texture = text; 再来看一下效果:上面就是XNA中最简单的纹理贴图的方法,下面就将进入主要的部分HLSL渲染。首先看一段很经典的HLSL代码:/声明一个4x4的矩阵。传入的参数是世界、观察、投影矩阵的积float4x4 mWorldViewProjection; /定义一个VertexShader函数,作为入口,并返回值的语义为POSITIONfloat4 vs_main(float4 position:POSITION):POSITION /将传入的坐标顶点与世界观察投影矩阵相乘,一交完成三个顶点的交换return mul(position,mWorldViewProjection); /定义一个PixelShader函数,作为入口,并定义返回语义为COLOR0float4 ps_main():COLOR0 /固定返回白色作为显示颜色 Return float4(1,1,1,1); /开始定义Techniquetechnique technique1 /一个passpass pass0 /将VertexShader的版本设置为1.1,并将执行函数设置为vs_main VertexShader=compile vs_1_1 vs_main(); /将PixelShader的版本设置为1.1,并将执行函数设置为ps_mainPixelShader=compile ps_1_1 ps_main(); 这段代码中包含了一个全局变量,两个全局函数。这两个全局函数分别用于VertexShader和PixelShader,也直接作为VertexShader和PixelShader的入口函数。而全局变量则用在了VertexShader的计算中。代码中定义了一个technique并且包含了一个pass,在这个pass中VertexShader和PixelShader的版本都是1.1。使用这段代码去渲染任何一个模型,得到的都是一个白色的物体。在XNA中使用HLSL的代码如下:protected override void Draw(GameTime gameTime)graphics.GraphicsDevice.Clear(Color.CornflowerBlue); /设置HLSL参数effe.ParametersmWorldViewProjection.SetValue(world * view * projection); effe.Begin();foreach (EffectPass pass in effe.CurrentTechnique.Passes) pass.Begin();foreach (ModelMesh mesh in cub.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) GraphicsDevice.VertexDeclaration = part.VertexDeclaration;GraphicsDevice.Indices = mesh.IndexBuffer;GraphicsDevice.Vertices0.SetSource(mesh.VertexBuffer,part.StartIndex, part.VertexStride);GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.BaseVertex, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount); pass.End();effe.End(); base.Draw(gameTime); 效果如下图: 让我们再近一步,用HLSL为我们的贴上纹理。修改Draw方法如下: protected override void Draw(GameTime gameTime) graphics.GraphicsDevice.Clear(Color.CornflowerBlue);effe.ParametersWorld.SetValue(world); effe.ParametersView.SetValue(view); effe.ParametersProjection.SetValue(projection); /下面的参数text类型为Textureeffe.ParametersdiffuseTexture.SetValue(text);effe.Begin();foreach (EffectPass pass in effe.CurrentTechnique.Passes) pass.Begin(); foreach (ModelMesh mesh in cub.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) GraphicsDevice.VertexDeclaration = part.VertexDeclaration; GraphicsDevice.Indices = mesh.IndexBuffer; GraphicsDevice.Vertices0.SetSource(mesh.VertexBuffer, part.StartIndex, part.VertexStride); GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.BaseVertex, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount); pass.End(); effe.End(); base.Draw(gameTime); HLSL代码如下:float4x4 World; float4x4 View;float4x4 Projection; texture diffuseTexture:Diffuse; sampler DiffuseTextureSampler = sampler_state Texture = ; MinFilter=linear; MagFilter=linear;MipFilter=linear; ; struct VertexShaderInput float4 Position : POSITION0; float3 normal:NORMAL;float2 uv:TEXCOORD0; ;struct VertexShaderOutput float4 Position : POSITION0; float2 uv:TEXCOORD0; ; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) VertexShaderOutput output; float4 worldPosition = mul(input.Position, World); float4 viewPosition = mul(worldPosition, View); output.Position = mul(viewPosition, Projection); output.uv=input.uv; return output; float4 P
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《P型硅掺杂浓度对》课件
- 《加强风险防范意识》课件
- 《老年性白内障》课件
- 血栓闭塞性脉管炎Ⅲ期护理查房
- 《数学建模培训资料》课件
- 《欺负干预》课件
- 《医学假设检验》课件
- 《家电产品策划书》课件
- 《保险专业保险学》课件
- 2026年国际医疗部+全科医学科第三季度考核试卷(N0-N2层级)测试卷及答案
- GB/T 34630.1-2026搅拌摩擦焊铝及铝合金第1部分:术语及定义
- 2026及未来5年中国电力线载波抄表数据监测研究报告
- 《半导体制备装置用超高纯净不锈钢管》
- 2026语文新教材 第19课 航天员写给孩子的信 教学课件
- 深基坑安全管理培训课件
- T∕CPCPA 0017-2026 托育机构婴幼儿回应性照护服务规范
- 上海市上海中学2025-2026学年高一上学期期中考试语文试卷(含答案)
- 警营文化建设投标方案
- 重庆市2025年高考真题化学试卷(含答案)
- 少儿脊柱健康课件
- JG/T 182-2008住宅轻钢装配式构件
评论
0/150
提交评论