You are on page 1of 41

TEKNIK INFORMATIKA

UNIBBA
K A
I F
R A
G
ER
T U
M P
KO

PERTEMUAN KE-2
(3 SKS – 16 X PERTEMUAN) Yaya Suharya, S.Kom., M.T.
Rabu, 15 Maret 2023 Pengenalan OpenGL & GLUT yaya.unibba@gmail.com
14:00-15:40 (Kelas 4A) yayasuharya@unibba.ac.id
19:00-21:30 (Kelas 4B) NIDN. 0407047706
HP/WA. 08112031124
NOW
Pengenalan OpenGL & GLUT
GLUT - The OpenGL Utility Toolkit
https://www.opengl.org/resources/libraries/glut/

The freeglut Project :: About (sourceforge.net)


http://freeglut.sourceforge.net/

OpenGL and GLUT Tutorial

https://
www2.cs.arizona.edu/classes/cs433/spring02/op
engl/index.html
OpenGL Tutorial - 1 | Getting Started | OpenGL in C++ with the GLUT library

https://
www.youtube.com/watch?v=3aJ8OR1C6pk&list=PLWzp0Bbyy_3jy34HlDrEWl
cG3rF99gkvk
Materi

• Apa itu OpenGL


• Sejarah dan Perkembangan OpenGL
• Konsep Perangkat Lunak OpenGL
• Pemrograman OpenGL menggunakan Visual
Studio C++
OpenGL
• OpenGL adalah sebuah program aplikasi interface yang
digunakan untuk mendefinisikan komputer grafis 2D dan
3D. Program lintas-platform API ini umumnya dianggap
ketetapan standar dalam industri komputer dalam
interaksi dengan komputer grafis 2D dan juga telah
menjadi alat yang biasa untuk digunakan dengan grafis
3D.
• Singkatnya, Open Graphics Library, OpenGL
menghilangkan kebutuhan untuk pemrogram untuk
menulis ulang bagian grafis dari sistem operasi setiap kali
sebuah bisnis akan diupgrade ke versi baru dari sistem.
OpenGL
• OpenGL adalah kumpulan standard API (Application Programming
Interface) yang menghubungkan software dengan hardware grafis
untuk membuat aplikasi 3D secara real time. Intinya OpenGL itu
adalah kumpulan library untuk mengakses hardware (GL= graphical
library).
• OpenGL mendefinisikan berbagai instruksi untuk menggambar objek,
image (umumnya 3D) dan melakukan berbagai operasi terhadap
objek-objek tersebut.
• OpenGL tidak mengandung source code, hanya spesifikasi saja.
Pembuat GPU (graphical processing unit) seperti NVIDIA, Intel,
Samsung dll yang akan membuat implementasi. Dengan cara ini
walaupun GPU diproduksi oleh berbagai produsen dengan berbagai
berbagai variasi tipe dan implementasi, semuanya dapat diperintah
dengan spesifikasi yang sama.
OpenGL
• OpenGL (Open Graphics Library)[3] adalah spesifikasi standar yang
mendefinisikan sebuah lintas-bahasa, lintas platform API untuk
mengembangkan aplikasi yang menghasilkan grafis komputer 2D
maupun3D.
• Antarmuka terdiri dari lebih dari 250 panggilan fungsi yang berbeda
yang dapat digunakan untuk menggambar tiga dimensi yang adegan-
adegan kompleks dari bentuk-bentuk primitif sederhana.
• OpenGL dikembangkan oleh Silicon Graphics Inc (SGI) pada tahun
1992 dan secara luas digunakan dalam CAD, realitas maya,
visualisasi ilmiah, visualisasi informasi, dan simulasi penerbangan.
Hal ini juga digunakan dalam video game, di mana bersaing dengan
Direct3D on Microsoft Windows platform (lihat vs OpenGL Direct3D).
OpenGL dikelola oleh sebuah teknologi konsorsium nirlaba yaitu
Khronos Group.
OpenGL
• OpenGL (Open Graphics Library) is a cross-language,
multi-platform application programming interface (API)
for rendering 2D and 3D vector graphics. The API is
typically used to interact with a graphics processing unit
(GPU), to achieve hardware-accelerated rendering.
• OpenGL was developed by Silicon Graphics Inc. (SGI)
from 1991 and released in January 1992 and is widely
used in CAD, virtual reality, scientific visualization,
information visualization, flight simulation, and video
games. OpenGL is managed by the non-profit
technology consortium Khronos Group.
Sejarah dan
Perkembangan
• 1973: Graphical Kernel System (GKS)

• 1982: Silicon Graphics (SGI) mengimplementasikan


konsep grafik pipeline 3D

• 1992: OpenGL menjadi platform-independent API

• 2002: OpenGL ES The Standard for Embedded


Accelerated 3D Graphics
Library OpenGL
• OpenGL core library
• OpenGL32 on Windows
• GL on most unix/linux systems (libGL.a)
• OpenGL Utility Library (GLU)
• Provides functionality in OpenGL core but avoids having to
rewrite code
• Links with window system
• GLX for X window systems
• WGL for Windows
• AGL for Macintosh
GLUT
• OpenGL Utility Toolkit (GLUT)
• Provides functionality common to all window systems
• Open a window
• Get input from mouse and keyboard
• Menus
• Event-driven

• Code is portable but GLUT lacks the functionality of a


good toolkit for a specific platform
• No slide bars

1
0
Organisasi
Perangkat Lunak
Arsitektur OpenGL
Fungsi-fungsi
OpenGL
• Primitives
• Points
• Line Segments
• Polygons

• Attributes
• Transformations
• Viewing
• Modeling

• Control (GLUT)
• Input (GLUT)
• Query
Format Fungsi
OpenGL
function name
dimensions

glVertex3f(x,y,z)

belongs to GL library x,y,z are floats

glVertex3fv(p)

p is a pointer to an array
OpenGL #defines
• Most constants are defined in the include files
gl.h, glu.h and glut.h
• Note #include <GL/glut.h> should automatically
include the others
• Examples
• glBegin(GL_POLYGON)
• glClear(GL_COLOR_BUFFER_BIT)

• include files also define OpenGL data types:


GLfloat, GLdouble,….
Contoh

type of object
location of vertex
glBegin(GL_POLYGON)
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 1.0);
glEnd( );

end of object definition


Program
Sederhana
Generate a square on a solid background
OpenGL Primitives

GL_POINTS GL_POLYGON
GL_LINES GL_LINE_STRIP

GL_LINE_LOOP

GL_TRIANGLES
GL_QUAD_STRIP

GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
simple.c
#include <GL/glut.h>
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
int main(int argc, char** argv){
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
Event Loop
• Note that the program defines a display
callback function named mydisplay
• Every glut program must have a display callback
• The display callback is executed whenever OpenGL
decides the display must be refreshed, for example
when the window is opened
• The main function ends with the program entering an
event loop
Defaults
• simple.c is too simple
• Makes heavy use of state variable default
values for
• Viewing
• Colors
• Window parameters

• Future OpenGL programs will make the defaults


more explicit
Program Structure
• Most OpenGL programs have a similar structure that consists of
the following functions
• main():
• defines the callback functions
• opens one or more windows with the required properties
• enters event loop (last executable statement)

• init(): sets the state variables


• Viewing
• Attributes

• callbacks
• Display function
• Input and window functions
simple.c revisited
• In this version, we shall see the same output
but we have defined all the relevant state
values through function calls using the default
values
• In particular, we set
• Colors
• Viewing conditions
• Window properties
main.c

#include <GL/glut.h> includes gl.h


int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple"); define window properties
glutDisplayFunc(mydisplay);

display callback
init();
set OpenGL state
glutMainLoop();
} enter event loop
GLUT functions
• glutInit allows application to get command line arguments and
initializes system
• gluInitDisplayMode requests properties for the window (the
rendering context)
• RGB color
• Single buffering
• Properties logically ORed together
• glutWindowSize in pixels
• glutWindowPosition from top-left corner of display
• glutCreateWindow create window with title “simple”
• glutDisplayFunc display callback
• glutMainLoop enter infinite event loop
init.c

black clear color


void init()
{ opaque window
glClearColor (0.0, 0.0, 0.0, 1.0);

glColor3f(1.0, 1.0, 1.0); fill/draw with white

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
viewing volume
RGB color
• Each color component is stored separately in
the frame buffer
• Usually 8 bits per component in buffer
• Note in glColor3f the color values range from
0.0 (none) to 1.0 (all), whereas in glColor3ub
the values range from 0 to 255
Indexed Color

• Colors are indices into tables of RGB values


• Requires less memory
• indices usually 8 bits
• not as important now
• Memory inexpensive
• Need more colors for shading
Color and State

• The color as set by glColor becomes part of the


state and will be used until changed
• Colors and other attributes are not part of the object
but are assigned when the object is rendered
• We can create conceptual vertex colors by code such
as
glColor
glVertex
glColor
glVertex
Smooth Color
• Default is smooth shading
• OpenGL interpolates vertex colors across visible polygons

• Alternative is flat shading


• Color of first vertex

determines fill color


• glShadeModel
(GL_SMOOTH)
or GL_FLAT
Rangkuman
• API berfungsi sebagai perantara antara aplikasi
dengan hardware
• Membuat tampilan 2 dimensi sederhana
menggunakan OpenGL
• Penjelasan Fungsi-fungsi dasar pembentuk
program OpenGL
Contoh Soal

Buat tampilan sebagai berikut:


Jawaban
#include "stdafx.h"
#include <GL/glut.h>

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);

glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f(-2.0,-2.0,0.0);
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(0.0,2.0,0.0);
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(2.0,-2.0,0.0);

glEnd();
glFlush();
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0,2.0,-2.0,2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);
}

int main(int argc, char* argv[])


{
if (argv[1] != NULL)
{ n=atoi(argv[1]);
}
else n=5;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Segitiga Warna");
glutDisplayFunc(display);
myinit();
glutMainLoop();

return 0;
}
Referensi
• Edward Angel, “Interactive Computer Graphics
Fourth Edition”, Pearson, 2006, ch 2, p 46 – 84
• F. S. Hill, Jr., “Computer Graphics Using
OpenGL Second Edition”, Prentice Hall, 2001,
ch 2, p 39 - 63
Referensi
Pemrograman
OpenGL pada VS
• Resources
• Visual Studio
• GLUT for Win32 version 3.7.6 as of Nov 8th 2001 (glut-3.7.6-bin.zip)

• Preparation
• Unzip file glut-3.7.6-bin.zip
• Copy (asumsi Visual Studio 2010)
• glut.h: ‘C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\GL\’

• glut32.lib: ‘C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\’

• glut32.dll: ‘C:\Windows\System32\’

• Catatan: untuk windows 64 bit, letakkan glut32.dll pada ‘C:\Windows\SysWOW64\’


Langkah-langkah
Dasar
• Buka Visual Studio buat project baru dengan template C++,
Win32 Console Applications, Isi Foldernya, Next, Empty Project
• Add New Item Source File C++ File, beri nama filenya
• Ketikan program OpenGLnya
• Tambahkan alamat include dan lib OpenGL pada properti
proyeknya
• Kompilasi programnya
• Dalami konsep dan sistem koordinat OpenGL dengan menganti
nilainya dan juga menambah vertexnya
• Contoh: Buat papan catur, papan halma, papan monopoli,
objek: mobil, komputer, logo, ruang 3D, dll.
LATIHAN
Buatlah program yang menampilkan kotak didibawah ini!
Tugas
Buatlah tampilan program Sierpinski Gasket

Kirim ke : yaya.unibba@gmail.com,

Format : Tugas-1 GK_NamaMahasiswa/i_NIM


NEXT
Objek & Output Primitif

You might also like