Unworking matrix transformations ?!

Started by
6 comments, last by White Scorpion 19 years, 6 months ago
Hi everyone ! I'm now learning how to rotate the world and set the viewport and projection but I noticed that nothing of that gets done properly... Even if I put extreme values, there's no difference and the shape doesn't rotate, why ? Here's all the code:
#include <windows.h>
#include "dx_funcs.h"
#include "dx_vertex.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

char g_Title[] = "Direct 3D v8 Test";
char g_Class[] = "D3D";

int __stdcall WinMain(HINSTANCE hPrevInstance,
					  HINSTANCE hInst,
					  LPSTR lpszCmdLine,
					  int nCmdShow)
{
	WNDCLASSEX wc;
	ZeroMemory(&wc, sizeof(wc));
	wc.cbSize			= sizeof(wc);
	wc.hbrBackground	= (HBRUSH)(BLACK_BRUSH);
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);
	wc.hIconSm			= LoadIcon(NULL, IDI_WINLOGO);
	wc.hInstance		= hInst;
	wc.lpfnWndProc		= WndProc;
	wc.lpszClassName	= g_Class;

	if(!RegisterClassEx(&wc))
	{
		MessageBox(0, "Class registration failed", g_Title, MB_ICONEXCLAMATION);
		return 0;
	}

	HWND hWnd = CreateWindow(
		g_Class, 
		g_Title, 
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		HWND_DESKTOP,
		NULL,
		hInst,
		NULL);

	if(hWnd == INVALID_HANDLE_VALUE)
	{
		MessageBox(0, "Window creation failed", g_Title, MB_ICONEXCLAMATION);
		UnregisterClass(g_Class, hInst);
		return 0;
	}

	UpdateWindow(hWnd);
	ShowWindow(hWnd, nCmdShow);

	if(FAILED(dx_InitializeD3D(hWnd)))
	{
		MessageBox(0, "DirectX initialization failed", g_Title, MB_ICONEXCLAMATION);
		UnregisterClass(g_Class, hInst);
		return 0;
	}

	if(FAILED(dx_InitializeVertexBuffer()))
		MessageBox(0, 0, 0, 0);

	MSG msg;
	ZeroMemory(&msg, sizeof(msg));
	while(msg.message != WM_QUIT)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
			dx_Render();
	}
	
	dx_Cleanup();
	UnregisterClass("g_Class", hInst);
	return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_QUIT:
	case WM_DESTROY:
	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, msg, wParam, lParam);
		break;
	}
	return 0;
}
#ifndef _DX_FUNCS_H_
#define _DX_FUNCS_H_

#pragma comment(lib, "D3d8.lib")
#pragma comment(lib, "D3dx8.lib")
#pragma comment(lib, "Winmm.lib")
#include <d3d8.h>
#include <d3dx8math.h>
#include <windows.h>

const UINT D3DFVF_CUSTOMVERTEX = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;

struct CustomVertex
{
	float x, y, z, rhw;
	DWORD color;
};


extern 	IDirect3D8* g_D3D;
extern 	IDirect3DDevice8* g_Device;
extern 	IDirect3DVertexBuffer8* g_VertexBuffer;
extern 	D3DDISPLAYMODE g_DispMode;
extern 	D3DPRESENT_PARAMETERS g_Params;
extern 	CustomVertex g_Vertices[];

HRESULT dx_InitializeD3D(HWND);
HRESULT dx_InitializeVertexBuffer();
void dx_SetProjection();
void dx_SetView();
void dx_Rotate();
void dx_Render();
void dx_Cleanup();

#endif
#include "dx_funcs.h"

IDirect3D8*					g_D3D		= NULL;
IDirect3DDevice8*			g_Device	= NULL;
IDirect3DVertexBuffer8*		g_VertexBuffer	= NULL;
D3DDISPLAYMODE				g_DispMode;
D3DPRESENT_PARAMETERS		g_Params;

CustomVertex g_Vertices[] = 
{
	{ 5.0f, 150.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0) },
	{ 5.0f, 5.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,255,0) },
	{ 155.0f, 150.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255) },
	{ 155.0f, 5.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,255,255) }
};

HRESULT dx_InitializeD3D(HWND hWnd)
{	
	if((g_D3D = Direct3DCreate8(D3D_SDK_VERSION)) == NULL)
	{
		return E_FAIL;
	}

	if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &g_DispMode)))
	{
		return E_FAIL;
	}

	ZeroMemory(&g_Params, sizeof(g_Params));
	g_Params.BackBufferFormat = g_DispMode.Format;
	g_Params.Windowed = TRUE;
	g_Params.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
	
	if(FAILED(g_D3D->CreateDevice(
						D3DADAPTER_DEFAULT, 
						D3DDEVTYPE_HAL, 
						hWnd,							
						D3DCREATE_SOFTWARE_VERTEXPROCESSING,
						&g_Params,
						&g_Device)))
	{
		return E_FAIL;
	}

	return S_OK;
}

HRESULT dx_InitializeVertexBuffer()
{
	if(FAILED(g_Device->CreateVertexBuffer(sizeof(CustomVertex)*3,
											0, D3DFVF_CUSTOMVERTEX,
											D3DPOOL_DEFAULT, &g_VertexBuffer)))
	{
		return E_FAIL;
	}

	VOID* pVertices;
	if(FAILED(g_VertexBuffer->Lock(0, sizeof(g_Vertices), (BYTE**)&pVertices, 0)))
	{
		return E_FAIL;
	}
	memcpy(pVertices, g_Vertices, sizeof(g_Vertices));
	g_VertexBuffer->Unlock();
	return S_OK;
}

void dx_Render()
{
	g_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

	g_Device->BeginScene();

	dx_SetProjection();
	dx_SetView();
	dx_Rotate();

	g_Device->SetStreamSource(0, g_VertexBuffer, sizeof(CustomVertex));
	g_Device->SetVertexShader(D3DFVF_CUSTOMVERTEX);
	g_Device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

	g_Device->EndScene();

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

void dx_Cleanup()
{
	if(g_Device != NULL)
	{
		g_Device->Release();
		g_Device = NULL;
	}
	if(g_D3D != NULL)
	{
		g_D3D->Release();
		g_D3D = NULL;
	}
	if(g_VertexBuffer != NULL)
	{
		g_VertexBuffer->Release();
		g_VertexBuffer = NULL;
	}
}

void dx_SetProjection()
{
	D3DXMATRIX matProj;
	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 30.0f);
	g_Device->SetTransform(D3DTS_PROJECTION, &matProj);
}

void dx_SetView()
{
	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, -5.0f), // camera pos
								 &D3DXVECTOR3(0.0f, 0.0f, 0.0f),  // look-at pos
								 &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); // up direction
	g_Device->SetTransform(D3DTS_VIEW, &matView);
}

void dx_Rotate()
{
	static D3DXMATRIX matWorld;
	static UINT degree = 0;
	if(degree == 360)
		degree = 0;
	D3DXMatrixRotationY(&matWorld, timeGetTime()/150.0f); 
	g_Device->SetTransform(D3DTS_WORLD, &matWorld);
}
Thank you guys.
Advertisement
XYZRHW means "pre-transformed" which means DX will skip transforming, lighting, viewport scaling, etc and just draw to the pixel locations you've specified. You'll want XYZ coords (drop the 1.0f from the data, change the FVF, and change the stride of your streams)
Ok well I did this and the square disappeared... I then thought of giving it extreme values to see if it was hid but looks like it's not... I gave it a range from -500.0f to 500.0f, wich should cover the whole screen ( or nearly ) but I still only see the black background, what's the problem ?
CustomVertex g_Vertices[] = {	{ -1.0f, -1.0f, 0.5f, D3DCOLOR_XRGB(255,0,0) },	{ -1.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(0,255,0) },	{ 0.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(0,0,255) },	{ 0.0f, -1.0f, 0.5f, D3DCOLOR_XRGB(255,255,255) }};
Doesn't work neither.
Try setting D3DRS_LIGHTING, false. You've probably left lighting on and are drawing black (you have no lights) objects on a black background.
How would I do that ?
g_Device->SetRenderState(D3DRS_LIGHTING, false);
That was it !! Thank you man !! :D
(PS: I found out how to turn lights off)

This topic is closed to new replies.

Advertisement