Belajar Object Primitive Drawing OpenGL DEV C++


Yessss jangan bosan-bosan melototin blog saya , nah kali ini saya akaan mencoba untuk membuat dan belajar tentang object primitive drawing . Nah langsung saja kita coba :

1. Membuat Garis Putus-Putus

#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, 0x10ff);
glBegin(GL_LINE_STRIP);
glVertex2f(325, 75);
glVertex2f(5, 75);
glEnd();
glDisable (GL_LINE_STIPPLE);
glFlush();
}

void reshape1(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(reshape1);
glutMainLoop();
return 0;
}

Output : 


2. Membuat berbagai macam tumpukan strip atau garis

#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 : 


3. Membuat Kumbang

#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 :


4.  Membuat Kotak Warna - warni

#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 : 

5.  Membuat Text

#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 opengGL";

char *message = defaultMessage;

void selectFont(int newfont)

{

font = fonts[newfont];

glutPostRedisplay();

}

void selectMessage(int msg)

{

switch (msg) {

case 1:

message = "glut openGL...kecil.";

break;

case 2:

message = "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, "(Posisidalam PIXEL dengan dimulai atas kiri,...xixixixi)");

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 : 


6.  Garis Miring
#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 (600, 150);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

output :


7. Membuat Bunga
#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 (5.0, 0.1, 0.2);
   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, 1.0, 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 ("Reshape7");
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;  
}

output :


yesss okey , ini lah beberapa contoh cara membuat primitif drawing 
sampe disini dulu postingan saya , sampe ketemu lagi dipostingan berikut nya ... 
sekian dan terima kasih hehe :)

Komentar

Postingan populer dari blog ini

Perintah Dasar Linux

Perbandingan Spek HP Proliant Gen8 G2020T Micro Server dan HP Proliant G7 N54L NHP Micro Server