Sign in to follow this  
jakussano

can't make this work

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
what are debug runtimes? the output window? I work with visual studio 2005.

Main.cpp :

#include <windows.h>
#include "directx.h"
//GLOBALS
IDirect3DDevice9* Device = 0;
IDirect3DVertexBuffer9* VB = 0;
D3DXMATRIX WorldMatrix;


//struct
struct 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;

//functions
bool 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;
}

//MAIN
int 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);
}

Share this post


Link to post
Share on other sites
OMG HUME AMOUNT OF ERRORS:)) I've restarted visual;

output says:
Direct3D9: (ERROR) :Stream 0 does not have required number of vertices

First-chance exception at 0x7c812a7b in directx.exe: Microsoft C++ exception: long at memory location 0x0012fbf8..
Direct3D9: (ERROR) :DrawPrimitive failed.

are hundres of these:P

Share this post


Link to post
Share on other sites

const DWORD Vertex::FVF = D3DFVF_XYZ;



Your FVF is just positions, no colour/texture coordinates/etc. What were you expecting it to draw?

Try adding a color component to your vertex structure and adding D3DFVF_DIFFUSE to your FVF.


struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z, DWORD color)
{
_x=x; _y=y; _z=z; _color=color;
}
float _x, _y, _z;
DWORD _color;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;



The format for the color is AARRGGBB (alpha, red, green, blue). Try 0xffffffff for white for now:


vertices[0] = Vertex(-10.0f, 0.0f, 1.0f, 0xffffffff);
vertices[1] = Vertex( 0.0f, 10.0f, 1.0f, 0xffffffff);
vertices[2] = Vertex( 10.0f, 0.0f, 1.0f, 0xffffffff);



Alan

Share this post


Link to post
Share on other sites
Just a couple of things I noticed from your code.

1) You are rendering the triangle as wireframe, so it will be see-through
2) Your vertexdata only contains XYZ coordinates and no vertex color information, I'm not sure what the default color of the triangle will be when not defined, but if it defaults to black you will be in trouble since you are clearing the framebuffer to black as well.

Change the fillmode so it will draw a solid triangle.
Device->SetRenderState(D3DFILLMODE, D3DFILL_SOLID);


Try clearing the framebuffer to white instead and maybe a black triangle will pop up, or if your screen is still black your triangle is so large that it fills the entire screen.
Device->Clear(0,0,D3DCLEAR_TARGET,0xFFFFFFFF,1.0f,0);

Share this post


Link to post
Share on other sites
i changed the background; result: half of the window black- half white :)) now i'm tring diferent positions of camera;

worked! the triangle is black and the background is white. strange because alan put 0xffffffff; Anyway thanks! now i can play with colors :)

Thanks a lot guys!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this