新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 计算机科学论坛计算机技术与应用『 C/C++编程思想 』 → 高级纹理映射技术(3) 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 15947 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: 高级纹理映射技术(3) 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 高级纹理映射技术(3)

    对一些特殊的应用需要对纹理坐标进行处理,主要包括纹理坐标自动生成和纹理坐标变换。下图显示了纹理坐标的来源、处理过程以及到达光栅处理器的过程。

    按此在新窗口浏览图片

    纹理坐标自动生成

    在Direct3D程序中,不仅可以在模型载入阶段或渲染阶段指定物体的纹理坐标,还可以通过Direct3D渲染引擎自动生成纹理坐标,用于诸如环境映射等特殊的视觉效果。与手动设置纹理坐标相比,纹理坐标自动生成在Direct3D坐标变换和光照流水线中完成,执行速度更快。

    Direct3D系统可以使用经过变换的摄像机空间顶点位置坐标、法线信息来生成纹理坐标。如果使用纹理坐标自动生成,那么在顶点中就可以不用包含纹理坐标数据,从而可以降低图形渲染时的数据传输量。纹理坐标自动生成主要用于产生一些特殊效果,在大多数情况下还是手工为每个顶点指定纹理坐标。

    通过调用SetTextureStageState()并将第二个参数设置为D3DTSS_TEXCOORDINDEX来控制Direct3D系统如何自动生成纹理坐标。

    D3DTSS_TEXCOORDINDEX
    Index of the texture coordinate set to use with this texture stage. You can specify up to eight sets of texture coordinates per vertex. If a vertex does not include a set of texture coordinates at the specified index, the system defaults to the u and v coordinates (0,0).
    When rendering using vertex shaders, each stage's texture coordinate index must be set to its default value. The default index for each stage is equal to the stage index. Set this state to the zero-based index of the coordinate set for each vertex that this texture stage uses.

    Additionally, applications can include, as logical OR with the index being set, one of the constants to request that Direct3D automatically generate the input texture coordinates for a texture transformation. For a list of all the constants, see D3DTSS_TCI.

    With the exception of D3DTSS_TCI_PASSTHRU, which resolves to zero, if any of the following values is included with the index being set, the system uses the index strictly to determine texture wrapping mode. These flags are most useful when performing environment mapping.

    其中第三个参数可以设为下列列表中的成员:

    D3DTSS_TCI
    Driver texture coordinate capability flags.

    #define Value Description
    D3DTSS_TCI_PASSTHRU 0x00000000L Use the specified texture coordinates contained within the vertex format. This value resolves to zero.
    D3DTSS_TCI_CAMERASPACENORMAL 0x00010000L Use the vertex normal, transformed to camera space, as the input texture coordinates for this stage's texture transformation.
    D3DTSS_TCI_CAMERASPACEPOSITION 0x00020000L Use the vertex position, transformed to camera space, as the input texture coordinates for this stage's texture transformation.
    D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR 0x00030000L Use the reflection vector, transformed to camera space, as the input texture coordinate for this stage's texture transformation. The reflection vector is computed from the input vertex position and normal vector.
    D3DTSS_TCI_SPHEREMAP 0x00040000L Use the specified texture coordinates for sphere mapping.

    These constants are used by D3DTSS_TEXCOORDINDEX.

    D3DTSS_TEXCOORDINDEX用于指定特定纹理层使用顶点中的第几组纹理坐标,但如果指定了上表中的成员值,Direct3D将忽略顶点中的纹理坐标,转而使用自动生成的纹理坐标。

    D3DTSS_TEXTURETRANSFORMFLAGS用来控制生成的纹理坐标的输出,在大多数情况下纹理坐标是二维的,即将D3DTSS_TEXTURETRANSFORMFLAGS设置为D3DTTFF_COUNT2。但当绘制线段或三维纹理时,纹理坐标可能是一维或三维的。

    D3DTSS_TEXTURETRANSFORMFLAGS
    Member of the D3DTEXTURETRANSFORMFLAGS enumerated type that controls the transformation of texture coordinates for this texture stage. The default value is D3DTTFF_DISABLE.
    输出的纹理坐标维数由枚举类型D3DTSS_TEXTURETRANSFORMFLAGS指定,其定义如下:

    Defines texture coordinate transformation values.

    typedef enum D3DTEXTURETRANSFORMFLAGS{    D3DTTFF_DISABLE = 0,    D3DTTFF_COUNT1 = 1,    D3DTTFF_COUNT2 = 2,    D3DTTFF_COUNT3 = 3,    D3DTTFF_COUNT4 = 4,    D3DTTFF_PROJECTED = 256,    D3DTTFF_FORCE_DWORD = 0x7fffffff,} D3DTEXTURETRANSFORMFLAGS, *LPD3DTEXTURETRANSFORMFLAGS;
    Constants
    D3DTTFF_DISABLE
    Texture coordinates are passed directly to the rasterizer.
    D3DTTFF_COUNT1
    The rasterizer should expect 1D texture coordinates. This value is used by fixed function vertex processing; it should be set to 0 when using a programmable vertex shader.
    D3DTTFF_COUNT2
    The rasterizer should expect 2D texture coordinates. This value is used by fixed function vertex processing; it should be set to 0 when using a programmable vertex shader.
    D3DTTFF_COUNT3
    The rasterizer should expect 3D texture coordinates. This value is used by fixed function vertex processing; it should be set to 0 when using a programmable vertex shader.
    D3DTTFF_COUNT4
    The rasterizer should expect 4D texture coordinates. This value is used by fixed function vertex processing; it should be set to 0 when using a programmable vertex shader.
    D3DTTFF_PROJECTED
    This flag is honored by the fixed function pixel pipeline, as well as the programmable pixel pipeline in versions ps_1_1 to ps_1_3. When texture projection is enabled for a texture stage, all four floating point values must be written to the corresponding texture register. Each texture coordinate is divided by the last element before being passed to the rasterizer. For example, if this flag is specified with the D3DTTFF_COUNT3 flag, the first and second texture coordinates are divided by the third coordinate before being passed to the rasterizer.
    D3DTTFF_FORCE_DWORD
    Forces this enumeration to compile to 32 bits in size. Without this value, some compilers would allow this enumeration to compile to a size other than 32 bits. This value is not used.
    Remarks
    Texture coordinates can be transformed using a 4 x 4 matrix before the results are passed to the rasterizer. The texture coordinate transforms are set by calling IDirect3DDevice9::SetTextureStageState, and by passing in the D3DTSS_TEXTURETRANSFORMFLAGS texture stage state and one of the values from D3DTEXTURETRANSFORMFLAGS. For more information about texture transforms, see Texture Coordinate Transformations (Direct3D 9).

    首先,我们定义顶点结构和格式:

    struct sCustomVertex{ float x, y, z;};
    #define D3DFVF_CUSTOM_VERTEX D3DFVF_XYZ
    接着生成顶点数据,顶点数据中没有包含纹理坐标:

    // create vertex buffer and fill data
    sCustomVertex vertices[] =  {    { -1.0f, -1.0f,  0.0f},    { -1.0f,  1.0f,  0.0f},    {  1.0f, -1.0f,  0.0f},    {  1.0f,  1.0f,  0.0f}};
    pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_MANAGED, &g_vertex_buffer, NULL);
    void* ptr;g_vertex_buffer->Lock(0, sizeof(vertices), (void**)&ptr, 0);memcpy(ptr, vertices, sizeof(vertices));g_vertex_buffer->Unlock();
    然后让Direct3D自动生成纹理坐标:

    // create texture coordinate using vertex position in camera space
    pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
    pd3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);

    运行效果:

    按此在新窗口浏览图片

    若设置D3DTSS_TEXCOORDINDEX为以下D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR,则效果为:

    按此在新窗口浏览图片

    若设置D3DTSS_TEXCOORDINDEX为以下D3DTSS_TCI_SPHEREMAP,则效果为:

    按此在新窗口浏览图片


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/12/8 15:24:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2026/3/6 14:59:12

    本主题贴数5,分页: [1]

     *树形目录 (最近20个回帖) 顶端 
    主题:  高级纹理映射技术(3)(7478字) - 卷积内核,2008年12月8日
        回复:  主程序:#include "dxstdafx.h"#include "resource.h..(15232字) - 卷积内核,2008年12月8日
        回复:  三维场景中的物体不仅受光照影响,而且受周围环境的影响,可以映射出周围环境的图像,环境映射就是模拟物..(3416字) - 卷积内核,2008年12月8日
        回复:  [B]纹理坐标变换[/B]Direct3D提供了对生成的纹理坐标进行坐标变换的功能,与顶点坐标..(16429字) - 卷积内核,2008年12月8日
        回复:  主程序:#include "dxstdafx.h"#include "resource.h..(14540字) - 卷积内核,2008年12月8日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms