2D features

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
wetfire
Posts: 10
Joined: Wed Aug 30, 2006 12:10 pm
Location: Italy

2D features

Post by wetfire »

Hi, I added 2 functions to irrlicht:
draw2DFilledPolygon and draw2DPolygon

I use them to draw polygons in gui elements.
Currently it works only with opengl driver ( I don't know directX ).

Suggestions and a directX version would be appreciated.

Just add next code to IVideoDriver.h, CNullDriver.h, COpenGLDriver.h

Code: Select all

...

//! Draws a  filled  2d polyon.
/** This method can be used to draw a generic filled polygon.
      It uses the color stored in S3DVertex to hue the polygon
      \param vertices: Array of S3DVertex that stores the polygon vertices.
      \param verticesCount: The number of vertices to draw.
*/
virtual void draw2DFilledPolygon(const S3DVertex* vertices, int verticesCount ) = 0;
         
//! Draws a  non filled generic 2d polyon.
/** This method can be used to draw a generic  non filled 2D polygon.
      It uses the passed color to hue the polygon.
      S3DVertex.Color is ignored.
       \param vertices: Array of S3DVertex that stores the polygon vertices.
	\param verticesCount: The number of vertices to draw.
*/
virtual void draw2DPolygon(const S3DVertex* vertices, int verticesCount ) = 0;
...


Just add next code to CNullDriver.cpp

Code: Select all

void CNullDriver::draw2DFilledPolygon(const S3DVertex* vertices, int count )
{
	PrimitivesDrawn += 1;
}

void CNullDriver::draw2DPolygon(const S3DVertex* vertices, int count )
{
	PrimitivesDrawn += 1;
}
Just add next code to COpenGLDriver.cpp

Code: Select all

void COpenGLDriver::draw2DFilledPolygon(const S3DVertex* vertices, int count )
{
	setRenderStates2DMode(true, false, false);
	setTexture(0,0);

	core::dimension2d<s32> currentRendertargetSize = getCurrentRenderTargetSize();
	const s32 xPlus = -(currentRendertargetSize.Width>>1);
	const f32 xFact = 1.0f / (currentRendertargetSize.Width>>1);

	const s32 yPlus =
	currentRendertargetSize.Height-(currentRendertargetSize.Height>>1);
	const f32 yFact = 1.0f / (currentRendertargetSize.Height>>1);

	glBegin(GL_POLYGON);
    for( int i = 0; i < count; i++ )
	{
	  glColor4ub( vertices[i].Color.getRed(), vertices[i].Color.getGreen(), vertices[i].Color.getBlue(), vertices[i].Color.getAlpha() );


	glVertex2f(
	            (f32)( vertices[i].Pos.X + xPlus) * xFact,
	            (f32)(yPlus - vertices[i].Pos.Y) * yFact
		       );

	}
	glEnd();
}

void COpenGLDriver::draw2DPolygon(const S3DVertex* vertices, int count )
{
	setRenderStates2DMode(true, false, false);
	setTexture(0,0);

	core::dimension2d<s32> currentRendertargetSize = getCurrentRenderTargetSize();
	const s32 xPlus = -(currentRendertargetSize.Width>>1);
	const f32 xFact = 1.0f / (currentRendertargetSize.Width>>1);

	const s32 yPlus =
	currentRendertargetSize.Height-(currentRendertargetSize.Height>>1);
	const f32 yFact = 1.0f / (currentRendertargetSize.Height>>1);

	glBegin(GL_LINE_LOOP);

	for( int i = 0; i < count; i++ )
	{
	  glColor4ub( vertices[i].Color.getRed(), vertices[i].Color.getGreen(), vertices[i].Color.getBlue(), vertices[i].Color.getAlpha() );


	glVertex2f(
	            (f32)( vertices[i].Pos.X + xPlus) * xFact,
	            (f32)(yPlus - vertices[i].Pos.Y) * yFact
		       );

	}
	glEnd();
}

PS: USE IT AT YOUR OWN RISK! :lol:
Nothing is impossible!

My photolog
Post Reply