Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem with drawindexedprimitive


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Sesha   Members   -  Reputation: 107

Like
0Likes
Like

Posted 14 May 2012 - 08:53 AM

Hai people,

After long practice now i am stuck with a simple problem. The thing is i want to render the second triangle of a square but it draws the next adjacent triangle.

void D3DApp::Init_Vertexbuffer()
{
HRESULT hr;
float PosPtrX = -width*0.5f;
float PosPtrZ = depth*0.5f;
int i = 0;
hr=d3ddev->CreateVertexBuffer(numVertices*sizeof(Vertices),D3DUSAGE_WRITEONLY,D3D9T_CUSTOMVERTEX,D3DPOOL_MANAGED,&vb,0);
std::vector<D3DXVECTOR3>vert;
vert.resize(numVertices);
for(int j=0; j<numVertCols; j++)
{
  for(int k=0; k<numVertRows; k++)
  {
   vert[i].x = PosPtrX;
   vert[i].y = 0.0f;
   vert[i].z = PosPtrZ;
   PosPtrZ += -dz;
   ++i;
  }
  PosPtrX += +dx;
  PosPtrZ = depth*0.5f;
}
hr=vb->Lock(0,0,(void**)&v,0);
for(int i = 0; i<numVertices; ++i)
{
  v[i].pos = vert[i];
  v[i].col = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
}
vb->Unlock();
}

void D3DApp::Init_Indexbuffer()
{
HRESULT hr;
int numIndices = 3 * numTris;
hr=d3ddev->CreateIndexBuffer(numIndices*sizeof(Vertices),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&ib,0);
std::vector<DWORD>index;
index.resize(numIndices);
int c = 0;
for(int a=0; a<numCellCols; a++)
{
  for(int b=0; b<numCellRows; b++)
  {
   index[c++]   =  a    * numVertRows + b;
   index[c++] = (a + 1) * numVertRows + b;
   index[c++] =    a    * numVertRows + b + 1;
   index[c++] =    a    * numVertRows + b + 1;
   index[c++] = (a + 1) * numVertRows + b;
   index[c++] = (a + 1) * numVertRows + b + 1;
   //c += 6;
  }
}
WORD *k = 0;
hr=ib->Lock(0,0,(void**)&k,0);
for(int i = 0; i<numIndices; ++i)
  k[i] = (WORD)index[i];
ib->Unlock();
}

void D3DApp::Scene_Update(HWND &hWnd)
{
d3ddev->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xff888888,1.0f,0);
d3ddev->BeginScene();
cInput.Press();
if(cInput.KeyDown(DIK_LCONTROL))
{
  thetaY += cInput.MouseX() / 250.0f;
}
z -= cInput.MouseZ() / 3000.0f;
camx = (z * 1.5f * width * sin(thetaY));
camy = (z * 1.5f * width * sin(thetaZ));
camz = (z * 1.5f * depth * cos(thetaY));
eyevector=D3DXVECTOR3(camx, camy, -camz);
D3DXMatrixLookAtLH(&viewmatrix,&eyevector,&lookatvector,&upvector);
d3ddev->SetFVF(D3D9T_CUSTOMVERTEX);
d3ddev->SetStreamSource(0,vb,0,sizeof(Vertices));
d3ddev->SetIndices(ib);
mfx->SetMatrix(mMatrix,&(viewmatrix*projectionmatrix));
mfx->SetTechnique(mTech);
d3ddev->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
d3ddev->SetRenderState(D3DRS_LIGHTING,FALSE);
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE );
UINT numpass=0;
mfx->Begin(&numpass,0);
for(unsigned int a=0; a < numpass; ++a)
{
  mfx->BeginPass(a);
  d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,numVertices,0,numTris);
  mfx->EndPass();
}
mfx->End();
//d3ddev->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
if(cInput.KeyDown(DIK_LSHIFT))
  CheckHit(hWnd);
d3ddev->EndScene();
Time.End_Timer();
d3ddev->Present(0,0,0,0);
}

void D3DApp::CheckHit(HWND &hWnd)
{
POINT s;
float w, h;
D3DXVECTOR3 Origin;
D3DXVECTOR3 Dir;
D3DXMATRIX invView;
GetCursorPos(&s);
ScreenToClient(hWnd, &s);
w = (float)d3dpp.BackBufferWidth;
h = (float)d3dpp.BackBufferHeight;
float x =  ( (2.0f * s.x / w) - 1.0f) / viewmatrix._11;
float y = -( (2.0f * s.y / h) - 1.0f) / viewmatrix._22;
Origin = D3DXVECTOR3(x, y, 1.0f);
Dir    = D3DXVECTOR3(x, y, 100.0f);
D3DXMatrixInverse(&invView, 0, &viewmatrix);
D3DXVec3TransformCoord(&Origin, &Origin, &invView);
D3DXVec3TransformNormal(&Dir, &Dir, &invView);
D3DXVec3Normalize(&Dir, &Dir);
float bu = 0.0f;
float bv = 0.0f;
float Dist = 0.0f;
D3DXVECTOR3 vector0;
D3DXVECTOR3 vector1;
D3DXVECTOR3 vector2;
int c = 0;
for(int a=0; a<numCellCols; a++)
{
  for(int b=0; b<numCellRows; b++)
  {
   vector0 = v[   a    * numVertRows + b].pos;
   vector1 = v[(a + 1) * numVertRows + b].pos;
   vector2 = v[   a    * numVertRows + b + 1].pos;
   if(D3DXIntersectTri(&vector0, &vector1, &vector2, &Origin, &Dir, &bu, &bv, &Dist))
   {
    vb->Lock(0,0,(void**)&v,0);
    v[   a    * numVertRows + b].col	 = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b].col	 = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    v[   a    * numVertRows + b + 1].col = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    vb->Unlock();
    UINT numpass=0;
    mfx->Begin(&numpass,0);
    for(unsigned int d=0; d < numpass; ++d)
    {
	 mfx->BeginPass(d);
	 d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,(a * numVertRows + b),0,3,0,1);
	 mfx->EndPass();
    }
    mfx->End();
    vb->Lock(0,0,(void**)&v,0);
    v[   a    * numVertRows + b].col	 = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b].col	 = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    v[   a    * numVertRows + b + 1].col = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    vb->Unlock();
   }
   vector0 = v[   a    * numVertRows + b + 1].pos;
   vector1 = v[(a + 1) * numVertRows + b].pos;
   vector2 = v[(a + 1) * numVertRows + b + 1].pos;
   if(D3DXIntersectTri(&vector0, &vector1, &vector2, &Origin, &Dir, &bu, &bv, &Dist))
   {
    vb->Lock(0,0,(void**)&v,0);
    v[   a    * numVertRows + b + 1].col = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b].col	 = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b + 1].col = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
    vb->Unlock();
    UINT numpass=0;
    mfx->Begin(&numpass,0);
    for(unsigned int d=0; d < numpass; ++d)
    {
	 mfx->BeginPass(d);
	 d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,(a * numVertRows + b + 1),0,3,0,1);
	 mfx->EndPass();
    }
    mfx->End();
    vb->Lock(0,0,(void**)&v,0);
    v[   a    * numVertRows + b + 1].col = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b].col	 = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    v[(a + 1) * numVertRows + b + 1].col = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
    vb->Unlock();
   }//end if
  }//end for b
}//end for a
/*Vertices *vertex = 0;
hr=vb->Lock(0,0,(void**)&v,0);
for(int i = 0; i<numVertices; ++i)
{
  v[i].pos = vert[i];
  v[i].col = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
}
vb->Unlock();*/
}

Drawing the second drawindexprimitive of checkhit() is the focus it draws the wrong second vertex.

Thanks in advance.

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS