Contoh Program C++ || Membuat Gambar 2D With C++

Assalamualaikum hamba Allah...........
Pada postingan kali ini saya akan membagikan srouce C++ lagi dan lagi.
Postingan kali ini saya akan membuat meja dan kursi 2D (burik sih).
Langsung saja saya share sroucenya dibawah ini:


//program dua dimensi
#include<GL/glut.h>
#include<math.h>

typedef struct {
            float x,y;
} point2D_t;
typedef struct {
            int r,g,b;
} color_t;
void setColor ( color_t col )
{ glColor3f(col.r, col.g, col.b); }
void fillPolygon (point2D_t pnt[], int n, color_t color)
{
            int i;
            setColor(color);
            glBegin(GL_POLYGON);
            for (i=0; i<n; ++i)
            {
                        glVertex2f(pnt[i].x, pnt[i].y);
            }
            glEnd();
}

void drawPolygon(point2D_t pnt[], int n)
{
            int i;
            glBegin(GL_POLYGON);
                for(i=0; i<n; i++)
                        glVertex2f(pnt[i].x, pnt[i].y);
            glEnd();
}

void userdraw()
{  static int tic = 0;
   point2D_t
                        meja[4]={{20,80},{60,160},{200,160},{160,80}};
   color_t magenta ={1,0,1};
       fillPolygon(meja,4, magenta);
               //setColor(1,0,1);
               drawPolygon(meja,4);

   point2D_t
               depankiri[4]={{20,20}, {20,80}, {30,80}, {30,20}};
   color_t white ={1,1,1};
       fillPolygon(depankiri,4, white);
               //setColor(1,1,1);
               drawPolygon(depankiri,4);

   point2D_t
               depankanan[4]={{150,20},{150,80},{160,80},{160,20}};
   color_t green ={0,1,0};
       fillPolygon(depankanan,4, green);
               drawPolygon(depankanan,4);

               point2D_t
               belakangkiri[4]={{60,50},{60,80},{70,80},{70,50}};
   color_t yellow ={1,1,0};
       fillPolygon(belakangkiri,4, yellow);
               drawPolygon(belakangkiri,4);

               point2D_t
               belakangkanan[4]={{190,50},{190,140},{200,160},{200,50}};
   color_t red ={1,0,0};
       fillPolygon(belakangkanan,4, red);
               drawPolygon(belakangkanan,4);

               point2D_t
                        kursi[4]={{220,80},{240,120},{320,120},{300,80}};
   color_t birumuda ={0,1,1};
       fillPolygon(kursi,4, birumuda);
               //setColor(0,0,1);
               drawPolygon(kursi,4);

   point2D_t
               depankiri1[4]={{220,20}, {220,80}, {230,80}, {230,20}};
   color_t hijau ={0,1,0};
       fillPolygon(depankiri1,4, hijau);
               //setColor(0,1,0);
               drawPolygon(depankiri1,4);

   point2D_t
               depankanan1[4]={{290,20},{290,80},{300,80},{300,20}};
   color_t biru ={0,0,1};
       fillPolygon(depankanan1,4, biru);
               drawPolygon(depankanan1,4);

             point2D_t
               belakangkiri1[4]={{240,50},{240,80},{250,80},{250,50}};
   color_t kuning ={1,1,0};
       fillPolygon(belakangkiri1,4, kuning);
               drawPolygon(belakangkiri1,4);

               point2D_t
               belakangkanan1[4]={{310,50},{310,100},{320,120},{320,50}};
   color_t merah ={1,0,0};
       fillPolygon(belakangkanan1,4, merah);
               drawPolygon(belakangkanan1,4);
point2D_t
                        kursi1[4]={{300,80},{300,160},{320,200},{320,120}};
   color_t putih ={1,1,1};
       fillPolygon(kursi1,4, putih);
               //setColor(1,1,1);
               drawPolygon(kursi1,4);
              
               point2D_t
                        kursi2[4]={{290,160},{310,200},{320,200},{300,160}};
   color_t ungu ={1,0,1};
       fillPolygon(kursi2,4, ungu);
               //setColor(1,0,1);
               drawPolygon(kursi2,4);

                point2D_t
                        kursi3[4]={{290,160},{290,80},{300,80},{300,160}};
   color_t hijau1 ={0,1,0};
       fillPolygon(kursi3,4, hijau1);
               //setColor(0,1,0);
               drawPolygon(kursi3,4);
           
}
void display(void)
{          //clear screen
            glClear(GL_COLOR_BUFFER_BIT);
            userdraw();
            glutSwapBuffers();             }
int main(int argc, char **argv)            {
glutInit(&argc,argv);//Inisialisasi Toolkit
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow("Gambar rumah");
glClearColor(0.0,0.0,0.0,0.0);
gluOrtho2D(0.,640.,-240.,240.);
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Dan dibawah ini adalah output dari srouce diatas:



Sekian dari saya mungkin cuma itu saja terimakasih telah berkunjung ke blog ini.
Jangan lupa untuk FOLLOW blog ini.
Dan SUBSCRIBE My YouTube Channel.
@Qowi17
@Mas_Thom_17
@Busthomi TKJ
Share:
spacer

No comments:

Post a Comment