+ All Categories

OpenGL

Date post: 18-Jan-2016
Category:
Upload: ega-bagas-permana
View: 10 times
Download: 0 times
Share this document with a friend
Description:
Presentasi OpenGL
Popular Tags:
30
Pemrograman OpenGL Dasar Pertemuan 2-5 Hand out Komputer Grafi
Transcript
Page 1: OpenGL

Pemrograman OpenGL Dasar

Pertemuan 2-5

Hand out Komputer Grafik

Page 2: OpenGL

TIU:Mahasiswa mampu menghasilkan aplikasi Komputer Grafik sederhana

(2)Mampu menggunakan aplikasi pengolah grafis 3D untuk membuat

animasi 3 dimensi sederhana (C3,P3)

(1)Mampu menjelaskan konsep dasar grafika di komputer (C2)

Entry Behaviour

Memahami konsep pemrograman berorientasi

Obyek

(3)Mampu menganalisa aplikasi pengolah grafis yang

menampilkan gambar 2 dimensi (C4,P3)

(4)Mampu menghasilkan aplikasi pengolah grafis yang memiliki

kemampuan mentransformasi obyek vektor dan berinteraksi dengan

pengguna (C5,P3)

(5)Mampu menghasilkan aplikasi pengolah grafis yang memiliki

kemampuan mengatur viewing dan shading (C5,P3)

Memahami konsep Vektor, Persamaan Linier, Matrik, dan

Determinan

Page 3: OpenGL

Bahasan

• Pokok: Konsep dan cara pemrograman OpenGL API dasar untuk menampilkan grafis 2 dimensi

• Sub: • OpenGL API• GLUT• Primitif dan atributnya• Warna• Viewing dasar• Fungsi program dasar

Page 4: OpenGL

The Programmer’s Interface

• Programmer sees the graphics system through a software interface: the Application Programmer Interface (API)

Page 5: OpenGL

API Contents

• Functions that specify what we need to form an image• Objects• Viewer• Light Source(s)• Materials

• Other information• Input from devices such as mouse and keyboard• Capabilities of system

Page 6: OpenGL

OpenGL

The success of GL lead to OpenGL (1992), a platform-independent API that was

• Easy to use• Close enough to the hardware to get excellent performance• Focus on rendering• Omitted windowing and input to avoid window system

dependencies

Page 7: OpenGL

OpenGL function format

glVertex3f(x,y,z)

belongs to GL library

function name

x,y,z are floats

glVertex3fv(p)

p is a pointer to an array

dimensions

Page 8: OpenGL

Example

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( );

type of object

location of vertex

end of object definition

Page 9: OpenGL

OpenGL Primitives

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTS

GL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOP

GL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

Page 10: OpenGL

A Simple Program

Generate a square on a solid background

Page 11: OpenGL

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();

}

Page 12: OpenGL

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,….

Page 13: OpenGL

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

Page 14: OpenGL

Latihan

Buatlah Program yang menampilkan kotak diatas

Page 15: OpenGL

Defaults• simple.c is too simple• Makes heavy use of state variable default values for

• Viewing• Colors• Window parameters

• Next version will make the defaults more explicit

Page 16: OpenGL

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

Page 17: OpenGL

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

Page 18: OpenGL

main.c

#include <GL/glut.h>

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

init();

glutMainLoop();

}

includes gl.h

define window properties

set OpenGL state

enter event loop

display callback

Page 19: OpenGL

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

Page 20: OpenGL

init.c

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

glColor3f(1.0, 1.0, 1.0);

glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

}

black clear coloropaque window

fill/draw with white

viewing volume

Page 21: OpenGL

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

Page 22: OpenGL

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

Page 23: OpenGL

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

Page 24: OpenGL

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

Page 25: OpenGL

Rangkuman• API berfungsi sebagai perantara antara aplikasi dengan

hardware• Membuat tampilan 2 dimensi sederhana menggunakan

OpenGL• Penjelasan Fungsi-fungsi dasar pembentuk program

OpenGL

Page 26: OpenGL

Contoh Soal

Buat tampilan sebagai berikut:

Page 27: OpenGL

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();

}

Page 28: OpenGL

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;}

Page 29: OpenGL

Contoh SoalBuatlah tampilan program Sierpinski Gasket

Page 30: OpenGL

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


Recommended