Sign in to follow this  

Meshes load strangely

This topic is 3738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok here's the problem: I'm using the basic old "airplane 2.x" model that comes with directx. All i'm doing is loading it and placing it on the screen, but everytime i do it the textures come out all streched out and looking odd. If i view the model with DirectX Viewer it looks fine. Here's some of the code, which may be difficult to understand because it uses classes that I've made. The mesh has already been loaded before this function.
mesh.Create(L"..\\debug\\airplane 2.x", m_3d.Get3d());

HRESULT SD_MESH::Create(LPCTSTR filename, LPDIRECT3DDEVICE9 d3d, DWORD Options, LPD3DXBUFFER *Adjacency, LPD3DXBUFFER *Effects)
{
	d3ddev = d3d;
	if (FAILED(D3DXLoadMeshFromX(filename, Options, d3d, Adjacency, &mats, Effects, &num_mats, &mesh)))
	{
		ERR("SD_MESH::Create: Failure to load a model.");
		return E_FAIL;
	}

	D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)mats->GetBufferPointer();

	material = new D3DMATERIAL9[num_mats];
	texture = new LPDIRECT3DTEXTURE9[num_mats];

	for(DWORD i = 0; i < num_mats; i++)    // for each material...
	{
		material[i] = tempMaterials[i].MatD3D;    // get the material info...
		material[i].Ambient = material[i].Diffuse;    // and make ambient the same as diffuse
		USES_CONVERSION;    // allows certain string conversions
		if(FAILED(D3DXCreateTextureFromFile(d3d,
			CA2W(tempMaterials[i].pTextureFilename),
			&texture[i])))
			texture[i] = NULL;

	}

	model_loaded = TRUE;
	return S_OK;
}


void SD_GAME::RenderFrame()
{
	m_3d.BeginFrame();
	m_2d.Begin();
	m_3d.Get3d()->SetTransform(D3DTS_VIEW, &camera.GetView());
	m_3d.Get3d()->SetTransform(D3DTS_PROJECTION, &camera.GetProjection());

	mesh.Render();
	
	switch (state)
	{
	case loading_game:
		{
			Loading(-1);
			break;
		}
	case starting:
		{
			Starting();
			break;
		}
	case main_menu:
		{
			MainMenu();
			break;
		}
	default:
		{
			state = error;
			Error();
		}
	}

	m_2d.End();
	m_3d.EndFrame();
}

void SD_MESH::Render()
	{
		if (!model_loaded)
		{
			ERR("SD_MESH::Render: Cannot render the model because it hasn't been loaded.");
			PostQuitMessage(-1);
			return;
		}
		for(DWORD i = 0; i < num_mats; i++)    // loop through each subset
		{
			d3ddev->SetMaterial(&material[i]);    // set the appropriate material for the subset
			if(texture[i] != NULL)    // if the subset has a texture (if texture is not NULL)
				d3ddev->SetTexture(0, texture[i]);    // ...then set the texture

			mesh->DrawSubset(i);    // draw the subset
		}
	}

Oh, and I've also noticed that if i put the .x file into a different folder along with its image components, and change the loading path to match the files new location, it will only load the model and not even display the strange looking textures! [Edited by - orphankill on September 15, 2007 4:49:48 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by orphankill

Oh, and I've also noticed that if i put the .x file into a different folder along with its image components, and change the loading path to match the files new location, it will only load the model and not even display the strange looking textures!


Are you also changing the directory where you get your textures?

Share this post


Link to post
Share on other sites
how do i do that? don't the textures' paths automatically load from the same directory as the x file when i make this call:

if(FAILED(D3DXCreateTextureFromFile(d3d,
CA2W(tempMaterials[i].pTextureFilename),
&texture[i])))
texture[i] = NULL;

Share this post


Link to post
Share on other sites
Quote:
Original post by orphankill
how do i do that? don't the textures' paths automatically load from the same directory as the x file when i make this call:

if(FAILED(D3DXCreateTextureFromFile(d3d,
CA2W(tempMaterials[i].pTextureFilename),
&texture[i])))
texture[i] = NULL;


Nope. When you send a filename that D3DX texture function, it will look in the directory your executable is currently running in. You'll need to either add on a directory name in your mesh loading function, or add the directory name to the x-file.

Share this post


Link to post
Share on other sites
well that fixed the "nothing showing up but the mesh" problem, but the textures are still really stretched out and odd looking. i wish i knew how to post a picture up so you could see what i mean...any ideas of what to do?

EDIT:
Here's what the airplane is supposed to look like:
http://orphankill.freesuperhost.com/2.bmp

Here's what mine looks like:
http://orphankill.freesuperhost.com/1.bmp


[Edited by - orphankill on September 16, 2007 4:24:08 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by orphankill
well that fixed the "nothing showing up but the mesh" problem, but the textures are still really stretched out and odd looking. i wish i knew how to post a picture up so you could see what i mean...any ideas of what to do?

EDIT:
Here's what the airplane is supposed to look like:
http://orphankill.freesuperhost.com/2.bmp

Here's what mine looks like:
http://orphankill.freesuperhost.com/1.bmp


It looks like there's something wrong with the texture coordinates. There could be a problem in you shader, or when you load the model.

Share this post


Link to post
Share on other sites
i never set a shader and you can see 90% of the code right there, here's the rest:



LRESULT SD_DIRECT3D::Initialize(BOOL lighting_enable, BOOL ambient, BOOL directional)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (!d3d)
{
ERR("SD_DIRECT3D::Initialize: FATAL ERROR: Cannot activate DirectX9. Update to the latest version of DirectX.");
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information

ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.hDeviceWindow = hWnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames

if (!bFullscreen)
{
d3dpp.Windowed = TRUE; // program windowed, not fullscreen
} else
{
d3dpp.Windowed = FALSE; // program windowed, not fullscreen
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer
d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer
}

// create a device class using this information and information from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

d3ddev->CreateDepthStencilSurface(SCREEN_WIDTH,
SCREEN_HEIGHT,
D3DFMT_D16,
D3DMULTISAMPLE_NONE,
0,
TRUE,
&z_buffer,
NULL);

if (!d3d)
{
ERR("SD_DIRECT3D::Initialize: FATAL ERROR: Could not create 3d device.");
PostQuitMessage(-1);
return E_FAIL;
}

d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
if (!lighting_enable)
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
else
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
d3ddev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

if (ambient)
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));

if (directional)
{
D3DLIGHT9 light; // create the light struct
ZeroMemory(&light, sizeof(light)); // clear out the light struct for use
light.Type = D3DLIGHT_DIRECTIONAL; // make the light type 'directional light'
light.Diffuse.r = 0.5f; // .5 red
light.Diffuse.g = 0.5f; // .5 green
light.Diffuse.b = 0.5f; // .5 blue
light.Diffuse.a = 1.0f; // full alpha (we'll get to that soon)
D3DVECTOR vecDirection = {-1.0f, -0.3f, -1.0f}; // the direction of the light
light.Direction = vecDirection; // set the direction
d3ddev->SetLight(0, &light); // send the light struct properties to light #0
d3ddev->LightEnable(0, TRUE); // turn on light #0*/
}

#ifdef DEBUG_MODE
bDisplayFPS = TRUE;
#endif

D3DXCreateFont(d3ddev, // the Direct3D Device
20, 0, // font size twenty with the default width
FW_NORMAL, // normal font weight
1, // no mipmap levels
FALSE, // not italic
DEFAULT_CHARSET, // default character set
OUT_DEFAULT_PRECIS, // default precision
DEFAULT_QUALITY, // default quality
DEFAULT_PITCH || FF_DONTCARE, // more defaults...
L"Arial", // typeface "Arial"
&font); // address of the font object created above

return S_OK;
}


[Edited by - orphankill on September 16, 2007 8:12:17 PM]

Share this post


Link to post
Share on other sites
if anyone actually cares about the solution which I just figured out after hours of changing settings, it was acting strange because I had a sprite calling:
d3dspr->Begin(D3DXSPRITE_ALPHABLEND);
at the beginning of every frame before anything else happened!

Share this post


Link to post
Share on other sites

This topic is 3738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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