** DirectX9 3D ** ** 快速上手 ** ** 6
**
** By sssa2000
**
** 4/25/2005 ** **
**
讲了很多,最基础的部分就剩下纹理没有讲到了。 Texture 是 Directx 里面非常重要的一部分。为了简便起见,我们还是以 SDK 的 Tutorial 5 为例子。
纹理就像一张墙纸,用来贴在物体的表面,当然,如果足够大,贴一次就能覆盖整个物体的表面,也可以用适当的方法让纹理排列成你要的效果。
来看看纹理的比较重要的函数: Device.SetTexture
public void SetTexture (
int stage , // 纹理混合阶段序号,从 0 开始
BaseTexture texture // _ 要设置的纹理对象 _
);
public void SetTextureStageState (
int stage , // 纹理混合阶段序号
TextureStageStates state , // TextureStageStates enumeration 的成员
int value // _ 对应阶段状态的值 _
);
** SetTextureStageState ** ** 函数对处理不同的纹理坐标,颜色操作, Alpha ** ** 操作,和凹凸映射 / ** ** 环境映射比较适用,但是这些操作只对 DX9 ** ** 的固定功能的多纹理单元有效,不能将他们与像素 shader ** ** 连用。
**
public void SetSamplerState (
int stage , // 纹理混合阶段序号
SamplerStageStates state , // SamplerStageStates enumeration 的成员
int _value // _ _ 对应采样器状态的值 _
);
知道了这些下面读懂这些代码就很容易了,我们需要建立 Vertex ,这里我们需要有一点点地改变,在以前我们接触到的 Vertex 里面都不涉及到纹理,所以我们选择了 CustomVertex 里面不包括纹理的类型,现在我们要用 CustomVertex.PositionNormalTextured ,从名字就可以看出来,这个类型包括了法线还包括了位置的 X,Y,Z ,以及纹理坐标的 Tu 和 Tv 。
当然如果使用 CustomVertex.PositionTextured 也是可以的,它不包括法线信息。
接下来我们需要为每个 Vertex 指定信息,我们先打断一下讲讲纹理坐标,为了通过指定纹理坐标来访问纹理中的每个图素, DX 采用了一个一般化的编址方案,纹理地址由 [0.0,1.0] 区间内的坐标组成,这样我们就不用关心纹理的实际尺寸,例如可以使用 ( 0.0f , 0.0f ) ,( 1.0f , 0.0f ),( 1.0f , 1.0f ),( 0.0f , 1.0f ) 把一个纹理贴到一个矩形上,同样如果 ( 0.0f , 0.0f ) ,(0 。 5f , 0.0f ),(0.5, 1.0f ),( 0.0f , 1.0f ) 就是纹理的左半边。
我们可以通过 TextureLoader.FromFile 方法来读入图片作为纹理。
这里代码很简单里面有详细的注释,我就不多讲了,
//-----------------------------------------------------------------------------
// File: texture.cs
//
// Desc: Better than just lights and materials, 3D objects look much more
// convincing when texture-mapped. Textures can be thought of as a sort
// of wallpaper, that is shrinkwrapped to fit a texture. Textures are
// typically loaded from image files, and D3DX provides a utility to
// function to do this for us. Like a vertex buffer, textures have
// Lock() and Unlock() functions to access (read or write) the image
// data. Textures have a width, height, miplevel, and pixel format. The
// miplevel is for "mipmapped" textures, an advanced performance-
// enhancing feature which uses lower resolutions of the texture for
// objects in the distance where detail is less noticeable. The pixel
// format determines how the colors are stored in a texel. The most
// common formats are the 16-bit R 5G 6B5 format (5 bits of red, 6-bits of
// green and 5 bits of blue) and the 32-bit A8R 8G 8B8 format (8 bits each
// of alpha, red, green, and blue).
//
// Textures are associated with geometry through texture coordinates.
// Each vertex has one or more sets of texture coordinates, which are
// named tu and tv and range from 0.0 to 1.0. Texture coordinates can be
// supplied by the geometry, or can be automatically generated using
// Direct3D texture coordinate generation (which is an advanced feature).
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D=Microsoft.DirectX.Direct3D;
namespace TextureTutorial
{
public class Textures : Form
{
// Our global variables for this project
Device device = null ; // Our rendering device
VertexBuffer vertexBuffer = null ;
Texture texture = null ;
PresentParameters presentParams = new PresentParameters();
bool pause = false ;
public Textures()
{
// Set the initial size of our form
this .ClientSize = new System.Drawing.Size(400,300);
// And its caption
this .Text = "Direct3D Tutorial 5 - Textures";
}
public bool InitializeGraphics()
{
try
{
presentParams.Windowed= true ; // We don't want to run fullscreen
presentParams.SwapEffect = SwapEffect.Discard; // Discard the frames
presentParams.EnableAutoDepthStencil = true ; // Turn on a Depth stencil
presentParams.AutoDepthStencilFormat = DepthFormat.D16; // And the stencil format
device = new Device(0, DeviceType.Hardware, this , CreateFlags.SoftwareVertexProcessing, presentParams); //Create a device
device.DeviceReset += new System.EventHandler( this .OnResetDevice);
this .OnCreateDevice(device, null );
this .OnResetDevice(device, null );
pause = false ;
return true ;
}
catch (DirectXException)
{
// Catch any errors and return a failure
return false ;
}
}
public void OnCreateDevice( object sender, EventArgs e)
{
Device dev = (Device)sender;
// Now Create the VB
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 );
}
public void OnResetDevice( object sender, EventArgs e)
{
Device dev = (Device)sender;
// Turn off culling, so we see the front and back of the triangle
dev.RenderState.CullMode = Cull.None;
// Turn off D3D lighting
dev.RenderState.Lighting = false ;
// Turn on the ZBuffer
dev.RenderState.ZBufferEnable = true ;
// Now create our texture
texture = TextureLoader.FromFile(dev, Application.StartupPath + @"\..\..\banana.bmp");
<p clas