Sign in to follow this  
GSide

Why loaded mesh color and texture disappear?

Recommended Posts

Hi! Again, a question of a DX D3D novice programmer. I created a 3d Object and I saved it in 3ds format, and I converted it in .x format so that I can load it into my D3D program. I looked at it in mesh viewer and its fine the material colours and textures that I put on it are preserves. But why when I loaded it into my d3d program the colours and textures are disappeared? What I’d like to do is to load the .x file as is, I mean what you see in mesh viewer, is what you see in a d3d program. Is that possible? Thanks in advance!

Share this post


Link to post
Share on other sites
The .x mesh is just a simple box and it has a vertex normal, lighting is on, and it has a directional light. But the meshes are all completely in black colour.

This is the entire code:


#define STRICT
#define WIN32_LEAN_AND_MEAN

#include <d3d9.h>
#include <d3dx9.h>
#include "resource.h"

//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
HWND g_hWnd = NULL;
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPD3DXMESH g_pBoxMesh = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
D3DLIGHT9 g_pLight0;

D3DMATERIAL9 g_redClayMtrl;
D3DMATERIAL9 g_greenPlasticMtrl;
D3DMATERIAL9 g_silverMetalMtrl;

float g_fSpinX = 0.0f;
float g_fSpinY = 0.0f;

struct Vertex
{
float x, y, z; // Position of vertex in 3D space
float nx, ny, nz; // Normal for lighting calculations
DWORD diffuse; // Diffuse color of vertex

enum FVF
{
FVF_Flags = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE
};
};

//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void init(void);
void shutDown(void);
void render(void);

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
WNDCLASSEX winClass;
MSG uMsg;

memset(&uMsg,0,sizeof(uMsg));

winClass.lpszClassName = "MY_WINDOWS_CLASS";
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = WindowProc;
winClass.hInstance = hInstance;
winClass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
winClass.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;

if( !RegisterClassEx(&winClass) )
return E_FAIL;

g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS",
"DX9 D3D ",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 640, 480, NULL, NULL, hInstance, NULL );

if( g_hWnd == NULL )
return E_FAIL;

ShowWindow( g_hWnd, nCmdShow );
UpdateWindow( g_hWnd );

init();

while( uMsg.message != WM_QUIT )
{
if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &uMsg );
DispatchMessage( &uMsg );
}
else
render();
}

shutDown();

UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance );

return uMsg.wParam;
}

//-----------------------------------------------------------------------------
// Name: WindowProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
static POINT ptLastMousePosit;
static POINT ptCurrentMousePosit;
static bool bMousing;

switch( msg )
{
case WM_KEYDOWN:
{
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
}
break;

case WM_LBUTTONDOWN:
{
ptLastMousePosit.x = ptCurrentMousePosit.x = LOWORD (lParam);
ptLastMousePosit.y = ptCurrentMousePosit.y = HIWORD (lParam);
bMousing = true;
}
break;

case WM_LBUTTONUP:
{
bMousing = false;
}
break;

case WM_MOUSEMOVE:
{
ptCurrentMousePosit.x = LOWORD (lParam);
ptCurrentMousePosit.y = HIWORD (lParam);

if( bMousing )
{
g_fSpinX -= (ptCurrentMousePosit.x - ptLastMousePosit.x);
g_fSpinY -= (ptCurrentMousePosit.y - ptLastMousePosit.y);
}

ptLastMousePosit.x = ptCurrentMousePosit.x;
ptLastMousePosit.y = ptCurrentMousePosit.y;
}
break;

case WM_CLOSE:
{
PostQuitMessage(0);
}

case WM_DESTROY:
{
PostQuitMessage(0);
}
break;

default:
{
return DefWindowProc( hWnd, msg, wParam, lParam );
}
break;
}

return 0;
}

//-----------------------------------------------------------------------------
// Name: init()
// Desc:
//-----------------------------------------------------------------------------
void init( void )
{
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

D3DDISPLAYMODE d3ddm;

g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );

d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice );



D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ),
640.0f / 480.0f, 0.1f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );




//-------------------------------------------------------------------------
// Setup material - Used to create a reddish clay Box
//-------------------------------------------------------------------------

ZeroMemory( &g_redClayMtrl, sizeof(D3DMATERIAL9) );

//Diffuse
g_redClayMtrl.Diffuse.r = 1.0f;
g_redClayMtrl.Diffuse.g = 0.5f;
g_redClayMtrl.Diffuse.b = 0.2f;
g_redClayMtrl.Diffuse.a = 1.0f;

//-------------------------------------------------------------------------
// Setup material - Used to create a green plastic Box
//-------------------------------------------------------------------------

ZeroMemory( &g_greenPlasticMtrl, sizeof(D3DMATERIAL9) );

// Set the material's main color to green.
g_greenPlasticMtrl.Diffuse.r = 0.0f;
g_greenPlasticMtrl.Diffuse.g = 1.0f;
g_greenPlasticMtrl.Diffuse.b = 0.0f;
g_greenPlasticMtrl.Diffuse.a = 1.0f;

g_greenPlasticMtrl.Ambient.r = 0.5f;
g_greenPlasticMtrl.Ambient.g = 1.0f;
g_greenPlasticMtrl.Ambient.b = 0.5f;
g_greenPlasticMtrl.Ambient.a = 1.0f;

g_greenPlasticMtrl.Specular.r = 0.5f;
g_greenPlasticMtrl.Specular.g = 1.0f;
g_greenPlasticMtrl.Specular.b = 0.5f;
g_greenPlasticMtrl.Specular.a = 1.0f;

g_greenPlasticMtrl.Power = 40.0f;

//-------------------------------------------------------------------------
// Setup material - Used to create a silver metallic Box
//-------------------------------------------------------------------------

ZeroMemory( &g_silverMetalMtrl, sizeof(D3DMATERIAL9) );

//silver gray color.
g_silverMetalMtrl.Diffuse.r = 0.5f;
g_silverMetalMtrl.Diffuse.g = 0.5f;
g_silverMetalMtrl.Diffuse.b = 0.5f;
g_silverMetalMtrl.Diffuse.a = 1.0f;

g_silverMetalMtrl.Ambient.r = 1.0f;
g_silverMetalMtrl.Ambient.g = 1.0f;
g_silverMetalMtrl.Ambient.b = 1.0f;
g_silverMetalMtrl.Ambient.a = 1.0f;

g_silverMetalMtrl.Emissive.r = 0.1f;
g_silverMetalMtrl.Emissive.g = 0.1f;
g_silverMetalMtrl.Emissive.b = 0.1f;
g_silverMetalMtrl.Emissive.a = 1.0f;

// specular Pure white.
g_silverMetalMtrl.Specular.r = 1.0f;
g_silverMetalMtrl.Specular.g = 1.0f;
g_silverMetalMtrl.Specular.b = 1.0f;
g_silverMetalMtrl.Specular.a = 1.0f;

g_silverMetalMtrl.Power = 5.0f;


g_pd3dDevice->SetRenderState( D3DRS_COLORVERTEX, FALSE );


g_pLight0.Type = D3DLIGHT_DIRECTIONAL;
g_pLight0.Direction = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );

g_pLight0.Diffuse.r = 1.0f;
g_pLight0.Diffuse.g = 1.0f;
g_pLight0.Diffuse.b = 1.0f;
g_pLight0.Diffuse.a = 1.0f;

g_pLight0.Specular.r = 1.0f;
g_pLight0.Specular.g = 1.0f;
g_pLight0.Specular.b = 1.0f;
g_pLight0.Specular.a = 1.0f;

g_pd3dDevice->SetLight( 0, &g_pLight0 );
g_pd3dDevice->LightEnable( 0, TRUE );


g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_COLORVALUE( 0.2f, 0.2f, 0.2f, 1.0f ) );


//Load the Texture
D3DXCreateTextureFromFile( g_pd3dDevice, "Box.bmp", & g_pTexture );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );

//Load the mesh
LPD3DXMESH pTempTeapotMesh = NULL;
D3DXLoadMeshFromX( "Box.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice,
NULL, NULL, NULL, NULL, &pTempTeapotMesh );

LPDIRECT3DVERTEXBUFFER9 pTempVertexBuffer;

pTempTeapotMesh->CloneMeshFVF( 0, Vertex::FVF_Flags, g_pd3dDevice, &g_pBoxMesh );

if( SUCCEEDED( g_pBoxMesh->GetVertexBuffer( &pTempVertexBuffer ) ) )
{
int nNumVerts = g_pBoxMesh->GetNumVertices();
Vertex *pVertices = NULL;

pTempVertexBuffer->Lock( 0, 0, (void**)&pVertices, 0 );
{
// Make each vertex blue
for( int i = 0; i < nNumVerts; ++i )
pVertices[i].diffuse = D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 );
}
pTempVertexBuffer->Unlock();

pTempVertexBuffer->Release();
}

pTempTeapotMesh->Release();
}

//-----------------------------------------------------------------------------
// Name: shutDown()
// Desc:
//-----------------------------------------------------------------------------
void shutDown( void )
{

if( g_pTexture != NULL )
g_pTexture->Release();

if( g_pBoxMesh != NULL )
g_pBoxMesh->Release();

if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();

if( g_pD3D != NULL )
g_pD3D->Release();
}

//-----------------------------------------------------------------------------
// Name: render()
// Desc:
//-----------------------------------------------------------------------------
void render( void )
{
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_COLORVALUE(0.0f,0.0f,1.0f,1.0f), 1.0f, 0 );

g_pd3dDevice->BeginScene();

D3DXMATRIX matView;
D3DXMATRIX matWorld;
D3DXMATRIX matTrans;
D3DXMATRIX matRot;

D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, 6.5f );
matView = matTrans;
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

//Rotation
D3DXMatrixRotationYawPitchRoll( &matRot,
D3DXToRadian(g_fSpinX),
D3DXToRadian(g_fSpinY),
0.0f );


//Set Texture
g_pd3dDevice->SetTexture(0, g_pTexture );


//
// Render the first Box using a red clay material
//

D3DXMatrixTranslation( &matTrans, 0.0f, 1.2f, 0.0f );
matWorld = matRot * matTrans;
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

g_pd3dDevice->SetMaterial( &g_redClayMtrl );
g_pBoxMesh->DrawSubset(0);

//
// Render the second Box using a green plastic material
//

D3DXMatrixTranslation( &matTrans, 1.7f, -0.8f, 0.0f );
matWorld = matRot * matTrans;
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

g_pd3dDevice->SetMaterial( &g_greenPlasticMtrl );
g_pBoxMesh->DrawSubset(0);

//
// Render the third Box using a silver metallic material
//

D3DXMatrixTranslation( &matTrans, -1.7f, -0.8f, 0.0f );
matWorld = matRot * matTrans;
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

g_pd3dDevice->SetMaterial( &g_silverMetalMtrl );
g_pBoxMesh->DrawSubset(0);

g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


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