////////////////////////////////////////////////////////////////////////////////
/////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 ) ) )
{
return E_FAIL;
MessageBox(NULL, "error initD3d" , "error",MB_OK|MB_ICONEXCLAMATION);
}
D3DDISPLAYMODE d3ddm;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
{
return E_FAIL;
MessageBox(NULL, "error display" , "error",MB_OK|MB_ICONEXCLAMATION);
}
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 ) ) )
{
return E_FAIL;
MessageBox(NULL, "error create device" , "error",MB_OK|MB_ICONEXCLAMATION);
}
// Create the font
ZeroMemory(&Font, sizeof(Font));
strcpy(Font.lfFaceName, "Arial");
Font.lfHeight = -12;
D3DXCreateFontIndirect(g_pd3dDevice, &Font, &g_pFont);
// return S_OK;
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0xffffffff);
////////////////////////////////////////////////////////////////////////////////
}
HRESULT InitGeometry()
{
LPD3DXBUFFER pD3DXMtrlBuffer;
//
{
LPD3DXBUFFER pD3DXMtrlBuffer;
// Load the mesh from the specified file
if( FAILED( D3DXLoadMeshFromX( "Tiger.x", D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, &g_dwNumMaterials,
&g_pMesh ) ) )
{
return E_FAIL;
MessageBox(NULL, "error display" , "error",MB_OK|MB_ICONEXCLAMATION);
}
// 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;
}
MYVERTEX vCubeVertices[8] =
{
{ 0.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 0, 255), }, // x, y, z, color point 0
{ 0.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 0, 255), }, //point 1
{ 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 0, 255), }, //point 2
{ 1.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(0, 0, 255, 255), }, //point 3
{ 1.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255), }, // x, y, z, color point 4
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(255, 0, 0, 255), }, //point 5
{ 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(0, 0, 0, 255), }, //point 6
{ 0.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.
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();
}
////////////////////////////////////////////////////////////////////////////////
VOID Cleanup()
{
if( g_pCubeIB != NULL)
g_pCubeIB->Release();
// if our cube Vertex Buffer still exists, then release it''s memory
if( g_pCubeVB !=NULL )
g_pCubeVB->Release();
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if( g_pD3D != NULL)
g_pD3D->Release();
// Release font
if(g_pFont != NULL)
g_pFont->Release();
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();
}
////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
// 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, 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.
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 3.0f,-5.0f ),
&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,
D3DCOLOR_RGBA(0,64,128,255), 1.0f, 0);
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
{
CalculateFrameRate();
SetupMatrices();
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->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);
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;
// case WM_PAINT:
// ValidateRect( hWnd, NULL );
// 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, 0, 0, 300, 300,
NULL, NULL, (HINSTANCE) wc.hInstance, NULL );
///////////////////////////////////////////////////////////////////////////////
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// 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;
}
////////////////////////////////////////////////////////////////////////////////
How do you load more then one object
I have a box loaded And i am trying to load a x file. But it keeps crashing. so how do I load more then one object at a time?
Juat a question, why must the screen be 16 color?
-~-The Cow of Darkness-~-
If you see the image I am online
Check out PGNet.tk
-~-The Cow of Darkness-~-
If you see the image I am online
Check out PGNet.tk
quote:Original post by kingpinzs
when it is in 24 bit it crashes and when it is in 32 bit it dont run.
that is all I know
How about 8 and 16 bit? 4 bit seems a bit extreme IMO...
-~-The Cow of Darkness-~-
If you see the image I am online
Check out PGNet.tk
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement