Is it good to use drawindexprimitive as in drawprimitive objects are rendering fine with good frame rate?
Shall i use drawprimitive or drawindexprimitive?
I need your help here.
// File: Vertices.cpp
//
// Desc: In this tutorial, we are rendering some vertices. This introduces the
// concept of the vertex buffer, a Direct3D object used to store
// vertices. Vertices can be defined any way we want by defining a
// custom structure and a custom FVF (flexible vertex format). In this
// tutorial, we are using vertices that are transformed (meaning they
// are already in 2D window coordinates) and lit (meaning we are not
// using Direct3D lighting, but are supplying our own colors).
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <stdio.h>
#include <d3dx9.h>
#include <d3dx9math.h>
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB1;
LPDIRECT3DINDEXBUFFER9 g_pIB1;
IDirect3DTexture9* g_Texture1 = NULL;
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
//////////////////////////////////////////////////////////////////////
//
// Octree.h: interface for the COctree class.
//
//////////////////////////////////////////////////////////////////////
struct D1DVERTEX
{
D3DXVECTOR3 p;
FLOAT tu, tv;
};
struct D2DVERTEX
{
D3DXVECTOR3 p;
FLOAT tu, tv;
FLOAT tu2, tv2;
};
const DWORD D1DVERTEX_FVF = D3DFVF_XYZ | D3DFVF_TEX1;
WORD nCountVertices = 0;
WORD nCountIndices = 0;
struct TRIANGLE
{
WORD nIndex[3];
int nTexID;
int nLtMapID;
int nMode;
};
//-----------------------------------------------------------------------------
// Name: ReadFromFile()
// Desc:
//-----------------------------------------------------------------------------
VOID ReadFromFile()
{
FILE *fp1 = fopen("C:\\Documents and Settings\\Haque\\Desktop\\MAP.var","rb");
FILE *fp2 = fopen("C:\\Documents and Settings\\Haque\\Desktop\\MAP.bun","rb");
D1DVERTEX *p1DVertList = NULL;
WORD d1DVertices;
D2DVERTEX *p2DVertList = NULL;
WORD d2DVertices;
TRIANGLE *pTriList = NULL;
WORD dTriangles;
D3DXVECTOR3 vBoundingMin,vBoundingMax;
fread(&d2DVertices, sizeof(WORD), 1, fp1);
fread(&d1DVertices, sizeof(WORD), 1, fp1);
p2DVertList = new D2DVERTEX[d2DVertices];
fread(p2DVertList, sizeof(struct D2DVERTEX), d2DVertices, fp1);
p1DVertList = new D1DVERTEX[d1DVertices];
fread(p1DVertList, sizeof(struct D1DVERTEX), d1DVertices, fp1);
D1DVERTEX* pVertices;
if( FAILED( g_pVB1->Lock( 0, d2DVertices*sizeof(D1DVERTEX), (void**)&pVertices, D3DLOCK_DISCARD ) ) )
return ;
for(int j = 0; j < d2DVertices; j++)
{
pVertices[j].p = p2DVertList[j].p;
pVertices[j].tu = p2DVertList[j].tu;
pVertices[j].tv = p2DVertList[j].tv;
}
g_pVB1->Unlock();
pVertices = NULL;
if( FAILED( g_pVB1->Lock( d2DVertices*sizeof(D1DVERTEX), d1DVertices*sizeof(D1DVERTEX), (void**)&pVertices, D3DLOCK_NOOVERWRITE) ) )
return ;
memcpy( pVertices, p1DVertList, d1DVertices*sizeof(D1DVERTEX) );
g_pVB1->Unlock();
SAFE_DELETE_ARRAY(p1DVertList);
SAFE_DELETE_ARRAY(p2DVertList);
fclose(fp1);
fp1 = NULL;
fread(&dTriangles, sizeof(WORD), 1, fp2);
printf("No Of Tri = %d\n",dTriangles);
fread(&vBoundingMin, sizeof(D3DXVECTOR3), 1, fp2);
fread(&vBoundingMax, sizeof(D3DXVECTOR3), 1, fp2);
pTriList = new TRIANGLE[dTriangles];
fread(pTriList, sizeof(struct TRIANGLE), dTriangles, fp2);
WORD* pIndices = NULL;
if( FAILED( g_pIB1->Lock( 0, dTriangles*sizeof(WORD), (void**)&pIndices, D3DLOCK_DISCARD) ) )
return;
for(int i = 0, k = 0; i < dTriangles; i++,k+=3)
{
pIndices[k+0] = pTriList[i].nIndex[0];
pIndices[k+1] = pTriList[i].nIndex[1];
pIndices[k+2] = pTriList[i].nIndex[2];
}
g_pIB1->Unlock();
SAFE_DELETE_ARRAY(pTriList);
fclose(fp2);
fp2 = NULL;
nCountVertices = d1DVertices + d2DVertices;
nCountIndices = dTriangles*3;
}
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
return S_OK;
}
HRESULT InitVB()
{
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 12000*sizeof(D1DVERTEX),
D3DUSAGE_DYNAMIC /* Usage */, D1DVERTEX_FVF, D3DPOOL_DEFAULT, &g_pVB1, NULL ) ) )
return E_FAIL;
if( FAILED( g_pd3dDevice->CreateIndexBuffer( 30000 *sizeof(WORD),
D3DUSAGE_DYNAMIC, D3DFMT_INDEX16,
D3DPOOL_DEFAULT, &g_pIB1, NULL ) ) )
return E_FAIL;
if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "texture1.png", &g_Texture1))) {
MessageBox(NULL, "Failed to load 'texture1.png'", "Error", MB_OK);
return false;
}
ReadFromFile();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if(g_Texture1 != NULL) g_Texture1->Release();
if(g_pVB1 != NULL) g_pVB1->Release();
if(g_pIB1 != NULL) g_pIB1->Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation( &matWorld, 0,0,0 );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DXVECTOR3 vEyePt( 00, 1000.0f,500 );
D3DXVECTOR3 vLookatPt( 300.0f, 1480.0f, 00.0f );
D3DXVECTOR3 vUpVec(0.0f, 0.0f, 1.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 2000.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
HRESULT hr = g_pd3dDevice->SetTexture( 0, g_Texture1);
hr = g_pd3dDevice->SetStreamSource( 0, g_pVB1, 0, sizeof(D1DVERTEX_FVF) );
hr = g_pd3dDevice->SetFVF( D1DVERTEX_FVF );
hr = g_pd3dDevice->SetIndices( g_pIB1 );
hr = g_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, nCountVertices, 0, nCountIndices/60 );
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 02: Vertices",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the vertex buffer
if( SUCCEEDED( InitVB() ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}
}
UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}












