Rabu, 11 Oktober 2017

Drawing Primitive 2 dengan menggunakan OpenGL Dev C++

Assalamualaikum hai hai hai saya hadir lagi ceman dengan membuat berbagai macam bentuk,garis,dll.Kali ini saya akan memberikan contoh "Drawing Primitive 2 dengan menggunakan OpenGL Dev C++" semoga contoh berikut bermanfaat ya ceman ☺☺☺.Jangan lewatkan contoh contoh berikutnya ya.............


Program 1 : Membuat Garis Lurus


CODING :

#include <iostream>
#include <GL/glut.h>

void display(void)
{
            glClear (GL_COLOR_BUFFER_BIT);
            glColor3f (1.0, 1.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 (325, 75);
                        glVertex2f (5, 75);
                        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 (400,150);
            glutInitWindowPosition (100, 100);
            glutCreateWindow (argv[0]);
            glutDisplayFunc (display);
            glutReshapeFunc (reshape);
            glutMainLoop ();
            return 0;
}


/* run this program using the console pauser or add your own getch, system("pause") or input loop */

OUTPUT :

______________________________________________________

Program 2 : Membuat macam strip

CODING :

#include <iostream>
#include <GL/glut.h>
#include <stdlib.h>

#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.0, 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;

}

OUTPUT :


______________________________________________________

Program 3 : Membuat Kumbang


CODING :

#include <iostream>
#include <GL/glut.h>

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;
}

OUTPUT :


______________________________________________________

Program 4 : Membuat Gradasi Warna

CODING :

#include <iostream>
#include <GL/glut.h>
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;
}

OUTPUT :


______________________________________________________

Program 5 : Membuat Tulisan

CODING :

#include <iostream>
#include <string.h>
#include <GL/glut.h>
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) {
case 1:
message = "pustaka glut openGL...kecil.";
break;
case 2:
message = "PUSTAKA GLUT OPENGL...BESAR.";
break;
}
}
void selectColor(int color)
{
switch (color) {
case 1:
glColor3f(0.0, 1.0, 0.0);
break;
case 2:
glColor3f(1.0, 0.0, 0.0);
break;
case 3:
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 SAYA BELAJAR GLUT bitmap font.");
output(100, 100, message);
output(0, 145, "(Posisi dalam PIXEL dengan dimulai atas kiri ,...xixixi)");
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;

}

OUTPUT :

______________________________________________________

Program 6 : Membuat Garis Strip

CODING :

#include <iostream>
#include <GL/glut.h>

void display(void)
{
                glClear (GL_COLOR_BUFFER_BIT);
                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 (400,150);
                glutInitWindowPosition (100, 100);
                glutCreateWindow (argv[0]);
                glutDisplayFunc (display);
                glutReshapeFunc (reshape);
                glutMainLoop ();
                return 0;

}
OUTPUT :

______________________________________________________

Program 7 : Membuat Kupu-kupu

CODING :

 #include <iostream>
#include <GL/glut.h>
void display(void)
{
GLubyte fly[] = {
      0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x03, 0x00, 
      0x00, 0xC0, 0x03, 0x00, 0x06, 0x18, 0x18, 0x60,
      0x06, 0x18, 0x18, 0x60, 0x00, 0x00, 0x00, 0x00, 
      0x18, 0x0C, 0x30, 0x18, 0x18, 0x0C, 0x30, 0x18, 
      0x00, 0x00, 0x00, 0x00, 0x18, 0x06, 0x60, 0x18,
      0x18, 0x06, 0x60, 0x18, 0x00, 0x00, 0x00, 0x00, 
      0x0C, 0x01, 0x80, 0x60, 0x0C, 0x01, 0x80, 0x60, 
      0x00, 0xC0, 0x03, 0x00, 0x00, 0xCE, 0x73, 0x00,
      0x00, 0x0E, 0x70, 0x00, 0x00, 0x0F, 0xF0, 0x00,
      0x00, 0x03, 0xC0, 0x00, 0x01, 0x83, 0xC1, 0x80,
      0x01, 0x8F, 0xF1, 0x80, 0x0A, 0x0E, 0x70, 0x30,
      0x0A, 0x0E, 0x70, 0x30, 0x00, 0x00, 0x00, 0x00,
      0x30, 0x00, 0x00, 0x0C, 0x30, 0x18, 0x18, 0x0C,
      0x00, 0x18, 0x18, 0x00, 0x0A, 0x00, 0x00, 0x30,
      0x0A, 0xC0, 0x03, 0x30, 0x00, 0xC0, 0x03, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.5, 0.7);
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 (1.0, 1.0, 0.1, 1.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;
}
OUTPUT :


Senin, 09 Oktober 2017

Cara Membuat Rumah dari Dev C++

Assalamualaikum, yey pembahasan kali ini seru gengs , apa kalian tau  ??? kalau membuat rumah itu tidak hanya dari semen,pasir,kayu,dll. tapi juga bisa dari aplikasi yang satu ini walaupun sederhana tetapi cukup rumit gengs hehehe ,yuks simak codingnya di bawah ini  ⤋⤋⤋⤋⤋ 


Coding :

#include <iostream>
#include <windows.h>           //use as needed for your system
#include <gl/GL.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <stdio.h>
int screenWidth=640;
int screenHeight=480;
struct Point
{
            int x;
            int y;
};
struct GLintPoint
{
            GLint x,y;
};

/*struct GLintPointArray
{
            const int MAX_NUM = 10000;
            int num;
            GLintPoint pt[MAX_NUM];
};*/

void myInit(void)
{
            glClearColor(0.0,0,0.0,0); //for background
            glColor3f(1.0f,0.0f,1.0f); //for point color
            glPointSize(4.0);
            glLineWidth(4.0);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);
           
}

void paraHouse(struct Point peak,GLint width,GLint height)          // to make a house
{
            glBegin(GL_LINE_LOOP);
           
                        glVertex2i(peak.x - width / 2, peak.y - height);
                        glVertex2i(peak.x - width / 2, peak.y - 4 * height/9);
                        glVertex2i(peak.x - width / 3, peak.y - 2 *height/9);
                        glVertex2i(peak.x + width / 3, peak.y - 2* height /9);
                        glVertex2i(peak.x + width / 2, peak.y - 4* height /9);
                        glVertex2i(peak.x + width / 2, peak.y - height);
            glEnd();
           
            glBegin(GL_LINE_STRIP); // Atap
                        glVertex2i(peak.x - width / 6, peak.y - 2* height /9);
                        glVertex2i(peak.x, peak.y);
                        glVertex2i(peak.x + width / 6, peak.y - 2* height/9);
            glEnd();
           
            glBegin(GL_LINE_STRIP); // Pintu
                        glVertex2i(peak.x - width / 3, peak.y - height);
                        glVertex2i(peak.x - width / 3, peak.y - 5 * height / 9);
                        glVertex2i(peak.x - 5 * width/60, peak.y - 5 * height /9);
                        glVertex2i(peak.x - 5 * width/60, peak.y - height);
            glEnd();
            glBegin(GL_LINE_LOOP); //Jendela
                        glVertex2i(peak.x + width/12, peak.y - 7 * height / 9);
                        glVertex2i(peak.x + width/12, peak.y - 5 * height / 9);
                        glVertex2i(peak.x + width /4, peak.y - 5 * height / 9);
                        glVertex2i(peak.x + width /4, peak.y - 7 * height / 9);
                       
            glEnd();
}

void Display(void)
{
            struct Point p;
            p.x=400;
            p.y=400;
            glClear(GL_COLOR_BUFFER_BIT);
            glBegin(GL_POINT);
            paraHouse(p,100,200);
            glBegin(GL_LINE_LOOP);
                        glVertex2i(10,10);
                        glVertex2i(10,60);
                        glVertex2i(20,80);
                        glVertex2i(60,80);
                        glVertex2i(70,60);
                        glVertex2i(70,10);
            glEnd();
            glBegin(GL_LINE_STRIP);
                        glVertex2i(20,10);      
                        glVertex2i(20,50);      
                        glVertex2i(35,50);      
                        glVertex2i(35,10);      
            glEnd();
            glBegin(GL_LINE_STRIP);
                        glVertex2i(30,80);
                        glVertex2i(40,100);
                        glVertex2i(50,80);
            glEnd();
            glBegin(GL_LINE_LOOP);
                        glVertex2i(45,30);
                        glVertex2i(45,50);
                        glVertex2i(55,50);
                        glVertex2i(55,30);
            glEnd();
            glFlush();
                       
}         
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv)
{
            glutInit(&argc,argv);
            glutInitDisplayMode(GLUT_SINGLE);
            glutInitWindowSize(800,600);
            glutInitWindowPosition(100,150);
            glutCreateWindow("HOUSE");        
            glutDisplayFunc(Display);
            myInit();
            glutMainLoop();

}

maka hasil output dari coding di atas akan menjadi seperti di bawah ini : 

Bagaimana nih mba bro & mas bro apakah berhasil  ? mudah kan ,semoga tidak typo ya ☺☺☺ semangat terus ,dan semoga bermanfaat ya ... mampir terus ya di blog saya ☺☺, akan ada cara cara pembuatan dari Dev C++ dengan glut. byee wassalamua'laikum ☺☺☺