Game Programming and Development Tools

Starting DX Engine Again – brandon

Brandon

Member

Posts: 594
From: Kansas City, Mo, USA
Registered: 02-02-2004
I started working on a DX9 game engine a few months ago, but set the project aside. Well my reason for the post is cause I'm trying to decide if I should re-design the engine to be compatible with both DX and OpenGL. The thing that scares me about OpenGL is that I'd have to develop everything else that's not related to 3D graphics from scratch!!! Anyway. I'm trying to find as many tutorials on DX9 as I can, I'm a bit confused in the render pipeline... the .X 3D models load correctly, but the problem is that I haven't got them animating yet. And another is the texturing, if I try to create a cube for instance and then texture it, I get probs! Actually the program crashes :( I guess I'll post a snippet here of my render pipeline:


void DrawWorld(){
Entity* cam = mFirst(Entity);

D3DVIEWPORT9 vp;

//begins scene
if(SUCCEEDED(Window::D3D_Device->BeginScene())){
int thisCam = 1;
while(cam){
if (cam->etype == "CAMERA"){

if (thisCam == 1 ){
//set drawing modes (Set for 1st viewport only)
Window::D3D_Device->SetRenderState(D3DRS_LIGHTING, World::LightEnable);
Window::D3D_Device->SetRenderState(D3DRS_ZENABLE, World::ZEnable);
Window::D3D_Device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

int r = World::ambRed;
int g = World::ambGreen;
int b = World::ambBlue;
Window::D3D_Device->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(r,g,b));
}

//set camera drawing modes
int fr = cam->fogRed;
int fg = cam->fogGreen;
int fb = cam->fogBlue;
Window::D3D_Device->SetRenderState(D3DRS_FOGENABLE, cam->fogEnable);
Window::D3D_Device->SetRenderState(D3DRS_FOGTABLEMODE, cam->fogTableMode);
Window::D3D_Device->SetRenderState(D3DRS_FOGSTART, *(DWORD *) (&cam->fogStart));
Window::D3D_Device->SetRenderState(D3DRS_FOGEND, *(DWORD *) (&cam->fogEnd));
Window::D3D_Device->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(fr,fg,fb));


//create viewport settings
vp.X = cam->vpLeft;
vp.Y = cam->vpTop;
vp.Width = cam->vpWidth;
vp.Height = cam->vpHeight;
vp.MinZ = cam->vpMinZ;
vp.MaxZ = cam->vpMaxZ;

//set the viewport
Window::D3D_Device->SetViewport(&vp);

int tr = cam->clsRed;
int tg = cam->clsGreen;
int tb = cam->clsBlue;

//clear the scene
Window::D3D_Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(tr,tg,tb), 1.0f, 0 );

//build the camera projection matrix
D3DXMATRIX projMatrix;
//D3DXMatrixPerspectiveFovLH(&projMatrix, D3DX_PI/4, 1.2f, 1.0, 700.0);
D3DXMatrixPerspectiveFovLH(&projMatrix, cam->fovy, cam->aspect, cam->zNear, cam->zFar);

//set drawing projection matrix to camera projection matrix
Window::D3D_Device->SetTransform(D3DTS_PROJECTION, &projMatrix);

//Build the camera view matrix
D3DXMATRIX viewMatrix;
BuildCamViewMatrix(&viewMatrix, cam);

//set drawing view matrix to camera view matrix
Window::D3D_Device->SetTransform(D3DTS_VIEW, &viewMatrix);


//Draw the entities
Entity* tEnt = mFirst(Entity);
while(tEnt){
DrawEnt(tEnt);
tEnt = mAfter(Entity, tEnt);
}

//Draw 2D and text (Only for first viewport)
if (thisCam == 1){
//Set Drawing mode for 2D

//Draw 2D Images

//Draw Text
TextC::DrawAllText();
}
thisCam++;
}//End IF
cam = mAfter(Entity, cam); //Move to next camera
}//End WH
//End the scene

Window::D3D_Device->EndScene();
}

HRESULT hr;
//Flip
hr=Window::D3D_Device->Present(NULL, NULL, NULL, NULL);

//Lost Display
if(hr == D3DERR_DEVICELOST) {
hr = Window::D3D_Device->TestCooperativeLevel();
if(hr == D3DERR_DEVICENOTRESET) {
Window::D3D_Device->Reset(&Window::d3d_pp);
}
}
}// endfn DrawWorld

And this is the DrawEnt Function:


void DrawEnt(Entity *ent){

D3DXMATRIX worldMat;

if ( ent->etype=="MESH" || ent->etype=="TERRAIN" || ent->etype=="SPRITE"){

DWORD mtrl = 0;

//Set the drawing mode
Window::D3D_Device->SetRenderState(D3DRS_SHADEMODE, ent->shadeMode);
Window::D3D_Device->SetRenderState(D3DRS_FILLMODE, ent->fillMode);
Window::D3D_Device->SetRenderState(D3DRS_CULLMODE, ent->cullMode);
Window::D3D_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, ent->blendable );
Window::D3D_Device->SetRenderState(D3DRS_SRCBLEND, ent->blendSRC);
Window::D3D_Device->SetRenderState(D3DRS_DESTBLEND, ent->blendDEST);
Window::D3D_Device->SetRenderState(D3DRS_SPECULARENABLE, ent->specularable );
Window::D3D_Device->SetRenderState(D3DRS_COLORVERTEX, ent->colorVex);
Window::D3D_Device->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, ent->ambSrc);
Window::D3D_Device->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, ent->diffSrc);
Window::D3D_Device->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, ent->emmiSrc);
Window::D3D_Device->SetRenderState(D3DRS_SPECULARMATERIALSOURCE , ent->specSrc);

//Build and set world matrix
BuildEntWorldMatrix(ent, &worldMat);
Window::D3D_Device->SetTransform(D3DTS_WORLD, &worldMat);

//go through each material
for(mtrl=0; mtrl < ent->numMtrls; mtrl++){

for(int layer = 0; layer <= ent->TexLayers; layer++){

//Set Materials
Window::D3D_Device->SetMaterial(&ent->materials[mtrl].MatD3D);

//Set Textures
if(ent->textures[mtrl] != NULL){
Window::D3D_Device->SetTexture(layer, ent->textures[mtrl]);

for ( int I =0; I< ent->TexStage; I++){
Window::D3D_Device->SetTextureStageState( I,ent->TextureStageStateType[i][mtrl], ent->TextureBlendArg[I][mtrl] );
}
}

}

//Window::D3D_Device->SetTextureStageState( mtrl,D3DTSS_COLORARG1, D3DTA_TEXTURE );
//Window::D3D_Device->SetTextureStageState( mtrl,D3DTSS_COLOROP, D3DTOP_MODULATE2X );

//Draw this subset
ent->dxMesh->DrawSubset(mtrl);

}
}

//Lights
if (ent->etype == "LIGHT"){
if(ent->lightChange == TRUE){
Window::D3D_Device->SetLight(ent->lx, &ent->light);
ent->lightChange = FALSE;
}
//Build and set world matrix
BuildEntWorldMatrix(ent, &worldMat);
Window::D3D_Device->SetTransform(D3DTS_WORLD, &worldMat);
}
}

The problem is most likely somewhere in the DrawEnt() function. Any type of help would be greatly appreciated :)


------------------
If I were a drummer I would use a cymbal.
If I were a writer I would use a pencil.
I would use my voice if I were a singer.
No matter who or what we are we must praise.


[This message has been edited by brandon (edited May 07, 2005).]

[This message has been edited by brandon (edited May 09, 2005).]

Brandon

Member

Posts: 594
From: Kansas City, Mo, USA
Registered: 02-02-2004
Okay, the problem isn't in either of those functions. But I've started over completly and am now working on an engine that will start off supporting OpenGL, then I'll add the DX9 support after I get the OpenGL side of things going good,... mainly after I get models loading and animating... I'm also considering using a custom model format as well.

This might sound dumb, but should I base it off of quads or tri's? I think that .obj uses both... but I'm not sure how using one or the other is better... anyways, I'm working on the mesh\entity class today... so I hope I'll have it where you can at least create your custom meshes using the function calls before the day is done... k, enough babbling... now.. back to work


------------------
If I were a drummer I would use a cymbal.
If I were a writer I would use a pencil.
I would use my voice if I were a singer.
No matter who or what we are we must praise.

[This message has been edited by brandon (edited May 10, 2005).]

nfektious
Member

Posts: 408
From:
Registered: 10-25-2002
cool job brandon. i'm excited to see this project grow and check your progress.
my opinion on the model issue: it really depends on how much data you want to pack...tris are more of the standard - and more likely to provide you with content to use (freeware models, etc) and a community base for your engine. quads of course offer more detail and more realism - and thus can become complex fast. if you want decent rendering and speed in processing, tris still offer enough bang for the buck. like i said, just my opinion.
Brandon

Member

Posts: 594
From: Kansas City, Mo, USA
Registered: 02-02-2004
Thx!

Yeah, I think I'll go with the tris instead. I've gotten textures loading, but still using the basic hardcoded style of rendering. I'm currently devising a set of classes for my mesh\entity system.

I think the part that will give me the most problems right now is thinking on how I'll use the display lists when loading the meshes. Does anyone know of any alternative to using display lists? But one that is just as fast or faster? It's cool if not, I might use display lists anyway. I guess I was just wandering if it was the normal way of doing it.

Okay, as for my mesh class I have some idea as to what I want to do. Here are some what I think that I'll need for basic mesh stuff (NOTE: this isn't final code just an overview, I'll probably add this into structs or classes once I get it all planned out right) :



int vertCount; //Total number of vertices for the model
vec *vertNorms; //Normal vector for each vertex
vec *vertPos; //Position of each vertex
vec *vertColor; //Color of each vertex
GLuint displayList; //display list storage variable


Also, ... I don't know but pointers might not be the best way to store the data for the vertNorms, vertPos, and vertColor, could get messy, but we'll see =) I'm also thinking that the display list storage variable should be a dynamic global variable instead. Sheesh... OpenGL engine programing really makes you think more than using DX enging programming does! No .X file loader!

------------------
If I were a drummer I would use a cymbal.
If I were a writer I would use a pencil.
I would use my voice if I were a singer.
No matter who or what we are we must praise.

HeardTheWord

Member

Posts: 224
From: Des Moines, IA
Registered: 08-16-2004
I wish I would take the time to do some of this stuff as well! I do know a bit about OpenGL though...

Triangle strips would be the fastest way to load and render a model. They also take up less hard drive space.
As for display lists, they are nice for prototyping code but you really should use vertex arrays. They are MUCH faster at rendering your model data.

Hope this helps you and Good Luck!

[This message has been edited by HeardTheWord (edited May 11, 2005).]

nfektious
Member

Posts: 408
From:
Registered: 10-25-2002
Brandon - there are several engines out there based on OpenGL that include model/render code that might serve as a reference resource for you.
Brandon

Member

Posts: 594
From: Kansas City, Mo, USA
Registered: 02-02-2004
Cool, I'll look into triangle strips and I could look at the Irrlicht source code for the OpenGL rendering then, just to have some type of understanding as to how it's done correctly. Thanks for the tips guys, it's much appreciated!

I haven't had time to do much within the past 24 hours! Hopefully I can get back on track later today. Anyways, I'd better get fonts working next chance that I get so I can at least show data on screen. Oh, and linked lists before I code the entity class.

------------------
If I were a drummer I would use a cymbal.
If I were a writer I would use a pencil.
I would use my voice if I were a singer.
No matter who or what we are we must praise.