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);
glutInitWindowPosition(100,100);
glutCreateWindow("Percobaan
1");
glClearColor(0.0,0.0,0.0,0.0);
//warna latar belakang/
4 komponen warna yaitu RGB, dan alpha
glMatrixMode(GL_PROJECTION);
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glOrtho(1.0,1.0,1.0,1.0,-1.0,1.0);
glutDisplayFunc(titik);
glutMainLoop();
}
void titik()
{
glClear(GL_COLOR_BUFFER_BIT);//membersihkan
semua piksel
glPointSize(5.5);//ukuran
titik piksel
glBegin(GL_POINTS);//perintah
untuk grafik primitif
glColor3f(1.0,1.0,1.0);//warna
latar antarmuka/3 komponen warna RGB
glVertex2f(0.25,0.25);
glColor3f(0.1,1.0,1.0);
glVertex2f(0.5,0.5);
//glPointSize(2.0);
glEnd();
glFlush();
}
OUTPUT :
2. Program membuat titik v2
nah kalo ini sudah saya ubah, dasarnya pakai program 1, ada yang tau ini titik maksudnya apa?
#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);
glutInitWindowPosition(100,100);
glutCreateWindow("Percobaan
1");
glClearColor(0.0,0.0,0.0,0.0);
//warna latar belakang/ 4 komponen warna yaitu RGB, dan alpha
glMatrixMode(GL_PROJECTION);
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glOrtho(1.0,1.0,1.0,1.0,-1.0,1.0);
glutDisplayFunc(titik);
glutMainLoop();
}
void titik()
{
glClear(GL_COLOR_BUFFER_BIT);//membersihkan
semua piksel
glPointSize(5.5);//ukuran
titik piksel
glBegin(GL_POINTS);//perintah
untuk grafik primitif
glColor3f(1.0,1.0,1.0);//warna
latar antarmuka/3 komponen warna RGB
glVertex2f(-0.5,0.25);
glColor3f(1.0,1.0,1.0);
glVertex2f(-0.25,0.5);
glColor3f(1.0,1.0,1.0);
glVertex2f(0.0,0.3);
glColor3f(1.0,1.0,1.0);
glVertex2f(-0.4,-0.12);
glColor3f(1.0,1.0,1.0);
glVertex2f(-0.10,-0.09);
glColor3f(1.0,1.0,1.0);
glVertex2f(-0.5,-0.18);
//glPointSize(2.0);
glEnd();
glFlush();
}
OUTPUT :
3. Program membuat garis
#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);
glutInitWindowPosition(100,100);
glutCreateWindow("Percobaan
1");
glClearColor(0.0,0.0,0.0,0.0);
//warna latar belakang/ 4 komponen warna yaitu RGB, dan alpha
glMatrixMode(GL_PROJECTION);
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glOrtho(1.0,1.0,1.0,1.0,-1.0,1.0);
glutDisplayFunc(titik);
glutMainLoop();
}
void titik()
{
glClear(GL_COLOR_BUFFER_BIT);//membersihkan
semua piksel
glBegin(GL_LINES);//perintah
untuk grafik primitif
glColor3f(1.0,1.0,1.0);//warna
latar antarmuka/3 komponen warna RGB
glVertex2f(0.25,0.25);
glColor3f(0.1,1.0,1.0);
glVertex2f(0.5,0.5);
glVertex2f(0.75,0.5);
glVertex2f(0.5,0.8);
glPointSize(50.0f);
glEnd();
glFlush();
}
OUTPUT :
4. Program membuat garis v2
bedanya dengan di atas adalah program ini garisnya menyambung
#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(800,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Percobaan
3");
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glutDisplayFunc(titik);
glutMainLoop();
}
void titik()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
glColor3f(1.0,0.0,0.0);
glVertex2f(0.25,0.25);
glColor3f(0.1,1.0,0.0);
glVertex2f(0.75,0.5);
glVertex2f(0.7,0.8);
glPointSize(50.0f);
glEnd();
glFlush();
}
OUTPUT :
5. Program membuat segitiga
#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);
glutInitWindowPosition(100,100);
glutCreateWindow("Percobaan
1");
glClearColor(0.0,0.0,0.0,0.0);
//warna latar belakang/ 4 komponen warna yaitu RGB, dan alpha
glMatrixMode(GL_PROJECTION);
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glOrtho(1.0,1.0,1.0,1.0,-1.0,1.0);
glutDisplayFunc(titik);
glutMainLoop();
}
void titik()
{
glClear(GL_COLOR_BUFFER_BIT);//membersihkan
semua piksel
glBegin(GL_POLYGON);
//jika ingin segitiganya hanya garis ganti POLYGON dengan LINES
glColor3f(0.0,0.0,1.0);//warna
latar antarmuka/3 komponen warna RGB
glVertex2f(-0.5,-0.2);
glVertex2f(0.0,0.5);
glVertex2f(0.5,-0.2);
glVertex2f(0.0,0.5);
glVertex2f(0.5,-0.2);
glVertex2f(-0.5,-0.2);
glPointSize(50.0f);
glLineWidth(50.0f);
glEnd();
glFlush();
}
OUTPUT :
6. Program membuat kubus
#include <iostream>
#include <Gl/glut.h>
#include <stdio.h>
void garis();
main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(600,800);
glutInitWindowPosition(100,100);
glutCreateWindow("Percobaan 3");
glClearColor(0.0,0.0,0.0,0.0); //warna latar belakang/ 4
komponen warna yaitu RGB, dan alpha
glMatrixMode(GL_PROJECTION);
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glOrtho(1.0,1.0,1.0,1.0,-1.0,1.0);
glutDisplayFunc(garis);
glutMainLoop();
}
void garis()
{
glClear(GL_COLOR_BUFFER_BIT);//membersihkan semua piksel
glBegin(GL_LINES);//perintah untuk grafik primitif
glColor3f(1.0,0.0,0.0);
glVertex2f(-0.5,-0.3);
glVertex2f(-0.5,0.5);
glVertex2f(0.5,-0.3);
glVertex2f(0.5,0.5);
glVertex2f(0.5,-0.3);
glVertex2f(-0.5,-0.3);
glVertex2f(-0.5,0.5);
glVertex2f(0.5,0.5);
glColor3f(0.0,0.0,1.0);
glVertex2f(-0.3,-0.1);
glVertex2f(-0.3,0.7);
glVertex2f(0.7,-0.1);
glVertex2f(0.7,0.7);
glVertex2f(0.7,-0.1);
glVertex2f(-0.3,-0.1);
glVertex2f(-0.3,0.7);
glVertex2f(0.7,0.7);
glColor3f(0.0,1.0,0.0);//warna yang diset terakhir akan
menjadi warna garis selanjutnya
glVertex2f(-0.3,0.7);
glVertex2f(-0.5,0.5);
glVertex2f(0.5,0.5);
glVertex2f(0.7,0.7);
glVertex2f(0.5,-0.3);
glVertex2f(0.7,-0.1);
glVertex2f(-0.5,-0.3);
glVertex2f(-0.3,-0.1);
glEnd();
glPointSize(500.0f);
glFlush();
}
OUTPUT :
7. Program membuat garis-garis
untuk bentuknya yang zig-zag admin cuma iseng hehehe :P
#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);
glutInitWindowPosition(100,100);
glutCreateWindow("Percobaan
1");
glClearColor(0.0,0.0,0.0,0.0);
//warna latar belakang/ 4 komponen warna yaitu RGB, dan alpha
glMatrixMode(GL_PROJECTION);
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glOrtho(1.0,1.0,1.0,1.0,-1.0,1.0);
glutDisplayFunc(titik);
glutMainLoop();
}
void titik()
{
glClear(GL_COLOR_BUFFER_BIT);//membersihkan
semua piksel
glBegin(GL_LINES);//perintah
untuk grafik primitif
glColor3f(1.0,0.0,0.0);//warna
latar antarmuka/3 komponen warna RGB
glVertex2f(-0.5,0.1);
glVertex2f(-0.4,-0.1);
glColor3f(1.0,0.5,0.0);
glVertex2f(-0.4,-0.1);
glVertex2f(-0.3,0.1);
glColor3f(1.0,1.0,0.0);
glVertex2f(-0.3,0.1);
glVertex2f(-0.2,-0.1);
glColor3f(0.0,1.0,0.0);
glVertex2f(-0.2,-0.1);
glVertex2f(-0.1,0.1);
glColor3f(0.0,0.0,1.0);
glVertex2f(-0.1,0.1);
glVertex2f(0.0,-0.1);
glColor3f(0.0,1.0,1.0);
glVertex2f(0.0,-0.1);
glVertex2f(0.1,0.1);
glColor3f(1.0,0.0,1.0);
glVertex2f(0.1,0.1);
glVertex2f(0.2,-0.1);
glPointSize(50.0f);
glLineWidth(300);
glEnd();
glFlush();
}
OUTPUT :
8. Program membuat kotak di dalam kotak
#include<GL/glut.h>
void titik();
void titik()
{
glClear(GL_COLOR_BUFFER_BIT);
//merah
glBegin(GL_POLYGON);
glColor3f(0.0,1.0,0.0);
glVertex2f(-0.7f,-0.7f);
glVertex2f(-0.7f,0.7f);
glVertex2f(0.7f,0.7f);
glVertex2f(0.7f,-0.7f);
glVertex2f(-0.7f,-0.7f);
glPointSize(50.0f);
glEnd();//akhir
perintah OpenGL
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2f(-0.3f,-0.3f);
glVertex2f(-0.3f,0.3f);
glVertex2f(0.3f,0.3f);
glVertex2f(0.3f,-0.3f);
glVertex2f(-0.3f,-0.3f);
glPointSize(50.0f);
glEnd();//akhir
perintah OpenGL
glFlush();//untuk
mengeksekusi openGL infinite time
}
main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,800);
glutInitWindowPosition(100,100);
glutCreateWindow("Percobaan
3");
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glOrtho(1.0,1.0,1.0,1.0,-1.0,1.0);
glutDisplayFunc(titik);
glutMainLoop();
}
OUTPUT :
Comments
Post a Comment