PDA

View Full Version : giúp đỡ-gấp lắm



ngocquang19877
06-11-2003, 04:23
theo yêu cầu,em đã post source lên đây.xin mọi người giúp đỡ em.tại seo em vẽ 2 tam giác riêng biệt kô được vậy???mọi người chỉ cần xem hàm render và initvb là được rùi.khỏi cân quan tâm tới header file vì nó chỉ chứa nguyên mẫu hàm


#include "Header.h"
int z=1;
int m=0;
char string[6]="hello";
Object3d::Object3d()
{
g_pD3D = NULL; // Used to create the D3DDevice
g_pd3dDevice = NULL; // Our rendering device
g_pVB = NULL;
g_font= NULL ;
}
Object3d::~Object3d()
{
if( g_pVB != NULL )
g_pVB->Release();

if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();

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

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

void Object3d::CreateElipse()
{for(int r=0;r<v_count;r++)
{
m++;
vertices[r].x=tx*cos(m);
vertices[r].y=tx*sin(m);
vertices[r].z=0.0f;
vertices[r].color=RGB(0,255,0);
vertices[r].rhw=1.0f;
}
tx+=5;
if(tx>700)
{tx=200;}
}

void Object3d::CreateLine()
{for(int r=0;r<v_count;r++)
{

vertices[r].x+=5;
vertices[r].y=200;
vertices[r].z=0.0f;
vertices[r].color=RGB(0,255,0);
vertices[r].rhw=1.0f;
}
}


HRESULT Object3d::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;
}
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE,TRUE);
g_pd3dDevice->SetRenderState(D3DRS_AMBIENT,0xfff);
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,TRUE);
CreateFont( 22, //Font height
0, //Font width
FW_NORMAL, //Font Weight
1, //MipLevels
false, //Italic
DEFAULT_CHARSET, //CharSet
OUT_DEFAULT_PRECIS, //OutputPrecision
ANTIALIASED_QUALITY, //Quality
DEFAULT_PITCH|FF_DONTCARE,//PitchAndFamily
"Arial", //pFacename,
&g_font);
// Device state would normally be set here

return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InitVB()
// Desc: Creates a vertex buffer and fills it with our vertices. The vertex
// buffer is basically just a chuck of memory that holds vertices. After
// creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
// also uses index buffers. The special thing about vertex and index
// buffers is that they can be created in device memory, allowing some
// cards to process them in hardware, resulting in a dramatic
// performance gain.
//-----------------------------------------------------------------------------
HRESULT Object3d::InitVB()
{
z++;
if(z<50)
{
CreateLine();
}
if(z>50)
{
if(z==100)
{z-=100;}
CreateElipse();
}




if( FAILED( g_pd3dDevice->CreateVertexBuffer( (v_count+2)*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}

// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, vertices, sizeof(vertices) );
g_pVB->Unlock();

return S_OK;
}







//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Object3d::Render()
{

RECT font_rect;
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 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).
//ppFon
SetRect(&font_rect,0,30,200,200);
g_font->DrawText( string, //pString
1, //Count
&font_rect, //pRect
DT_LEFT|DT_WORDBREAK, //Format,
0xFFFFFFFF); //Color

g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitiveUP( D3DPT_POINTLIST ,(v_count-200),vertices,sizeof(CUSTOMVERTEX));

// 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:
PostQuitMessage( 0 );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}

Object3d hinhchunhat;


//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
WNDCLAS*** wc = { sizeof(WNDCLAS***), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClas***( &wc );

// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 02: Vertices",
NULL, 100, 100, 400, 400,
GetDesktopWindow(), NULL, wc.hInstance, NULL );

// Initialize Direct3D
if( SUCCEEDED( hinhchunhat.InitD3D( hWnd ) ) )
{
// Create the vertex buffer

// 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
{
hinhchunhat.InitVB() ;
hinhchunhat.Render();
}
}
}

UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}

ngocquang19877
11-11-2003, 07:02
chà.chả có ai chịu giùp đỡ hết,bó tay.may mà em cũng đã giải quyết được rùi.