Archived

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

shakazed

Rotation not working to well....

Recommended Posts

Okay, this is my main .cpp file,
    
#include<windows.h>
#include"Direct3D.h"


//Globals//////////////////////////////////////////////////////////////////////////




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

HWND hWnd;
MSG msg;

cDGfx cDXGraphics; //Global d3d class



//Functions/////////////////////////////////////////////////////////////////////////


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

	case WM_PAINT:
		{
			cDXGraphics.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, IDI_APPLICATION);
	wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wClass.hInstance = hInstance;
	wClass.lpfnWndProc = WndProc;
	wClass.lpszMenuName = NULL;
	wClass.lpszClassName = WINDOW_CLASS_NAME;
	wClass.style = CS_VREDRAW|CS_HREDRAW;

	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;

	if(cDXGraphics.D3DInit(hWnd) == false)
		return 0;
	
	if(cDXGraphics.D3DTriangle() == false)
		return 0;


	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
		{	
			cDXGraphics.D3DRender();
		}

	}
	
	cDXGraphics.D3DRelease(); //Release all pointers used for d3d


	return 0;

}
  

And this is my class...

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


class cDGfx
{
public:
	bool D3DInit(HWND hWnd);
	void D3DRender(void);
	void D3DRelease(void);
	bool D3DTriangle(void);
	void D3DSetupMatrices(void);

private:
	IDirect3D8 *m_iD3D8;
	IDirect3DDevice8 *m_iD3DDevice8;
	IDirect3DVertexBuffer8 *m_iD3DVertBuf8;

	D3DDISPLAYMODE m_d3ddm;
	D3DPRESENT_PARAMETERS m_d3dpp;

	//Vertex format

	struct CUSTOMVERTEX 
	{
		float x,y,z,rhw;
		D3DCOLOR diffuse;
	};

	CUSTOMVERTEX m_sVertex[3];
};


//Function defs/////////////////////////////////////////////


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

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

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


}

bool cDGfx::D3DInit(HWND hWnd)
{
	m_iD3D8 = Direct3DCreate8(D3D_SDK_VERSION);
	if(m_iD3D8 == NULL)
		return false;

	ZeroMemory(&m_d3ddm, sizeof(m_d3ddm));
	m_d3ddm.Width = 640;
	m_d3ddm.Height = 480;
	m_d3ddm.RefreshRate = 0; //Use default

	m_d3ddm.Format = D3DFMT_R5G6B5;

	ZeroMemory(&m_d3dpp, sizeof(m_d3dpp));
	m_d3dpp.BackBufferFormat = m_d3ddm.Format;
	m_d3dpp.BackBufferWidth = m_d3ddm.Width;
	m_d3dpp.BackBufferHeight = m_d3ddm.Height;
	m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	m_d3dpp.Windowed = false;

	if(FAILED(m_iD3D8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&m_d3dpp, &m_iD3DDevice8)))
		return false;

	return true;
}

void cDGfx::D3DRelease(void)
{
	if(m_iD3D8 != NULL)
	{
		m_iD3D8->Release();
		m_iD3D8 = NULL;
	}

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

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

}

void cDGfx::D3DRender(void)
{
	m_iD3DDevice8->Clear(0,NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0,0,255,0),1.0f,0);

	m_iD3DDevice8->BeginScene();
	
	D3DSetupMatrices();
	m_iD3DDevice8->SetStreamSource(0, m_iD3DVertBuf8, sizeof(CUSTOMVERTEX));
	m_iD3DDevice8->SetVertexShader(D3DFVF_DIFFUSE|D3DFVF_XYZRHW);
	m_iD3DDevice8->DrawPrimitive(D3DPT_TRIANGLELIST,0, 1);

	m_iD3DDevice8->EndScene();

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

bool cDGfx::D3DTriangle(void) 
{
	//Fill out the structure with vertex data, x,y,z and color

	m_sVertex[0].x = 150.0f;
	m_sVertex[0].y = 50.0f;
	m_sVertex[0].z = 0.0f;
	m_sVertex[0].rhw = 1.0f;
	m_sVertex[0].diffuse = D3DCOLOR_RGBA(0,0,255,0);

	m_sVertex[1].x = 250.0f;
	m_sVertex[1].y = 250.0f;
	m_sVertex[1].z = 0.0f;
	m_sVertex[1].rhw = 1.0f;
	m_sVertex[1].diffuse = D3DCOLOR_RGBA(255,0,0,0);

	m_sVertex[2].x = 50.0f;
	m_sVertex[2].y = 250.0f;
	m_sVertex[2].z = 0.0;
	m_sVertex[2].rhw = 1.0f;
	m_sVertex[2].diffuse = D3DCOLOR_RGBA(0,255,9,0);

	if(FAILED(m_iD3DDevice8->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0,
		D3DFVF_DIFFUSE|D3DFVF_XYZRHW, D3DPOOL_DEFAULT, &m_iD3DVertBuf8)))
		return false;

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

	memcpy(ptr,m_sVertex, sizeof(m_sVertex));
	
	if(FAILED(m_iD3DVertBuf8->Unlock()))
		return false;

	return true;

}

    
Simply put this code won´t make my polygon rotate. Why? Answer me or I will start murdering like....tiny, white...........rabbits.
http://www.shakazed.tk [edited by - shakazed on July 27, 2002 6:45:27 PM]

Share this post


Link to post
Share on other sites
D3DFVF_XYZRHW means your vertex is already transformed (i.e. 2D) and therefore will not be affected by your transformation and rotation matrices. Try removing rhw from your vertex structure and changing your FVF to (D3DFVF_DIFFUSE|D3DFVF_XYZ).

Hope this helps!



"There are only three types of people in this world: those who can count, and those who can''t."

Just3D
Justin Nordin
J Squared Productions
www.jsquaredproductions.com

Share this post


Link to post
Share on other sites