• Advertisement
Sign in to follow this  

Code compiles but doesnt render anything.

This topic is 4617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im using this code:
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>

HWND MainWindowHandle = 0;

IDirect3D9* d3d9;
IDirect3DDevice9* d3ddevice = 0;
IDirect3DVertexBuffer9* VB = 0;
IDirect3DIndexBuffer9* IB = 0;

struct MyVertex
{
	MyVertex(){}
	MyVertex(float x, float y, float z)
	{
		x = x;
		y = y;
		z = z;
	}
	float x, y, z;
	static const DWORD FVF;
};

const DWORD MyVertex::FVF = D3DFVF_XYZ;

bool InitWindowApp(HINSTANCE instanceHandle, int Show);
bool StartDX();
bool InitGeo();

int Run();

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd)
{
	if(!InitWindowApp(hInstance, nShowCmd))
	{
		::MessageBox(0, "Failed to create Window!", "Fail", MB_OK);
		return 0;
	}

	
	if(StartDX() == false)
	{
		::MessageBox(0, "NoDX", "NoDX", MB_OK);
		return 0;
	}

	if(InitGeo() == false)
	{
		::MessageBox(0, "NoGgeo", "", MB_OK);
		return 0;
	}

	return Run();
}

bool InitWindowApp(HINSTANCE instanceHandle, int Show)
{
	WNDCLASS wc;

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = instanceHandle;
	wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
	wc.hCursor = ::LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = (HBRUSH) ::GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = 0;
	wc.lpszClassName = "MyWindow";

	if(!::RegisterClass(&wc))
	{
		::MessageBox(0, "Failed to register Window!", "Fail", MB_OK);
		return false;
	}

	MainWindowHandle = ::CreateWindow("MyWindow", "MyWindow", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, instanceHandle, 0);

	if(MainWindowHandle == 0)
	{
		::MessageBox(0, "Failed to create Window!", "Fail", MB_OK);
		return false;
	}

	::ShowWindow(MainWindowHandle, Show);
	::UpdateWindow(MainWindowHandle);

	return true;
}

bool StartDX()
{
	// Create D3D9 Object.
	d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

	// Caps Declare.
	D3DCAPS9 caps;

	// Get HAL Device Caps.
	d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);

	// Int to hold vertex processing cap.
	int VertexProcessStatus;

	// Can we use Hardware Vertext Processing?
	if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
	{
		// Yes.
		VertexProcessStatus = D3DCREATE_HARDWARE_VERTEXPROCESSING;
	}
	else
	{
		// No.
		VertexProcessStatus = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
	}
	
	// Our Present Description.
	D3DPRESENT_PARAMETERS d3dpp;

	// Fill d3dpp.
	d3dpp.BackBufferWidth = 1024; // Width;
	d3dpp.BackBufferHeight = 768; // Height;
	d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; // Pixel Format.
	d3dpp.BackBufferCount  = 1; // Number of Back Buffers.
	d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; // No AA;
	d3dpp.MultiSampleQuality = 0; // No AA;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // Flip Method.
	d3dpp.hDeviceWindow = MainWindowHandle; // Target Window.
	d3dpp.Windowed = false; //run fullscreen.
	d3dpp.EnableAutoDepthStencil = true; // Let DX do Stencil and depth.
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // Depth Format.
	d3dpp.Flags = 0; // No Flags.
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; // Refresh Rate. Use Default.
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Swap Timing (?).

	HRESULT hr;

	hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, MainWindowHandle, VertexProcessStatus, &d3dpp, &d3ddevice);

	if(FAILED(hr))
	{
		::MessageBox(0, "Failed CreateDevice()", "FAIL", MB_OK);
		return false;
	}

	return true;
}

bool InitGeo()
{
	d3ddevice->CreateVertexBuffer(8 * sizeof(MyVertex), D3DUSAGE_WRITEONLY, MyVertex::FVF, D3DPOOL_MANAGED, &VB, 0);

	d3ddevice->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &IB, 0);

	MyVertex* vertices;

	VB->Lock(0, 0, (void**)&vertices, 0);

	vertices[0] = MyVertex(-1.0f, -1.0f, -1.0f);
	vertices[1] = MyVertex(-1.0f, 1.0f, -1.0f);
	vertices[2] = MyVertex(1.0f, 1.0f, -1.0f);
	vertices[3] = MyVertex(1.0f, -1.0f, -1.0f);
	vertices[4] = MyVertex(-1.0f, -1.0f, 1.0f);
	vertices[5] = MyVertex(-1.0f, 1.0f, 1.0f);
	vertices[6] = MyVertex(1.0f, 1.0f, 1.0f);
	vertices[7] = MyVertex(1.0f, -1.0f, 1.0f);

	VB->Unlock();

	WORD* indices = 0;

	IB->Lock(0, 0, (void**)&indices, 0);

	// front side
	indices[0] = 0; indices[1] = 1; indices[2] = 2;
	indices[3] = 0; indices[4] = 2; indices[5] = 3;

	// back side
	indices[6] = 4; indices[7] = 6; indices[8] = 5;
	indices[9] = 4; indices[10] = 7; indices[11] = 6;

	// left side
	indices[12] = 4; indices[13] = 5; indices[14] = 1;
	indices[15] = 4; indices[16] = 1; indices[17] = 0;

	// right side
	indices[18] = 3; indices[19] = 2; indices[20] = 6;
	indices[21] = 3; indices[22] = 6; indices[23] = 7;

	// top
	indices[24] = 1; indices[25] = 5; indices[26] = 6;
	indices[27] = 1; indices[28] = 6; indices[29] = 2;

	// bottom
	indices[30] = 4; indices[31] = 0; indices[32] = 3;
	indices[33] = 4; indices[34] = 3; indices[35] = 7;

	IB->Unlock();

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

	d3ddevice->SetTransform(D3DTS_VIEW, &V);

	//Projection
	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)1024 / (float)768, 1.0f, 1000.0f);

	d3ddevice->SetTransform(D3DTS_PROJECTION, &proj);

	//render state
	//d3ddevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

	return true;
}

int Run()
{
	MSG msg;
	
	static float lasttime = (float)timeGetTime();

	while(true)
	{
		if(::PeekMessage(&msg,0,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				break;
			}
			
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}
		else
		{
			float currtime = (float)timeGetTime();
			float timedelta = (currtime - lasttime) * 0.001f;

			D3DXMATRIX Rx, Ry;

			D3DXMatrixRotationX(&Rx, 3.14f / 4.0f);

			static float y = 0.0f;

			D3DXMatrixRotationY(&Ry, y);

			y += timedelta;

			if(y >= 6.28f)
			{
				y = 0.0f;
			}

			D3DXMATRIX p = Rx * Ry;

			d3ddevice->SetTransform(D3DTS_WORLD, &p);

			d3ddevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);

			d3ddevice->BeginScene();
			d3ddevice->SetStreamSource(0, VB, 0, sizeof(MyVertex));
			d3ddevice->SetIndices(IB);
			d3ddevice->SetFVF(MyVertex::FVF);
			d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
			d3ddevice->EndScene();

			d3ddevice->Present(0,0,0,0);

			lasttime = currtime;
		}
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_KEYDOWN:
		if(wParam == VK_ESCAPE)
		{
			::DestroyWindow(MainWindowHandle);
		}
		return 0;
	case WM_DESTROY:
		::PostQuitMessage(0);
		return 0;
	}

	return ::DefWindowProc(windowHandle, msg, wParam, lParam);
}



It compiles fine and runs the window but I dont see anything other then a blank screen. What did I do wrong? Thanks for any help :) [Edited by - mengha on June 1, 2005 3:38:51 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hi there,

First things first my friend, you're trying to draw a black cube on a black surface....
Notice the colour you clear the screen to...
d3ddevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
Try putting white in there '0xffffffff'

Next point...
Your vertex structure
struct MyVertex
{
MyVertex(){}
MyVertex(float x, float y, float z)
{
x = x;
y = y;
z = z;
}
float x, y, z;
static const DWORD FVF;
};


Here you seem to have a scope problem, while in the constructing method for the structure, the values 'x', 'y' + 'z' are that of the same named arguments passed to the method, so you're effectively assigning the value of the argument, to the argument.

That's probabally not overly clear, so here's what you're doing with different parameter names...
struct MyVertex
{
MyVertex(){}
MyVertex(float x_in, float y_in, float z_in)
{
x_in = x_in;
y_in = y_in;
z_in = z_in;
}

float x, y, z;
static const DWORD FVF;
};



Note here that what you're doing is assigning the argument to itself, and NOT that of the structure, and so effectively you've not been building the vertex structure correctly (or at all).

Here is a corrected version
struct MyVertex
{
MyVertex(){}
MyVertex(float x_in, float y_in, float z_in)
{
x = x_in;
y = y_in;
z = z_in;
}

float x, y, z;
static const DWORD FVF;
};

Now the structure co-ords 'x', 'y' + 'z' are now assigned correctly from the values passed to the method.
Fix these changes and you'll have your rotating cube. [wink]

Also feel free to un-comment the:
//d3ddevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
Line, it should work fine withthe above changes, and it looks cooler :)

Hope this has helped.

Regards,
Add

Share this post


Link to post
Share on other sites
Quote:
Original post by mengha
Worked perfectly :D

Thanks alot Add.


Good stuff,
And you're welcome... [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement