Archived

This topic is now archived and is closed to further replies.

shakazed

Now, what´s wrong with this triangle rotation?

Recommended Posts

Hmm, I´ve been working on this for quite some while now and I just can´t get a break. This code SHOULD rotate a triangle.
    
#include<windows.h>
#include<d3d8.h>
#include<d3dx8.h>

#define MY_FVF D3DFVF_DIFFUSE|D3DFVF_XYZ

//GLOBALS/////////////////////////////////////////////////


struct CUSTOMVERTEX
{
	float x,y,z;
	DWORD color;
};

const char *WINDOW_CLASS_NAME = "Window Class";
const char *WINDOW_NAME = "My Window";
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

IDirect3D8 *iD3D8;
IDirect3DDevice8 *iD3DDevice8;
IDirect3DVertexBuffer8 *iD3DVertBuffer;

HWND hWnd;

//FUNCTIONS///////////////////////////////////////////////



bool D3DInit(void)
{
	iD3D8 = Direct3DCreate8(D3D_SDK_VERSION);
	
	D3DDISPLAYMODE d3ddm;

	if(FAILED(iD3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
		&d3ddm)))
		return false;
	
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.BackBufferFormat = d3ddm.Format;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.Windowed = true;

	if(FAILED(iD3D8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
		hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &iD3DDevice8)))
		return false;

	return true;
}

void D3DSetupMatrices(void)
{
	D3DXMATRIX matWorld;
	D3DXMatrixRotationY(&matWorld, timeGetTime()/150.0f);
	iD3DDevice8->SetTransform(D3DTS_WORLD, &matWorld);

	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 3.0f, -5.0f),
		&D3DXVECTOR3(150.0f, 50.0f, 0.0f),
		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
	iD3DDevice8->SetTransform(D3DTS_VIEW, &matView);

	D3DXMATRIX matProj;
	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f,1.0f, 100.0f);
	iD3DDevice8->SetTransform(D3DTS_PROJECTION, &matProj);
}


void D3DRender(void)
{
	iD3DDevice8->Clear(0, NULL ,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255),1.0f, 0 );
	
	iD3DDevice8->BeginScene();

	D3DSetupMatrices();
	
	iD3DDevice8->SetStreamSource(0, iD3DVertBuffer, sizeof(CUSTOMVERTEX));
	iD3DDevice8->SetVertexShader(MY_FVF);
	iD3DDevice8->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

	iD3DDevice8->EndScene();

	iD3DDevice8->Present(NULL, NULL, NULL, NULL);
}

void D3DRelease(void)
{
	if(iD3D8 != NULL)
	{
		iD3D8->Release();
		iD3D8 = NULL;
	}

	if(iD3DDevice8 != NULL)
	{
		iD3DDevice8->Release();
		iD3D8 = NULL;
	}
}


bool D3DTriangle(void)
{
	CUSTOMVERTEX sVertex[] = 
	{
		{150.0f, 50.0f, 0.5f, D3DCOLOR_XRGB(0,0,255),},
		{250.0f, 250.0f, 0.5f, D3DCOLOR_XRGB(255,0,0),},
		{50.0f, 250.0f, 0.5f, D3DCOLOR_XRGB(0,255,0),},
	};
	
	if(FAILED(iD3DDevice8->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0,
		MY_FVF, D3DPOOL_DEFAULT, &iD3DVertBuffer)))
		return false;

	VOID *ptr;

	if(FAILED(iD3DVertBuffer->Lock(0, sizeof(sVertex), (BYTE**)&ptr, 0)))
		return false;

	memcpy(ptr, sVertex, sizeof(sVertex));

	if(FAILED(iD3DVertBuffer->Unlock()))
		return false;


	return true;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, 
						 WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}break;
	case WM_PAINT:
		{
			D3DRender();
		}break;
	}

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

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wClass;

	wClass.cbClsExtra = 0;
	wClass.cbSize = sizeof(WNDCLASSEX);
	wClass.cbWndExtra = 0;
	wClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wClass.hInstance = hInstance;
	wClass.lpfnWndProc = WndProc;
	wClass.lpszMenuName = NULL;
	wClass.style = CS_HREDRAW|CS_VREDRAW;
	wClass.lpszClassName = WINDOW_CLASS_NAME;

	RegisterClassEx(&wClass);
	
	hWnd = CreateWindowEx(NULL,
		WINDOW_CLASS_NAME, WINDOW_NAME,
		WS_OVERLAPPEDWINDOW|WS_VISIBLE, 
		100,100,WINDOW_WIDTH,WINDOW_HEIGHT, 
		NULL,
		NULL,
		hInstance,
		NULL);

	if(hWnd == NULL)
		return 0;

	MSG msg;

	D3DInit();
	D3DTriangle();
	D3DRender();

	while(1)
	{
		if(PeekMessage(&msg, NULL, 0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;
			if(GetAsyncKeyState(VK_ESCAPE))
				break;
	

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			D3DRender();
		}
	}

	D3DRelease();
	return 0;

}
    
Although the triangle renders when I don´t use matrixtransformation it gets all screwed up when I do and dissapears. Any help appreciated.
http://www.shakazed.tk [edited by - shakazed on July 28, 2002 6:03:09 PM]

Share this post


Link to post
Share on other sites
I''ve gone through the source you provided and found that the errors weren''t in the transformations. Well, actually that was kinda obvious at first glance. Here''s the corrected source. The ZBuffer is enabled too now. Anyway, wherever you see a /**/ in the source, you know I changed it there. Here''s the edited source. Be sure to include d3d8.lib, d3dx8.lib and dxguid.lib


  
#include<windows.h>
#include<d3d8.h>
#include<d3dx8.h>

#define MY_FVF D3DFVF_DIFFUSE|D3DFVF_XYZ
//GLOBALS/////////////////////////////////////////////////

struct CUSTOMVERTEX
{
float x,y,z;
DWORD color;
};

const char *WINDOW_CLASS_NAME = "Window Class";
const char *WINDOW_NAME = "My Window";
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

IDirect3D8 *iD3D8;
IDirect3DDevice8 *iD3DDevice8;
IDirect3DVertexBuffer8 *iD3DVertBuffer;
HWND hWnd;

//FUNCTIONS///////////////////////////////////////////////

bool D3DInit(void)
{
iD3D8 = Direct3DCreate8(D3D_SDK_VERSION);
D3DDISPLAYMODE d3ddm;
if(FAILED(iD3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)))
return false;

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
/**/
d3dpp.Windowed=true;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat=d3ddm.Format;
d3dpp.EnableAutoDepthStencil=true;
d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
if(FAILED(iD3D8->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&iD3DDevice8)))
return false; return true;
}
void D3DSetupMatrices(void)
{
D3DXMATRIX matWorld;
D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationY(&matWorld,timeGetTime()/150.0f);
iD3DDevice8->SetTransform(D3DTS_WORLD, &matWorld);
D3DXMATRIX matView;
/**/
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, -50.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
iD3DDevice8->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIX matProj;
/**/
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4,(float)WINDOW_WIDTH/(float)WINDOW_HEIGHT,1.0f, 100.0f);
iD3DDevice8->SetTransform(D3DTS_PROJECTION, &matProj);
}
void D3DRender(void)
{
/**/iD3DDevice8->SetRenderState(D3DRS_LIGHTING,false);
/**/iD3DDevice8->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
/**/iD3DDevice8->Clear(0, NULL ,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255),1.0f, 0 );
//this should be done before calling begin scene because stuff between begin scene and
//end scene should be just rendering info. also, the view and projection matrices do not
//change in your app so they should be created at run time, not here.
D3DSetupMatrices();
iD3DDevice8->BeginScene();
iD3DDevice8->SetStreamSource(0, iD3DVertBuffer, sizeof(CUSTOMVERTEX));
iD3DDevice8->SetVertexShader(MY_FVF);
iD3DDevice8->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
iD3DDevice8->EndScene();
iD3DDevice8->Present(NULL, NULL, NULL, NULL);
}
void D3DRelease(void)
{
if(iD3D8 != NULL)
{
iD3D8->Release();
iD3D8 = NULL;
}
if(iD3DDevice8 != NULL)
{
iD3DDevice8->Release();
iD3D8 = NULL;
}
}
bool D3DTriangle(void)
{
/**/

CUSTOMVERTEX sVertex[] = { {-10.0f, -5.0f, 0.0f, D3DCOLOR_XRGB(0,0,255),},
{0.0f, 5.0f, 0.0f, D3DCOLOR_XRGB(255,0,0),},
{10.0f, -5.0f, 0.0f, D3DCOLOR_XRGB(0,255,0),}, };

if(FAILED(iD3DDevice8->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0,
MY_FVF, D3DPOOL_DEFAULT, &iD3DVertBuffer)))
return false;

VOID *ptr;
if(FAILED(iD3DVertBuffer->Lock(0, sizeof(sVertex), (BYTE**)&ptr, 0)))
return false;
memcpy(ptr, sVertex, sizeof(sVertex));
if(FAILED(iD3DVertBuffer->Unlock()))
return false;

return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}break;
case WM_PAINT:
{
D3DRender();
}break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wClass;
wClass.cbClsExtra = 0;
wClass.cbSize = sizeof(WNDCLASSEX);
wClass.cbWndExtra = 0;
wClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wClass.hInstance = hInstance;
wClass.lpfnWndProc = WndProc;
wClass.lpszMenuName = NULL;
wClass.style = CS_HREDRAW|CS_VREDRAW;
wClass.lpszClassName = WINDOW_CLASS_NAME;

RegisterClassEx(&wClass);

hWnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW|WS_VISIBLE,
100,100,WINDOW_WIDTH,WINDOW_HEIGHT,
NULL, NULL, hInstance,
NULL);

if(hWnd == NULL)
return 0;

MSG msg;
D3DInit();
D3DTriangle();
D3DRender();
while(1)
{
if(PeekMessage(&msg, NULL, 0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
if(GetAsyncKeyState(VK_ESCAPE))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
} else
{
D3DRender();
}

}
D3DRelease();

return 0;
}



---
My Site
Come join us on IRC in #directxdev @ irc.afternet.org

Share this post


Link to post
Share on other sites