Skip to main content
GameDev.net gamedev.net
🔒 Locked

Render Class

Started by Darobat Jan 13, 2005 at 9:16 PM 5 replies 1.6k views
Original Post
Darobat
Darobat
Greetings Many games/engines I see have a "Render" class. I still can't figure out what the puropse of such a class is. Would a render class perhaps look like this
class CRender
{
public:
    bool Terrain(CHeightmap terrain);
    bool Players(Players *players);
    bool Fog(int density)
};
That seems kinda pointless. Normaly I wouldn't even group that. I would have terrain->Render(). Not render->Terrain(heightmap). That is if I'm even using a Render class correctly. Can somebody please clarify what one is for? Thanks
--------------------C++ Home - Check it out!Lol... - Amazing video
Drew_Benton
Drew_Benton
Well a render class would contain rendering functions specific to the API that is being used. Here is a short example of something I have done for testing purposes:

Base:
class CRenderer{protected:	CFramework* m_framework;public:	CRenderer ( );	virtual ~CRenderer ( );	virtual int Create ( CFramework* framework ) = 0;	virtual void Begin ( ) = 0;	virtual void DrawTriangle ( float x, float y, float z ) = 0;	virtual void End ( ) = 0;	virtual void Destroy ( ) = 0;};


D3D
class CRendererD3D : public CRenderer{private:	CFrameworkD3D* d3dfw;	static IDirect3DVertexBuffer9 *pVertexBuffer;	D3DXMATRIXA16 matWorld;public:	CRendererD3D ( );	~CRendererD3D ( );	void Destroy ( );	int Create ( CFramework* fw );	void Begin ( );	void End ( );	void DrawTriangle ( float x, float y, float z );};...d3dfw->pD3DDevice->SetFVF ( D3DFVF_XYZ | D3DFVF_DIFFUSE );	d3dfw->pD3DDevice->SetStreamSource ( 0, pVertexBuffer, 0, sizeof ( tVertex) );	D3DXMatrixTranslation ( &matWorld, x, y, -z );	d3dfw->pD3DDevice->SetTransform ( D3DTS_WORLD, &matWorld );	d3dfw->pD3DDevice->DrawPrimitive ( D3DPT_TRIANGLELIST, 0, 1 );


OGL
class CRendererOGL : public CRenderer{private:	public:	CRendererOGL ( );	~CRendererOGL ( );	void Destroy ( );	int Create ( CFramework* fw );	void Begin ( );	void End ( );	void DrawTriangle ( float x, float y, float z );};...void CRendererOGL::DrawTriangle ( float x, float y, float z ){	glPushMatrix();	glTranslatef ( x, y, z );	glBegin ( GL_TRIANGLES );								// Begin Drawing Triangles		glColor3f (1.f, 0.f, 0.f);	glVertex3f ( 0.0f, 1.0f, 0.0f );		glColor3f (0.f, 1.f, 0.f);	glVertex3f (-1.0f, -1.0f, 1.0f );		glColor3f (0.f, 0.f, 1.f);	glVertex3f ( 1.0f, -1.0f, 1.0f );	glEnd ( );											// Done Drawing Triangles	glPopMatrix();}


Basically you add in the rendering functions that you will be using in the game. In this example - only being able to draw a triangle is quite useless - but you should be able to see how you could expand this. Imagine if you had a DrawMap function instead that took in a .3ds model data. You could easily pass this data into that function and have it draw using whatever dervived class you choose - either OGL or D3D. That would be the basics of a render class - even though you can have a lot more. In my 2D version, I had a BeginScene and EndScene. In OGL, BeginScene had the opengl code to reset the view and clear the buffers and in the d3d version - it had the same, but in d3d code. I hope this helps a bit!

- Drew

PS as for your class that you showed - instead it would be more like DrawTerrain, DrawPlayer, DrawFog - or something like that.
Xetrov
Xetrov
The point is so that as far as the rest of your code is concerned, it doesn't matter if you are using OpenGL or Direct3D or whatever. The render class can be changed for the different graphics APIs, and you dont have to change any of your code.
Tarviathun
Tarviathun
It's a method of abstraction so that you can extend the base interface class and specifically program to take advantage of an API.
emosoft05
emosoft05
maybe the render class is supposed to be similar to a node in a scene graph?
Darobat
Darobat
Ahhh.... Thankyou.

On another, slightly related note, would a Camera class and a Lighting class be part of the games engine?
--------------------C++ Home - Check it out!Lol... - Amazing video
Drew_Benton
Drew_Benton
Quote:
Original post by Darobat
Ahhh.... Thankyou.

On another, slightly related note, would a Camera class and a Lighting class be part of the games engine?


In actuality, you can add whatever your heart desires to your engine [smile], it is your engine. In my personaly opinion, camera and lighting I think would be more part of the rendering portion of the engine and not just free components - since the way you draw your levels and characters will have an effect on how your cameras and lighting works. So to answer your question - yes - but they would be sub components of something else. Just one idea on the matter.

- Drew

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.