Tesselate Q3 Maps

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
siberianstar
Posts: 11
Joined: Wed Jun 28, 2006 5:13 pm

Tesselate Q3 Maps

Post by siberianstar »

Hello, i need to tesselate triangles in the Q3 Map loader, i made this code but it's not working good. Someone can help me ? pls

CQ3LevelMesh.cpp:

Code: Select all

// EDIT: currentVertex -> currentVertex[3]
video::S3DVertex2TCoords currentVertex[3];
for (s32 vu=0; vu<3; ++vu)
{
	
	tBSPVertex *v = &Vertices[vidxes[vu]];
		//currentVertex.Color = video::SColor(v->color[3], v->color[0], v->color[1], v->color[2]);
	currentVertex[vu].Color.set(255,255,255,255);
	currentVertex[vu].Pos.X = v->vPosition[0];
	currentVertex[vu].Pos.Y = v->vPosition[2];
	currentVertex[vu].Pos.Z =  v->vPosition[1];
	currentVertex[vu].Normal.X = v->vNormal[0];
	currentVertex[vu].Normal.Y = v->vNormal[2];
	currentVertex[vu].Normal.Z = v->vNormal[1];
	currentVertex[vu].TCoords.X = v->vTextureCoord[0];
	currentVertex[vu].TCoords.Y = v->vTextureCoord[1];
	currentVertex[vu].TCoords2.X = v->vLightmapCoord[0];
	currentVertex[vu].TCoords2.Y = v->vLightmapCoord[1];
			
	//meshBuffer->Vertices.push_back(currentVertex);
}


// check the size of a triangle, if it is too big i tesselate it.
float b = (currentVertex[2].Pos - currentVertex[0].Pos).getLength();
float h = (currentVertex[1].Pos - currentVertex[2].Pos).getLength();

/*  too big */
if (b > 3 || h > 3) {
	
	core::vector3df vt[6], n[6]; /* vertices and normals */
	core::vector2df st[6], lt[6]; /* texture and lightmap coords */
	const int vindex[] = {0,3,5,3,1,4,5,4,2,3,4,5};
	
		vt[0] = currentVertex[0].Pos; // position
  		vt[1] = currentVertex[1].Pos;
		vt[2] = currentVertex[2].Pos;
		t[3] = (vt[0] + vt[1]) * 0.5f;
		vt[4] = (vt[1] + vt[2]) * 0.5f;
		vt[5] = (vt[0] + vt[2]) * 0.5f;

		st[0] = currentVertex[0].TCoords; // texture
		st[1] = currentVertex[1].TCoords;
		st[2] = currentVertex[2].TCoords;
		st[3] = (st[0] + st[1]) * 0.5f;
		st[4] = (st[1] + st[2]) * 0.5f;
		st[5] = (st[0] + st[2]) * 0.5f;

		n[0] = currentVertex[0].Normal; // normal
		n[1] = currentVertex[1].Normal;
		n[2] = currentVertex[2].Normal;
		n[3] = (n[0] + n[1]) * 0.5f;
		n[4] = (n[1] + n[2]) * 0.5f;
		n[5] = (n[0] + n[2]) * 0.5f;
		
		lt[0] = currentVertex[0].TCoords2; // lightmap
		lt[1] = currentVertex[1].TCoords2;
		lt[2] = currentVertex[2].TCoords2;
		lt[3] = (lt[0] + lt[1]) * 0.5f;
		lt[4] = (lt[1] + lt[2]) * 0.5f;
		lt[5] = (lt[0] + lt[2]) * 0.5f;

		
		video::S3DVertex2TCoords vtxs;
  			
		for (int li = 0; li < 11; li ++ ) {
		
		vtxs.Pos = vt[vindex[li]];
		vtxs.TCoords = st[vindex[li]];
		vtxs.TCoords2 = lt[vindex[li]];
		vtxs.Normal = n[vindex[li]];
		
		meshBuffer->Vertices.push_back(vtxs);

	
		}
		
	      for (int lz = 0; lz < 12; lz++) 
	       meshBuffer->Indices.push_back(idx+lz);
	
	
} else {
       for (s32 x = 0; x < 3; x++) meshBuffer->Vertices.push_back(currentVertex[x]);
       meshBuffer->Indices.push_back(idx);
       meshBuffer->Indices.push_back(idx+1);
       meshBuffer->Indices.push_back(idx+2);
       
}
Post Reply