纹理就是3D模型外表面上的图案。在3D场景中纹理极大地增加了物体的真实性。纹理的本质是把平面图形贴到3D物体表面。在Direct3D中纹理的x和y坐标一般称为Tu和Tv坐标,纹理坐标范围都是0.0-1.0。如果知道一个面的顶点坐标,使面的顶点坐标和句型图片纹理坐标相对应,就可以将这些二维图片贴到3D图形表面。在Direct 3D中Texture类定义纹理。

如果希望为墙壁贴上瓷砖,可以只绘制一个瓷砖的平面图形,然后把瓷砖图形自动平铺到墙壁上。这里运用了Direct 3D默认的寻址模式TextureAddress.Wrap,如果纹理的x坐标大于1,例如为n,表示图片沿X轴方向重复贴图n次。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;namespace 墙壁贴瓷砖
{public partial class Form1 : Form{private Device device = null;bool pause = false;VertexBuffer vertexBuffer1 = null;Texture texture = null;public Form1(){InitializeComponent();}public bool InitializeGraphics(){try{PresentParameters presentParams = new PresentParameters();presentParams.Windowed = true;               //不是全屏显示,在一个窗口显示presentParams.SwapEffect = SwapEffect.Discard;       //后备缓存交换的方式presentParams.EnableAutoDepthStencil = true;            //允许使用自动深度模板测试//深度缓冲区单元为16位二进制数presentParams.AutoDepthStencilFormat = DepthFormat.D16;device = new Device(0, DeviceType.Hardware, this,   //建立设备类对象CreateFlags.SoftwareVertexProcessing, presentParams);//设置设备重置事件(device.DeviceReset)事件函数为this.OnResetDevicedevice.DeviceReset += new System.EventHandler(this.OnResetDevice);this.OnCreateDevice(device, null);//自定义方法,初始化Device的工作放到这个方法中this.OnResetDevice(device, null);//调用设备重置事件(device.DeviceReset)事件函数}        //设备重置事件函数要设置Device参数,初始函数中必须调用该函数catch (DirectXException){return false;}return true;}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;vertexBuffer1 = new VertexBuffer(typeof(CustomVertex.PositionTextured), 6, dev, 0, CustomVertex.PositionTextured.Format, Pool.Default);vertexBuffer1.Created += new EventHandler(vertexBuffer1_Created);this.vertexBuffer1_Created(vertexBuffer1, null);texture = TextureLoader.FromFile(dev, Application.StartupPath + @"\..\..\..\p2.bmp");}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;dev.RenderState.CullMode = Cull.None;        //取消背面剔除dev.RenderState.Lighting = false;          //取消灯光SetupMatrices();      //在程序运行期间,Device的3个变换不改变,因此放在此处}public void Render()      //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架{if (device == null) //如果未建立设备对象,退出return;if (pause)return;device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.LightCoral, 1.0f, 0);device.BeginScene();//开始渲染SetupMatrices();device.SetTexture(0, texture);    //为Device类对象device增加使用的第0个纹理device.SetStreamSource(0, vertexBuffer1, 0);device.VertexFormat = CustomVertex.PositionTextured.Format;device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);device.EndScene();      //渲染结束device.Present();     //更新显示区域,把后备缓存的D图形送到图形卡的显存中显示}private void Form1_Paint(object sender, PaintEventArgs e){this.Render();}private void Form1_Resize(object sender, EventArgs e){pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);}void vertexBuffer1_Created(object sender, EventArgs e){CustomVertex.PositionTextured[] verts =(CustomVertex.PositionTextured[])vertexBuffer1.Lock(0, 0);//墙壁verts[0].Position = new Vector3(-2.0f, -2.0f, 2.0f);     //顶点0位置verts[0].Tu = 0.0f;           //顶点0纹理坐标Tuverts[0].Tv = 10.0f;verts[1].Position = new Vector3(-2.0f, 2.0f, 2.0f);          //顶点1位置verts[1].Tu = 0.0f;           //顶点1纹理坐标Tuverts[1].Tv = 0.0f;verts[2].Position = new Vector3(2.0f, 2.0f, 2.0f);        //顶点2位置verts[2].Tu = 10.0f;      //顶点2纹理坐标Tuverts[2].Tv = 0.0f;verts[3].Position = new Vector3(-2.0f, -2.0f, 2.0f);      //顶点3位置verts[3].Tu = 0.0f;           //顶点3纹理坐标Tuverts[3].Tv = 10.0f;verts[4].Position = new Vector3(2.0f, 2.0f, 2.0f);       //顶点4位置verts[4].Tu = 10.0f;      //顶点4纹理坐标Tuverts[4].Tv = 0.0f;verts[5].Position = new Vector3(2.0f, -2.0f, 2.0f);       //顶点5位置verts[5].Tu = 10.0f;      //顶点5纹理坐标Tuverts[5].Tv = 10.0f;vertexBuffer1.Unlock();}private void SetupMatrices()        //修改Device的3个变换{device.Transform.World = Matrix.RotationY(0);  //世界变换矩阵device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -4.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);  //投影变换矩阵}private void Form1_Load(object sender, EventArgs e){InitializeGraphics();this.Show();Render();}}
}

为有光照的空心圆柱增加纹理,此时还需要考虑光照的影响。Device类对象的TextureState数组记录了每一个纹理的状态,允许多层纹理,数组的索引值为0~7,其属性ColorOperation记录了该元素所代表的纹理图片颜色和其他参数(e.g.光照颜色)的混合方法;属性ColorArgument1和ColorArgument2用来指定属性ColorOperation使用的参数

实现如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace 为空心圆柱增加纹理
{public partial class Form1 : Form{private Device device = null;bool pause = false;VertexBuffer vertexBuffer = null;Material mtrl;Texture texture = null;public Form1(){InitializeComponent();}public bool InitializeGraphics(){try{PresentParameters presentParams = new PresentParameters();presentParams.Windowed = true;              //不是全屏显示,在一个窗口显示presentParams.SwapEffect = SwapEffect.Discard;       //后备缓存交换的方式presentParams.EnableAutoDepthStencil = true;            //允许使用自动深度模板测试//深度缓冲区单元为16位二进制数presentParams.AutoDepthStencilFormat = DepthFormat.D16;device = new Device(0, DeviceType.Hardware, this,   //建立设备类对象CreateFlags.SoftwareVertexProcessing, presentParams);//设置设备重置事件(device.DeviceReset)事件函数为this.OnResetDevicedevice.DeviceReset += new System.EventHandler(this.OnResetDevice);this.OnCreateDevice(device, null);//自定义方法,初始化Device的工作放到这个方法中this.OnResetDevice(device, null);//调用设备重置事件(device.DeviceReset)事件函数}        //设备重置事件函数要设置Device参数,初始函数中必须调用该函数catch (DirectXException){return false;}return true;}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;           //注意阴影部分vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 100, dev,Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default);vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);this.OnCreateVertexBuffer(vertexBuffer, null);mtrl = new Material();mtrl.Diffuse = System.Drawing.Color.Yellow;        //物体的颜色mtrl.Ambient = System.Drawing.Color.Red;            //反射环境光的颜色texture = TextureLoader.FromFile(dev, Application.StartupPath + @"\..\..\..\p1.JPG");}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;dev.RenderState.CullMode = Cull.None;              //取消背面剔除device.RenderState.ZBufferEnable = true;           //打开Z缓冲device.RenderState.Lighting = true;             //打开灯光SetupLights();                //设置灯光}public void Render()     //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架{if (device == null)  //如果未建立设备对象,退出return;if (pause)return;device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.WhiteSmoke, 1.0f, 0);SetupMatrices();device.BeginScene();       //开始渲染device.SetTexture(0, texture);device.TextureState[0].ColorOperation = TextureOperation.Modulate;device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;//纹理颜色device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;//光照颜色device.TextureState[0].AlphaOperation = TextureOperation.Disable;device.SetStreamSource(0, vertexBuffer, 0);device.VertexFormat = CustomVertex.PositionNormalTextured.Format;device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, (4 * 25) - 2);device.EndScene();//渲染结束device.Present();//更新显示区域,把后备缓存的D图形送到图形卡的显存中显示}private void Form1_Paint(object sender, PaintEventArgs e){this.Render();}private void Form1_Resize(object sender, EventArgs e){pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);}public void OnCreateVertexBuffer(object sender, EventArgs e){CustomVertex.PositionNormalTextured[] verts =(CustomVertex.PositionNormalTextured[])vertexBuffer.Lock(0, 0);for (int i = 0; i < 50; i++){float theta = (float)(2 * Math.PI * i) / 49;verts[2 * i].Position = new Vector3((float)Math.Sin(theta), -1, (float)Math.Cos(theta));verts[2 * i].Normal = new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta));verts[2 * i].Tu = ((float)i) / (50 - 1);verts[2 * i].Tv = 1.0f;verts[2 * i + 1].Position = new Vector3((float)Math.Sin(theta), 1, (float)Math.Cos(theta));verts[2 * i + 1].Normal = new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta));verts[2 * i + 1].Tu = ((float)i) / (50 - 1);verts[2 * i + 1].Tv = 0.0f;}vertexBuffer.Unlock();}private void SetupMatrices(){device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 3000.0f);device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, -5.0f),new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);}private void SetupLights(){device.Material = mtrl;device.Lights[0].Type = LightType.Directional;device.Lights[0].Diffuse = System.Drawing.Color.White;device.Lights[0].Direction = new Vector3((float)Math.Cos(Environment.TickCount / 250.0f),1.0f, (float)Math.Sin(Environment.TickCount / 250.0f));device.Lights[0].Enabled = true;device.RenderState.Ambient = System.Drawing.Color.FromArgb(0x404040);}private void Form1_load(object sender, EventArgs e){InitializeGraphics();this.Show();Render();}}
}

可以为上面的空心圆柱添加背景,所谓的背景就是固定不动的图像,也就是在进行三个变换时,背景位置不变。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;namespace 空心圆柱添加背景
{public partial class Form1 : Form{private Device device = null;bool pause = false;VertexBuffer vertexBuffer = null;Material mtrl;Texture texture = null;VertexBuffer vertexBuffer1 = null;Texture texture1 = null;public Form1(){InitializeComponent();}public bool InitializeGraphics(){try{PresentParameters presentParams = new PresentParameters();presentParams.Windowed = true;              //不是全屏显示,在一个窗口显示presentParams.SwapEffect = SwapEffect.Discard;       //后备缓存交换的方式presentParams.EnableAutoDepthStencil = true;            //允许使用自动深度模板测试//深度缓冲区单元为16位二进制数presentParams.AutoDepthStencilFormat = DepthFormat.D16;device = new Device(0, DeviceType.Hardware, this,   //建立设备类对象CreateFlags.SoftwareVertexProcessing, presentParams);//设置设备重置事件(device.DeviceReset)事件函数为this.OnResetDevicedevice.DeviceReset += new System.EventHandler(this.OnResetDevice);this.OnCreateDevice(device, null);//自定义方法,初始化Device的工作放到这个方法中this.OnResetDevice(device, null);//调用设备重置事件(device.DeviceReset)事件函数}        //设备重置事件函数要设置Device参数,初始函数中必须调用该函数catch (DirectXException){return false;}return true;}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;           //阴影部分是所作的修改vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 100, dev,Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default);vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);this.OnCreateVertexBuffer(vertexBuffer, null);mtrl = new Material();mtrl.Diffuse = System.Drawing.Color.Yellow;        //物体的颜色mtrl.Ambient = System.Drawing.Color.Red;            //反射环境光的颜色texture = TextureLoader.FromFile(dev, Application.StartupPath + @"\..\..\..\p1.JPG");vertexBuffer1 = new VertexBuffer(typeof(CustomVertex.TransformedTextured), 4, dev, 0, CustomVertex.TransformedTextured.Format, Pool.Default);vertexBuffer1.Created += new EventHandler(vertexBuffer1_Created);this.vertexBuffer1_Created(vertexBuffer1, null);texture1 = TextureLoader.FromFile(dev, Application.StartupPath + @"\..\..\..\p2.jpg");}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;dev.RenderState.CullMode = Cull.None;              //取消背面剔除device.RenderState.ZBufferEnable = true;           //打开Z缓冲device.RenderState.Lighting = true;             //打开灯光SetupLights();}public void Render()       //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架{if (device == null)                  //如果未建立设备对象,退出return;if (pause)return;device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.WhiteSmoke, 1.0f, 0);SetupMatrices();device.BeginScene();                   //开始渲染device.SetTexture(0, texture1);           //这段代码必须放在渲染圆柱体代码之前device.RenderState.ZBufferEnable = false;device.SetStreamSource(0, vertexBuffer1, 0);device.VertexFormat = CustomVertex.TransformedTextured.Format;device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);device.RenderState.ZBufferEnable = true;device.SetTexture(0, texture);device.TextureState[0].ColorOperation = TextureOperation.Modulate;device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;device.TextureState[0].AlphaOperation = TextureOperation.Disable;device.SetStreamSource(0, vertexBuffer, 0);device.VertexFormat = CustomVertex.PositionNormalTextured.Format;device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, (4 * 25) - 2);device.EndScene();       //渲染结束device.Present();     //更新显示区域,把后备缓存的D图形送到图形卡的显存中显示}private void Form1_Paint(object sender, PaintEventArgs e){this.Render();}private void Form1_Resize(object sender, EventArgs e){pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);}public void OnCreateVertexBuffer(object sender, EventArgs e){CustomVertex.PositionNormalTextured[] verts =(CustomVertex.PositionNormalTextured[])vertexBuffer.Lock(0, 0);for (int i = 0; i < 50; i++){float theta = (float)(2 * Math.PI * i) / 49;verts[2 * i].Position = new Vector3((float)Math.Sin(theta), -1, (float)Math.Cos(theta));verts[2 * i].Normal = new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta));verts[2 * i].Tu = ((float)i) / (50 - 1);verts[2 * i].Tv = 1.0f;verts[2 * i + 1].Position = new Vector3((float)Math.Sin(theta), 1, (float)Math.Cos(theta));verts[2 * i + 1].Normal = new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta));verts[2 * i + 1].Tu = ((float)i) / (50 - 1);verts[2 * i + 1].Tv = 0.0f;}vertexBuffer.Unlock();}private void SetupMatrices(){device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 3000.0f);device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, -5.0f),new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);}private void SetupLights(){device.Material = mtrl;device.Lights[0].Type = LightType.Directional;device.Lights[0].Diffuse = System.Drawing.Color.White;device.Lights[0].Direction = new Vector3((float)Math.Cos(Environment.TickCount / 250.0f),1.0f, (float)Math.Sin(Environment.TickCount / 250.0f));device.Lights[0].Enabled = true;device.RenderState.Ambient = System.Drawing.Color.FromArgb(0x404040);}void vertexBuffer1_Created(object sender, EventArgs e){CustomVertex.TransformedTextured[] verts =(CustomVertex.TransformedTextured[])vertexBuffer1.Lock(0, 0);verts[0].Position = new Vector4(0, 0, 0, 1);verts[0].Tu = 0.0f;verts[0].Tv = 0.0f;verts[1].Position = new Vector4(this.Width, 0, 0, 1);verts[1].Tu = 1.0f;verts[1].Tv = 0.0f;verts[2].Position = new Vector4(this.Width, this.Height, 0, 1);verts[2].Tu = 1.0f;verts[2].Tv = 1.0f;verts[3].Position = new Vector4(0, this.Height, 0, 1);verts[3].Tu = 0.0f;verts[3].Tv = 1.0f;vertexBuffer1.Unlock();}private void Form1_Load(object sender, EventArgs e){InitializeGraphics();this.Show();Render();}}
}

为正方形增加纹理,如果正方形尺寸较大,而纹理的分辨率较低,这时纹理图像可能要出现马赛克,如果正方形尺寸较大,而纹理图片分辨率较高时,此时要缩小图形以适应正方形尺寸。为了解决这个问题,可以使用纹理滤波器,纹理滤波器的本质是数学差值。3D图形的表面可以贴多个图片,称为多层纹理。为正方形增加两个纹理,一个是一面不规则的墙的图案,另一个是一个光影。多纹理的混合需要一个是半透明的,两个混合后才有效果,通过设置纹理0的混合方法,再设置纹理1的混合方法,然后再把纹理0图案的颜色设置为半透明的即可。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;namespace 正方形添加多纹理
{public partial class Form1 : Form{private Device device = null;bool pause = false;VertexBuffer vertexBuffer1 = null;Texture texture = null;Texture texture1 = null;public Form1(){InitializeComponent();}public bool InitializeGraphics(){try{PresentParameters presentParams = new PresentParameters();presentParams.Windowed = true;              //不是全屏显示,在一个窗口显示presentParams.SwapEffect = SwapEffect.Discard;       //后备缓存交换的方式presentParams.EnableAutoDepthStencil = true;            //允许使用自动深度模板测试//深度缓冲区单元为16位二进制数presentParams.AutoDepthStencilFormat = DepthFormat.D16;device = new Device(0, DeviceType.Hardware, this,   //建立设备类对象CreateFlags.SoftwareVertexProcessing, presentParams);//设置设备重置事件(device.DeviceReset)事件函数为this.OnResetDevicedevice.DeviceReset += new System.EventHandler(this.OnResetDevice);this.OnCreateDevice(device, null);//自定义方法,初始化Device的工作放到这个方法中this.OnResetDevice(device, null);//调用设备重置事件(device.DeviceReset)事件函数}        //设备重置事件函数要设置Device参数,初始函数中必须调用该函数catch (DirectXException){return false;}return true;}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;       //注意有阴影部分vertexBuffer1 = new VertexBuffer(typeof(CustomVertex.PositionTextured), 6, dev, 0,CustomVertex.PositionTextured.Format, Pool.Default);vertexBuffer1.Created += new EventHandler(vertexBuffer1_Created);this.vertexBuffer1_Created(vertexBuffer1, null);texture = TextureLoader.FromFile(dev, Application.StartupPath + @"\..\..\..\p1.bmp");texture1 = TextureLoader.FromFile(dev, Application.StartupPath + @"\..\..\..\p2.bmp");}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;dev.RenderState.CullMode = Cull.None;      //取消背面剔除dev.RenderState.Lighting = false;          //取消灯光SetupMatrices();      //在程序运行期间,Device的3个变换不改变,因此放在此处device.SamplerState[0].MagFilter = TextureFilter.Linear;  //使用线性滤波器}public void Render()      //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架{if (device == null)      //如果未建立设备对象,退出return;if (pause)return;device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);device.BeginScene();             //开始渲染  device.SetTexture(0, texture);      //索引号为0的纹理是墙壁图案device.SetTexture(1, texture1);      //索引号为1的纹理是光影图案device.TextureState[0].TextureCoordinateIndex = 0;  //纹理坐标Tu和Tv初始值device.TextureState[1].TextureCoordinateIndex = 0;device.SamplerState[0].MagFilter = TextureFilter.Linear;  //放大图形使用线形滤波器device.SamplerState[1].MagFilter = TextureFilter.Linear;  //缩小图形使用线形滤波器device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;//以下两句设置纹理0为半透明的,和纹理1混合后能看到混合效果,透明效果见10.2节device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1;device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;device.TextureState[1].ColorOperation = TextureOperation.Modulate;device.TextureState[1].ColorArgument1 = TextureArgument.TextureColor;device.TextureState[1].ColorArgument2 = TextureArgument.Current;//device.TextureState[1].AlphaOperation = TextureOperation.Disable;//device.TextureState[2].ColorOperation = TextureOperation.Disable;//device.TextureState[2].AlphaOperation = TextureOperation.Disable;device.SetStreamSource(0, vertexBuffer1, 0);device.VertexFormat = CustomVertex.PositionTextured.Format;device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);device.EndScene();     //渲染结束device.Present();     //更新显示区域,把后备缓存的图形送到图形卡的显存中显示}private void Form1_Paint(object sender, PaintEventArgs e){this.Render();}private void Form1_Resize(object sender, EventArgs e){pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);}void vertexBuffer1_Created(object sender, EventArgs e){CustomVertex.PositionTextured[] verts =(CustomVertex.PositionTextured[])vertexBuffer1.Lock(0, 0);     //墙壁verts[0].Position = new Vector3(-2.0f, -2.0f, 2.0f);       //顶点0位置verts[0].Tu = 0.0f;       //顶点0纹理坐标Tuverts[0].Tv = 1.0f;verts[1].Position = new Vector3(-2.0f, 2.0f, 2.0f);               //顶点1位置verts[1].Tu = 0.0f;       //顶点1纹理坐标Tuverts[1].Tv = 0.0f;verts[2].Position = new Vector3(2.0f, 2.0f, 2.0f);                //顶点2位置verts[2].Tu = 1.0f;       //顶点2纹理坐标Tuverts[2].Tv = 0.0f;verts[3].Position = new Vector3(-2.0f, -2.0f, 2.0f);          //顶点3位置verts[3].Tu = 0.0f;       //顶点3纹理坐标Tuverts[3].Tv = 1.0f;verts[4].Position = new Vector3(2.0f, 2.0f, 2.0f);                //顶点4位置verts[4].Tu = 1.0f;       //顶点4纹理坐标Tuverts[4].Tv = 0.0f;verts[5].Position = new Vector3(2.0f, -2.0f, 2.0f);               //顶点5位置verts[5].Tu = 1.0f;       //顶点5纹理坐标Tuverts[5].Tv = 1.0f;vertexBuffer1.Unlock();}private void SetupMatrices()       //修改Device的3个变换{device.Transform.World = Matrix.RotationY(0);  //世界变换矩阵device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -4.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);  //投影变换矩阵}private void Form1_Load(object sender, EventArgs e){InitializeGraphics();this.Show();Render();}}
}

在程序运行时有时需要多个不同分辨率的同一张图片,以适合大小不同的物体表面贴图用,当物体离观察者近的适合使用高质量的纹理,而物体离观察者远的时候使用低分辨率的纹理。这时就需要使用多级渐进纹理来控制渲染时纹理的质量,但是这样做的代价是会吃掉更多的内存。

DirectX 3D纹理相关推荐

  1. DirectX 3D基础复习

    DirectX 3D基础复习 主要是用于期末考试复习 D3D期末复习(书本) 第一部分:基础知识 1.在D3DX库中,我们用类D3DXVECTOR3表示3D空间中的向量 2.向量的计算: (1)点积: ...

  2. DirectX 3D相关资源参考

    Direct3D9和Direct3D 9Ex的区别 DirectX3D画线接口ID3DXLine 读取图片的信息 DirectX函数之UpdateSurface IDirect3DStateBlock ...

  3. DirectX 3D学习笔记(一)

    DirectX 3D学习笔记(一) Ⅰ.渲染状态 设备的渲染状态控制Direct3D设备的光栅化组件的行为.通过改变光栅渲染状态属性,可以设置使用何种方式来进行渲染着色,以及如何进行雾化等. 在Dir ...

  4. Cesium中使用Sampler3D,3D纹理,实现体渲染

    Cesium中使用Sampler3D,3D纹理,实现体渲染 Cesium目前(20221231)还不支持直接使用3D纹理,但是其实内部已经可以WebGL2,而且内置常量也有3DTexture.所以,可 ...

  5. 不用找,你想要的游戏3d纹理图片素材都在这里

    前方注意咯!建议先收藏再看哦!为大家整理游戏3d纹理图片素材,总有满足你需求的一款,除此之外,免费,资源质量好,一键打包下载,你还不心动吗? 在找寻资源的时候,无意中发现了这个网站,资源多,质量好,让 ...

  6. 按头安利 好看又实用的建筑3d纹理图片素材看这里

    家人们,最近我找到了一个很好用的建筑3d纹理图片免费素材网站 资源贼多,重点是免费!!!嘿嘿嘿!!!感兴趣的可以进去看看 接下来就给大家介绍一下我珍藏已久的网站,我的工作灵感都是来源它哦,里面的建筑3 ...

  7. OpenGL ES绘制3D纹理贴图

    最近看了<疯狂android讲义>的图形相关的内容,结合自己的理解,整理了一下. 下图是做出来的3D纹理贴图效果,手指在屏幕滑动时,图片可以随之转动. 要实现一个纹理贴图,很简单,大致需要 ...

  8. 再也不愁渲染素材了?AI 生成3D纹理 #Polycam3D 推出新功能

    最近有不少群友运用 AIGC 工具来提升工作效率,我听说连 3D 数字资产的渲染贴图素材都能生成了. Mixlab 小杜 3D 内容制作工具也是我非常感兴趣的领域,Polycam3D 本是一款扫描建模 ...

  9. 3D纹理绘制工具:Mari for mac(支持m1)

    为大家分享最新激活的The Foundry Mari for mac,这是一款专业的3D纹理绘制工具,具备全面的无限制3D绘画功能和工具,新版本的mari mac版带来了重新设计的货架调色板.货架标签 ...

最新文章

  1. 史上最强音视频下载神器youtube-dl回归,GitHub75k星
  2. JavaScript学习知识点归纳
  3. firefox下可恶的value
  4. java 判断对象为控制_Java流程控制
  5. 国外虚拟机下linux及mysql常用命令
  6. ISA2004升级到ISA2006需要注意的事项
  7. 阶段3 2.Spring_10.Spring中事务控制_7 spring基于注解的声明式事务控制
  8. Setup Factory 9打包Windows后台服务
  9. windows 系统arp命令
  10. 君莫笑:小白的堆(bai_dui)
  11. 为师弟师妹们连载(二)
  12. 用 Python 写软件原来这么简单!
  13. vos3000 更换ip具体操作方法
  14. OPENWRT路由器设置
  15. 录屏储存失败因为5823_为什么屏幕录制失败因为5823
  16. 转录组表达量计RPKM、FPKM、TPM说明
  17. oracle远程数据库导入导出。
  18. 难以置信的成就“楼天成”
  19. 【计算几何】求三角面和直线交点
  20. CSS3绘制的军曹giroro卡通图像

热门文章

  1. 酒店怎么提高会员忠诚度
  2. NVIDIA Jetson Nano GPIO口和通信协议简单介绍及点亮第一个程序LED灯闪烁
  3. Excel技巧—自动标记颜色条件格式的妙用
  4. rsync+inotify介绍
  5. 计算机十六进制4BH表示多大,【转】windows消息16进制对应表
  6. 组播mac地址什么用_MAC地址到底是用来做什么的?
  7. idea中刷新项目快捷键_IntelliJ Idea中常用快捷键(持续更新中)
  8. 解决URL传参中文乱码
  9. Tommy Hilfiger在第二届Tommy Hilfiger时尚前沿挑战赛上庆祝更加包容的时尚格局
  10. Windows 部署SFTP服务端