博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

纹理映射

Posted on 2011-03-02 22:56  +石头+  阅读(223)  评论(0)    收藏  举报

长话短说,为一个图形设置纹理的流程如下:

  1. 创建纹理:D3DXCreateTextureFromFile(LPDIRECT3DDEVICE9 pDevice, LPCSTR pSrcFile, LPDIRECT3DTEXTURE9* ppTexture);
  2. 设置纹理过滤器和设置多级渐进纹理过滤器,纹理三角形和屏幕三角形不能大小完全匹配时,使用纹理过滤器来克服这种畸变,多级渐进纹理过滤器是创建一个多级渐进的纹理链

    (例如创建256×256,128*128,64*64.大小的图片,选择合适目标三角形。)

    纹理过滤器又分最近点采样,线性纹理过滤,异性纹理过滤

    这里写了常用的线性纹理过滤

    g_d3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

    g_d3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

    多级渐进纹理

g_d3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);

  1. 设置纹理:

    m_device->SetTexture(0, tex);

Cube::Cube(IDirect3DDevice9* device)

{

    m_device = device;

    

    m_device->CreateVertexBuffer(24*sizeof(Vertex), D3DUSAGE_WRITEONLY, FVF_VERTEX, D3DPOOL_MANAGED, &m_vb, 0);

 

    Vertex* v;

    m_vb->Lock(0, 0, (void**)&v, 0);

    

    // fill in the front face vertex data

    v[0] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);

    v[1] = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);

    v[2] = Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);

    v[3] = Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);

 

    // fill in the back face vertex data

    v[4] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);

    v[5] = Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);

    v[6] = Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);

    v[7] = Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);

 

    // fill in the top face vertex data

    v[8] = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);

    v[9] = Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);

    v[10] = Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);

    v[11] = Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);

 

    // fill in the bottom face vertex data

    v[12] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f);

    v[13] = Vertex( 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f);

    v[14] = Vertex( 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f);

    v[15] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);

 

    // fill in the left face vertex data

    v[16] = Vertex(-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f);

    v[17] = Vertex(-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f);

    v[18] = Vertex(-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f);

    v[19] = Vertex(-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f);

 

    // fill in the right face vertex data

    v[20] = Vertex( 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);

    v[21] = Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);

    v[22] = Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);

    v[23] = Vertex( 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);

 

    m_vb->Unlock();

 

    m_device->CreateIndexBuffer(36*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ib, 0);

 

    WORD* i = 0;

    m_ib->Lock(0, 0, (void**)&i, 0);

    // fill in the front face index data

    i[0] = 0; i[1] = 1; i[2] = 2;

    i[3] = 0; i[4] = 2; i[5] = 3;

 

    // fill in the back face index data

    i[6] = 4; i[7] = 5; i[8] = 6;

    i[9] = 4; i[10] = 6; i[11] = 7;

 

    // fill in the top face index data

    i[12] = 8; i[13] = 9; i[14] = 10;

    i[15] = 8; i[16] = 10; i[17] = 11;

 

    // fill in the bottom face index data

    i[18] = 12; i[19] = 13; i[20] = 14;

    i[21] = 12; i[22] = 14; i[23] = 15;

 

    // fill in the left face index data

    i[24] = 16; i[25] = 17; i[26] = 18;

    i[27] = 16; i[28] = 18; i[29] = 19;

 

    // fill in the right face index data

    i[30] = 20; i[31] = 21; i[32] = 22;

    i[33] = 20; i[34] = 22; i[35] = 23;

 

    m_ib->Unlock();

}

Cube::~Cube()

{

    if(m_ib)

    {

        m_ib->Release();

        m_ib = NULL;

    }

    if(m_vb)

    {

        m_vb->Release();

        m_vb = NULL;

    }

}

void Cube::draw(D3DXMATRIX* world, D3DMATERIAL9* mtrl, IDirect3DTexture9* tex)

{

    if(world)

        m_device->SetTransform(D3DTS_WORLD, world);

    if(mtrl)

        m_device->SetMaterial(mtrl);

    if(tex)

        m_device->SetTexture(0, tex);

 

    m_device->SetStreamSource(0, m_vb, 0, sizeof(Vertex));

    m_device->SetIndices(m_ib);

    m_device->SetFVF(FVF_VERTEX);

    m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);

}