Jump to content
  • Advertisement
Sign in to follow this  
monkeyboi

Logical Architecture of Mesh Classes in Game Engine

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

Lots of game engines nowadays are using config file to specify the setting details, like resolution and renderer(dx or opengl). Assume the renderer has been settled after parsing config file.My question is "how could I use one class or an alias to represent a specific mesh class depending on the type of renderer" I know usually engine has its own mesh file format. But DirectX has implemented numerous interfaces for mesh class which is really attractive. Limited knowledge about opengl put me into a really difficult situation. My expected result would be like this:
//dx renderer
MyMesh* Tree = new MyMesh;//actually create a MyDxMesh
Tree->draw();//calling the function in MyDxMesh
Tree->update();//calling the function in MyDxMesh
...

//gl renderer
MyMesh* Tree = new MyMesh;//actually create a MyGLMesh
Tree->draw();//calling the function in MyGLMesh
Tree->update();//calling the function in MyGLMesh
...
Only reason I want to do this is I don't have to change any code even I switch the renderer.

Any help will be appreciated!

Thanks in advance

Jerry

Share this post


Link to post
Share on other sites
Advertisement
In creating a dx mesh, one of the parameters is a pointer to the d3d device. That way DrawSubset(n) has a pointer to the device to render its mesh data. I'm not familiar with ogl but the goal is to have multiple renderers using one interface.


[source lang="cpp"]enum INTERFACE_3D
{
INTERFACE_DX9,
INTERFACE_DX10,
INTERFACE_DX11,
INTERFACE_OPENGL
}

class MyMesh
{
public:
MyMesh(INTERFACE_3D iType, void* vpDevice);

int Render(void);

private:
int pmRenderDx9(void);
int pmRenderDx10(void);
int pmRenderDx11(void);
int pmRenderOpenGL(void);

INTERFACE_3D i3dType;
void* vpRenderDevice;
};

int MyMesh::Render(void)
{
switch(i3dType)
{
case INTERFACE_DX9:
return pmRenderDx9(void);
case INTERFACE_DX10:
return pmRenderDx10(void);
case INTERFACE_DX11:
return pmRenderDx11(void);
case INTERFACE_OPENGL:
return pmRenderOpenGL(void);
}
}

int MyMesh::pmRenderDx9(void)
{
if(!vpRenderDevice) return -1;
LPDIRECT3DDEVICE9 pDevice = (LPDIRECT3DDEVICE9)vpRenderDevice;

//Draw stuff using pDevice

return 0;
}[/source]


MyMesh Mesh(INTERFACE_DX9, (void*)lpd3dDevice);

int ec = Mesh.Render(); Edited by Neometron

Share this post


Link to post
Share on other sites

In creating a dx mesh, one of the parameters is a pointer to the d3d device. That way DrawSubset(n) has a pointer to the device to render its mesh data. I'm not familiar with ogl but the goal is to have multiple renderers using one interface.


<Snipped>


This solution absolutely does not scale if you were to add any additional rendering solutions, and is pretty much considered as bad design when looking at it from an OO point of view. Your mesh class would also get cluttered very fast since you'll be cramming multiple class implementations into a single class.

Correct use of inheritance and an interface-based design can solve this problem with the possibility of scaling with any additional rendering systems without having to edit the original mesh class. The factory pattern has already been mentioned in the posts above and is a good option for implementing this.

Share this post


Link to post
Share on other sites
So something like this:


[source lang="cpp"]enum INTERFACE_3D
{
INTERFACE_DX9,
INTERFACE_DX10,
INTERFACE_DX11,
INTERFACE_OPENGL
}

class MyMesh
{
public:
static MyMesh* Create(INTERFACE_3D iType, void* vpDevice);

virtual int Render(void) = 0;
};

class MyDx9Mesh : public MyMesh
{
public:
MyDx9Mesh(LPDIRECT3DDEVICE9 pDevice);
int Render(void);
private:
LPDIRECT3DDEVICE9 mp_Device;
}

//Other classes with MyMesh as base

//MyMesh Create Function
MyMesh* MyMesh::Create(INTERFACE_3D iType, void* vpDevice)
{
switch(iType)
{
case INTERFACE_DX9:
return new MyDx9Mesh((LPDIRECT3DDEVICE9)vpDevice);
case INTERFACE_DX10:
return new MyDx10Mesh((ID3D10Device*)vpDevice);
case INTERFACE_DX11:
return new MyDx11Mesh((ID3D11Device*)vpDevice);
case INTERFACE_OPENGL:
return new MyOpenGlMesh();
}
}


int MyDx9Mesh::Render(void)
{

if(![color=#000000]mp_Device) return -1;

//Draw stuff using [color=#000000]mp_Device

return 0;
}

MyMesh* pMesh = MyMesh::Create(INTERFACE_DX9, (void*)pd3dDevice);

int ec = pMesh->Render();[/source] Edited by Neometron

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!