Archived

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

Sfpiano

Loading bsp level

Recommended Posts

Sfpiano    126
Here is the code for my vb:
tBSPVertex  *m_pVerts;

pDevice->CreateVertexBuffer(m_numOfVerts*sizeof(tBSPVertex), 0, D3DFVF_BSPFACE, D3DPOOL_MANAGED,
                                      &p_VB, NULL);

	VOID* pVerticeLock=0;
	p_VB->Lock(0, m_numOfVerts*sizeof(tBSPVertex), &pVerticeLock, 0);
	memcpy(pVerticeLock, m_pVerts, m_numOfVerts*sizeof(tBSPVertex));
	p_VB->Unlock();
 
And then I render each face like so:
pDevice->SetStreamSource(0, p_VB, 0, sizeof(tBSPVertex));
pDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, pFace->startVertIndex, pFace->numOfVerts-2); 
However nothing shows up on screen. If I change the stream source stride to the size of a face, I get this mutilated object, but I thought I should be doing everything in reference to the vertices and just using the faces to tell me which vertices to load. //------------------------------------------------------------------------------------------------------ The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one." [edited by - Sfpiano on January 18, 2004 12:04:53 PM]

Share this post


Link to post
Share on other sites
neneboricua19    634
I don''t want this to come out the wrong way, but there is absolutely no way for anyone to tell just from the code you posted why it doesn''t work. We don''t know how you built the BSP tree, how you''ve traversed it, or if the pointer to a particular face you''re using is even valid.

As a suggestion, a better way to implement this is to create a dynamic vertex buffer at initialization. As you traverse your BSP tree, add to this dynamic vertex buffer the vertices of the triangles you want to render. When you''re done traversing the tree, call DrawPrimitive once to draw all of your geometry. Calling DrawPrimitive for each face is a HUGE waste of resources. Graphics cards are optimized to render 100''s of primitives at a time. Rendering them one by one will totally kill your performance.

Oh, and use a triangle list or triangle strip as their both a little faster than fans.

neneboricua

Share this post


Link to post
Share on other sites
Sfpiano    126
Ok, well I figured everybody loaded the basic level the same way; here's the code (it's from gametutorials.com):

bool CQuake3BSP::LoadBSP(const char *strFileName, LPDIRECT3DDEVICE9 &pDevice){
FILE *fp = NULL;
int i = 0;

if((fp = fopen(strFileName, "rb")) == NULL){
MessageBox(NULL, "Could not find BSP file!", "Error", MB_OK);
return false;
}

// Initialize the header and lump structures

tBSPHeader header = {0};
tBSPLump lumps[kMaxLumps] = {0};

// Read in the header and lump data

fread(&header, 1, sizeof(tBSPHeader), fp);
fread(&lumps, kMaxLumps, sizeof(tBSPLump), fp);

// Allocate the vertex memory

m_numOfVerts = lumps[kVertices].length / sizeof(tBSPVertex);
m_pVerts = new tBSPVertex [m_numOfVerts];

// Allocate the face memory

m_numOfFaces = lumps[kFaces].length / sizeof(tBSPFace);
m_pFaces = new tBSPFace [m_numOfFaces];

m_numOfTextures = lumps[kTextures].length / sizeof(tBSPTexture);
tBSPTexture *pTextures = new tBSPTexture [m_numOfTextures];
p_Tex = new LPDIRECT3DTEXTURE9[m_numOfTextures];

for(i=0; i<m_numOfTextures; i++)
p_Tex[i] = NULL;

// Seek to the position in the file that stores the vertex information

fseek(fp, lumps[kVertices].offset, SEEK_SET);

// Since Quake has the Z-axis pointing up, we want to convert the data so

// that Y-axis is pointing up (like normal!) :)


// Go through all of the vertices that need to be read

for(i = 0; i < m_numOfVerts; i++){
// Read in the current vertex

fread(&m_pVerts[i], 1, sizeof(tBSPVertex), fp);

// Swap the y and z values, and negate the new z so Y is up.

float temp = m_pVerts[i].vPosition.y;
m_pVerts[i].vPosition.y = m_pVerts[i].vPosition.z;
m_pVerts[i].vPosition.z = -temp;

// Negate the V texture coordinate because it is upside down otherwise...

m_pVerts[i].vTextureCoord.y *= -1;
}

// Seek to the position in the file that stores the face information

fseek(fp, lumps[kFaces].offset, SEEK_SET);

// Read in all the face information

fread(m_pFaces, m_numOfFaces, sizeof(tBSPFace), fp);

// Seek to the position in the file that stores the texture information

fseek(fp, lumps[kTextures].offset, SEEK_SET);

// Read in all the texture information

fread(pTextures, m_numOfTextures, sizeof(tBSPTexture), fp);

for(i = 0; i < m_numOfTextures; i++){
FindTextureExtension(pTextures[i].strName);

D3DXCreateTextureFromFile(pDevice, pTextures[i].strName, &p_Tex[i]);
}

SAFE_DELETE_ARRAY(pTextures);
fclose(fp);

pDevice->CreateVertexBuffer(m_numOfVerts*sizeof(tBSPVertex), 0, D3DFVF_BSPFACE, D3DPOOL_MANAGED,
&p_VB, NULL);

VOID* pVerticeLock=0;
p_VB->Lock(0, m_numOfVerts*sizeof(tBSPVertex), &pVerticeLock, 0);
memcpy(pVerticeLock, m_pVerts, m_numOfVerts*sizeof(tBSPVertex));
p_VB->Unlock();

m_FacesDrawn.Resize(m_numOfFaces);

return true;
}

void CQuake3BSP::RenderFace(int faceIndex, LPDIRECT3DDEVICE9 &pDevice){
tBSPFace *pFace = &m_pFaces[faceIndex];

pDevice->SetFVF(D3DFVF_XYZ | D3DFVF_TEX2);
pDevice->SetTexture(pFace->textureID, p_Tex[pFace->textureID]);

pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, pFace->numOfVerts-2, &(m_pVerts[pFace->startVertIndex].vPosition), sizeof(tBSPVertex));

//pDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, m_numOfFaces*3);

}

void CQuake3BSP::RenderLevel(const D3DXVECTOR3 &vPos, LPDIRECT3DDEVICE9 &pDevice){
int i = 0;
m_FacesDrawn.ClearAll();

while(i!=m_numOfFaces){
if(m_pFaces[i].type != FACE_POLYGON) continue;

if(!m_FacesDrawn.On(i)) {
m_FacesDrawn.Set(i);
RenderFace(i, pDevice);
}
i++;
}
}


And I know you shouldn't use UP and less than 500 tris per call if possible, but I'm just trying to get working as is. For the VB I loaded the vertex data, and then tried using the face data to tell dx which vertices to draw but that didn't work either. I can't get anything to appear on screen.

[edited by - Sfpiano on January 21, 2004 4:40:53 PM]

Share this post


Link to post
Share on other sites