Sign in to follow this  
Pero83

First DX app

Recommended Posts

Hey all!




I have been learning dx (9) for 1 month or so. I'm using dx9 books, also paying a visit a lot to directxtutorials.com, and also searching around as much as i can on subjects i learn.

Anyway, so far, i was studing how to make basic dx window, how to render triangle, how to use vertex and index buffers, and so on. And, before i continue, i wanted to do very simple dx app, and was hoping you guys can take a look at my code and tell me, at this stage, am i doing somethign wrong, especially in the "logic", so to speak.




So, goal was, to render n * n grid of blocks. Meaning, if i set n to 5, i will get 5 * 5 blocks = 25 blocks. I wanted to controls two things with global variable; number of blocks per "line" (like 5 in this example), and how big those blocks are.

Also, i wanted the pivot of "entire mesh" to be centered in the 0,0,0. Lastly, i wanted to make it so that the whole thing rotates. Oh and also, i wanted to put all vertecies inside one drawIndexedPrimitive() function.

I didn't use lights, or texture so far.

This is example of n=70, size = 5:: (4900 blocks in total)

[URL=http://imageshack.us/photo/my-images/266/4900cubes.jpg/][img]http://img266.imageshack.us/img266/2674/4900cubes.jpg[/img][/URL

And this is code: (c++, dx9):

[code]
#define SCREEN_WIDTH 1680
#define SCREEN_HEIGHT 1050
#define CUstOMFVF D3DFVF_XYZ|D3DFVF_DIFFUSE

#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
LPDIRECT3DINDEXBUFFER9 i_buffer = NULL;
HINSTANCE hInstance;
HWND hWnd;

//parameters for setting up blocks
const short numBlocks = 70; //number of blocks in line
const short blockSize = 5; //size of each edge of the block

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


bool init_window(HINSTANCE hInstance);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void init_d3d(HWND hWnd);
void init_graphics(void);
void render_frame(void);
void clean_d3d(void);


/**********************************************************************************************************************/
/********************* ---------- WIN MAIN -------- *****************/
/**********************************************************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
if(!init_window(hInstance))
return 0;

MSG msg;
init_d3d(hWnd);

while(true)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
break;

render_frame();
}
clean_d3d();
return msg.wParam;
}



/**********************************************************************************************************************/
/********************* INIT WINDOW *****************/
/**********************************************************************************************************************/
bool init_window(HINSTANCE hInstance)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInstance;
wc.lpszClassName = "Simple Polygon Class";
wc.lpfnWndProc = WindowProc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL, "Simple Polygon Class", "Simple Polygon Window", WS_EX_TOPMOST|WS_POPUP, 0,0,SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
if(!hWnd)
return false;

ShowWindow(hWnd, SW_SHOW);
return true;

}



/**********************************************************************************************************************/
/********************* WINDOW PROC *****************/
/**********************************************************************************************************************/
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY|WM_KEYDOWN:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);

}



/**********************************************************************************************************************/
/********************* INIT D3D *****************/
/**********************************************************************************************************************/
void init_d3d(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = false;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;


d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

init_graphics();

d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); //turns off lighting
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); //turn on z-buffer
}


/**********************************************************************************************************************/
/********************* INIT GRAPHICS *****************/
/**********************************************************************************************************************/
void init_graphics(void)
{
HRESULT hResult;

const int numVertex = numBlocks * numBlocks * 8 ; //total number of vertecies
int counter = 0;
float startPos = ((blockSize*numBlocks)/2)*(-1) ; //finds position for first vertex;
Vertexstruct vertex[numVertex];



//vertex buffer
for (int n=0; n<numBlocks; n++)
{
for(int m=0; m<numBlocks; m++)
{
//0
vertex[counter + 0].color=D3DCOLOR_XRGB(41,140,78);
vertex[counter + 0].x = (float)((startPos) + (n*blockSize));
vertex[counter + 0].y = blockSize / 2;
vertex[counter + 0].z = (float)((startPos) + (m*blockSize));

//1
vertex[counter + 1].color=D3DCOLOR_XRGB(41,140,78);
vertex[counter + 1].x = (float)((startPos) + (n*blockSize) + blockSize);
vertex[counter + 1].y = blockSize / 2;
vertex[counter + 1].z = (float)((startPos) + (m*blockSize));

//2
vertex[counter + 2].color=D3DCOLOR_XRGB(119,80,25);
vertex[counter + 2].x = (float)((startPos) + (n*blockSize));
vertex[counter + 2].y = -(blockSize / 2);
vertex[counter + 2].z = (float)((startPos) + (m*blockSize));

//3
vertex[counter + 3].color=D3DCOLOR_XRGB(119,80,25);
vertex[counter + 3].x = (float)((startPos) + (n*blockSize) + blockSize);
vertex[counter + 3].y = -(blockSize / 2);
vertex[counter + 3].z = (float)((startPos) + (m*blockSize));

//4
vertex[counter + 4].color=D3DCOLOR_XRGB(41,140,78);
vertex[counter + 4].x = (float)((startPos) + (n*blockSize));
vertex[counter + 4].y = blockSize / 2;
vertex[counter + 4].z = (float)((startPos) + (m*blockSize)+blockSize);

//5
vertex[counter + 5].color=D3DCOLOR_XRGB(41,140,78);
vertex[counter + 5].x = (float)((startPos) + (n*blockSize) + blockSize);
vertex[counter + 5].y = blockSize / 2;
vertex[counter + 5].z = (float)((startPos) + (m*blockSize)+blockSize);

//6
vertex[counter + 6].color=D3DCOLOR_XRGB(119,80,25);
vertex[counter + 6].x = (float)((startPos) + (n*blockSize));
vertex[counter + 6].y = -(blockSize / 2);
vertex[counter + 6].z = (float)((startPos) + (m*blockSize)+blockSize);

//7
vertex[counter + 7].color=D3DCOLOR_XRGB(119,80,25);
vertex[counter + 7].x = (float)((startPos) + (n*blockSize) + blockSize);
vertex[counter + 7].y = -(blockSize / 2);
vertex[counter + 7].z = (float)((startPos) + (m*blockSize)+blockSize);

counter += 8;
}
};

d3ddev->CreateVertexBuffer(sizeof(vertex)*sizeof(Vertexstruct), 0, CUstOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);
VOID* pVoid;
hResult = v_buffer->Lock(0, 0, &pVoid, 0);
memcpy(pVoid, &vertex, sizeof(vertex));
v_buffer->Unlock();


//index buffer
const int numIndex = numBlocks*numBlocks * 36;
short indices[numIndex];
short numIndices = numBlocks*numBlocks;
for (int n=0; n<(numIndices); n++)
{
indices[(n*36)+0] = (n*8)+0; indices[(n*36)+1] = (n*8)+1; indices[(n*36)+2] = (n*8)+2; //side 1
indices[(n*36)+3] = (n*8)+2; indices[(n*36)+4] = (n*8)+1; indices[(n*36)+5] = (n*8)+3;

indices[(n*36)+6] = (n*8)+4; indices[(n*36)+7] = (n*8)+0; indices[(n*36)+8] = (n*8)+6; //side 2
indices[(n*36)+9] = (n*8)+6; indices[(n*36)+10] = (n*8)+0; indices[(n*36)+11] = (n*8)+2;

indices[(n*36)+12] = (n*8)+7; indices[(n*36)+13] = (n*8)+5; indices[(n*36)+14] = (n*8)+6; //side 3
indices[(n*36)+15] = (n*8)+6; indices[(n*36)+16] = (n*8)+5; indices[(n*36)+17] = (n*8)+4;

indices[(n*36)+18] = (n*8)+3; indices[(n*36)+19] = (n*8)+1; indices[(n*36)+20] = (n*8)+7; //side 4
indices[(n*36)+21] = (n*8)+7; indices[(n*36)+22] = (n*8)+1; indices[(n*36)+23] = (n*8)+5;

indices[(n*36)+24] = (n*8)+4; indices[(n*36)+25] = (n*8)+5; indices[(n*36)+26] = (n*8)+0; //side 5
indices[(n*36)+27] = (n*8)+0; indices[(n*36)+28] = (n*8)+5; indices[(n*36)+29] = (n*8)+1;

indices[(n*36)+30] = (n*8)+3; indices[(n*36)+31] = (n*8)+7; indices[(n*36)+32] = (n*8)+2; //side 6
indices[(n*36)+33] = (n*8)+2; indices[(n*36)+34] = (n*8)+7; indices[(n*36)+35] = (n*8)+6;

};

d3ddev->CreateIndexBuffer(numBlocks*numBlocks*36*sizeof(short), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &i_buffer, NULL);
i_buffer->Lock(0, 0, &pVoid, 0);
memcpy(pVoid, &indices, sizeof(indices));
i_buffer->Unlock();

}

/**********************************************************************************************************************/
/********************* RENDER FRAME *****************/
/**********************************************************************************************************************/
void render_frame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,55), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,55), 1.0f, 0);

d3ddev->BeginScene();
d3ddev->SetFVF(CUstOMFVF);

static float indexY = 0.0f; //ever increasing float value for Y axes
static float indexZ = 0.0f; //ever increasing float value Z axes
indexY +=0.02f;


D3DXMATRIX matScale, matRotateY, matRotateZ, matView;
D3DXMATRIX matProjection;

D3DXMatrixRotationY(&matRotateY, indexY); //build a matrix to rotate the model, based on index
d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY));

//View matrix
D3DXMatrixLookAtLH
(
&matView,
&D3DXVECTOR3(500.0f, 500.0f, 500.0f), //camera position
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), //the look at position (where camera is aimed at)
&D3DXVECTOR3(0.0f, 1.0f, 0.0f) //the up direction
);
d3ddev->SetTransform(D3DTS_VIEW, &matView);

//Projection Matrix
D3DXMatrixPerspectiveFovLH
(
&matProjection,
D3DXToRadian(45), //Field of View; converts degrees to radians
(FLOAT)SCREEN_WIDTH/(FLOAT)SCREEN_HEIGHT, //aspect ratio
2.0f, //near camera clipping plane
2000.0f //far camera clipping plane
);
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);


d3ddev->SetStreamSource(0,v_buffer,0,sizeof(Vertexstruct));
d3ddev->SetIndices(i_buffer);

d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8*numBlocks*numBlocks, 0, 12*numBlocks*numBlocks);

d3ddev->EndScene();

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

/**********************************************************************************************************************/
/********************* CLEAN D3D *****************/
/**********************************************************************************************************************/
void clean_d3d(void)
{
v_buffer->Release();
i_buffer->Release();
if(d3ddev != NULL)
d3ddev->Release();
d3d->Release();
}

[/code]




So, if possible, i would like if some could tell me, if i am doing something in wrong way(i mean, app does work).....for example, i'm not sure if the way i am calculating each vertecies and then indices, is optimized way to do it.

Just trying to see if i'm going in the right direction.
And if any more info is needed, let me know!

Oh, and one question. The farther i set camera from center of scene, the lower a framerate is. At camera pos X = 1500 or so, it starts to lag a LOT. Any ideas?






Cheers and tnx!

P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this