Tampilkan postingan dengan label OpenGL. Tampilkan semua postingan
Tampilkan postingan dengan label OpenGL. Tampilkan semua postingan

April 14, 2016

Penggambaran Garis Dengan Algoritma Bressenham

Kali ini saya akan berbagi source code openGL yang berhubungan dengan objek primitif khususnya penggambaran garis dengan algoritma Bresenham.
Sebelumnya, saya akan membahas sedikit mengenai openGL. OpenGL (Open Graphics Library) yang merupakan standar API yang dapat digunakan untuk membuat aplikasi berbasis grafik, baik dua dimensi (2D) maupun tiga dimensi (3D). Untuk dapat membuat konsep windowing pada OpenGL, diperlukan suatu tool tertentu. Dalam praktikum ini yang digunakan adalah GLUT (OpenGL Utility Toolkit). GLUT dipilih karena di dalamnya telah terdapat banyak fungsi yang dapat dipakai untuk pembuatan application window. Disamping itu, windowing pada GLUT juga bersifat independen terhadap sistem operasi, sehingga kita tidak perlu repot-repot untuk mengubah kode program jika diterapkan pada sistem operasi yang berbeda.
Banyak sekali IDE yang ada sekarang, beberapa contohnya adalah: Visual Studio, Codeblocks, Netbeans, Eclipse, dsb. Pada pemrograman OpenGL kali ini, IDE yang digunakan adalah Dev c++. untuk dapat menggunakan openGL pada Dev c++ jangan lupa memperbarui library GLUT yang dibutuhkan.
Berikut ini source code nya:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <gl/glut.h>
#include <math.h>
void display(){
set display-window background color to white
glClearColor(1.0,1.0,1.0,0.0);
set projection paramaters
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel(GLint xCoordinate, GLint yCoordinate)
{
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
glFlush();
}
Procedure garis lurus
void lineBres(GLint x0, GLint y0, GLint xEnd, GLint yEnd)
{
GLint dx = (float)fabs((float) xEnd - x0);
GLint dy = (float)fabs((float) yEnd - y0);
GLint p = 2 * (dy - dx);
GLint twoDy = 2 * dy;
GLint twoDyMinusDx = 2 * (dy-dx);
GLint x,y;
determine which endpoint to use as start position
if (x0 > xEnd){
x = xEnd;
y = yEnd;
xEnd = x;
}else{
x = x0;
y = y0;
}
setPixel(x,y);
while(y<yEnd){
y++;
if(p>0)
p += twoDy;
else{
x++;
p += twoDyMinusDx;
}
setPixel(x,y);
}
}
Procedure garis miring kiri
void lineBres1(GLint x0, GLint y0, GLint xEnd, GLint yEnd)
{
GLint dx = (float)fabs((float) xEnd - x0);
GLint dy = (float)fabs((float) yEnd - y0);
GLint p = 2 * dy - dx;
GLint twoDy = 2 * dy;
GLint twoDyMinusDx = 2 * (dy-dx);
GLint x,y;
// determine which endpoint to use as start position
if (x0 > xEnd){
x = xEnd;
y = yEnd;
xEnd = x;
}else{
x = x0;
y = y0;
}
setPixel(x,y);
while(x<xEnd){
x++;
if(p<0)
p += twoDy;
else{
y--;
p += twoDyMinusDx;
}
setPixel(x,y);
}
}
void drawMyLine(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glPointSize(7.0);
GLint x0 = 30;
GLint y0 = 50;
GLint xEnd = 30;
GLint yEnd =  200;
lineBres(x0,y0,xEnd,yEnd);//menampilkan garis 1
glPointSize(7.0);
glColor3f(1.0,0.0,0.0);
GLint x1 = 80;
GLint y1 = 50;
GLint x2 = 80;
GLint y2 = 200;
lineBres(x1,y1,x2,y2); //menampilkan garis 2
glPointSize(7.0);
glColor3f(1.0,0.0,0.0);
GLint x3 = 250;
GLint y3 = 50;
GLint x4 = 250;
GLint y4 = 200;
lineBres(x3,y3,x4,y4); //menampilkan garis 4
glPointSize(5.0);
glColor3f(1.0,0.0,0.0);
GLint x5 = 80;
GLint y5 = 200;
GLint x6 = 250;
GLint y6 = 50;
lineBres1(x5,y5,x6,y6); //menampilkan garis 3
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
//initialize display mode
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
//set display-window width & height
glutInitWindowSize(400,400);
//set display-window upper-left position
glutInitWindowPosition(0,0);
//create display-window with a title
glutCreateWindow("Digital Differential Analyzer Algorithm");
//initialze OpenGL
display();
//call graphics to be displayed on the window
glutDisplayFunc(drawMyLine);
//display everything and wait
glutMainLoop();
return 0;
}
maka, outputnya sebagai berikut :