My 2D Software Renderer

Uwaga! Informacje na tej stronie mają ponad 5 lat. Nadal je udostępniam, ale prawdopodobnie nie odzwierciedlają one mojej aktualnej wiedzy ani przekonań.

# My 2D Software Renderer

Tue
18
Aug 2009

I've been recently working on rendering 2D primitives in pure software, without any hardware acceleration. Sure It's about foundations of computer graphics, but I did it just for fun. External libraries I've used are DevIL to read bitmap files (like TGA) and FreeType to read and render font files (like TTF). I compose graphics in my software renderer to my "surface" in RAM and finally present it on the window with SetDIBitsToDevice function. As you can see I've managed to reach real-time performance, despite I haven't done any advanced optimizations like MMX, SSE or multithreading.

Software Renderer 2D

My renderer supports drawing rectangles, lines, polygons, circles, ellipses, triangles, blitting bitmaps, as well as scissor test (clipping to rect), alpha test and alpha blending. Here is how the renderer interface looks like:

class Surface
{
public:
  Surface(const POINT_ &size);
  ~Surface();
  void LockRect(void **outPtr, int &outPitch, const RECTI *rect);
  void UnlockRect();
  ...
private:
  POINT_ m_Size;
  scoped_ptr<COLOR, DeleteArrayPolicy> m_Data;
};

class Renderer
{
public:
  Renderer();
  ~Renderer();
  void Paint(HDC hdc, const POINT_ &size);
  void SetSwapChain(Surface **surfaces, uint surfaceCount);
  void Present();
  Surface * GetBackBuffer();
  Surface * GetFrontBuffer();
  void SetRenderTarget(Surface *surface);
  void ResetRenderTarget();
  void SetClipRect(const RECTI &rect);
  void ResetClipRect();
  void SetColor(COLOR v);
  void SetAlphaTest(uint1 alphaRef);
  void ResetAlphaTest();
  void SetBlending(BLENDOP blendOp, BLEND srcBlend, BLEND dstBlend);
  void ResetBlending();
  void DrawPixel(const POINT_ &pos);
  void FillRect(const RECTI &rect);
  void DrawRect(const RECTI &rect);
  void Blt(const RECTI &srcRect, const POINT_ &dstPos, Surface *src);
  void DrawLine(const POINT_ &beg, const POINT_ &end);
  void DrawPolygon(const POINT_ *points, uint pointCount);
  void DrawCircle(const POINT_ &center, int radius);
  void FillCircle(const POINT_ &center, int radius);
  void DrawEllipse(const POINT_ &center, const POINT_ &radius);
  void FillEllipse(const POINT_ &center, const POINT_ &radius);
  void DrawTriangle(
    const VEC2 &p1, const VEC2 &p2, const VEC2 &p3,
    const COLORF &c1, const COLORF &c2, const COLORF &c3);
private:
  ...
};

extern Renderer *g_Renderer;

Surface * LoadSurfaceFromFile(const string &fileName);

class Font
{
public:
  void LoadFromFile(const string &fileName, int fontSize);
  void DrawText(const string &s, const common::POINT_ &pos, uint alignment);
  ...
};

Comments | #rendering #productions Share

Comments

STAT NO AD
[Stat] [STAT NO AD] [Download] [Dropbox] [pub] [Mirror] [Privacy policy]
Copyright © 2004-2019