Sign in to follow this  
pascalosti

Problems with ID3DXLine::DrawTransform

Recommended Posts

Im very new to directX. ID3DXLine::DrawTransform is not working for me, Im pretty much guessing, and the errors are beyond me. Im using the tiger.x file (from sdk samples), the rest of the program works fine. Currently the tiger is rotating and has 2 lights on it. Now I want to draw lines using ID3DXLine::DrawTransform which keeps kicking me in the balls.

// sample code from google
// Dont know what the definition of "ppLine" is?
// this code does not work either but looks farther along then mine, I believe 
// the problem for this one is the scaling is to high
D3DXMATRIX mTransfo;
        D3DXMatrixTranslation(&mTransfo,1.0f,1.0f,-1.0f);
        ppLine->Begin();
        if ( ppLine->DrawTransform( myCoordVectPtr, myVertexCount, &mTransfo, D3DCOLOR_RGBA( 0, 0, 0, 255 ) ) != D3D_OK )
        {
                cout << "DrawTransform failed!" << endl;        
        }
        // line is displayed correctly when myCoordVectPtr is declared as D3DXVECTOR2,
        // using ID3DXLine::Draw() API
        //ppLine->Draw( myCoordVectPtr, myVertexCount, D3DCOLOR_RGBA( 0, 0, 0, 150 ) );
        ppLine->End();


// here is my code (draw problem is near the end)

VOID RenderD3D()
{
	
	// If the device isn't set yet, exit.
    if( g_pD3DDevice == NULL )
	{
        return;
	}

    // Clear the back-buffer to a black color
    g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,100,0), 1.0f, 0 );
    
    // Begin the scene
    if( SUCCEEDED( g_pD3DDevice->BeginScene() ) )
    {
		SetupLights4();
		SetupLights3();
 // Setup the world, view, and projection matrices
        SetupMatrices();

        // Meshes are divided into subsets, one for each material. Render them in
        // a loop
        for( DWORD i=0; i<g_dwNumMaterials; i++ )
        {
            // Set the material and texture for this subset
            g_pD3DDevice->SetMaterial( &g_pMeshMaterials[i] );
            g_pD3DDevice->SetTexture( 0, g_pMeshTextures[i] );
        
            // Draw the mesh subset
            g_pMesh->DrawSubset( i );
        }

		///////////////////////////////bounding boxes/////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////
		BYTE* pVertices=NULL;

		HRESULT hr = g_pMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
		if(FAILED(hr))
			return;// FALSE;

		D3DXVECTOR3 minBounds;
		D3DXVECTOR3	maxBounds;

		D3DXComputeBoundingBox((D3DXVECTOR3*)pVertices, g_pMesh->GetNumVertices(),
			D3DXGetFVFVertexSize(g_pMesh->GetFVF()), &minBounds, &maxBounds);

		g_pMesh->UnlockVertexBuffer();

		// problems begin////////////////////////////////////////////////////////
		CONST D3DXVECTOR3 TempMinBounds(0.1f,1.0f,0.0f);
		CONST D3DXVECTOR3	TempMaxBounds = *maxBounds;
		CONST D3DXVECTOR2* pVertexList = NULL;
			

		ID3DXLine::DrawTransform( TempMinBounds, g_dwNumMaterials, &DrawLineMatrix ,0x55502020); 
		//ID3DXLine::Draw(pVertexList, 10,0x55502020);  
		//

		//D3DXMATRIX mTransfo;
		//D3DXMatrixTranslation(&mTransfo,1.0f,1.0f,-1.0f);
       
       //DrawTransform( myCoordVectPtr, myVertexCount, &mTransfo, D3DCOLOR_RGBA( 0, 0, 0, 255 ) );

		// problems end////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

        // End the scene
        g_pD3DDevice->EndScene();
    }

    // Present the back-buffer contents to the display
    g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}



// all the code
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <iostream>
#include "globals.h"
#include <strsafe.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "winmm.lib")




//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
// D3D9 object pointer
LPDIRECT3D9				g_pD3D = NULL;
// D3D9 rendering device pointer
LPDIRECT3DDEVICE9		g_pD3DDevice = NULL;
// Window handle
HWND					g_hWnd;
// Application instance handle
HINSTANCE				g_hInstance;
// Window width
DWORD					g_wWidth = 1024;
// Window height
DWORD					g_wHeight = 768;
// Window active status
BOOL					g_bWindowActive = FALSE;


LPD3DXMESH              g_pMesh          = NULL; // Our mesh object in sysmem
D3DMATERIAL9*           g_pMeshMaterials = NULL; // Materials for our mesh
LPDIRECT3DTEXTURE9*     g_pMeshTextures  = NULL; // Textures for our mesh
DWORD                   g_dwNumMaterials = 0L;   // Number of mesh materials
D3DXMATRIX DrawLineMatrix;






bool                    g_bForce32ByteFVF = true;

HRESULT InitializeD3D( HWND hWnd );
VOID RenderD3D( );
VOID SetupMatrices();
VOID Cleanup();
VOID SetupLights();
VOID SetupLights2();
VOID SetupLights3();
VOID SetupLights4();
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow	)
{
	// Make a console, for debugging with stdout
	AllocConsole();
	freopen("CONOUT$","w",stdout);

	// Save a global copy of the window instance.
    g_hInstance = hInstance;

	// Register the window with Windows. First, define the window class we want.
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX); 
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(hInstance, (LPCTSTR)2);
	wc.hIconSm = LoadIcon(hInstance, (LPCTSTR)2);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "viewer";
	wc.hIconSm = NULL;
	
	// Register
	RegisterClassEx(&wc);

	// Create a rectangle to define the window
	RECT winRect;
	SetRect( &winRect, 0, 0, g_wWidth, g_wHeight );
	AdjustWindowRect( &winRect, WS_OVERLAPPEDWINDOW, false );

	// Create the window
	g_hWnd = CreateWindow(	"viewer",
								"Texture Normals X-file",
								WS_OVERLAPPEDWINDOW,
								0,
								0,
								winRect.right - winRect.left,
								winRect.bottom - winRect.top,
								NULL,
								NULL,
								g_hInstance,
								NULL	);

	// Initialize D3D now that the window is all set.
	if( FAILED( InitializeD3D( g_hWnd ) ) )
	{
		// Unregister the class we created.
		UnregisterClass( "viewer", wc.hInstance );
		FreeConsole();
		return -1;
	}

	ShowWindow( g_hWnd, SW_SHOWDEFAULT );
	UpdateWindow( g_hWnd );

	// Infinite (or, long) message loop.
    MSG mess;
	memset( &mess, 0, sizeof(mess) );
	while( mess.message != WM_QUIT )
    {
		if( PeekMessage( &mess, NULL, 0, 0, PM_REMOVE ) )
		{
			TranslateMessage( &mess );
			DispatchMessage( &mess );
		}
		else
		{
            RenderD3D();
		}
    }

	// Unregister the class we created.
	UnregisterClass( "viewer", wc.hInstance );

	// Clean up what was created in InitializeD3D().
	//SAFE_RELEASE( g_pD3D );
	//SAFE_RELEASE( g_pD3DDevice );
	FreeConsole();
	std::cout << "Exiting..." << std::endl;

	return 0;
}

/**
* Sets up a D3D object and device and saves pointers to them in global variables.
*
* @param hWnd the window handle
* @return the success status
*/
HRESULT InitializeD3D( HWND hWnd )
{
	// Structure needed for device creation.
	D3DPRESENT_PARAMETERS d3dpp;

	LPD3DXMESH   pMeshSysMem = NULL;
    LPD3DXMESH   pMeshTemp;
	LPD3DXBUFFER pMeshSysMemLoaded = NULL;
	HRESULT      hr = S_OK;
	 

	IDirect3DDevice9 *pd3dDevice;


	// Create a D3D object (needed to create device).
	g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
	if( !g_pD3D )
	{
		return E_FAIL;
	}

	// Get device capabilities before actually creating the device.
	D3DCAPS9 devCaps;
	if( FAILED( g_pD3D->GetDeviceCaps(	D3DADAPTER_DEFAULT,	// Default card
											D3DDEVTYPE_HAL,		// Hardware rasterization (not REF!)
											&devCaps	) ) )	// Device caps structure
	{
		SAFE_RELEASE( g_pD3D );
		SAFE_RELEASE( g_pD3DDevice );
		return E_FAIL;
	}

	// Get current display mode.
	D3DDISPLAYMODE d3ddm;
	if( FAILED( g_pD3D->GetAdapterDisplayMode( devCaps.AdapterOrdinal, &d3ddm )))
	{
		return E_FAIL;
	}

	// Clear the structure.
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
	// 16-bit depth/stencil buffers (best, while most common).
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	// Double-buffering.
	d3dpp.BackBufferCount = 1;
	// Same back buffer format as desktop's (only in windowed mode).
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	// Back buffer width and height.
	d3dpp.BackBufferWidth = g_wWidth;
	d3dpp.BackBufferHeight = g_wHeight;
	// Auotmatic depth+stencil buffers.
	d3dpp.EnableAutoDepthStencil = TRUE;
	// No flags.
	d3dpp.Flags = 0;
	// Default refresh rate for fullscreen.
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	// The window handle.
	d3dpp.hDeviceWindow = hWnd;
	// Default back buffer swap wait rate.
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
	// Simple double-buffering.
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	// We want the app to be windowed.
    d3dpp.Windowed = TRUE;
	// Multisampling (check for support 1st).
	DWORD MaxSampleQualities;
	if( SUCCEEDED( g_pD3D->CheckDeviceMultiSampleType(
										devCaps.AdapterOrdinal,		// Default card
										devCaps.DeviceType,			// HAL most likely (not REF)
										d3ddm.Format,				// Surface format (same as desktop)
										TRUE,						// Windowed
										D3DMULTISAMPLE_NONMASKABLE,	// Nonmaskable; enables quality value
										&MaxSampleQualities )))		// Gets the max sample quality
	{
		d3dpp.MultiSampleType = D3DMULTISAMPLE_NONMASKABLE;
		d3dpp.MultiSampleQuality = MaxSampleQualities - 1;
	}
	else
	{

		d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
		d3dpp.MultiSampleQuality = 0;
	}

	// Actually create the D3D device. (TODO: For now, assuming this is possible to create; no fallback!)
	if( FAILED(g_pD3D->CreateDevice(	D3DADAPTER_DEFAULT,	// Default adapter (most people only have 1 installed)
										D3DDEVTYPE_HAL,		// Requesting HW device, not SW one
										hWnd,				
										D3DCREATE_HARDWARE_VERTEXPROCESSING,	// TODO: Try HW...
										&d3dpp,
										&g_pD3DDevice	)) )
	{
		SAFE_RELEASE( g_pD3D );
		SAFE_RELEASE( g_pD3DDevice );
		return E_FAIL;
	}

	// Show current screen resolution and refresh rate.
	std::cout	<< "Current desktop resolution: " << d3ddm.Width << "x" << d3ddm.Height
				<< "@" << d3ddm.RefreshRate << "Hz" << std::endl;

	// Show pixel & vertex shader versions
	std::cout	<< "Vertex shader version: "
				<< D3DSHADER_VERSION_MAJOR( devCaps.VertexShaderVersion )
				<< "." << D3DSHADER_VERSION_MINOR( devCaps.VertexShaderVersion )
				<< std::endl;
	std::cout	<< "Pixel shader version: "
				<< D3DSHADER_VERSION_MAJOR( devCaps.PixelShaderVersion )
				<< "." << D3DSHADER_VERSION_MINOR( devCaps.PixelShaderVersion )
				<< std::endl;

	// Set a default render state

	//g_pD3DDevice->SetRenderState( D3DRS_AMBIENT, RGB(255,255,255) );	// Max ambient lighting though
	//g_pD3DDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );
	//g_pD3DDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );
	//g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );


	float fAspect = (float)g_wWidth / g_wHeight;
	D3DXMATRIX ProjectionMatrix;
	D3DXMatrixPerspectiveFovLH( &ProjectionMatrix, 45.0f, fAspect, 1.0f, 100.0f );
	g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &ProjectionMatrix );


	DrawLineMatrix = ProjectionMatrix;
	// Lighting (off), Z-test (on), back-face culling (off)
	//g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );			// No lights
	//g_pD3DDevice->SetRenderState( D3DRS_AMBIENT, RGB(255,255,255) );	// Max ambient lighting though
	g_pD3DDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );
	g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );




	// Set a flexible vertex format.
	g_pD3DDevice->SetFVF( MyFVF );

       LPD3DXBUFFER pD3DXMtrlBuffer;

    // Load the mesh from the specified file
    if( FAILED( D3DXLoadMeshFromX( "tiger.x", D3DXMESH_SYSTEMMEM, 
                                   g_pD3DDevice, NULL, 
                                   &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, 
                                   &g_pMesh ) ) )
    {
        // If model is not in current folder, try parent folder
        if( FAILED( D3DXLoadMeshFromX( "..\\tiger.x", D3DXMESH_SYSTEMMEM, 
                                    g_pD3DDevice, NULL, 
                                    &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, 
                                    &g_pMesh ) ) )
        {
            MessageBox(NULL, "Could not find tiger.x", "Meshes.exe", MB_OK);
            return E_FAIL;
        }
    }

	

    // We need to extract the material properties and texture names from the 
    // pD3DXMtrlBuffer
    D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
    g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];
    if( g_pMeshMaterials == NULL )
        return E_OUTOFMEMORY;
    g_pMeshTextures  = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];
    if( g_pMeshTextures == NULL )
        return E_OUTOFMEMORY;

	
    for( DWORD i=0; i<g_dwNumMaterials; i++ )
    {
        // Copy the material
        g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;

        // Set the ambient color for the material (D3DX does not do this)
        g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;

        g_pMeshTextures[i] = NULL;
        if( d3dxMaterials[i].pTextureFilename != NULL && 
            lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
        {
            // Create the texture
            if( FAILED( D3DXCreateTextureFromFile( g_pD3DDevice, 
                                                d3dxMaterials[i].pTextureFilename, 
                                                &g_pMeshTextures[i] ) ) )
            {
                // If texture is not in current folder, try parent folder
                const TCHAR* strPrefix = TEXT("..\\");
                TCHAR strTexture[MAX_PATH];
                StringCchCopy( strTexture, MAX_PATH, strPrefix );
                StringCchCat( strTexture, MAX_PATH, d3dxMaterials[i].pTextureFilename );
                // If texture is not in current folder, try parent folder
                if( FAILED( D3DXCreateTextureFromFile( g_pD3DDevice, 
                                                    strTexture, 
                                                    &g_pMeshTextures[i] ) ) )
                {
                    MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK);
                }
            }
        }
    }
//HRESULT OptimizeInplace(
//  DWORD Flags,
//  CONST DWORD * pAdjacencyIn,
//  DWORD * pAdjacencyOut,
//  DWORD * pFaceRemap,
//  LPD3DXBUFFER * ppVertexRemap
	CONST DWORD pAdjacencyIn = NULL;
	DWORD pAdjacencyOut = NULL;
	DWORD pFaceRemap = NULL;

 // Load mesh
    //\LPD3DXMESH   pMeshSysMem = NULL;
    LPD3DXBUFFER pAdjacencyBuffer = NULL;


   // hr = LoadMeshData( pd3dDevice, MESHFILENAME, &pMeshSysMem, &pAdjacencyBuffer );
    if( SUCCEEDED(hr) )
    {
       //hr = OptimizeMeshData( pMeshSysMem, pAdjacencyBuffer, D3DXMESHOPT_ATTRSORT, &g_MeshAttrSorted );
      //  if( SUCCEEDED(hr) )
          //  hr = OptimizeMeshData( pMeshSysMem, pAdjacencyBuffer, D3DXMESHOPT_STRIPREORDER, &g_MeshStripReordered );

        
		//	hr = ID3DXMesh::OptimizeInplace( D3DXMESHOPT_VERTEXCACHE,*pAdjacencyIn, *pAdjacencyOut , *pFaceRemap, *pAdjacencyBuffer );

        SAFE_RELEASE( pMeshSysMem );
        SAFE_RELEASE( pAdjacencyBuffer );
    } 
        // ignore load errors, just draw blank screen if mesh is invalid




///////////////////////////////////////////////////////
/// http://www.scurvysoft.com/cse190/doc/dxmesh.html

//HRESULT OptimizeMesh( D3DXMESHOPT_VERTEXCACHE );

//HRESULT OptimizeinPlace(D3DXMESHOPT_VERTEXCACHE, bufAdjacency.GetBufferPointer, bufAdjacency.getbufferpointer, nil, nil);





///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
////////////////////////////////NORMALS////////////////////////////////
///////////////////////////////////////////////////////////////////////
// remember if there were normals in the file, before possible clone operation
    bool bNormalsInFile = ( g_pMesh->GetFVF() & D3DFVF_NORMAL ) != 0;

  // Set a new FVF
  LPD3DXMESH pTempMesh;
  if( SUCCEEDED( g_pMesh->CloneMeshFVF(D3DXMESH_MANAGED, D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1, g_pD3DDevice, &pTempMesh) ) )
  {
    SAFE_RELEASE(g_pMesh);
    g_pMesh = pTempMesh;

// Compute normals for the mesh, if not present
    if( !bNormalsInFile )
    D3DXComputeNormals(g_pMesh, NULL);
  }
 ///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////


    // Done with the material buffer
    pD3DXMtrlBuffer->Release();

	return S_OK;
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	// Handle various messages we care about, or assign default action to message we
	// don't care about.
	switch (message) 
	{
		case WM_ACTIVATEAPP:
		{
			g_bWindowActive = (BOOL)wParam;
			break;
		}
		case WM_PAINT:
			// Render and update.
			RenderD3D();
            ValidateRect( hWnd, NULL );
			break;
		case WM_DESTROY:
		case WM_CLOSE:
			Cleanup();
			// Return control to the system (WinMain()?)
			PostQuitMessage(0);
			break;
		default:
			// We do not want to handle this message, so pass back to Windows
			// to handle it in a default way.
			return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;
}

/**
* Main rendering call.
*/
VOID RenderD3D()
{
	
	// If the device isn't set yet, exit.
    if( g_pD3DDevice == NULL )
	{
        return;
	}

    // Clear the back-buffer to a black color
    g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,100,0), 1.0f, 0 );
    
    // Begin the scene
    if( SUCCEEDED( g_pD3DDevice->BeginScene() ) )
    {
		SetupLights4();
		SetupLights3();
 // Setup the world, view, and projection matrices
        SetupMatrices();

        // Meshes are divided into subsets, one for each material. Render them in
        // a loop
        for( DWORD i=0; i<g_dwNumMaterials; i++ )
        {
            // Set the material and texture for this subset
            g_pD3DDevice->SetMaterial( &g_pMeshMaterials[i] );
            g_pD3DDevice->SetTexture( 0, g_pMeshTextures[i] );
        
            // Draw the mesh subset
            g_pMesh->DrawSubset( i );
        }

		///////////////////////////////bounding boxes/////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////
		BYTE* pVertices=NULL;

		HRESULT hr = g_pMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
		if(FAILED(hr))
			return;// FALSE;

		D3DXVECTOR3 minBounds;
		D3DXVECTOR3	maxBounds;

		D3DXComputeBoundingBox((D3DXVECTOR3*)pVertices, g_pMesh->GetNumVertices(),
			D3DXGetFVFVertexSize(g_pMesh->GetFVF()), &minBounds, &maxBounds);

		g_pMesh->UnlockVertexBuffer();

		// problems begin////////////////////////////////////////////////////////
		CONST D3DXVECTOR3 TempMinBounds(0.1f,1.0f,0.0f);
		CONST D3DXVECTOR3	TempMaxBounds = *maxBounds;
		CONST D3DXVECTOR2* pVertexList = NULL;
			

		ID3DXLine::DrawTransform( TempMinBounds, g_dwNumMaterials, &DrawLineMatrix ,0x55502020); 
		//ID3DXLine::Draw(pVertexList, 10,0x55502020);  
		//

		//D3DXMATRIX mTransfo;
		//D3DXMatrixTranslation(&mTransfo,1.0f,1.0f,-1.0f);
       
       //DrawTransform( myCoordVectPtr, myVertexCount, &mTransfo, D3DCOLOR_RGBA( 0, 0, 0, 255 ) );

		// problems end////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

        // End the scene
        g_pD3DDevice->EndScene();
    }

    // Present the back-buffer contents to the display
    g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}


//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    if( g_pMeshMaterials != NULL ) 
        delete[] g_pMeshMaterials;

    if( g_pMeshTextures )
    {
        for( DWORD i = 0; i < g_dwNumMaterials; i++ )
        {
            if( g_pMeshTextures[i] )
                g_pMeshTextures[i]->Release();
        }
        delete[] g_pMeshTextures;
    }
    if( g_pMesh != NULL )
        g_pMesh->Release();
    
    if( g_pD3DDevice != NULL )
        g_pD3DDevice->Release();

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


//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
    // Set up world matrix
    D3DXMATRIXA16 matWorld;
    D3DXMatrixRotationY( &matWorld, timeGetTime()/1000.0f );
    g_pD3DDevice->SetTransform( D3DTS_WORLD, &matWorld );

    // Set up our view matrix. A view matrix can be defined given an eye point,
    // a point to lookat, and a direction for which way is up. Here, we set the
    // eye five units back along the z-axis and up three units, look at the 
    // origin, and define "up" to be in the y-direction.
    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
    g_pD3DDevice->SetTransform( D3DTS_VIEW, &matView );

    // For the projection matrix, we set up a perspective transform (which
    // transforms geometry from 3D view space to 2D viewport space, with
    // a perspective divide making objects smaller in the distance). To build
    // a perpsective transform, we need the field of view (1/4 pi is common),
    // the aspect ratio, and the near and far clipping planes (which define at
    // what distances geometry should be no longer be rendered).
    D3DXMATRIXA16 matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
    g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////LIGHTS///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

VOID SetupLights()
{
    // Set up a material. The material here just has the diffuse and ambient
    // colors set to yellow. Note that only one material can be used at a time.
    D3DMATERIAL9 mtrl;
    ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
       



// Set the RGBA for diffuse reflection.
mtrl.Diffuse.r = 0.1f;
mtrl.Diffuse.g = 0.1f;
mtrl.Diffuse.b = 1.0f;
mtrl.Diffuse.a = 1.0f;

// Set the RGBA for ambient reflection.
mtrl.Ambient.r = 0.1f;
mtrl.Ambient.g = 0.1f;
mtrl.Ambient.b = 0.1f;
mtrl.Ambient.a = 1.0f;

// Set the color and sharpness of specular highlights.
mtrl.Specular.r = 0.0f;
mtrl.Specular.g = 0.0f;
mtrl.Specular.b = 0.0f;
mtrl.Specular.a = 0.0f;
mtrl.Power = 40.0f;

// Set the RGBA for emissive color.
// glows on its own
mtrl.Emissive.r = 0.0f;
mtrl.Emissive.g = 0.3f;
mtrl.Emissive.b = 0.0f;
mtrl.Emissive.a = 1.0f;
g_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
g_pD3DDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);



g_pD3DDevice->SetMaterial( &mtrl );

	
    // Set up a white, directional light, with an oscillating direction.
    // Note that many Lights may be active at a time (but each one slows down
    // the rendering of our scene). However, here we are just using one. Also,
    // we need to set the D3DRS_LIGHTING renderstate to enable lighting
    D3DXVECTOR3 vecDir1;
    D3DLIGHT9 light;
    ZeroMemory( &light, sizeof(D3DLIGHT9) );
    light.Type       = D3DLIGHT_DIRECTIONAL;
    light.Diffuse.r  = 0.9f;
    light.Diffuse.g  = 0.9f;
    light.Diffuse.b  = 0.9f;

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


	
    vecDir1 = D3DXVECTOR3(cosf(timeGetTime()/350.0f),
                         1.0f,
                         sinf(timeGetTime()/350.0f) );
    D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir1 );
    light.Range       = 1000.0f;
    g_pD3DDevice->SetLight( 0, &light );
    g_pD3DDevice->LightEnable( 0, true );
    g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

    // Finally, turn on some ambient light.
  //  g_pD3DDevice->SetRenderState( D3DRS_AMBIENT, 0x55502020 );
}
//////////////////////////////////////// setuplights  2
VOID SetupLights2()
{
    // Set up a material. The material here just has the diffuse and ambient
    // colors set to yellow. Note that only one material can be used at a time.
    D3DMATERIAL9 mtrl;
    ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
       


// Set the RGBA for diffuse reflection.
mtrl.Diffuse.r = 1.0f;
mtrl.Diffuse.g = 1.0f;
mtrl.Diffuse.b = 1.0f;
mtrl.Diffuse.a = 1.0f;

// Set the RGBA for ambient reflection.
mtrl.Ambient.r = 0.0f;
mtrl.Ambient.g = 0.0f;
mtrl.Ambient.b = 0.0f;
mtrl.Ambient.a = 1.0f;

// Set the color and sharpness of specular highlights.
mtrl.Specular.r = 1.0f;
mtrl.Specular.g = 1.0f;
mtrl.Specular.b = 1.0f;
mtrl.Specular.a = 0.0f;
mtrl.Power = 10.0f;

// Set the RGBA for emissive color.
// glows on its own
mtrl.Emissive.r = 0.0f;
mtrl.Emissive.g = 0.0f;
mtrl.Emissive.b = 0.3f;
mtrl.Emissive.a = 1.0f;
g_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
g_pD3DDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);



g_pD3DDevice->SetMaterial( &mtrl );

	
    // Set up a white, directional light, with an oscillating direction.
    // Note that many Lights may be active at a time (but each one slows down
    // the rendering of our scene). However, here we are just using one. Also,
    // we need to set the D3DRS_LIGHTING renderstate to enable lighting
    D3DXVECTOR3 vecDir2;
	D3DXVECTOR3 pos;
	//vecDir = D3DXVECTOR3(0.0f,0.0f,0.0f);
    D3DLIGHT9 light;
    ZeroMemory( &light, sizeof(D3DLIGHT9) );
    light.Type       = D3DLIGHT_POINT;;
    light.Diffuse.r  = 1.0f;
    light.Diffuse.g  = 0.0f;
    light.Diffuse.b  = 0.0f;

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





	vecDir2 = D3DXVECTOR3(20,-20,0);
	pos = D3DXVECTOR3(10,20,0);
	// Point lights have no direction but do have a position
	light.Position = pos;

	/*vecDir2 = D3DXVECTOR3(cosf(timeGetTime()/350.0f),
                         1.0f,
                         sinf(timeGetTime()/350.0f) );*/
    D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir2 );
	 light.Attenuation0 = 0.1f;
    light.Range       = 100.0f;
    g_pD3DDevice->SetLight( 0, &light );
    g_pD3DDevice->LightEnable( 0, true );
    g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

    // Finally, turn on some ambient light.
    //g_pD3DDevice->SetRenderState( D3DRS_AMBIENT, 0x55502020 );
}

VOID SetupLights3()
{
    // Set up a material. The material here just has the diffuse and ambient
    // colors set to yellow. Note that only one material can be used at a time.
    D3DMATERIAL9 mtrl;
    ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
     
// Set the RGBA for diffuse reflection.
mtrl.Diffuse.r = 1.0f;
mtrl.Diffuse.g = 1.0f;
mtrl.Diffuse.b = 1.0f;
mtrl.Diffuse.a = 1.0f;

// Set the RGBA for ambient reflection.
mtrl.Ambient.r = 0.5f;
mtrl.Ambient.g = 0.5f;
mtrl.Ambient.b = 0.5f;
mtrl.Ambient.a = 1.0f;

// Set the color and sharpness of specular highlights.
mtrl.Specular.r = 1.0f;
mtrl.Specular.g = 1.0f;
mtrl.Specular.b = 1.0f;
mtrl.Specular.a = 0.0f;
mtrl.Power = 50.0f;

// Set the RGBA for emissive color.
// glows on its own
mtrl.Emissive.r = 0.0f;
mtrl.Emissive.g = 0.0f;
mtrl.Emissive.b = 0.0f;
mtrl.Emissive.a = 1.0f;
g_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
g_pD3DDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);



g_pD3DDevice->SetMaterial( &mtrl );

	
    // Set up a white, directional light, with an oscillating direction.
    // Note that many Lights may be active at a time (but each one slows down
    // the rendering of our scene). However, here we are just using one. Also,
    // we need to set the D3DRS_LIGHTING renderstate to enable lighting
    D3DXVECTOR3 vecDir3;
    D3DLIGHT9 light;
    ZeroMemory( &light, sizeof(D3DLIGHT9) );
    light.Type       = D3DLIGHT_SPOT;
    light.Diffuse.r  = 1.0f;
    light.Diffuse.g  = 1.0f;
    light.Diffuse.b  = 1.0f;

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

	light.Position = D3DXVECTOR3(10.0f,5.0f, 10.0f);
	light.Direction = D3DXVECTOR3(0.0f,0.0f, 0.0f);

	light.Range=1.0f;
	light.Theta=0.5f;
	light.Phi=1.0f;
	light.Falloff=1.0f;
	light.Attenuation0= 1.0f;

	
   /* vecDir3 = D3DXVECTOR3(cosf(timeGetTime()/350.0f),
                         1.0f,
                         sinf(timeGetTime()/350.0f) );*/
	vecDir3 = D3DXVECTOR3(0.0f,0.0f,0.0f);
    D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir3 );
    light.Range       = 50.0f;
    g_pD3DDevice->SetLight( 1, &light );
    g_pD3DDevice->LightEnable( 1, true );
    g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

    // Finally, turn on some ambient light.
  //  g_pD3DDevice->SetRenderState( D3DRS_AMBIENT, 0x55502020 );
}
// 
VOID SetupLights4()
{
    // Set up a material. The material here just has the diffuse and ambient
    // colors set to yellow. Note that only one material can be used at a time.
    D3DMATERIAL9 mtrl;
    ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
       



// Set the RGBA for diffuse reflection.
mtrl.Diffuse.r = 1.0f;
mtrl.Diffuse.g = 0.0f;
mtrl.Diffuse.b = 0.0f;
mtrl.Diffuse.a = 1.0f;

// Set the RGBA for ambient reflection.
mtrl.Ambient.r = 0.0f;
mtrl.Ambient.g = 0.0f;
mtrl.Ambient.b = 0.0f;
mtrl.Ambient.a = 0.0f;

// Set the color and sharpness of specular highlights.
mtrl.Specular.r = 1.0f;
mtrl.Specular.g = 1.0f;
mtrl.Specular.b = 1.0f;
mtrl.Specular.a = 1.0f;
mtrl.Power = 20.0f;

// Set the RGBA for emissive color.
// glows on its own
mtrl.Emissive.r = 0.0f;
mtrl.Emissive.g = 0.1f;
mtrl.Emissive.b = 0.1f;
mtrl.Emissive.a = 1.0f;
g_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
g_pD3DDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);



g_pD3DDevice->SetMaterial( &mtrl );

	
    // Set up a white, directional light, with an oscillating direction.
    // Note that many Lights may be active at a time (but each one slows down
    // the rendering of our scene). However, here we are just using one. Also,
    // we need to set the D3DRS_LIGHTING renderstate to enable lighting
    D3DXVECTOR3 vecDir;
    D3DLIGHT9 light;
    ZeroMemory( &light, sizeof(D3DLIGHT9) );
    light.Type       = D3DLIGHT_DIRECTIONAL;
    light.Diffuse.r  = 0.0f;
    light.Diffuse.g  = 1.0f;
    light.Diffuse.b  = 1.0f;

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


	
    vecDir = D3DXVECTOR3(cosf(timeGetTime()/350.0f),
                         1.0f,
                         sinf(timeGetTime()/350.0f) );
    D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );
    light.Range       = 1000.0f;
    g_pD3DDevice->SetLight( 0, &light );
    g_pD3DDevice->LightEnable( 0, true );
    g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

    // Finally, turn on some ambient light.
  //  g_pD3DDevice->SetRenderState( D3DRS_AMBIENT, 0x55502020 );
}



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