SlideShare una empresa de Scribd logo
1 de 18
Descargar para leer sin conexión
////////////////////////////////////////////
//
// A Virtual FrameBuffer
//
//

class CDrawSurface
{

private:
  int m_x;                           // Width
  int m_y;                           // Height
  int m_size;                        // Width*Height*4
  bool m_init_val;                   // Has Allocated Buffer ?
  UINT *pixels;                     // Array of Pixels
  int m_curr_x;                     // Current Raster (Cursor) position - X
  int m_curr_y;                     // Current Raster poisition – Y

    <.... Rest of the Code ....>
}
//////////////////////////////////////////////
//
// Ctor
//



CdrawSurface::CDrawSurface( int x , int y )
{
    m_x = x;
    m_y = y;
    m_size = m_x*m_y;
    pixels = new UINT[ m_size ];
    m_init_val = true;
}
///////////////////////////////////////////
//
// COLOR data structure
// Red , Green , Blue and Alpha ( 255 opaque , 0 - transparent )
//
typedef struct
{
  BYTE r;
  BYTE g;
  BYTE b;
  BYTE a;
}COLOR;



void CdrawSurface::Clear( COLOR *col )
  {
       UINT Value = ( col->a << 24 | col->r << 16 | col->g << 8 | col->b);
       UINT *p_pixels = pixels;
       int i=0;
       while ( i < m_size )
       {
             *p_pixels++ = Value;
             i++;

       }
 }
////////////////////////////////////////////////////////////
//
// Plot a Pixel at (x,y)
//
//
void CdrawSurface::PutPixel( int x , int y , COLOR *col )
{

      if ( ( x < 0 || x > m_x ) || (y < 0 || y > m_y ) )
                              return;

      int offset = y*m_y*4 + x*4;
      UINT r = col->r;
      UINT g = col->g;
      UINT b = col->b;
      char *rs =(char *)((char *)pixels + offset);
      *rs++=b;
      *rs++=g;
      *rs++=r;
      *rs++=0xFF;
  }
/////////////////////////////////////////////////
//
// How do i transfer it into a Window ?
//
//
int CdrawSurface::Render( HDC dc )
   {
           BITMAPINFO bmi;
           LPVOID pvBits;
      ZeroMemory(&bmi, sizeof(BITMAPINFO));

     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
     bmi.bmiHeader.biWidth = m_x;
     bmi.bmiHeader.biHeight = -m_y;
     bmi.bmiHeader.biPlanes = 1;
     bmi.bmiHeader.biBitCount = 32;     // four 8-bit components
     bmi.bmiHeader.biCompression = BI_RGB;
     bmi.bmiHeader.biSizeImage = m_x * m_y * 4;
     HBITMAP hbitmap = CreateDIBSection(dc, &bmi,
                             DIB_RGB_COLORS,
                             &pvBits, NULL, 0x0);

     memcpy(pvBits,pixels,m_x*m_y*4); // Copy the pixels into DIB

      StretchDIBits(dc,
                // destination rectangle
                0, 0, m_x, m_y,
                // source rectangle
                0, 0, m_x,m_y,
                pvBits,
                &bmi,
                DIB_RGB_COLORS,
                SRCCOPY);
     return 1;
 }
////////////////////////////////////////
//
// A Simple Demo to Clear the Screen
//
void Demo2( HDC hdc , int width,int height )
{
    CDrawSurface ds(width,height);
    COLOR clr;
         clr.a = 255;
         clr.b = 0;
         clr.g = 0;
         clr.r = 255;
         ds.Clear(&clr);
    ds.Render(hdc);

}
////////////////////////////////////////
//
// A Simple Demo to Show the primitives
//
void Demo3( HDC hdc , int width,int height )
{
    CDrawSurface ds(width,height);
    COLOR clr;
         clr.a = 255;
         clr.b = 0;
         clr.g = 0;
         clr.r = 0;
         ds.Clear(&clr);
         clr.a = 255;
         clr.b=255;
         clr.g=0;
         clr.r=255;
         ds.Line(10,10,100,100,&clr);
         ds.Circle ( 100,100,150,&clr);
         ds.FilledCircle ( 100,100,100,&clr);
         ds.Render(hdc);

}
PART 2
class Matrix2D
{
private :
      double m11,m12,m13;
      double m21,m22,m23;
      double m31,m32,m33;


};
Scale




 void Matrix2D::Scale( double xscale , double yscale )
{
            Matrix2D *mat = new Matrix2D();
           mat->SetIdentity ();
           mat->m11=xscale;
           mat->m22=yscale;
           PreMultiply(*mat);
           delete mat;
}
Translate




void Matrix2D::Translate( double xtrans , double ytrans )
     {
           Matrix2D *mat = new Matrix2D();
          mat->SetIdentity ();
           mat->m13=xtrans;
          mat->m23=ytrans;
          PreMultiply(*mat);
          delete mat;

     }
Rotate




void Matrix2D::Rotate( double rot )
     {
           double angle = rot*3.14159/180.0;
           Matrix2D *mat = new Matrix2D();
           mat->SetIdentity ();
           mat->m11=cos(angle);
           mat->m12=-sin(angle);
           mat->m21=sin(angle);
           mat->m22=cos(angle);
           PreMultiply (*mat);
           delete mat;


     }
class CTransformDrawSurface : public CDrawSurface
{

private:
   Matrix2D tr_mat;

public:

void Scale( double xscale , double yscale ) {
    tr_mat.Scale (xscale,yscale);
}

void Rotate( double rot ){
           tr_mat.Rotate( rot);
 }

void Translate( double xtrans , double ytrans ) {
    tr_mat.Translate(xtrans,ytrans);
}

void Transform( double& x , double& y )
{
     tr_mat.Transform ( x,y);
}

};
void TestPolyFill( HDC dc , CTransformDrawSurface& dr , COLOR *col)
{
      POINT2D Poly[] = { { 0,0 } , { 0 , 150 },{ 150,150 },{ 100,100}};

     dr.ResetTransform ();
     dr.Rotate(-90);
     dr.Scale(1,-1);
     dr.Translate(200,200);
     for( int i = 0; i<sizeof(Poly)/sizeof(Poly[0]);i++)
     {
           double x = Poly[i].X;
           double y = Poly[i].Y;
           dr.Transform ( x,y );
           Poly[i].Y = y;
           Poly[i].X = x;
     }

      POLYGON2D rs ;
     rs.Length = sizeof(Poly)/ sizeof(POINT2D);
     rs.Points = Poly;
     dr.FillPolygon ( &rs , col );
     return;
}
////////////////////////////////////////
//
// A Simple Demo to Show Filled Polygon with Transformation
//
void Demo4( HDC hdc , int width,int height )
{
         CTransformDrawSurface ds(width,height);
         COLOR clr;
         clr.a = 255;
         clr.b = 0;
         clr.g = 0;
         clr.r = 0;
         ds.Clear(&clr);
         clr.r = 127;
        TestPolyFill(hdc,ds,&clr);
         ds.Render(hdc);

}
void TestQuad(HDC dc , CTransformDrawSurface& dr , COLOR *col)
{
      POINT2D Quad[] = { { 0,0},{0,100},{100,100},{100,0} };

     dr.ResetTransform ();
     dr.Scale ( 1,-1);
     dr.Translate(dr.m_x /2,dr.m_y /2);

     for( int i = 0; i<sizeof(Quad)/sizeof(POINT2D);i++)
     {
            double x = Quad[i].X;
            double y = Quad[i].Y;
            dr.Transform ( x,y );
            Quad[i].Y = y;
            Quad[i].X = x;
     }
     POLYGON2D rs ;
     rs.Length = sizeof(Quad)/ sizeof(POINT2D);
     rs.Points = Quad;
     col->g = 255;
     dr.FillPolygon ( &rs , col );
}
////////////////////////////////////////
//
// A Simple Demo to Show Quad Transformation
//
void Demo5( HDC hdc , int width,int height )
{
    CTransformDrawSurface ds(width,height);
         COLOR clr;
         clr.a = 255;
         clr.b = 0;
         clr.g = 0;
         clr.r = 0;
         ds.Clear(&clr);
         clr.r = 127;
    TestQuad(hdc,ds,&clr);
         ds.Render(hdc);

}
Bresenham's Line Algorithm




Bresenham's Circle algorithm

Más contenido relacionado

La actualidad más candente

La actualidad más candente (9)

Cg lab cse-vii
Cg lab cse-viiCg lab cse-vii
Cg lab cse-vii
 
Gaussel Method
Gaussel MethodGaussel Method
Gaussel Method
 
Passato
PassatoPassato
Passato
 
Solidos de revolución
Solidos de revoluciónSolidos de revolución
Solidos de revolución
 
Linker
LinkerLinker
Linker
 
Tugas graphik komputer wendy
Tugas graphik komputer wendyTugas graphik komputer wendy
Tugas graphik komputer wendy
 
Los fantastico
Los fantasticoLos fantastico
Los fantastico
 
Contoh TPW2
Contoh TPW2Contoh TPW2
Contoh TPW2
 
Inm si impa nr reale rep prin lit
Inm si impa nr reale rep prin litInm si impa nr reale rep prin lit
Inm si impa nr reale rep prin lit
 

Destacado

Emporkommen executive coaching ppt
Emporkommen executive coaching pptEmporkommen executive coaching ppt
Emporkommen executive coaching pptYateesh Prahlad
 
The internet and democracy
The internet and  democracyThe internet and  democracy
The internet and democracyhotmanila
 

Destacado (6)

Child Health
Child HealthChild Health
Child Health
 
Blood Search
Blood SearchBlood Search
Blood Search
 
Emporkommen executive coaching ppt
Emporkommen executive coaching pptEmporkommen executive coaching ppt
Emporkommen executive coaching ppt
 
The internet and democracy
The internet and  democracyThe internet and  democracy
The internet and democracy
 
South korea1
South korea1South korea1
South korea1
 
Pooling The Carpool
Pooling The CarpoolPooling The Carpool
Pooling The Carpool
 

Más de Barcamp Kerala

Más de Barcamp Kerala (9)

Multi Monitor Machines
Multi Monitor MachinesMulti Monitor Machines
Multi Monitor Machines
 
The Drupal Way
The Drupal WayThe Drupal Way
The Drupal Way
 
Udev
UdevUdev
Udev
 
Ltsp
LtspLtsp
Ltsp
 
Bar Camp Kerala 7
Bar Camp Kerala 7Bar Camp Kerala 7
Bar Camp Kerala 7
 
Praseed Pai
Praseed PaiPraseed Pai
Praseed Pai
 
Starting Up - What they did not teach you at B-School
Starting Up - What they did not teach you at B-SchoolStarting Up - What they did not teach you at B-School
Starting Up - What they did not teach you at B-School
 
Bar Camp 11 Oct09 Hacking
Bar Camp 11 Oct09 HackingBar Camp 11 Oct09 Hacking
Bar Camp 11 Oct09 Hacking
 
Young Kerala Technopark
Young Kerala TechnoparkYoung Kerala Technopark
Young Kerala Technopark
 

Code Snippet

  • 1. //////////////////////////////////////////// // // A Virtual FrameBuffer // // class CDrawSurface { private: int m_x; // Width int m_y; // Height int m_size; // Width*Height*4 bool m_init_val; // Has Allocated Buffer ? UINT *pixels; // Array of Pixels int m_curr_x; // Current Raster (Cursor) position - X int m_curr_y; // Current Raster poisition – Y <.... Rest of the Code ....> }
  • 2. ////////////////////////////////////////////// // // Ctor // CdrawSurface::CDrawSurface( int x , int y ) { m_x = x; m_y = y; m_size = m_x*m_y; pixels = new UINT[ m_size ]; m_init_val = true; }
  • 3. /////////////////////////////////////////// // // COLOR data structure // Red , Green , Blue and Alpha ( 255 opaque , 0 - transparent ) // typedef struct { BYTE r; BYTE g; BYTE b; BYTE a; }COLOR; void CdrawSurface::Clear( COLOR *col ) { UINT Value = ( col->a << 24 | col->r << 16 | col->g << 8 | col->b); UINT *p_pixels = pixels; int i=0; while ( i < m_size ) { *p_pixels++ = Value; i++; } }
  • 4. //////////////////////////////////////////////////////////// // // Plot a Pixel at (x,y) // // void CdrawSurface::PutPixel( int x , int y , COLOR *col ) { if ( ( x < 0 || x > m_x ) || (y < 0 || y > m_y ) ) return; int offset = y*m_y*4 + x*4; UINT r = col->r; UINT g = col->g; UINT b = col->b; char *rs =(char *)((char *)pixels + offset); *rs++=b; *rs++=g; *rs++=r; *rs++=0xFF; }
  • 5. ///////////////////////////////////////////////// // // How do i transfer it into a Window ? // // int CdrawSurface::Render( HDC dc ) { BITMAPINFO bmi; LPVOID pvBits; ZeroMemory(&bmi, sizeof(BITMAPINFO)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = m_x; bmi.bmiHeader.biHeight = -m_y; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; // four 8-bit components bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biSizeImage = m_x * m_y * 4; HBITMAP hbitmap = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0); memcpy(pvBits,pixels,m_x*m_y*4); // Copy the pixels into DIB StretchDIBits(dc, // destination rectangle 0, 0, m_x, m_y, // source rectangle 0, 0, m_x,m_y, pvBits, &bmi, DIB_RGB_COLORS, SRCCOPY); return 1; }
  • 6. //////////////////////////////////////// // // A Simple Demo to Clear the Screen // void Demo2( HDC hdc , int width,int height ) { CDrawSurface ds(width,height); COLOR clr; clr.a = 255; clr.b = 0; clr.g = 0; clr.r = 255; ds.Clear(&clr); ds.Render(hdc); }
  • 7. //////////////////////////////////////// // // A Simple Demo to Show the primitives // void Demo3( HDC hdc , int width,int height ) { CDrawSurface ds(width,height); COLOR clr; clr.a = 255; clr.b = 0; clr.g = 0; clr.r = 0; ds.Clear(&clr); clr.a = 255; clr.b=255; clr.g=0; clr.r=255; ds.Line(10,10,100,100,&clr); ds.Circle ( 100,100,150,&clr); ds.FilledCircle ( 100,100,100,&clr); ds.Render(hdc); }
  • 9. class Matrix2D { private : double m11,m12,m13; double m21,m22,m23; double m31,m32,m33; };
  • 10. Scale void Matrix2D::Scale( double xscale , double yscale ) { Matrix2D *mat = new Matrix2D(); mat->SetIdentity (); mat->m11=xscale; mat->m22=yscale; PreMultiply(*mat); delete mat; }
  • 11. Translate void Matrix2D::Translate( double xtrans , double ytrans ) { Matrix2D *mat = new Matrix2D(); mat->SetIdentity (); mat->m13=xtrans; mat->m23=ytrans; PreMultiply(*mat); delete mat; }
  • 12. Rotate void Matrix2D::Rotate( double rot ) { double angle = rot*3.14159/180.0; Matrix2D *mat = new Matrix2D(); mat->SetIdentity (); mat->m11=cos(angle); mat->m12=-sin(angle); mat->m21=sin(angle); mat->m22=cos(angle); PreMultiply (*mat); delete mat; }
  • 13. class CTransformDrawSurface : public CDrawSurface { private: Matrix2D tr_mat; public: void Scale( double xscale , double yscale ) { tr_mat.Scale (xscale,yscale); } void Rotate( double rot ){ tr_mat.Rotate( rot); } void Translate( double xtrans , double ytrans ) { tr_mat.Translate(xtrans,ytrans); } void Transform( double& x , double& y ) { tr_mat.Transform ( x,y); } };
  • 14. void TestPolyFill( HDC dc , CTransformDrawSurface& dr , COLOR *col) { POINT2D Poly[] = { { 0,0 } , { 0 , 150 },{ 150,150 },{ 100,100}}; dr.ResetTransform (); dr.Rotate(-90); dr.Scale(1,-1); dr.Translate(200,200); for( int i = 0; i<sizeof(Poly)/sizeof(Poly[0]);i++) { double x = Poly[i].X; double y = Poly[i].Y; dr.Transform ( x,y ); Poly[i].Y = y; Poly[i].X = x; } POLYGON2D rs ; rs.Length = sizeof(Poly)/ sizeof(POINT2D); rs.Points = Poly; dr.FillPolygon ( &rs , col ); return; }
  • 15. //////////////////////////////////////// // // A Simple Demo to Show Filled Polygon with Transformation // void Demo4( HDC hdc , int width,int height ) { CTransformDrawSurface ds(width,height); COLOR clr; clr.a = 255; clr.b = 0; clr.g = 0; clr.r = 0; ds.Clear(&clr); clr.r = 127; TestPolyFill(hdc,ds,&clr); ds.Render(hdc); }
  • 16. void TestQuad(HDC dc , CTransformDrawSurface& dr , COLOR *col) { POINT2D Quad[] = { { 0,0},{0,100},{100,100},{100,0} }; dr.ResetTransform (); dr.Scale ( 1,-1); dr.Translate(dr.m_x /2,dr.m_y /2); for( int i = 0; i<sizeof(Quad)/sizeof(POINT2D);i++) { double x = Quad[i].X; double y = Quad[i].Y; dr.Transform ( x,y ); Quad[i].Y = y; Quad[i].X = x; } POLYGON2D rs ; rs.Length = sizeof(Quad)/ sizeof(POINT2D); rs.Points = Quad; col->g = 255; dr.FillPolygon ( &rs , col ); }
  • 17. //////////////////////////////////////// // // A Simple Demo to Show Quad Transformation // void Demo5( HDC hdc , int width,int height ) { CTransformDrawSurface ds(width,height); COLOR clr; clr.a = 255; clr.b = 0; clr.g = 0; clr.r = 0; ds.Clear(&clr); clr.r = 127; TestQuad(hdc,ds,&clr); ds.Render(hdc); }