Archived

This topic is now archived and is closed to further replies.

bodyboarder20

Texture problems

Recommended Posts

Hey, well, I''m using 3D Studio Max 5, witht he pada exporter, and everythign is working great. I''m using the CD3DMesh structure for loading my mesh. My textures are not showing up. I checked to ensure it was loading a texture, adn it is, but instead of seeing a box with a texture, I jsut see a gray box. To display my mesh im using mesh->Render(m_pd3dDevice); Here is my code....
#define STRICT
#include <tchar.h>
#include <math.h>
#include <stdio.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DFile.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "resource.h"

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    D3DXVECTOR3 p;
    D3DXVECTOR3 n;
    FLOAT       tu, tv;
};

#define SCHOOLMAP _T("test11.x")

#define FVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)

//-----------------------------------------------------------------------------
// Name: struct Object
// Desc: Structure for holding data for each object
//-----------------------------------------------------------------------------
struct Object
{
    D3DXVECTOR3 vLoc;		// Location/Translation 
	D3DXVECTOR3	vR;			// Rotation vector
    D3DMATRIX   matLocal;	
    D3DMATERIAL8 mtrl;		// material data
    CD3DMesh* mesh;	// mesh data
};

//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the 
//       generic functionality needed in all Direct3D samples. CMyD3DApplication 
//       adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
    CD3DFont* m_pFont;
	Object mySchool;

	FLOAT m_fStartTimeKey,     // Time reference for calculations
		  m_fTimeElapsed;

    // Variables for determining view position
	D3DXVECTOR3	m_pvVelocity, 
				m_pvAngularVelocity, 
				m_pvPosition, 
				m_pvYPR;

	D3DXMATRIX m_matPosition, 
			   m_matView;

	FLOAT m_fAngularSpeed, 
		  m_fSpeed;

    BYTE m_bKey[256];			// keyboard state buffer

    HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );

protected:
    HRESULT OneTimeSceneInit();
    HRESULT InitDeviceObjects();
    HRESULT RestoreDeviceObjects();
    HRESULT InvalidateDeviceObjects();
    HRESULT DeleteDeviceObjects();
    HRESULT Render();
    HRESULT FrameMove();
    HRESULT FinalCleanup();

public:
    LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
    CMyD3DApplication();
};

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
//       message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    CMyD3DApplication d3dApp;

    if( FAILED( d3dApp.Create( hInst ) ) )
        return 0;

    return d3dApp.Run();
}

//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
    m_strWindowTitle  = _T("Virtual Ocean Lakes");
    m_bUseDepthBuffer = TRUE;

    m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
    mySchool.mesh = new CD3DMesh();

	m_fAngularSpeed = 0.5;
	m_fSpeed= 5.0f;
	m_pvVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_pvAngularVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
    
	ZeroMemory( m_bKey, 256 );
}

//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
//       permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
	mySchool.vLoc   = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
    mySchool.vR.x   = 0.0f;
    mySchool.vR.y   = 0.0f;
    mySchool.vR.z   = 0.0f;

   return S_OK;
}

//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
//       the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
	// timing code: 
	// the object should move/rotate in the same speed 
	// at every possible fps
	const cTimeScale = 5;
	
	// calculate elapsed time
	m_fTimeElapsed=(m_fTime-m_fStartTimeKey) * cTimeScale;
	
	// store last time
	m_fStartTimeKey=m_fTime;

	//**********************************************************
	// yellow object
	//**********************************************************
	if (m_bKey[''J'']) mySchool.vR.z -= m_fTimeElapsed;
    if (m_bKey[''L'']) mySchool.vR.z += m_fTimeElapsed;
	if (m_bKey[''I'']) mySchool.vR.y -= m_fTimeElapsed;
    if (m_bKey[''K'']) mySchool.vR.y += m_fTimeElapsed;

    D3DXMATRIX matWorld;
	D3DXMatrixTranslation(&matWorld, mySchool.vLoc.x, mySchool.vLoc.y, mySchool.vLoc.z);

    D3DXMATRIX matTemp, matRotateX, matRotateY, matRotateZ;
    D3DXMatrixRotationY( &matRotateY, -mySchool.vR.x );
	D3DXMatrixRotationX( &matRotateX, -mySchool.vR.y );
	D3DXMatrixRotationZ( &matRotateZ, -mySchool.vR.z );

	D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
	D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
	D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld ); 

    mySchool.matLocal = matWorld;

	//************************************************************
	// camera stuff
	//************************************************************
    D3DXVECTOR3 vTrans(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 vRot(0.0f, 0.0f, 0.0f);

	// Process keyboard input
    if (m_bKey[VK_UP])			vTrans.z += 0.2f; // Move Forward
    if (m_bKey[VK_DOWN])		vTrans.z -= 0.2f; // Move Backward
    if (m_bKey[VK_NUMPAD4])		vTrans.x -= 0.1f; // Slide Left
    if (m_bKey[VK_NUMPAD6])		vTrans.x += 0.1f; // Slide Right
    if (m_bKey[VK_NUMPAD8])     vTrans.y += 0.1f; // Slide Down
    if (m_bKey[VK_NUMPAD2])     vTrans.y -= 0.1f; // Slide Up

	if (m_bKey[VK_HOME])		vRot.y += 0.1f; // Pitch Up
    if (m_bKey[VK_END])			vRot.y -= 0.1f; // Pitch Down
    if (m_bKey[VK_RIGHT])	    vRot.x += 0.1f; // Turn Left
    if (m_bKey[VK_LEFT])	    vRot.x -= 0.1f; // Turn Right
    if (m_bKey[''C''])		    vRot.z += 0.1f; // Rotate Left
    if (m_bKey[''X''])		    vRot.z -= 0.1f; // Rotate Right
	
	// turn cfSmooth to 0.98f
	const FLOAT cfSmooth = 0.8f;

	// transform and rotation velocity
    m_pvVelocity = m_pvVelocity * cfSmooth + vTrans;
    m_pvAngularVelocity = m_pvAngularVelocity * cfSmooth + vRot;

	// transform and rotation value
    vTrans = m_pvVelocity * m_fTimeElapsed * m_fSpeed;
    vRot = m_pvAngularVelocity* m_fTimeElapsed * m_fAngularSpeed;

    // Update position and view matricies
    D3DXMATRIX matT, matR;
    D3DXQUATERNION qR;

	D3DXMatrixTranslation (&matT, vTrans.x, vTrans.y, vTrans.z);		// step 1
	D3DXMatrixMultiply (&m_matPosition, &matT, &m_matPosition);
	D3DXQuaternionRotationYawPitchRoll (&qR, vRot.x, vRot.y, vRot.z);	// step 2
	D3DXMatrixRotationQuaternion (&matR, &qR);							// step 3
	D3DXMatrixMultiply (&m_matPosition, &matR, &m_matPosition);			// step 4
	D3DXMatrixInverse (&m_matView, NULL, &m_matPosition);				// step 5

	m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView );

   return S_OK;
}

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
//       rendering. This function sets up render states, clears the
//       viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{

    // Clear the viewport | z-buffer
    m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0L );

    // Begin the scene
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {
        // Apply the object''s local matrix
        m_pd3dDevice->SetTransform(D3DTS_WORLD, &mySchool.matLocal );

		mySchool.mesh->Render(m_pd3dDevice, true, true);

        // Output statistics
        m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
        m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );

      // End the scene.
      m_pd3dDevice->EndScene();
    }
   return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
    m_pFont->InitDeviceObjects( m_pd3dDevice );

	// load the file based objects
    if( FAILED(mySchool.mesh->Create( m_pd3dDevice, SCHOOLMAP ) ) )
        return E_FAIL;

    // Set the FVF type to match the vertex format we want
    mySchool.mesh->SetFVF( m_pd3dDevice, FVF_CUSTOMVERTEX);

	return S_OK;
}

//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
    m_pFont->RestoreDeviceObjects();
    mySchool.mesh->RestoreDeviceObjects( m_pd3dDevice );
    // Set the transform matrices
    D3DXMatrixTranslation(&m_matView, 0.0f, 5.0f, -50.0f);
    D3DXMatrixTranslation(&m_matPosition, 0.0f, 5.0f, -200.0f);

	// Set the projection matrix
    D3DXMATRIX  matProj;
    FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 500.0f );
    m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

	m_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);

    // Set up the lights
    if( m_d3dCaps.VertexProcessingCaps & D3DVTXPCAPS_DIRECTIONALLIGHTS )
    {
        D3DLIGHT8 light;
        D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.5f, -5.0f, -1.0f );
        light.Ambient.r = 0.1f;
        light.Ambient.g = 0.1f;
        light.Ambient.b = 0.1f;
        m_pd3dDevice->SetLight( 0, &light );
        m_pd3dDevice->LightEnable( 0, TRUE );

        D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.5f,  5.0f, -1.0f );
        light.Ambient.r = 0.2f;
        light.Ambient.g = 0.2f;
        light.Ambient.b = 0.2f;
        m_pd3dDevice->SetLight( 1, &light );
        m_pd3dDevice->LightEnable( 1, TRUE );
        m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
     }
	else
	{
	    m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(255, 255, 255));
	}



  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
    m_pFont->InvalidateDeviceObjects();
    mySchool.mesh->InvalidateDeviceObjects();

   return S_OK;
}

//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
    m_pFont->DeleteDeviceObjects();
    mySchool.mesh->Destroy();

   return S_OK;
}


//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
//       to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
    SAFE_DELETE( m_pFont );
    SAFE_DELETE( mySchool.mesh);

   return S_OK;
}

//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
//       for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, 
                                          D3DFORMAT Format )
{
    return S_OK;
}


//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
                                    LPARAM lParam )
{
    // Record key presses
    if( WM_KEYDOWN == uMsg )
    {
        m_bKey[wParam] = 1;
    }

    // Perform commands when keys are rleased
    if( WM_KEYUP == uMsg )
    {
        m_bKey[wParam] = 0;
    }

    return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
 
Thanks for your help very much! -Jared

Share this post


Link to post
Share on other sites