Kamis, 12 Oktober 2017

Membuat Berbagai Macam Bentuk di Dev C++ Part 2

1. Membuat garis putus-putus


syntax:
 
#include  
#include  

#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES);         \ 
                                 glVertex2f ((x1),(y1));    \ 
                                 glVertex2f ((x2),(y2));    \ 
                                 glEnd();  
void init(void)  
{ 
   glClearColor (0.0, 0.0, 0.0, 0.0); 
   glShadeModel (GL_FLAT); 
} 
void display(void) 

int
 i; 
   glClear (GL_COLOR_BUFFER_BIT); 
   glColor3f (0.0, 1.0, 0.0); 
   glEnable (GL_LINE_STIPPLE); 
   glLineStipple (1, 0x0101);               /*  dotted  */ 
   drawOneLine (50.0, 125.0, 150.0, 125.0); 
   glLineStipple (1, 0x00FF);               /*  dashed  */ 
   drawOneLine (150.0, 125.0, 250.0, 125.0); 
   glLineStipple (1, 0x1C47);               /*  dash/dot/dash  */ 
   drawOneLine (250.0, 125.0, 350.0, 125.0); 
   glLineWidth (50.0); 
   glLineStipple (1, 0x0101);               /*  dotted  */ 
   drawOneLine (50.0, 100.0, 150.0, 100.0); 
   glLineStipple (1, 0x00FF);               /*  dashed  */ 
   drawOneLine (150., 100.0, 250.0, 100.0); 
   glLineStipple (1, 0x1C47);               /*  dash/dot/dash  */   
   drawOneLine (250.0, 100.0, 350.0, 100.0); 
   glLineWidth (1.0); 
   glLineStipple (1, 0x1C47);               /*  dash/dot/dash  */ 
   glBegin (GL_LINE_STRIP); 
   for (i = 0; i < 7; i++) 
      glVertex2f (50.0 + ((GLfloat) i * 50.0), 75.0); 
   glEnd (); 

for (i = 0; i < 6; i++) 
{ 
      drawOneLine (50.0 + ((GLfloat) i * 50.0), 50.0,
  50.0 + ((GLfloat)(i+1) * 50.0), 50.0); 
} 
   glLineStipple (5, 0x1C47);              /*  dash/dot/dash  */ 
   drawOneLine (50.0, 25.0, 350.0, 25.0); 
   glDisable (GL_LINE_STIPPLE); 
   glFlush (); 
}
void reshape (int w, int h) 
{ 
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION); 
   glLoadIdentity (); 
   gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h); 
} 
int main(int argc, char** argv) 
{ 
   glutInit(&argc, argv); 
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
   glutInitWindowSize (400, 150);  
   glutInitWindowPosition (100, 100); 
   glutCreateWindow (argv[0]); 
   init (); 
   glutDisplayFunc(display);  
   glutReshapeFunc(reshape); 
   glutMainLoop(); 
return 0; 
} 




2. Membuat seperti barcode


syntax:
 

#include  
#include  

#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES);         \ 
                                 glVertex2f ((x1),(y1));    \ 
                                 glVertex2f ((x2),(y2));    \ 
                                 glEnd();  
void init(void)  
{ 
   glClearColor (0.0, 0.0, 0.0, 0.0); 
   glShadeModel (GL_FLAT); 
} 
void display(void) 

int
 i; 
   glClear (GL_COLOR_BUFFER_BIT); 
   glColor3f (0.0, 1.0, 0.0); 
   glEnable (GL_LINE_STIPPLE); 
   glLineStipple (1, 0x0101);               /*  dotted  */ 
   drawOneLine (50.0, 125.0, 150.0, 125.0); 
   glLineStipple (1, 0x00FF);               /*  dashed  */ 
   drawOneLine (150.0, 125.0, 250.0, 125.0); 
   glLineStipple (1, 0x1C47);               /*  dash/dot/dash  */ 
   drawOneLine (250.0, 125.0, 350.0, 125.0); 
   glLineWidth (50.0); 
   glLineStipple (1, 0x0101);               /*  dotted  */ 
   drawOneLine (50.0, 100.0, 150.0, 100.0); 
   glLineStipple (1, 0x00FF);               /*  dashed  */ 
   drawOneLine (150., 100.0, 250.0, 100.0); 
   glLineStipple (1, 0x1C47);               /*  dash/dot/dash  */   
   drawOneLine (250.0, 100.0, 350.0, 100.0); 
   glLineWidth (1.0); 
   glLineStipple (1, 0x1C47);               /*  dash/dot/dash  */ 
   glBegin (GL_LINE_STRIP); 
   for (i = 0; i < 7; i++) 
      glVertex2f (50.0 + ((GLfloat) i * 50.0), 75.0); 
   glEnd (); 

for (i = 0; i < 6; i++) 
{ 
      drawOneLine (50.0 + ((GLfloat) i * 50.0), 50.0,
  50.0 + ((GLfloat)(i+1) * 50.0), 50.0); 
} 
   glLineStipple (5, 0x1C47);              /*  dash/dot/dash  */ 
   drawOneLine (50.0, 25.0, 350.0, 25.0); 
   glDisable (GL_LINE_STIPPLE); 
   glFlush (); 
}
void reshape (int w, int h) 
{ 
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION); 
   glLoadIdentity (); 
   gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h); 
} 
int main(int argc, char** argv) 
{ 
   glutInit(&argc, argv); 
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
   glutInitWindowSize (400, 150);  
   glutInitWindowPosition (100, 100); 
   glutCreateWindow (argv[0]); 
   init (); 
   glutDisplayFunc(display);  
   glutReshapeFunc(reshape); 
   glutMainLoop(); 
return 0; 
} 



3. Membuat seperti Kumbang.


syntax:
 

#include 
void display(void)
{
   GLubyte fly[] = {
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
 0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
 0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
 0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
 0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
 0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
 0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
 0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
 0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
 0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
 0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
 0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};

   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (0.0, 1.0, 0.0);
   glRectf (25.0, 125.0, 125.0, 350.0);
   glEnable (GL_POLYGON_STIPPLE);
   glPolygonStipple (fly);
   glRectf (200.0, 125.0, 800.0, 350.0);
   glDisable (GL_POLYGON_STIPPLE);
   glFlush ();
}
void init (void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);    
}
void reshape (int w, int h) {
  glViewport (50, 0,(GLsizei) w, (GLsizei) h);
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (1000, 500);    glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
return 0;  

}


4. Membuat Kotak warna warni.


syntax:
 

#include  
int board[3][3];   /* jumlah warna untuk tiap kotak   */ 
/*  Clear nilai warna untuk setiap kotak pada board   */ 
void init(void) 
{ 
int i, j; 
for (i = 0; i < 3; i++)  
for (j = 0; j < 3; j ++) 
         board[i][j] = 0; 
   glClearColor (0.0, 0.0, 0.0, 0.0); 
} 
void drawSquares(GLenum mode) 
{ 
   GLuint i, j; for (i = 0; i < 3; i++)  
   { 
if (mode == GL_SELECT) 
glLoadName (i); 
for (j = 0; j < 3; j ++)  
{ 
if (mode == GL_SELECT) 
            glPushName (j); 
            glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0, (GLfloat) board[i][j]/3.0); 
            glRecti (i, j, i+1, j+1); 
if (mode == GL_SELECT) glPopName (); 
          } 
   } 
} 
void display(void) 
{ 
   glClear(GL_COLOR_BUFFER_BIT); 
   drawSquares (GL_RENDER); 
   glFlush(); 
} 
void reshape(int w, int h) 
{ 
   glViewport(0, 0, w, h); 
   glMatrixMode(GL_PROJECTION); 
   glLoadIdentity(); 
   gluOrtho2D (0.0, 3.0, 0.0, 3.0
); 
   glMatrixMode(GL_MODELVIEW); 
   glLoadIdentity(); 
} 
int main(int argc, char** argv) 
{ 
   glutInit(&argc, argv); 
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
   glutInitWindowSize (400, 400); 
   glutInitWindowPosition (100, 100); 
   glutCreateWindow (argv[0]); 
   init (); 
   glutReshapeFunc (reshape); 
   glutDisplayFunc(display);  
   glutMainLoop(); 
return 0;  
} 


5. Membuat bitmap font



syntax:
 

#include  
#include  
void *font = GLUT_BITMAP_TIMES_ROMAN_24; 
void *fonts[] ={GLUT_BITMAP_9_BY_15, GLUT_BITMAP_TIMES_ROMAN_10, GLUT_BITMAP_TIMES_ROMAN_24}; 
char defaultMessage[] = "Pustaka GLUT OpenGL."; 
char *message = defaultMessage; 
void selectFont(
int newfont) 
{ 
  font = fonts[newfont]; 
  glutPostRedisplay(); 
} 
void selectMessage(int msg) 
{ 
switch (msg)
{ 
case1: message = "pustaka glut openGL...kecil."; 
break; 
case2: message = "PUSTAKA GLUT OPENGL...BESAR."; 
break; 
  } 
} 
void selectColor(int color) 
{ 
switch (color)
{ 
case1: glColor3f(0.0, 1.0, 0.0); 
break; 
case2: glColor3f(1.0, 0.0, 0.0); 
break; 
case3: glColor3f(1.0, 1.0, 1.0); 
break; 
} 
  glutPostRedisplay(); 
} 
void tick(void) 
{ 
  glutPostRedisplay(); 
} 
void output(int x, int y, char *string) 
{ 
int len, i; 
 glRasterPos2f(x, y); 
  len = (int) strlen(string);
for (i = 0; i < len; i++)  
  { 
    glutBitmapCharacter(font, string[i]); 
  } 
} 
void display(void) 
{ 
  glClear(GL_COLOR_BUFFER_BIT); 
  output(0, 24, "HELLO NAMA SAYA NIDYA OKTAVIANA BELAJAR GLUT bitmap font."); 
  output(100, 100, message); 
  output(0, 145,"(Posisi dalam PIXEL dengan dimulai atas kiri, hohoho)"); 
  glutSwapBuffers(); 
} 
void reshape(int w, int h) 
{ 
  glViewport(0, 0, w, h); 
  glMatrixMode(GL_PROJECTION); 
  glLoadIdentity(); 
  gluOrtho2D(0, w, h, 0); 
  glMatrixMode(GL_MODELVIEW); 
} 
int main(int argc, char**argv) 
{ 
int i, msg_submenu, color_submenu; 
  glutInit(&argc, argv); 
for (i = 1; i < argc; i++)
{ 
if (!strcmp(argv[i], "-mono"))
{ 
      font = GLUT_BITMAP_9_BY_15; 
    } 
  } 
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
  glutInitWindowSize(600, 150); 
  glutCreateWindow("GLUT bitmap font example"); 
  glClearColor(0.0, 0.0, 0.0, 1.0); 
  glutDisplayFunc(display); 
  glutReshapeFunc(reshape); 
  glutIdleFunc(tick); 
  msg_submenu = glutCreateMenu(selectMessage); 
  glutAddMenuEntry("huruf kecil",1); 
  glutAddMenuEntry("HURUF BESAR", 2); 
  color_submenu = glutCreateMenu(selectColor); 
  glutAddMenuEntry("HIJAU", 1); 
  glutAddMenuEntry("MERAH", 2); 
  glutAddMenuEntry("PUTIH", 3); 
  glutCreateMenu(selectFont); 
  glutAddMenuEntry("Default", 0); 
  glutAddMenuEntry("Times Roman 10", 1); 
  glutAddMenuEntry("Times Roman 24", 2); 
  glutAddSubMenu("Messages", msg_submenu); 
  glutAddSubMenu("Warna", color_submenu); 
  glutAttachMenu(GLUT_RIGHT_BUTTON); 
  glutMainLoop(); 
return 0;              
} 


Tugas :

1. Buatlah program untuk menentukan garis strip dengan warna lain dengan koordinat vertek 1 (127,80), dan vertex 2 (500,100)


syntax:
 
#include 
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 1.0);
glColor3f (1.0, 0.0, 1.0);
glEnable (GL_LINE_STIPPLE);
// glLineStipple (1, 0x0101); /* membuat titik */
glLineStipple (1, 0x00ff); /* membuat strip-strip */
//glLineStipple (1, 0x10ff); /* membuat strip titik strip */
glBegin(GL_LINE_STRIP);
glVertex2f (127, 80);
glVertex2f (500, 100);
glEnd();
glDisable (GL_LINE_STIPPLE);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 150);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;

}


2. Seperti pada program 3, buatlah gambar lain yg di sukai.


syntax:
 

#include 
void display(void)
{
GLubyte line[] = {
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
 0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
 0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
 0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
 0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
 0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
 0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
 0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
 0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
 0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08,
 0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
 0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
 0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
 0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
 0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
 0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
 0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08
};
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (2.0, 1.0, 3.0);
glEnable (GL_POLYGON_STIPPLE);
glPolygonStipple (line);
glRectf (100.0, 100.0, 300.0, 320.0);
glDisable (GL_POLYGON_STIPPLE);
glFlush ();
}
void init (void)
{
glClearColor (1.1, 0.0, 0.0, 0.1);
glShadeModel (GL_FLAT);
}
void reshape (int w, int h)
{
glViewport (50, 0,(GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (1000, 500);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;

}


Tidak ada komentar:

Posting Komentar