can't make this work
i want to draw a triangle but i can't. Can anyone help me?
This is the code:
bool Setup()
{
Device->CreateVertexBuffer(3*sizeof(Vertex),0,D3DFVF_XYZ,D3DPOOL_MANAGED,&Triunghi,0);
Vertex* vertices;
Triunghi->Lock(0,0,(void**)&vertices,0);
vertices[0] = Vertex(-1.0f, 0.0f, 1.0f);
vertices[1] = Vertex( 0.0f, 1.0f, 1.0f);
vertices[2] = Vertex( 1.0f, 0.0f, 1.0f);
Triunghi->Unlock();
// position and aim the camera
D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 target(-.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
// set projection matrix
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)800 / (float)600,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
return true;
}
bool Display()
{
if(Device)
{
Device->Clear(0,0,D3DDEVTYPE_HAL,0x00000000,1.0f,0);
Device->BeginScene();
Device->SetStreamSource(0,Triunghi,0,sizeof(Vertex));
Device->SetFVF( Vertex::FVF );
Device->DrawPrimitive(D3DPT_TRIANGLELIST,0,4);
Device->EndScene();
Device->Present(0,0,0,0);
}
return true;
}
What does "can't" mean? The code doesn't compile? It crashes? You're unable to work your compiler/IDE?
What exactly do you need help with?
What exactly do you need help with?
This looks wrong to me:
Device->Clear(0,0,D3DDEVTYPE_HAL,0x00000000,1.0f,0);
I suspect that D3DDEVTYPE_HAL happens to have the same value as D3DCLEAR_TARGET, which is what you should have in there. If you have a Z-buffer, you'll need to clear that too (By changing the line to Device->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0f,0);).
Do you have a Z buffer? Any output from the Debug Runtimes? What does your vertex struct look like? What is Vertex::FVF?
Device->Clear(0,0,D3DDEVTYPE_HAL,0x00000000,1.0f,0);
I suspect that D3DDEVTYPE_HAL happens to have the same value as D3DCLEAR_TARGET, which is what you should have in there. If you have a Z-buffer, you'll need to clear that too (By changing the line to Device->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0f,0);).
Do you have a Z buffer? Any output from the Debug Runtimes? What does your vertex struct look like? What is Vertex::FVF?
i only used an IDirect3DVertexBuffer9*; The Vertex look's like this:
struct Vertex
{
Vertex(float x, float y, float z)
{
_x=x; _y=y; _z=z;
}
float _x, _y, _z;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;
I don't think that i used a zbuffer. I'm tring to draw a triangle reading from this book: "introduction to 3d game programming with directx 9". I'm lost at this moment.
struct Vertex
{
Vertex(float x, float y, float z)
{
_x=x; _y=y; _z=z;
}
float _x, _y, _z;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;
I don't think that i used a zbuffer. I'm tring to draw a triangle reading from this book: "introduction to 3d game programming with directx 9". I'm lost at this moment.
This also looks wrong actually:
Device->DrawPrimitive(D3DPT_TRIANGLELIST,0,4);
You're trying to draw 4 primitives, but you only have one in your vertex buffer. Try changing the last parameter to 1 - also, set up the debug runtimes, this is the sort of thing they'd shout about.
Device->DrawPrimitive(D3DPT_TRIANGLELIST,0,4);
You're trying to draw 4 primitives, but you only have one in your vertex buffer. Try changing the last parameter to 1 - also, set up the debug runtimes, this is the sort of thing they'd shout about.
And the debug runtimes? Anything from them? Can we see your full code (In [ source][ /source] tags please)?
what are debug runtimes? the output window? I work with visual studio 2005.
Main.cpp :
directx.h:
directx.cpp:
Main.cpp :
#include <windows.h>#include "directx.h"//GLOBALSIDirect3DDevice9* Device = 0;IDirect3DVertexBuffer9* VB = 0;D3DXMATRIX WorldMatrix;//structstruct Vertex{ Vertex(){} Vertex(float x, float y, float z) { _x=x; _y=y; _z=z; } float _x, _y, _z; static const DWORD FVF;};const DWORD Vertex::FVF = D3DFVF_XYZ;//functionsbool Setup(){ Device->CreateVertexBuffer(3*sizeof(Vertex),D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&VB,0); Vertex* vertices; VB->Lock(0,0,(void**)&vertices,0); vertices[0] = Vertex(-10.0f, 0.0f, 1.0f); vertices[1] = Vertex( 0.0f, 10.0f, 1.0f); vertices[2] = Vertex( 10.0f, 0.0f, 1.0f); VB->Unlock(); // position and aim the camera D3DXVECTOR3 position(0.0f, 0.0f, -5.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMATRIX V; D3DXMatrixLookAtLH(&V, &position, &target, &up); Device->SetTransform(D3DTS_VIEW, &V); D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, // 90 - degree (float)800/ (float)600, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); return true;}bool Display(){ if(Device) { Device->Clear(0,0,D3DCLEAR_TARGET,0x00000000,1.0f,0); Device->BeginScene(); Device->SetFVF( Vertex::FVF ); Device->SetStreamSource(0,VB,0,sizeof(Vertex)); Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); Device->EndScene(); Device->Present(0,0,0,0); } return true;}bool Cleanup(){ VB->Release(); return true;}//MAINint WINAPI WinMain( HINSTANCE hInstance, HINSTANCE prevInstance , PSTR cmdline , int showCmd ){ if(!d3d::D3DInit(hInstance,800,600,true,D3DDEVTYPE_HAL,&Device)) { ::MessageBox(0,"init d3d - FAILED",0,0); } if(!Setup()) { ::MessageBox(0,"init d3d - FAILED",0,0); } MSG msg; while(TRUE) { while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } if(msg.message == WM_QUIT) break; Display(); } Cleanup(); Device->Release(); return 0;};
directx.h:
#ifndef __d3dUtilityH__#define __d3dUtilityH__#include <d3dx9.h>namespace d3d{ bool D3DInit( HINSTANCE hInstance, int width, int height, bool windowed, D3DDEVTYPE deviceType, IDirect3DDevice9** device); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); }#endif
directx.cpp:
#include "directx.h"bool d3d::D3DInit( HINSTANCE hInstance, int width, int height, bool windowed, D3DDEVTYPE deviceType, IDirect3DDevice9** device){ WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(0, IDI_APPLICATION); wc.hCursor = LoadCursor(0, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = 0; wc.lpszClassName = "Direct3D9App"; if( !RegisterClass(&wc) ) { MessageBox(0, "RegisterClass() - FAILED", 0, 0); return false; } HWND hwnd = 0; hwnd = CreateWindow("Direct3D9App", "Direct3D9App", WS_EX_TOPMOST, 0, 0, width, height, 0 , 0 , hInstance, 0); if( !hwnd ) { MessageBox(0, "CreateWindow() - FAILED", 0, 0); return false; } ShowWindow(hwnd, SW_SHOW); HRESULT hr = 0; IDirect3D9* d3d9 = 0; d3d9 = Direct3DCreate9(D3D_SDK_VERSION); if( !d3d9 ) { ::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0); return false; } D3DCAPS9 caps; d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps); int vp = 0; if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; D3DPRESENT_PARAMETERS d3dpp; d3dpp.BackBufferWidth = width; d3dpp.BackBufferHeight = height; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.MultiSampleQuality = 0; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hwnd; d3dpp.Windowed = windowed; d3dpp.EnableAutoDepthStencil = true; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.Flags = 0; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; hr = d3d9->CreateDevice( D3DADAPTER_DEFAULT, // primary adapter deviceType, // device type hwnd, // window associated with device vp, // vertex processing &d3dpp, // present parameters device); // return created device if( FAILED(hr) ) { // try again using a 16-bit depth buffer d3dpp.AutoDepthStencilFormat = D3DFMT_D16; hr = d3d9->CreateDevice( D3DADAPTER_DEFAULT, deviceType, hwnd, vp, &d3dpp, device); if( FAILED(hr) ) { d3d9->Release(); // done with d3d9 object ::MessageBox(0, "CreateDevice() - FAILED", 0, 0); return false; } } d3d9->Release(); // done with d3d9 object return true;}LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){ switch( msg ) { case WM_DESTROY: ::PostQuitMessage(0); break; case WM_KEYDOWN: if( wParam == VK_ESCAPE ) ::DestroyWindow(hwnd); break; } return ::DefWindowProc(hwnd, msg, wParam, lParam);}
Quote:Original post by jakussanoClicky
what are debug runtimes? the output window? I work with visual studio 2005.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement