Sign in to follow this  
Dom_152

Help me track down my rendering problem

Recommended Posts

Dom_152    476
Hi all! I've finally got to the stage where my "engine" should be able to render triangles to the screen. There are no more errors, memory leaks or anything. It all runs fine. Apart form one major problem. Nothing is displayed on the screen. Now this could be because of one or many things. But it's hard to work out exactly what is preventing it from displaying. SO I'm asking you all for your help. I'm going to list down what happens and the relevant code right up to the point where I call the D3D Render Primitives functions. Can you see if anything is obviously (Or not quite so) wrong? Well first I setup vertex, index and material data for the test cube I'm trying to draw:
//Indices
			indices[0] = 0; indices[3] = 0; indices[6] = 1; indices[9] = 1; indices[12] = 4; indices[15] = 4; indices[18] = 7; indices[21] = 7; indices[24] = 9; indices[27] = 9;
			indices[1] = 2; indices[4] = 3; indices[7] = 5; indices[10] = 2; indices[13] = 6; indices[16] = 5; indices[19] = 3; indices[22] = 6; indices[25] = 5; indices[28] = 8;
			indices[2] = 1; indices[5] = 2; indices[8] = 4; indices[11] = 5; indices[14] = 7; indices[17] = 6; indices[20] = 0; indices[23] = 3; indices[26] = 2; indices[29] = 5;

			indices[30] = 0; indices[33] = 0; 
			indices[31] = 11; indices[34] = 10; 
			indices[32] = 10; indices[35] = 7;

			renderData->SetIndexData(0, indices, 36);

			unsigned long colour = 0xFFFFFF;

			//Vertex data
			vertices[0].X = 0.0f; vertices[0].Y = 0.0f; vertices[0].Z = 0.0f; vertices[0].NX = -1.0f; vertices[0].NY = -1.0f; vertices[0].NZ = -1.0f; vertices[0].diffuse = colour;
			vertices[1].X = 1.0f; vertices[1].Y = 0.0f; vertices[1].Z = 0.0f; vertices[1].NX = 1.0f; vertices[1].NY = 1.0f; vertices[1].NZ = 1.0f; vertices[1].diffuse = colour;
			vertices[2].X = 1.0f; vertices[2].Y = 1.0f; vertices[2].Z = 0.0f; vertices[2].NX = 1.0f; vertices[2].NY = 1.0f; vertices[2].NZ = -1.0f; vertices[2].diffuse = colour;
			vertices[3].X = 0.0f; vertices[3].Y = 1.0f; vertices[3].Z = 0.0f; vertices[3].NX = -1.0f; vertices[3].NY = 1.0f; vertices[3].NZ = -1.0f; vertices[3].diffuse = colour;
			vertices[4].X = 1.0f; vertices[4].Y = 0.0f; vertices[4].Z = 1.0f; vertices[4].NX = 1.0f; vertices[4].NY = -1.0f; vertices[4].NZ = 1.0f; vertices[4].diffuse = colour;
			vertices[5].X = 1.0f; vertices[5].Y = 1.0f; vertices[5].Z = 1.0f; vertices[5].NX = 1.0f; vertices[5].NY = 1.0f; vertices[5].NZ = 1.0f; vertices[5].diffuse = colour;
			vertices[6].X = 0.0f; vertices[6].Y = 1.0f; vertices[6].Z = 1.0f; vertices[6].NX = -1.0f; vertices[6].NY = 1.0f; vertices[6].NZ = 1.0f; vertices[6].diffuse = colour;
			vertices[7].X = 0.0f; vertices[7].Y = 0.0f; vertices[7].Z = 1.0f; vertices[7].NX = -1.0f; vertices[7].NY = -1.0f; vertices[7].NZ = 1.0f; vertices[7].diffuse = colour;
			vertices[8].X = 0.0f; vertices[8].Y = 1.0f; vertices[8].Z = 1.0f; vertices[8].NX = -1.0f; vertices[8].NY = 1.0f; vertices[8].NZ = 1.0f; vertices[8].diffuse = colour;
			vertices[9].X = 0.0f; vertices[9].Y = 1.0f; vertices[9].Z = 0.0f; vertices[9].NX = -1.0f; vertices[9].NY = 1.0f; vertices[9].NZ = -1.0f; vertices[9].diffuse = colour;
			vertices[10].X = 1.0f; vertices[10].Y = 0.0f; vertices[10].Z = 1.0f; vertices[10].NX = 1.0f; vertices[10].NY = -1.0f; vertices[10].NZ = 1.0f; vertices[10].diffuse = colour;
			vertices[11].X = 1.0f; vertices[11].Y = 0.0f; vertices[11].Z = 0.0f; vertices[11].NX = 1.0f; vertices[11].NY = -1.0f; vertices[11].NZ = -1.0f; vertices[11].diffuse = colour;
			
renderData->SetVertexData((void *)vertices, 12);

mat.materialType = EMT_DIFFUSE;		
mat.transparent = false;		

renderData->SetMaterial(0, mat);
renderData->SetVertexType(EVT_COLOURED);						

SetRenderData(renderData);

The "node" in which this data resides is then added to the "SceneManager". That's all the setup that's needed. You then call the SceneManagers DrawAll method which will draw all the nodes including the cube. It sets up view and projection matrices for the camera like so: View Matrix:
//CreateLookAtMatrix
	//Creates a look at matrix for use with a camera
	void Matrix::CreateLookAtMatrix(Vector<float> position, Vector<float> lookAt, Vector<float> upAxis)
	{
		Vector<float> zAxis, xAxis, yAxis;

		zAxis = lookAt - position;
		zAxis.Normalise();

		xAxis = upAxis.Cross(zAxis);
		xAxis.Normalise();

		yAxis = zAxis.Cross(xAxis);

		_11 = xAxis.GetX(); _12 = yAxis.GetX(); _13 = zAxis.GetX(); _14 = 0.0f; 
		_21 = xAxis.GetY(); _22 = yAxis.GetY(); _23 = zAxis.GetY(); _24 = 0.0f; 
		_31 = xAxis.GetZ(); _32 = yAxis.GetZ(); _33 = zAxis.GetZ(); _34 = 0.0f; 
		_41 = -xAxis.Dot(position); _42 = -yAxis.Dot(position); _43 = -zAxis.Dot(position); _44 = 1.0f; 
	}

Projection Matrix:
	//CreatePerspectiveProjectionMatrix
	//Creates a perspective projection matrix for use with rendering
	void Matrix::CreatePerspectiveProjectionMatrix(float FOV, float aspect, float near, float far)
	{		
		float h = (float)1 / tan(FOV / 2.0);
		float w = h / aspect;

		_11 = w; _12 = 0.0f; _13 = 0.0f; _14 = 0.0f; 
		_21 = 0.0f; _22 = h; _23 = 0.0f; _24 = 0.0f; 
		_31 = 0.0f; _32 = 0.0f; _33 = far / (far - near); _34 = 1.0f; 
		_41 = 0.0f; _42 = 0.0f; _43 = -near * far / (far - near); _44 = 0.0f; 		
	}

Here is how the above functions are called:
Matrix viewMat, projMat;

		viewMat.CreateLookAtMatrix(m_activeCamera->GetPosition(), m_activeCamera->GetLookAt(), Vector<float>(0.0f, 1.0f, 0.0f));
		projMat.CreatePerspectiveProjectionMatrix(m_activeCamera->GetFOV(), m_activeCamera->GetAspectRatio(), m_activeCamera->GetNear(), m_activeCamera->GetFar());

		Root::GetSingletonPtr()->GetRenderer()->GetRenderDevice()->SetTransform(ETT_VIEW, viewMat);
		Root::GetSingletonPtr()->GetRenderer()->GetRenderDevice()->SetTransform(ETT_PROJECTION, projMat);

Then each scenenode has it's draw method called which sets the World matrix:
void MeshNode::Draw()
	{
		Matrix worldMatrix, transMatrix, rotMatrix;

		transMatrix.Translate(m_position.GetX(), m_position.GetY(), m_position.GetZ());
		rotMatrix.Rotate(m_rotation.GetX(), m_rotation.GetY(), m_rotation.GetZ());

		//Set the world matrix to that of our mesh
		worldMatrix = transMatrix.Multiply(rotMatrix);

		Root::GetSingletonPtr()->GetRenderer()->GetRenderDevice()->SetTransform(ETT_WORLD, worldMatrix);
		Root::GetSingletonPtr()->GetRenderer()->GetRenderDevice()->Render(m_renderData);
	}

In the render device Render method I first create a vertex declaration based on the vertex type of the node:
//Create a vertex declaration based on the vertex type		
			IDirect3DVertexDeclaration9 *vertexDecl = 0;

			switch(rData->GetVertexType())
			{
case(EVT_COLOURED):
				{
					//Stream, Offset, Type, Method, Usage, Usage Index
					D3DVERTEXELEMENT9 vertexElement[] = {
					{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},					
					{0, sizeof(float) * 3, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
					{0, sizeof(float) * 3 + sizeof(DWORD), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, D3DDECL_END()};			

					m_d3d9Device->CreateVertexDeclaration(vertexElement, &vertexDecl);
					break;
				}
}

if(vertexDecl)
				m_d3d9Device->SetVertexDeclaration(vertexDecl);
			else return;

Finally I order the index data into a pointer and call D3D Draw method:
unsigned long *indexData = new unsigned long[rData->GetIndexData(i).size()];
				for(int y = 0; y < rData->GetIndexData(i).size(); y++)
				{
					indexData[y] = rData->GetIndexData(i).at(y);
				}

				switch(rData->GetVertexType())
				{
case(EVT_COLOURED):
					{
						m_d3d9Device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, rData->GetVertexCount(), rData->GetIndexCount() / 3, 
							(void *)indexData, D3DFMT_INDEX16, rData->GetVertexData(), sizeof(Vertex_Diffuse));
						break;
					}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this