////////////////////////////////////////////////////////////////////////////////
/////Screen has to be in 16 bit color///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include <d3dx8.h>
#include <mmsystem.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "d3dx8.lib")
#pragma comment(lib, "d3d8.lib")
#pragma comment(lib, "winmm.lib")
// Global variables
LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device
//LPD3DXBUFFER pD3DxMtrlBuffer = NULL;
LPDIRECT3DINDEXBUFFER8 g_pCubeIB = NULL; //INDEX BUFER
LPDIRECT3DVERTEXBUFFER8 g_pCubeVB = NULL;//Vertex buffer
ID3DXFont *g_pFont = NULL;
LPD3DXMESH g_pMesh = NULL;
D3DMATERIAL8* g_pMeshMaterials = NULL;
LPDIRECT3DTEXTURE8* g_pMeshTextures = NULL;
DWORD g_dwNumMaterials = 0L;
////////////////////////////////////////////////////////////////////////////////
struct MYVERTEX
{
FLOAT x, y, z;
D3DCOLOR color;
};
#define D3DFVF_MYVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
////////////////////////////////////////////////////////////////////////////////
// Desc: Initializes Direct3D///////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
HRESULT InitD3D( HWND hWnd)
{
if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
{
MessageBox(NULL, "error initD3d" , "error",MB_OK|MB_ICONEXCLAMATION);
return E_FAIL;
}
D3DDISPLAYMODE d3ddm;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
{
MessageBox(NULL, "error display" , "error",MB_OK|MB_ICONEXCLAMATION);
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
LOGFONT Font;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;//change display colors
d3dpp.EnableAutoDepthStencil = TRUE ;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// d3dpp.BackBufferWidth = d3ddm.Width;
// d3dpp.BackBufferHeight = d3ddm.Height;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
MessageBox(NULL, "error create device" , "error",MB_OK|MB_ICONEXCLAMATION);
return E_FAIL;
}
// Create the font
ZeroMemory(&Font, sizeof(Font));
strcpy(Font.lfFaceName, "Arial");
Font.lfHeight = -12;
D3DXCreateFontIndirect(g_pd3dDevice, &Font, &g_pFont);
//g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
//g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, 255);
////////////////////////////////////////////////////////////////////////////////
MYVERTEX vCubeVertices[8] =
{
{ 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 0, 255), }, // x, y, z, color point 0
{ 1.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 0, 255), }, //point 1
{ 2.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 0, 255), }, //point 2
{ 2.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(0, 0, 255, 255), }, //point 3
{ 2.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255), }, // x, y, z, color point 4
{ 2.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(255, 0, 0, 255), }, //point 5
{ 1.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(0, 0, 0, 255), }, //point 6
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(255, 0, 0, 255), } //point 7
};
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 8 * sizeof(MYVERTEX), //allocate memory
0 ,// Usage
D3DFVF_MYVERTEX, // format of MYVERTEX
D3DPOOL_DEFAULT, // default D3DPOOL values
&g_pCubeVB ) ) )
{
return E_FAIL;
MessageBox(NULL, "error create device" , "error",MB_OK|MB_ICONEXCLAMATION);
}
VOID* pVertices;
if( FAILED( g_pCubeVB->Lock( 0, sizeof(vCubeVertices), (BYTE**)&pVertices, 0 ) ) )
{
return E_FAIL;
MessageBox(NULL, "error create device" , "error",MB_OK|MB_ICONEXCLAMATION);
}
memcpy( pVertices, vCubeVertices, sizeof(vCubeVertices) );
g_pCubeVB->Unlock();
if( FAILED( g_pd3dDevice->CreateIndexBuffer( 36 * sizeof(WORD), // there''s 36 triangles in a 6-sided cube
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_DEFAULT,
&g_pCubeIB ) ) )
{return E_FAIL;}
WORD cube_indices[36] = {0, 1, 2,
2, 1, 3,
2, 3, 4,
4, 3, 5,
4, 5, 6,
6, 5, 7,
6, 7, 0,
0, 7, 1,
1, 7, 3,
3, 7, 5,
0, 2, 6,
2, 4, 6};
VOID* pIndices;
if( FAILED( g_pCubeIB->Lock( 0, //Fill from start of the buffer.
sizeof(cube_indices), // Size of the data to load.
(BYTE**)&pIndices, // Returned index data.
0 ) ) ) // Send default flags to the lock.
{
MessageBox(NULL, "error create device" , "error",MB_OK|MB_ICONEXCLAMATION);
return E_FAIL;
}
memcpy( pIndices, cube_indices, sizeof(cube_indices) );
g_pCubeIB->Unlock();
return S_OK;
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void CalculateFrameRate()
{
RECT Rect = { 0,0,115,35};
static float framesPerSecond = 0.0f;
static float lastTime = 0.0f;
static char strFrameRate[50] = {0};
static float frameTime = 0.0f;
float g_FrameInterval = 0.0f;
float currentTime = timeGetTime() * 0.001f;
++framesPerSecond;
g_FrameInterval = currentTime - frameTime;
frameTime = currentTime;
if( currentTime - lastTime > 1.0f )
{
lastTime = currentTime;
sprintf(strFrameRate, "Current FPS: %d", int(framesPerSecond));
framesPerSecond = 0;
}
g_pFont->Begin();
g_pFont->DrawText(strFrameRate, -1, &Rect, DT_CENTER | DT_VCENTER, 0xFFFFFFFF);
g_pFont->End();
}
////////////////////////////////////////////////////////////////////////////////
HRESULT InitGeometry()
{
LPD3DXBUFFER pD3DXMtrlBuffer;
// Load the mesh from the specified file
if( FAILED( D3DXLoadMeshFromX( "Tiger.x", D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, &g_dwNumMaterials,
&g_pMesh ) ) )
{
MessageBox(NULL, "error create device" , "error",MB_OK|MB_ICONEXCLAMATION);
return E_FAIL;
}
// We need to extract the material properties and texture names from the
// pD3DXMtrlBuffer
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
g_pMeshMaterials = new D3DMATERIAL8[g_dwNumMaterials];
g_pMeshTextures = new LPDIRECT3DTEXTURE8[g_dwNumMaterials];
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;
// Create the texture
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,
d3dxMaterials[i].pTextureFilename,
&g_pMeshTextures[i] ) ) );
{
g_pMeshTextures[i] = NULL;
}
}
// Done with the material buffer
pD3DXMtrlBuffer->Release();
return S_OK;
}
///////////////////////////////////////////////////////////////////////////////////
VOID Cleanup()
{
if( g_pCubeIB != NULL)
{
MessageBox(NULL, "cubeib" , "error",MB_OK|MB_ICONEXCLAMATION);
g_pCubeIB->Release();
}
// if our cube Vertex Buffer still exists, then release it''s memory
if( g_pCubeVB !=NULL )
{
MessageBox(NULL, "Cubevb" , "error",MB_OK|MB_ICONEXCLAMATION);
g_pCubeVB->Release();
}
if( g_pd3dDevice != NULL)
{
MessageBox(NULL, "Device" , "error",MB_OK|MB_ICONEXCLAMATION);
g_pd3dDevice->Release();
}
if( g_pD3D != NULL)
{
MessageBox(NULL, "d3d" , "error",MB_OK|MB_ICONEXCLAMATION);
g_pD3D->Release();
}
// Release font
if(g_pFont != NULL)
{
g_pFont->Release();
MessageBox(NULL, "font" , "error",MB_OK|MB_ICONEXCLAMATION);
}
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();
MessageBox(NULL, "mesh" , "error",MB_OK|MB_ICONEXCLAMATION);
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
// For our world matrix, we will just leave it as the identity
D3DXMATRIX matWorld;
D3DXMatrixRotationY( &matWorld, 0);
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.
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( -5.0f, 0.0f,-3.0f ),//x turns it side ways,y moves it away,z moves it closer
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
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).
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
////////////////////////////////////////////////////////////////////////////////
// Desc: Draws the scene
VOID Render()
{
RECT Rect = { 0,0,115,115};
// if( NULL == g_pd3dDevice )
// return;
/*
D3DXMATRIX matWorld;
D3DXMatrixRotationY( &matWorld,NULL);//makes the cube spin around the y axis
//D3DXMatrixRotationX( &matWorld, timeGetTime()/50.0f); // x axis
//D3DXMatrixRotationZ( &matWorld, timeGetTime()/250.0f);// z axis
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView,
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),// Changes
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),// changes were the cube is at
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ) ); // Changes
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
*/
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0); // What ever this does it is important
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
{
CalculateFrameRate();
SetupMatrices();
g_pd3dDevice->SetVertexShader( D3DFVF_MYVERTEX );
g_pd3dDevice->SetStreamSource( 0, g_pCubeVB, sizeof(MYVERTEX) );
g_pd3dDevice->SetIndices( g_pCubeIB, 0 );
g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0,
8,
0,
12);
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 );
}
g_pd3dDevice->EndScene();
}
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Desc: Releases all previously initialized objects
////////////////////////////////////////////////////////////////////////////////
// Desc: The window''s message handler
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
////////////////////////////////////////////////////////////////////////////////
// Desc: The application''s entry point
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0,
NULL, NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx( &wc );
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice",
WS_CAPTION | WS_SYSMENU, 100, 100, 300, 300,
NULL, NULL, (HINSTANCE) wc.hInstance, NULL );
///////////////////////////////////////////////////////////////////////////////
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
MessageBox(NULL, "Initd3d worked" , "error",MB_OK|MB_ICONEXCLAMATION);
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) );
{
}
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(1)
{
if (PeekMessage( &msg, NULL, 0, 0,PM_REMOVE ) )
{
if(msg.message == WM_QUIT)
break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render();
}
}
}
Cleanup();
UnregisterClass( "D3D Tutorial", (HINSTANCE) wc.hInstance );
return 0;
}
////////////////////////////////////////////////////////////////////////////////
why is my tiger white
i am trying to use the direcx 8 mesh example that I modified to put fps and a box in it but the tiger is white and not textured
every thing that I try does not work but if I compile the microsoft one it works fine. What would be differnt?
Very often when the texture doesn''t load it''s because you are using a relative path. You might not be in the directory that you are expecting.
try using getcwd(); to check where you are and see if the texture is in that directory.
try using getcwd(); to check where you are and see if the texture is in that directory.
I am not sure how to check if is loading or if it is loading properly and how do you use getcwd(); ?
quote:Original post by kingpinzs
I am not sure how to check if is loading or if it is loading properly and how do you use getcwd(); ?
1. Change:
#pragma comment(lib, "d3dx8.lib")
To:
#pragma comment(lib, "d3dx8d.lib")
That''ll link your program with the DEBUG version of D3DX. If a D3DX call fails, it''ll report an error to the debug output window.
2.
a. Make sure you''ve installed the DEBUG version of the DirectX runtimes (you get the option when you install the SDK).
b. Make sure the debug D3D runtime is enabled in the DirectX Control Panel applet.
c. Increase the output level in the Direct3D tab of the DirectX Control Panel applet.
3. Run your application in the debugger (press F5 if you''re using MSVC). After the program has exhibited wrong behaviour, read through all of the text in debug output window (usually the bottom pane in MSVC). Any errors and warnings will be logged to there.
If you''re not using MSVC, you can use the debug viewer that comes with the DirectX SDK or download the one from www.sysinternals.com
--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement