Sign in to follow this  
SSJCORY

Yikes this triangle drawing thing aint so easy. What'd i do wrong?

Recommended Posts

Just tried to add triangles didn't work... Plz help. Source:
#include<d3d8.h>
#include<d3dx8.h>
#pragma comment(lib,"d3d8.lib")
#pragma comment(lib,"d3dx8.lib")
//Struct holding vertex info
struct vertex{
	int r,g,b,rhw;
	DWORD color;
};
#define vertexfmt (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
//Our triangles vertices
vertex trivertex[] = {
	{50,250,1,0,0xFFFFFFFF},
	{100,0,1,0,0xFFFFFFFF},
	{150,250,1,0,0xFFFFFFFF}
};

//All the function prototypes
void AskFullscreen();
void InitWindow();
void InitD3D();
void KillD3D();
void KillWindow();
void InitScene();
void Render();
void MessagePump();
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam, LPARAM lParam);
//DirectGraphics Interfaces
IDirect3D8 *pd3d = NULL;
IDirect3DDevice8 *pd3dd = NULL;
IDirect3DVertexBuffer8 *ptrianglevb = NULL;
//Windows Handle and Instance
HWND ghwnd;
HINSTANCE ginstance = NULL;
//Window Variables
char windowname[] = "My Window";
char windowclass[] = "My Window Class";
int windowheight = 480;
int windowwidth = 640;
//Fullscreen Boolean
bool fullscreen = false;
//Color Values
unsigned int red = 255,blue = 0,green = 0;
//Quit Variable
bool appdone = false;
//Main
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE pInstance, PSTR cmdLine,int iCmd){
	ginstance = hInstance;
	AskFullscreen();
	InitWindow();
	InitD3D();
	InitScene();
	while(appdone == false){
		MessagePump();
		Render();
	}
	KillD3D();
	KillWindow();
	return 0;
}
//Ask if user would prefer fullscreen mode
void AskFullscreen(){
	int messageanswer = 0;
	messageanswer = MessageBox(NULL,"Would you like to run fullscreen?","FULLSCREEN?",MB_YESNOCANCEL);
	switch(messageanswer){
	case IDYES:
		fullscreen = true;
		break;
	case IDNO:
		fullscreen = false;
		break;
	case IDCANCEL:
		exit(5);
		break;
	}
}
//Create the window
void InitWindow(){
	DWORD style;
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = NULL;
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = ginstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = windowclass;
	wc.lpszMenuName = NULL;
	wc.style = CS_OWNDC;
	RegisterClass(&wc);
	if(fullscreen){
		windowwidth = GetSystemMetrics(SM_CXSCREEN);
		windowheight = GetSystemMetrics(SM_CYSCREEN);
		style = WS_POPUP;
	}
	else{
		style = WS_OVERLAPPEDWINDOW|WS_SYSMENU;
	}
	ghwnd = CreateWindow(windowclass,windowname,style,0,0,windowwidth,windowheight,NULL,NULL,ginstance,NULL);
	ShowWindow(ghwnd,SW_SHOW);
	UpdateWindow(ghwnd);
}
//Setup the display mode and present parameters
void InitD3D(){
	D3DPRESENT_PARAMETERS d3dpp;
	D3DDISPLAYMODE d3ddm;
	pd3d = Direct3DCreate8(D3D_SDK_VERSION);
	pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
	ZeroMemory(&d3dpp,sizeof(d3dpp));
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow = ghwnd;
	d3dpp.BackBufferCount = 1;
	if(fullscreen){
		d3dpp.Windowed = FALSE;
		d3dpp.BackBufferHeight = windowheight;
		d3dpp.BackBufferWidth = windowwidth;
		d3dpp.BackBufferFormat = d3ddm.Format;
	}
	else{
		d3dpp.Windowed = TRUE;
		d3dpp.BackBufferFormat = d3ddm.Format;
	}
	pd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,ghwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dd);
}
//Release interfaces
void KillD3D(){
	if(pd3d){
		pd3d->Release();
		pd3d = NULL;
	}
	if(pd3dd){
		pd3dd->Release();
		pd3dd = NULL;
	}
}
//DestroyWindow
void KillWindow(){
	DestroyWindow(ghwnd);
	ghwnd = NULL;
	UnregisterClass("My Window Class",ginstance);
}
//Render our colored screens
void Render(){
	pd3dd->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(red,green,blue),1.0f,0);
	pd3dd->BeginScene();
	pd3dd->SetStreamSource(0,ptrianglevb,sizeof(vertex));
	pd3dd->SetVertexShader(vertexfmt);
	pd3dd->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
	pd3dd->EndScene();
	pd3dd->Present(NULL,NULL,NULL,NULL);
}
//Send and receive messages
void MessagePump(){
	MSG msg;
	if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}
//Initialize the scene
void InitScene(){
	pd3dd->CreateVertexBuffer(sizeof(vertex)*3,D3DUSAGE_WRITEONLY,vertexfmt,D3DPOOL_MANAGED,&ptrianglevb);
	BYTE *ptr;
	ptrianglevb->Lock(0,0,(BYTE**)&ptr,0);
	memcpy(ptr,trivertex,sizeof(trivertex));
	ptrianglevb->Unlock();
}

	
//Process the windows messages
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	switch(iMsg){
	case WM_DESTROY:    //If user wants to quit let them
		appdone = true;
		PostQuitMessage(0);
		break;
	case WM_KEYDOWN:
		switch(wParam){ //Process keypresses
		case 0x52://r
			red = 255;
			blue = 0;
			green = 0;
			break;
		case 0x47://g
			red = 0;
			blue = 0;
			green = 255;
			break;
		case 0x42://b
			red = 0;
			blue = 255;
			green = 0;
			break;
		case VK_ESCAPE://if user presses esc quit
			appdone = true;
			break;
		}
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

Share this post


Link to post
Share on other sites
Well first of all,

struct vertex{
float r,g,b,rhw;
DWORD color;
};

D3D expects floating point, and the storage of ints and floats is drastically different.

Second of all, check lighting, culling (though your triangle is winding the right direction), texture stage states, ...

Share this post


Link to post
Share on other sites
Quote:
Original post by Pipo DeClown
RHW should be 1, because the vertex is already transformed.


In addition, some cards/drivers discard the fragments if z is defined exactly 0.0 or 1.0 (because of rounding errors). To correct this, adjust the z to be definitely inside the range (0.99999 for example).

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