Sign in to follow this  
Kram

Mesh loading in backwards

Recommended Posts

I have a simple mesh that I created in Blender, its just a maze that I will be trying to make into a small game... But when I follow examples of how to load a mesh, they work fine, except for the mesh facing backwards! If I set the Y axis rotation on the model when I render it, I can see that the mesh is indeed simply backwards, but I have no Idea why! Here is the code I use to load the mesh:
bool Maze::LoadXFile()
{
	HRESULT hr = 0;

	//
	// Load the XFile data.
	//

	//Adjacency Buffer
	ID3DXBuffer* adjBuffer  = 0;
	//Materials Buffer
	ID3DXBuffer* mtrlBuffer = 0;
	//Number of Materials
	DWORD        numMtrls   = 0;

	hr = D3DXLoadMeshFromX(  
		TEXT("maze1.x"),
		D3DXMESH_MANAGED,
		this->mp_d3dDevice,
		&adjBuffer,
		&mtrlBuffer,
		0,
		&numMtrls,
		&this->m_mesh);

	if(FAILED(hr))
	{
		//MessageBox(0, TEXT("D3DXLoadMeshFromX() - FAILED"), 0, 0);
		MessageBox(NULL, DXGetErrorDescription9( hr ), DXGetErrorString9( hr ), MB_ICONERROR);
		return false;
	}

	//
	// Extract the materials, and load textures.
	//

	if( mtrlBuffer != 0 && numMtrls != 0 )
	{
		D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();

		for(int i = 0; i < numMtrls; i++)
		{
			// the MatD3D property doesn't have an ambient value set
			// when its loaded, so set it now:
			mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;

			// save the ith material
			m_mtrls.push_back( mtrls[i].MatD3D );

			// check if the ith material has an associative texture
			if( mtrls[i].pTextureFilename != 0 )
			{
				// load the texture for the ith subset
				IDirect3DTexture9* tex = 0;
				D3DXCreateTextureFromFile(
					this->mp_d3dDevice,
					//mtrls[i].pTextureFilename,
					TEXT(""),
					&tex);

				// save the loaded texture
				m_textures.push_back( tex );
			}
			else
			{
				// no texture for the ith subset
				m_textures.push_back( 0 );
			}
		}
	}

	mtrlBuffer->Release(); // done w/ buffer

	//
	// Optimize the mesh.
	//
	hr = this->m_mesh->OptimizeInplace(		
		D3DXMESHOPT_ATTRSORT |
		D3DXMESHOPT_COMPACT  |
		D3DXMESHOPT_VERTEXCACHE,
		(DWORD*)adjBuffer->GetBufferPointer(),
		0, 0, 0);

	adjBuffer->Release(); // done w/ buffer

	if(FAILED(hr))
	{
		//MessageBox(0, "OptimizeInplace() - FAILED", 0, 0);
		MessageBox(NULL, DXGetErrorDescription9( hr ), DXGetErrorString9( hr ), MB_ICONERROR);
		return false;
	}

	//
	// Set texture filters.
	//

	mp_d3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	mp_d3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	mp_d3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);

	// 
	// Set Lights.
	//

	D3DXVECTOR3 dir(1.0f, -1.0f, 1.0f);
	D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);
	D3DLIGHT9 light = InitDirectionalLight(&dir, &col);

	mp_d3dDevice->SetLight(0, &light);
	mp_d3dDevice->LightEnable(0, true);
	mp_d3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
	mp_d3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);

	return true;
}

And here is the Render method of the Maze object:
void Maze::Render(float timeDelta)
{
	if (this->mp_d3dDevice)
	{
		//
		// Update: Rotate the mesh.
		//
		static float y = 0.0f;
		D3DXMATRIX yRot;
		D3DXMatrixRotationY(&yRot, y);
		y += timeDelta;

		if( y >= 6.28f )
			y = 0.0f;

		D3DXMATRIX World = yRot;

		mp_d3dDevice->SetTransform(D3DTS_WORLD, &World);


		D3DXVECTOR3 cameraPos;
		this->m_camera->getPosition(&cameraPos);

		D3DXVECTOR3 pos( cameraPos.x, 
						 cameraPos.y, 
						 cameraPos.z );

		D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
		D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);

		D3DXMATRIX V;
		D3DXMatrixLookAtLH(
			&V,
			&pos,
			&target,
			&up);

		mp_d3dDevice->SetTransform(D3DTS_VIEW, &V);

		//
		// Set projection matrix.
		//

		D3DXMATRIX proj;
		D3DXMatrixPerspectiveFovLH(
				&proj,
				D3DX_PI * 0.5f, // 90 - degree
				(float)648 / (float)480,
				1.0f,
				1000.0f);
		mp_d3dDevice->SetTransform(D3DTS_PROJECTION, &proj);

		for(int i = 0; i < this->m_mtrls.size(); i++)
		{
			this->mp_d3dDevice->SetMaterial( &this->m_mtrls[i] );
			this->mp_d3dDevice->SetTexture(0, this->m_textures[i] );
			this->m_mesh->DrawSubset(i);
		}
	}
}

Any help is greatly appreciated! Thanks Mark

Share this post


Link to post
Share on other sites
If I'm not much mistaken, Blender uses a slightly different coordinate system to that of OpenGL. Specifically, it uses the positive z-axis as the up-axis (whereas OpenGL starts off with the positive y-axis as the up-axis), and, when rotated such that we are looking down the negative z-axis with the positive y-axis pointing up, I believe that it has the positive x-axis pointing to the left, instead of the right.

As to a solution, I'm honestly not sure, offhand, I'm afraid, although I'd be interested to find out. I think that most of my models thus far have been more or less symmetrical, and I've been correcting the up-axis issue by passing the final object through Wings.

[edit] Eep, sorry, I missed the fact that this was in the DirectX forum. ^^;;

[Edited by - Thaumaturge on May 7, 2008 6:39:16 AM]

Share this post


Link to post
Share on other sites
1. Some applications generate meshes with a different coordinate system than DirectX uses. What app do you use to generate the mesh?

2. Is the mesh backwards or are you looking at it from the wrong side? If the camera starts out "behind" the mesh, it will look backwards.

Share this post


Link to post
Share on other sites
Ok so thanks for the help so far, from both of your posts, it looks like it definately could be Blender exporting incorrectly.

So, I opened up my model in Blender and from the looks of things, the axis are all ok... but maybe im misunderstand things a bit here!

If you go here:
http://firch.serveftp.net/blender-model.jpg

You can see the coloured axis and what they represent.

I hope this helps in some way.

Thanks again

Share this post


Link to post
Share on other sites
Quote:
Original post by Buckeye
2. Is the mesh backwards or are you looking at it from the wrong side? If the camera starts out "behind" the mesh, it will look backwards.


Thats probably it!

The constructor of my camera class sets up the positioning, like so:


Camera::Camera()
{
_pos = D3DXVECTOR3(4.0f, 4.0f, 15.0f);
_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
_up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
_look = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
}



so it starts in the positive Z axis, if I change it to be -15.0f, its then looking correct.

But isn't the positive Z axis, "behind" the user when looking with the D3DXMatrixPerspectiveFovLH??

Share this post


Link to post
Share on other sites
Quote:
Original post by Buckeye
The left- or right-handedness doesn't matter!

If I stand at z = -10 and look at the origin, I'm looking at +z, whatever the axes orientation. :-)



Thats true, thanks Buckeye. Problem solved!

Share this post


Link to post
Share on other sites
Quote:
Originally posted by Buckeye
The left- or right-handedness doesn't matter!

If I stand at z = -10 and look at the origin, I'm looking at +z, whatever the axes orientation. :-)


True, you'll be looking at the positive z-axis, but to which side will the positive x-axis be? If it's to your left, then vertices with positive x-coordinates should appear on your left, if it's on your right, then they should appear on your right. In other words, changing the handedness can result in mirroring, it seems to me.

Share this post


Link to post
Share on other sites
Quote:
Original post by Thaumaturge
True, you'll be looking at the positive z-axis, but to which side will the positive x-axis be? If it's to your left, then vertices with positive x-coordinates should appear on your left, if it's on your right, then they should appear on your right. In other words, changing the handedness can result in mirroring, it seems to me.


True, but in this case I believe that I simply had the camera setup in the wrong spot to begin with.

In my situation, this wont be a problem, because camera movement will not be (at least to begin with) a consideration. All orientation will be fixed.

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