Войти
ПрограммированиеФорумФизика

"Invalid index in the index stream" при рендеринге тряпок (NxCloth) через Direct3D 9

Advanced: Тема повышенной сложности или важная.

#0
14:27, 28 мар. 2007

Рендерю так:

gwVoid gwCloth::Render(gwBool shadows)
{
  gwDWord numVertices  = *mReceiveBuffers.numVerticesPtr;
  gwDWord numIndices   = *mReceiveBuffers.numIndicesPtr;
//  gwDWord numTriangles = *mReceiveBuffers.numIndicesPtr / 3;
  if(!numVertices) return;

//  if(Texture)
  {
    Engine->Render->SetTexture(0, Texture);
    Engine->Render->SetColor(gwColor(1.0f, 1.0f, 1.0f,1.0f));
  }

  if(VertexBuffer) VertexBuffer->Release();
  if(IndexBuffer)  IndexBuffer->Release();

  gwPointer pVertices;

  NxVec3* v = (NxVec3*)mReceiveBuffers.verticesPosBegin;
  NxVec3* n = (NxVec3*)mReceiveBuffers.verticesNormalBegin;
  if(Texture)
  {
    UpdateTextureCoordinates();
    VertexBuffer = Engine->Render->CreateVertexBuffer(numVertices, GWVT_XYZ_NORMAL_TEX1);
    VertexBuffer->Lock(&pVertices, numVertices * sizeof(Vertex));
    Vertex *buffer = (Vertex*)pVertices;//(Vertex*)malloc(sizeof(Vertex)*numVertices);
    for(gwDWord i = 0; i < numVertices; ++i)
    {
      buffer[i].x = v[i].x;
      buffer[i].y = v[i].y;
      buffer[i].z = v[i].z;
      buffer[i].nx = n[i].x;
      buffer[i].ny = n[i].y;
      buffer[i].nz = n[i].z;
      buffer[i].u = mTexCoords[2*i];
      buffer[i].v = mTexCoords[2*i+1];
    }
  }
  else
  {
    VertexBuffer = Engine->Render->CreateVertexBuffer(numVertices, GWVT_XYZ_NORMAL);
    VertexBuffer->Lock(&pVertices, numVertices * sizeof(VertexNoUV));
    VertexNoUV *buffer = (VertexNoUV*)pVertices;//(Vertex*)malloc(sizeof(Vertex)*numVertices);
    for(gwDWord i = 0; i < numVertices; ++i)
    {
      buffer[i].x = v[i].x;
      buffer[i].y = v[i].y;
      buffer[i].z = v[i].z;
      buffer[i].nx = n[i].x;
      buffer[i].ny = n[i].y;
      buffer[i].nz = n[i].z;
    }
  }
//    gwCopyMem(pVertices, buffer, numVertices * sizeof(Vertex));
  VertexBuffer->Unlock();

  gwPointer pPolygon;

  IndexBuffer = Engine->Render->CreateIndexBuffer(numIndices, GWPIT_32BIT/*, GWPIT_16BIT, true*/);
  IndexBuffer->Lock(&pPolygon, numIndices * sizeof(gwUInt));
  gwUInt* indecies = (gwUInt*)mReceiveBuffers.indicesBegin;
/*  gwUInt *buffer = (gwUInt *) pPolygon;
  for(gwDWord i = 0; i < numIndices; ++i)
  {
    buffer[i] = indecies[i];
    assert(indecies[i] >= 0 && indecies[i] < numVertices);
  }*/
    gwCopyMem(pPolygon, indecies, numIndices * sizeof(gwUInt));
  IndexBuffer->Unlock();

  Engine->Render->SetIndexBuffer(IndexBuffer);
  Engine->Render->SetVertexBuffer(VertexBuffer);
  Engine->Render->RenderIndexedPrimitive(GWPT_TRIANGLES, numIndices);

//  free(buffer);

  if(Texture)
  {
    Engine->Render->SetTexture(0, GW_NULL_TEXTURE);
  }

  if(shadows)
  {
    const static gwFloat ShadowMat[] = { 1,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,0,1 };
    Engine->Render->PushViewMatrix();
    Engine->Render->MultViewMatrix(ShadowMat);
    Engine->Render->DisableLighting();
    Engine->Render->SetColor(gwColor(0.05f, 0.1f, 0.15f,1.0f));
    Engine->Render->RenderIndexedPrimitive(GWPT_TRIANGLES, numIndices);/**/
    Engine->Render->SetColor(gwColor(1.0f, 1.0f, 1.0f,1.0f));
    Engine->Render->EnableLighting();
    Engine->Render->PopViewMatrix();
  }
}

где RenderIndexedPrimitive:

gwResult gwDirect3D9::RenderIndexedPrimitive(gwPrimitiveType type, gwUInt PrimitiveNum)
{
  if(!CurrentVertexBuffer)
    return gwThrow(GW_ERR_DIRECT3D9_ERROR, _T("CurrentVertexBuffer = NULL"));

  if(!CurrentIndexBuffer)
    return gwThrow(GW_ERR_DIRECT3D9_ERROR, _T("CurrentIndexBuffer = NULL"));

  if(PrimitiveNum == 0) PrimitiveNum = CurrentIndexBuffer->Num;

/*
  if(type == GWPT_POINTS)             { if(FAILED(hr = d3d_device->DrawIndexedPrimitive(D3DPT_POINTLIST,     0, 0, CurrentVertexBuffer->Num, 0, PrimitiveNum)))  return gwThrow(GW_ERR_DIRECT3D9_ERROR, DXGetErrorString9(hr)); }
  else if(type == GWPT_LINES)         { if(FAILED(hr = d3d_device->DrawIndexedPrimitive(D3DPT_LINELIST,      0, 0, CurrentVertexBuffer->Num, 0, PrimitiveNum)))  return gwThrow(GW_ERR_DIRECT3D9_ERROR, DXGetErrorString9(hr)); }
  else if(type == GWPT_LINESTRIP)     { if(FAILED(hr = d3d_device->DrawIndexedPrimitive(D3DPT_LINESTRIP,     0, 0, CurrentVertexBuffer->Num, 0, PrimitiveNum)))  return gwThrow(GW_ERR_DIRECT3D9_ERROR, DXGetErrorString9(hr)); }
  else if(type == GWPT_TRIANGLES)     { if(FAILED(hr = d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,  0, 0, CurrentVertexBuffer->Num, 0, PrimitiveNum)))  return gwThrow(GW_ERR_DIRECT3D9_ERROR, DXGetErrorString9(hr)); if(CalculateTPF) TPF += PrimitiveNum; }
  else if(type == GWPT_TRIANGLESTRIP) { if(FAILED(hr = d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, CurrentVertexBuffer->Num, 0, PrimitiveNum)))  return gwThrow(GW_ERR_DIRECT3D9_ERROR, DXGetErrorString9(hr)); if(CalculateTPF) TPF += PrimitiveNum; }
  else if(type == GWPT_TRIANGLEFAN)   { if(FAILED(hr = d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLEFAN,   0, 0, CurrentVertexBuffer->Num, 0, PrimitiveNum)))  return gwThrow(GW_ERR_DIRECT3D9_ERROR, DXGetErrorString9(hr)); if(CalculateTPF) TPF += PrimitiveNum; }
  else                                return gwThrow(GW_ERR_INVALID_PARAM, _T("type."));
*/
  if(FAILED(hr = d3d_device->DrawIndexedPrimitive(GW_D3D9_PRIMITIVETYPES_TABLE[type], 0, 0, CurrentVertexBuffer->Num, 0, PrimitiveNum)))
    return gwThrow_ErrorDirect3D9(_T("Can't render indexed primitive, IDirect3DDevice9::DrawIndexedPrimitive failed"));

  if(type >= GWPT_TRIANGLES) if(CalculateTPF) TPF += PrimitiveNum;

  return hr;
}

в результате:

Direct3D9: (ERROR) :Invalid index in the index stream: -572662307

Как такое может быть?.. Ведь у DrawIndexedPrimitive все параметры правильные?..


ПрограммированиеФорумФизика

Тема в архиве.