Skip to main content

Kumpulan Code Dev C++ dan FreeGlut mengaplikasikan objek primitif dalam bentuk yang lain

Dalam blog sebelumnya kita membuat titik garis, dan bangun ruang. Dalam
blog kali ini kita melakukan penambahan pada program sebelumnya,
sebelum lanjut pastikan kalian sudah menginstal FreeGlut dan Dev C++ dan beberapa pengaturan dasarnya, Jika belum, kunjungi Postingan Panduan penginstalan Dev C++ dan FreeGlut

Untuk mempelajari program yang lebih lengkap cobalah
program-program dibawah ini

1. Program membuat strip titik strip
#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;

}
Output :

2. Program membuat bermacam-macam strip dan titik
#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. Program membuat banyak gambar lebah dengan matrix hexadecimal
#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. Program gradasi warna
#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. Program bitmap font example
#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[] = "Mata Kuliag Grafika Komputer";
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 NAMA SAYA M.NOOR ANSYARI");
output(100, 100, message);
output(0, 145, "(Cyberaandroid.blogspot.com)");
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. Program membuat strip strip
Program ini sama seperti Program no. 1 cuman hanya sedikit diubah admin
#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. Program membuat banyak gambar lilin dengan matrix hexadecimal
Program ini sama seperti Program no. 3 cuman hanya sedikit diubah admin
#include <GL/glut.h>
void display(void)
{
GLubyte line[] = {
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF
};
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);
glEnable (GL_POLYGON_STIPPLE);
glPolygonStipple (line);
glRectf (100.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 :

Comments

Popular posts from this blog

Kumpulan Code titik, garis dan bangun bidang menggunakan Dev C++ dan FreeGlut

Postingan sebelumnya saya membahas tentang instalasi Dev C++ dan GLUT, gak cocok rasanya tanpa dibarengi dengan beberapa contoh coding yang bisa di coba. yang sebelumnya belum menginstal Dev C++ dan GLUT silahkan klik disini ok ini beberapa code yang bisa di coba : 1. Program membuat titik     kelihatanya titik simpel tapi kalau bikinya pakai pemograman lumayan sulit loh... #include <iostream> #include <GL/glut.h> #include <stdio.h> void titik(); main(int argc, char** argv) {                 glutInit(&argc,argv);                 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);                 glutInitWindowSize(600,800);              ...

Panduan Instalasi Dev C++ dan GLUT untuk Mata Kuliah Grafika Komputer

yah sekian lama gak ngeblog akhirnya setelah 2 tahun vakum dalam dunia blog gua kembali juga dikarnakan tugas yang mengharuskan wkwkwkwk. ok tanpa berlama-lama walaupun post kali beda jauh dari tema blog ini, kali ini gua akan membawakan panduan Instalasi Dev C++ dan GLUT untuk Mata Kuliah Grafika Komputer ok pertama kita siapin dlu bahan-bahanya Dev C++  & Free G lut   nb. link hanya option bisa cari softwarenya dari sumber yang lain, tapi disarankan Dev C++ versi 5.11 keatas langkah-langkah penginstalan 1. instal dev c++ (bila muncul Administrator Notification pilih yes) pilih english lalu ok Pilih I Agree pilih Next pilih Instal tunggu beberapa saat pilih finish penginstalan Dev C++ sudah selesai 2. lalu kita meninstal Freeglut nya extract freeglut  copy lalu paste  dari extract folder freeglut\include\GL ke  C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\GL copy  lal...