Jump to content
  • Advertisement
Sign in to follow this  
nosajghoul

for beginners, alot here, source included

This topic is 5401 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

I spent the better part of the day (well 2 days now) getting aquainted with DX9, coming from DX8. The result is alot learnt, and here is the result of that orgy of picking through and reading about 20 or so tutorial sites. This would have been a great way for me to learn it, so I want to share it so anyone that wants actual working sourcecode to pick thru (and not have to type, edit, research, screw up, post obnoxious questions etc... all day long like I did) can have it. (edit : longest.sentence.ever.) Just select all the code below, copy and paste it into MSVC++ , include the proper libraries (see the comment at the top of the code for them) and compile and run. Everything from device creation up to lighting is there, in one long (i didnt use classes) stretch. Oh, you will need an image in your directory named tex1.bmp, I just use a 128x128 bmp i made in 15 seconds. There is even the beginnings of a vertex shader, but Im tired and am looking for a beer now, so here ya go.
//be sure to put in the libraries below
//dxguid.lib d3dxof.lib d3d9.lib d3dx9.lib dxerr9.lib 
//project->settings->link    add them to the front of that list that says 'kernel32.lib......"


#define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <stdio.h> //for sprintf
#include <d3d9.h>
#include <d3dx9.h>
#include <dxerr9.h>

HWND ghWnd = 0;
HINSTANCE ghInst = 0;
HRESULT ghr = 0;
HDC gdc = 0;

//#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
//#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)
#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)

struct vertex_colored
{
    float x, y, z; // The transformed position for the vertex.
    DWORD color;        // The vertex colour.
};

struct vertex_textured
{
	float x, y, z;
	float u, v;
};

struct vertex_textured_lit
{
	float x, y, z;
	D3DVECTOR n;
	float u, v;
};

vertex_colored polyvertices[] = 
{
	{  -10.0f,   10.0f, 0.0f,D3DCOLOR_XRGB(0,255,255)},
	{   10.0f,   10.0f, 0.0f,D3DCOLOR_XRGB(255,0,255)},
	{   10.0f,  -10.0f, 0.0f,D3DCOLOR_XRGB(255,255,0)},
	{  -10.0f,  -10.0f, 0.0f,D3DCOLOR_XRGB(255,255,255)}
};

vertex_textured texturedpoly[] =
{
	{-10.0f,   10.0f,   0.0f,   0.0f , 1.0f},
	{ 10.0f,   10.0f,   0.0f,   1.0f , 1.0f},
	{ 10.0f,  -10.0f,   0.0f,   1.0f , 0.0f},
	{-10.0f,  -10.0f,   0.0f,   0.0f , 0.0f}
};


vertex_textured_lit litpoly[] = 
{
	{-10.0f,   10.0f,   0.0f,   0.0f,  -1.0f,  0.0f, 0.0f , 1.0f},
	{ 10.0f,   10.0f,   0.0f,   0.0f,  -1.0f,  0.0f, 1.0f , 1.0f},
	{ 10.0f,  -10.0f,   0.0f,   0.0f,  -1.0f,  0.0f, 1.0f , 0.0f},
	{-10.0f,  -10.0f,   0.0f,   0.0f,  -1.0f,  0.0f, 0.0f , 0.0f}
};


D3DVERTEXELEMENT9 decl[] =
{
	{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },//3 position vectors
    { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },//a color vector
    D3DDECL_END()
};

	IDirect3D9				    *pD3D;
	IDirect3DDevice9		    *pD3DDevice;
	IDirect3DVertexBuffer9      *polyVB;
	IDirect3DVertexShader9      *pVertexShader;
	IDirect3DVertexDeclaration9 *pVertDecl;
	IDirect3DTexture9           *pTex;



LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_PAINT:
		ValidateRect(ghWnd, NULL);
		break;
	case WM_KEYDOWN:
		switch (wParam)	
		{
			case VK_ESCAPE:	DestroyWindow(ghWnd);break;
		}
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
	ghInst = hInstance;

	WNDCLASSEX wc;
	wc.cbSize =			sizeof(WNDCLASSEX);
	wc.style =			CS_CLASSDC, //CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc =	WndProc; //where to pass messages to (defined below)
	wc.cbClsExtra =		0;
	wc.cbWndExtra =		0;
	wc.hInstance =		hInstance;
	wc.hIcon =			NULL, //LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor =		(HCURSOR)LoadCursor(NULL, IDC_ARROW); //can be NULL
	wc.hbrBackground =	(HBRUSH)GetStockObject(WHITE_BRUSH); //color of bkgrnd, can be NULL
	wc.lpszMenuName =	NULL;
	wc.lpszClassName =	"WinApp";//classname
	wc.hIconSm =		NULL;

	RegisterClassEx(&wc);

	ShowCursor(1);

	ghWnd = CreateWindowEx(NULL, "WinApp", NULL, WS_OVERLAPPEDWINDOW, 0, 0, 648, 512, NULL, NULL, hInstance, NULL);

	ShowWindow(ghWnd, SW_SHOWDEFAULT);
	UpdateWindow(ghWnd);
	//////////////////setup
		//setup D3D
	pD3D = Direct3DCreate9(D3D_SDK_VERSION);
	
	D3DDISPLAYMODE d3ddm;
	ghr = pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);

	D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory(&d3dpp, sizeof(d3dpp));

	bool windowed = 1;

	switch(windowed) 
	{
	case 0:
		d3dpp.Windowed = FALSE; 
		d3dpp.BackBufferCount = 1;
		d3dpp.BackBufferWidth = 640;
		d3dpp.BackBufferHeight = 480;
		d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
		d3dpp.hDeviceWindow = ghWnd;
		d3dpp.EnableAutoDepthStencil = TRUE;
		d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
		break;
	case 1:
		d3dpp.Windowed = TRUE;
		d3dpp.BackBufferCount = 1;
		d3dpp.BackBufferWidth = 640;
		d3dpp.BackBufferHeight = 480;
		d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;//d3ddm.Format;
		d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
		d3dpp.hDeviceWindow = ghWnd;
		d3dpp.EnableAutoDepthStencil = TRUE;
		d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
		break;
	}

	ghr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, ghWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice);

	ghr = pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	ghr = pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
	ghr = pD3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(0,0,0));	
	ghr = pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	
	ghr = pD3DDevice->SetFVF(D3D8T_CUSTOMVERTEX);
		//create vertex buffer
	void *vertices = 0;
    ghr = pD3DDevice->CreateVertexBuffer(sizeof(litpoly), D3DUSAGE_WRITEONLY, D3D8T_CUSTOMVERTEX, D3DPOOL_MANAGED,
		&polyVB, NULL);      
	ghr = polyVB->Lock(0, sizeof(vertices), (void**)&vertices, 0); 
	memcpy(vertices, litpoly, sizeof(litpoly) );
	ghr = polyVB->Unlock();
	//create the shader	
	ID3DXBuffer *pCode;
	ID3DXBuffer *pErr;
	ghr = pD3DDevice->CreateVertexDeclaration(decl, &pVertDecl);
	ghr = D3DXAssembleShaderFromFile("shader.vsh", NULL, NULL, 0, &pCode, &pErr);//compile shader
	ghr = pD3DDevice->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(), &pVertexShader );//create vs interface
	ghr = pCode->Release();
	
	//setup camera
	D3DXMATRIX proj, view;
	D3DXMatrixPerspectiveFovLH(
        &proj, 
        D3DX_PI/4.0f, 
        1280/ 1024, //aspect ratio 
        0.1f, 
        1000.0f
        );

    // Create the camera matrix
    D3DXMatrixLookAtLH(
        &view, 
        &D3DXVECTOR3(0.0f, 20.0f, -80.0f), //eye
        &D3DXVECTOR3(0.0f, 0.0f, 0.0f), //at
        &D3DXVECTOR3(0.0f, 1.0f, 0.0f) //up
        );

    // Set the projection matrix 
    if( FAILED( pD3DDevice->SetTransform(D3DTS_PROJECTION, &proj ) ) )
        MessageBox(0, "settransform 1", "error", MB_OK);

    // Set the camera matrix
    if( FAILED( pD3DDevice->SetTransform(D3DTS_VIEW, &view ) ) )
		MessageBox(0, "settransform 2", "error", MB_OK);
	//load the texture
	D3DXCreateTextureFromFile(pD3DDevice, "tex1.bmp", &pTex);
	//set up lights
	D3DLIGHT9 light;
	ZeroMemory(&light, sizeof(light));
	light.Type = D3DLIGHT_DIRECTIONAL;
	light.Diffuse .r = light.Diffuse.g = light.Diffuse.b = 1.0f;
	light.Direction.x = 1.0f;
	light.Direction.y = 0.0f;
	light.Direction.z = 1.0f;
	light.Range = 1000.0f;
	pD3DDevice->SetLight(0, &light);
	pD3DDevice->LightEnable(0, true);
		//materials used with lights
	D3DMATERIAL9 material;
	ZeroMemory(&material, sizeof(material));
	material.Diffuse.r = material.Diffuse.g = material.Diffuse.b = 1.0f;




	MSG msg;
	while(1)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			ghr = pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(50,0,0), 1.0f, 0);
			ghr = pD3DDevice->BeginScene();
			//ghr = pD3DDevice->SetStreamSource(0,polyVB, 0 ,sizeof(vertex_colored));
			//ghr = pD3DDevice->SetStreamSource(0,polyVB, 0 ,sizeof(vertex_textured));
			ghr = pD3DDevice->SetStreamSource(0,polyVB, 0 ,sizeof(vertex_textured_lit));
			
			static float x = 0;
			x +=.15f;

			D3DXMATRIX world, rotx, roty, move, buffer;
			D3DXMatrixTranslation(&move, 0, 0, 0);
			D3DXMatrixRotationX(&rotx, D3DXToRadian(x * 9));
			D3DXMatrixRotationY(&roty, D3DXToRadian(x * 9));
			D3DXMatrixMultiply(&buffer, &rotx, &roty);
	 		ghr = pD3DDevice->SetTransform(D3DTS_WORLD, &buffer);//move it

			//use vertex shader on it
			/*
			pD3DDevice->SetVertexDeclaration(pVertDecl);
			pD3DDevice->SetVertexShader(pVertexShader);
			D3DXMATRIX p; pD3DDevice->GetTransform(D3DTS_PROJECTION, &p);
			D3DXMATRIX v; pD3DDevice->GetTransform(D3DTS_VIEW, &v);
			D3DXMATRIX shader =  p * world * v;
			pD3DDevice->SetVertexShaderConstantF( 0, (float*)&shader, 4 );
			*/
			
			ghr = pD3DDevice->SetTexture(0, pTex);//use the texture
			ghr = pD3DDevice->SetMaterial(&material);//use the material

			ghr = pD3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0, 2);
			ghr = pD3DDevice->EndScene();
			ghr = pD3DDevice->Present(NULL, NULL, NULL, NULL);
		}
	}

	polyVB->Release();
	pD3DDevice->Release();		  
	pD3D->Release();  
	
	UnregisterClass("WinApp", hInstance);
	
	return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

Barfs at trying to process the shader file


-------->> ghr = pD3DDevice->CreateVertexShader( (DWORD*)p

because "shader.vsh" doesnt exist.....

Share this post


Link to post
Share on other sites
my bad, just put this in a .vsh file. srry


vs_1_1 // We're using vertex shader version 1.1

dcl_position v0 // We want the vertex position info to come in through this register
dcl_color v1 // We want the color information to come in through this register

// Transform the vertex coordinate with the transformation matrix
dp4 oPos.x, v0, c0
dp4 oPos.y, v0, c1
dp4 oPos.z, v0, c2
dp4 oPos.w, v0, c3

// We dont want to do anything to the color. Just pass it to the color register.
mov oD0, v1

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!