#### Archived

This topic is now archived and is closed to further replies.

# Help me with this INDEX BUFFER!!

## Recommended Posts

Nacho    205
Hi there! Ok, here´s the background: I´m pretty new to Direct3D 8.0 and I would like to make a cube and render a different texture to each of its faces. The best approach that came to my mind is the following: 1 * Create 24 vertex structures with their own position, normal, diffuse and specular color and its vertex coordinates. 2 * Use those 24 vertex structures to define each of the 4 points for each face on the cube. That is, 4 vertices to describe the front face, 4 vertices for the back face, and so on. 3 * Create an array of shorts for the indices to the vertex buffer. So, if the face is something like this 1-----------------------0 | | | | | | ------------------------- 2 3 I would define this face as two triangles. So the indices would be (0,1,2) and (0,2,3). 4 * Create and fill on each frame the index and vertex buffers. 5 * Use the following code to render each face: for(int cont=0;cont<6;cont++) { if(m_pTexture[cont]) g_pDevice->SetTexture(0,m_pTexture[cont]); // This line is the problem g_pDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP,cont*4,4,0,1); if(m_pTexture[cont]) g_pDevice->SetTexture(0,NULL); } After several hours testing through all the code writing to a log file, I determined that the problem is on the DrawIndexedPrimitive() method call. The explanation on the SDK is still a little difficult for me to completely understand it and I don´t know if I´m passing the right parameters. So please somebody help me! Ah, one more question. I would like to build an .exe file of this application (when it works, of course) but I don´t want to distribute the sample with 6 .bmps or .jpgs files. Is it possible to integrate everything in the executable file?? Thanks!!

##### Share on other sites
Steveyboy    122
quote:

I would like to build an .exe file of this application (when it works, of course) but I don´t want to distribute the sample with 6 .bmps or .jpgs files

Look at resources in the MSDN, theres plently of info there. You want to have your 6 BMPs as resources.

Yeah, i know, i found this a right bugger to get the hang of.
  HRESULT DrawIndexedPrimitive( D3DPRIMITIVETYPE Type, UINT MinIndex, UINT NumVertices, UINT StartIndex, UINT PrimitiveCount);

Type : D3DPT_TRIANGLELIST - or D3DPT_TRIANGLESTRIP would be faster, but you'd need to arse about with the indices.
MinIndex - the lowest index used in this call, so if you use 1 index buffer per cube, this value should be 0.
NumVertices - the number of vertices in this call - will be 24 for 24 vertices.
StartIndex - the starting point in the index buffer. Will be 0 if you are using 1 index buffer per cube. This value is only useful if you have several objects in one index buffer - e.g. 1 buffer of 10 cubes.
PrimitiveCount - the number of primitives in the call. This will be 12 for Triangle lists, or 6 for triangle strips (1 strip per face).

HTH, Steve

Edited by - Steveyboy on February 5, 2002 8:25:17 PM

##### Share on other sites
Bullmax    122
pD3DDevice->SetIndices( pIndexBuffer, sStartIndex );
pD3DDevice->SetTexture( 0, pTexture );
pD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 6, 0, 2 );

In DrawIndexedPrimitive
- first parameter is the type and should be to D3DPT_TRIANGLELIST because you want faces
- second parameter should be to 0 and use instead SetIndices() method (fixed problem with my Matrox G400)
- third parameter is the number of indices (6 in your case)
- last (fifth) parameter is the number of triangle (2 in your case)

For the integrated images into the executable, you create resources (your sample images) in your project and use for example D3DXCreateTextureFromResource() instead of D3DXCreateTextureFromFile().

Cya

/* Bullmax */
-------------
Reality has many forms : good and evil, black and white, ying and yang.

##### Share on other sites
Nacho    205
Yeah! Thanks. I realized about the TRIANGLELIST and the vertex buffer some minutes later after I opened this thread. Now I´m having some problems with the faces: just two of them are rendered properly, but I´ll try to fix this today.

##### Share on other sites
Nacho    205
Hi! I fixed the render problem, so thanks for the help BullMax. I´ve been reading the method D3DXCreateTextureFromResourceA() in the SDK´s Help many times, but I cannot map those bmp´s in the cube. So far, I´ve imported the six bmp´s on the project with Insert->Resource->Import. Since they are photographs, the compiler told me that they were correctly loaded but that it cannot display them in the IDE. The resourced are IDB_BITMAP1 ... IDB_BITMAP6. The code I used is the following:
In GameInit():

char buffer[80];
for(int cont=0;cont<6;cont++) {
sprintf(buffer,"IDB_BITMAP%d",cont+1);
Cubo.SetTexture(buffer,cont);
}

In the class definition:

HRESULT TexturedCube::SetTexture(char * strPathName,int position)
{
HRESULT r = 0;
if(m_pTexture[position])
m_pTexture[position]->Release();

r = D3DXCreateTextureFromResourceA(g_pDevice,NULL,strPathName,&m_pTexture[position]);
if(SUCCEEDED(r)) {
m_bTextureSet[position] = TRUE;
return S_OK;
}

return E_FAIL;
}

g_pDevice->Global pointer to Idirect3DDevice8
m_pTexture[position]->One of the six textures contained in the class.
m_bTextureSet[position]->BOOL variable defining if a texture has a been loaded or not.
Any suggestions out there??