首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

地形纹理显示不了?该如何解决

2012-06-05 
地形纹理显示不了???定义了顶点结构:struct COSTOMVERTEX{float _x,_y,_z//XYZ坐标float _u,_v//uv纹理

地形纹理显示不了???
定义了顶点结构:
struct COSTOMVERTEX
{
  float _x,_y,_z;//XYZ坐标
  float _u,_v;//uv纹理坐标
  COSTOMVERTEX(float x,float y,float z,float u,float v)
  {
_x=x;
  _y=y;
  _z=z;
  _u=u;
  _v=v;
  }

};
#define COSTOM_FVF (D3DFVF_XYZ|D3DFVF_TEX1)

然后是给顶点赋值:

void GameLodTerrain::SetNodeVertex(QuadTreeNode *pNode)
{
   
if(pNode->m_Size<2||pNode==NULL) return;
  if (FALSE == pNode->m_bIsVisible||FALSE==pNode->m_bIsTouched)
  return;
  if (TRUE == pNode->m_bIsReached)
  {
// fill vertex buffer
  static INT nSizeDivide2 = m_row >> 1;
static INT R1=m_row-1;
  INT L = pNode->m_CenterX - (pNode->m_Size >> 1); // left
  INT R = pNode->m_CenterX + (pNode->m_Size >> 1); // right
  INT X = pNode->m_CenterX; // center X
  INT Y = pNode->m_CenterY; // center Y
  INT T = pNode->m_CenterY - (pNode->m_Size >> 1); // top
  INT B = pNode->m_CenterY + (pNode->m_Size >> 1); // bottom

  // 0 1 2 TL Top TR
  // 3 4 5 -> Left Center Right
  // 6 7 8 BL Bottom BR
  // TL
  m_pVerticesPtr[m_vbIndex + 0]._x = (L - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 0]._z = -(T - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 0]._y = m_heightmap[L *m_row+ T];
m_pVerticesPtr[m_vbIndex + 0]._u = float(L/R1);
m_pVerticesPtr[m_vbIndex + 0]._v = float(T/R1);

  // TOP
  m_pVerticesPtr[m_vbIndex + 1]._x = (X - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 1]._z = -(T - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 1]._y = m_heightmap[X *m_row+ T];
m_pVerticesPtr[m_vbIndex + 1]._u = float(X/R1);
m_pVerticesPtr[m_vbIndex + 1]._v = float(T/R1);

  // TR
  m_pVerticesPtr[m_vbIndex + 2]._x = (R - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 2]._z = -(T - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 2]._y = m_heightmap[R *m_row+ T];
m_pVerticesPtr[m_vbIndex + 2]._u = float(R/R1);
m_pVerticesPtr[m_vbIndex + 2]._v = float(T/R1);

  // LEFT
  m_pVerticesPtr[m_vbIndex + 3]._x = (L - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 3]._z = -(Y - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 3]._y = m_heightmap[L *m_row+ Y];
m_pVerticesPtr[m_vbIndex + 3]._u = float(L/R1);
m_pVerticesPtr[m_vbIndex + 3]._v = float(Y/R1);

  // CENTER
  m_pVerticesPtr[m_vbIndex + 4]._x = (X - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 4]._z = -(Y - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 4]._y = m_heightmap[X *m_row+ Y];
m_pVerticesPtr[m_vbIndex + 4]._u = float(X/R1);
m_pVerticesPtr[m_vbIndex + 4]._v = float(Y/R1);

  // RIGHT
  m_pVerticesPtr[m_vbIndex + 5]._x = (R - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 5]._z = -(Y - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 5]._y = m_heightmap[R *m_row+ Y];
m_pVerticesPtr[m_vbIndex + 5]._u = float(R/R1);
m_pVerticesPtr[m_vbIndex + 5]._v = float(Y/R1);

  // BL
  m_pVerticesPtr[m_vbIndex + 6]._x = (L - nSizeDivide2) * m_wScale;


  m_pVerticesPtr[m_vbIndex + 6]._z = -(B - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 6]._y = m_heightmap[L *m_row+ B];
m_pVerticesPtr[m_vbIndex + 6]._u = float(L/R1);
m_pVerticesPtr[m_vbIndex + 6]._v = float(B/R1);

  // BOTTOM
  m_pVerticesPtr[m_vbIndex + 7]._x = (X - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 7]._z = -(B - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 7]._y = m_heightmap[X *m_row+ B];
m_pVerticesPtr[m_vbIndex + 7]._u = float(X/R1);
m_pVerticesPtr[m_vbIndex + 7]._v = float(B/R1);

  // BR
  m_pVerticesPtr[m_vbIndex + 8]._x = (R - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 8]._z = -(B - nSizeDivide2) * m_wScale;
  m_pVerticesPtr[m_vbIndex + 8]._y = m_heightmap[R *m_row+ B];
m_pVerticesPtr[m_vbIndex + 8]._u = float(R/R1);
m_pVerticesPtr[m_vbIndex + 8]._v = float(B/R1);

  // fill index buffer
  // 0 1 2
  // 3 4 5
  // 6 7 8
  m_pIndicesPtr[m_ibIndex+ 0] = (WORD)m_vbIndex + 0;
  m_pIndicesPtr[m_ibIndex+ 1] = (WORD)m_vbIndex + 1;
  m_pIndicesPtr[m_ibIndex+ 2] = (WORD)m_vbIndex + 4;

  m_pIndicesPtr[m_ibIndex+ 3] = (WORD)m_vbIndex + 1;
  m_pIndicesPtr[m_ibIndex+ 4] = (WORD)m_vbIndex + 2;
  m_pIndicesPtr[m_ibIndex+ 5] = (WORD)m_vbIndex + 4;

  m_pIndicesPtr[m_ibIndex+ 6] = (WORD)m_vbIndex + 2;
  m_pIndicesPtr[m_ibIndex+ 7] = (WORD)m_vbIndex + 5;
  m_pIndicesPtr[m_ibIndex+ 8] = (WORD)m_vbIndex + 4;

  m_pIndicesPtr[m_ibIndex+ 9] = (WORD)m_vbIndex + 5;
  m_pIndicesPtr[m_ibIndex+ 10] = (WORD)m_vbIndex + 8;
  m_pIndicesPtr[m_ibIndex+ 11] = (WORD)m_vbIndex + 4;

  m_pIndicesPtr[m_ibIndex+ 12] = (WORD)m_vbIndex + 8;
  m_pIndicesPtr[m_ibIndex+ 13] = (WORD)m_vbIndex + 7;
  m_pIndicesPtr[m_ibIndex+ 14] = (WORD)m_vbIndex + 4;

  m_pIndicesPtr[m_ibIndex+ 15] = (WORD)m_vbIndex + 7;
  m_pIndicesPtr[m_ibIndex+ 16] = (WORD)m_vbIndex + 6;
  m_pIndicesPtr[m_ibIndex+ 17] = (WORD)m_vbIndex + 4;

  m_pIndicesPtr[m_ibIndex+ 18] = (WORD)m_vbIndex + 6;
  m_pIndicesPtr[m_ibIndex+ 19] = (WORD)m_vbIndex + 3;
  m_pIndicesPtr[m_ibIndex+ 20] = (WORD)m_vbIndex + 4;

  m_pIndicesPtr[m_ibIndex+ 21] = (WORD)m_vbIndex + 3;
  m_pIndicesPtr[m_ibIndex+ 22] = (WORD)m_vbIndex + 0;
  m_pIndicesPtr[m_ibIndex+ 23] = (WORD)m_vbIndex + 4;

m_vbIndex += 9;
  m_ibIndex+= 24;

}
else if (!pNode->m_bIsLeaf)
  {
  SetNodeVertex(pNode->m_pTLChildNode);
  SetNodeVertex(pNode->m_pTRChildNode);
  SetNodeVertex(pNode->m_pBLChildNode);
  SetNodeVertex(pNode->m_pBRChildNode);
  } 
}

渲染:
void GameLodTerrain::Render()


base::Instance().device->SetRenderState(D3DRS_LIGHTING, false);
base::Instance().device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
base::Instance().device->SetTransform(D3DTS_WORLD,&base::Instance().g_mWorld);
base::Instance().device->SetTexture(0,m_tex);

  base::Instance().device->SetStreamSource(0, m_vb, 0, sizeof(COSTOMVERTEX));
base::Instance().device->SetFVF(COSTOM_FVF);
base::Instance().device->SetIndices(m_ib);

  base::Instance().device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 
  m_vbIndex * 9, 0, m_vbIndex* 8);


  base::Instance().device->SetRenderState(D3DRS_LIGHTING, true);  
base::Instance().device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
}

最后得到的结果:

也就是纹理显示不出来,请问高手是怎么回事哦??




[解决办法]
以你的代码举例:
m_pVerticesPtr[m_vbIndex + 0]._v = float(T/R1);
这里面的,变量定义:
static INT R1=m_row-1;
INT T = pNode->m_CenterY - (pNode->m_Size >> 1); // top

显然,两个INT相除,结果必定是个INT,然后转换为float,肯定就不对了。
要先转换,再除:
m_pVerticesPtr[m_vbIndex + 0]._v = (float)T/R1;//或者T/(float)R1

热点排行