• Tidak ada hasil yang ditemukan

Your First OpenGL Program

N/A
N/A
Protected

Academic year: 2018

Membagikan "Your First OpenGL Program"

Copied!
56
0
0

Teks penuh

(1)

GRAFIKA

KOMPUTER

(2)

Your First

(3)

Simple

Program

Architecture

Inisialisasi

Update logika

Rendering

(4)
(5)

Recall

OpenGL

can’t

performing

windowing tasks or obtaining

(6)
(7)

#include <windows.h> #include <GL/glut.h>

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd();

glFlush(); }

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);

glutCreateWindow("OpenGL Setup Test"); glutInitWindowSize(320, 320);

glutInitWindowPosition(50, 50); glutDisplayFunc(display);

glutMainLoop(); return 0;

(8)

#include <windows.h>

#include <GL/glut.h>

The Header

(9)

#include <windows.h>

#include <GL/glut.h>

The Header

// GLUT, includes glu.h and gl.h

/* Handler for window-repaint event.

Call back when the window first appears

and whenever the window needs

(10)

The Main

/* Main function: GLUT runs

as a console application starting at main() */

GLUT command startswith

glut*

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutCreateWindow("OpenGL Setup Test");

glutInitWindowSize(320, 320); glutInitWindowPosition(50, 50); glutDisplayFunc(display);

glutMainLoop(); return 0;

(11)

The Main

// Initialize GLUT

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutCreateWindow("OpenGL Setup Test");

glutInitWindowSize(320, 320); glutInitWindowPosition(50, 50); glutDisplayFunc(display);

glutMainLoop(); return 0;

(12)

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);

glutCreateWindow("OpenGL Setup Test"); glutInitWindowSize(320, 320);

glutInitWindowPosition(50, 50); glutDisplayFunc(display);

glutMainLoop(); return 0;

}

The Main

// Specify the display Mode

(13)

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);

glutCreateWindow("OpenGL Setup Test"); glutInitWindowSize(320, 320);

glutInitWindowPosition(50, 50); glutDisplayFunc(display);

glutMainLoop(); return 0;

}

The Main

(14)

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutCreateWindow("OpenGL Setup Test");

glutInitWindowSize(320, 320);

glutInitWindowPosition(50, 50);

glutDisplayFunc(display); glutMainLoop();

return 0; }

The Main

(15)

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutCreateWindow("OpenGL Setup Test");

glutInitWindowSize(320, 320); glutInitWindowPosition(50, 50);

glutDisplayFunc(display);

glutMainLoop(); return 0;

}

The Main

(16)

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutCreateWindow("OpenGL Setup Test");

glutInitWindowSize(320, 320); glutInitWindowPosition(50, 50); glutDisplayFunc(display);

glutMainLoop();

return 0; }

The Main

(17)

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd();

glFlush(); }

The Display

(18)

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd();

glFlush(); }

The Display

(19)

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd();

glFlush(); }

The Display

(20)

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd();

glFlush(); }

The Display

(21)

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f);

glEnd(); glFlush(); }

The Display

(22)

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd();

glFlush();

}

The Display

(23)

OPENGL

(24)

Command Syntax

OpenGL commands use the

prefix

gl

and

initial capital

letters

for each word making

up the command name (recall

(25)

Command Syntax

OpenGL defined constants

begin with

GL_

, use

all capital

letters

, and use underscores to

separate words (for example,

(26)

Command Syntax

You might also have noticed

some seemingly

extraneous

letters

appended to some

command names (for

example, the

3f

in

glColor3f()

(27)

Command Syntax

gl

Vertex

2

f

()

Number of Component

2

– (x, y)

3

– (x, y, z)

4

– (x, y, z, w)

(28)
(29)

Command Syntax

Thus, the two commands are

equivalent

glVertex2i(1, 3);

(30)

Command Syntax

> When only x and y are

specified,

z defaults to

0.0

and w defaults to

1.0

.

> When x, y, and z are

(31)

Command Syntax

Thus, the two commands are

equivalent

glVertex2i(1, 3);

(32)

Command Syntax

Some OpenGL commands can

take a final letter

v

, which

indicates that the command takes

a pointer to a

vector

(or array) of

values, rather than a series of

(33)

Command Syntax

glColor3f(1.0, 0.0, 0.0);

(34)

OPENGL AS A

(35)

Command Syntax

OpenGL is a

state machine

,

particularly if you’re using the

fixed-function pipeline. You put it

into various states (or modes)

that then

remain in effect

until

(36)

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f); glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd();

glFlush(); }

The Display

(37)

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex2f(-0.5f, -0.5f); glVertex2f( 0.5f, -0.5f);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex2f( 0.5f, 0.5f); glVertex2f(-0.5f, 0.5f); glEnd();

glFlush(); }

The Display

(38)
(39)

Callback Func

> Your OpenGL program will be in

infinite loop

> Event-driven: Programs that use

windows

~ Input/Output

~ Wait until an event happens and then

execute some pre-defined functions

(40)

Callback Func

> Your OpenGL program will be in

infinite loop

> Event-driven: Programs that use

windows

~ Input/Output

~ Wait until an event happens and then

execute some pre-defined functions

(41)

Callback Func

(42)

Callback Func

Callback function : Routine to

call

when an event happens

~ Window resize or redraw

~ User input (mouse, keyboard)

(43)

Callback Func

“Register” callbacks with GLUT

glutDisplayFunc( my_display_func );

glutIdleFunc( my_idle_func );

(44)

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutCreateWindow("OpenGL Setup Test");

glutInitWindowSize(320, 320); glutInitWindowPosition(50, 50);

glutDisplayFunc(display);

glutMainLoop(); return 0;

}

Callback Func

(45)

int main(int argc, char** argv) { glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutCreateWindow("OpenGL Setup Test");

glutInitWindowSize(320, 320); glutInitWindowPosition(50, 50);

glutDisplayFunc(display);

glutIdleFunc( my_idle_func );

glutKeyboardFunc(my_key_events_func);

glutMouseFunc (my_mouse_events_func);

glutMainLoop(); return 0;

}

(46)

Rendering Callbacks

> Callback function where all our

drawing is done

> Every GLUT program must have a

display callback

(47)

Rendering Callbacks

glutDisplayFunc( my_display_func );

/* this part is in main.c */

void

my_display_func

(void )

{

glClear(

GL_COLOR_BUFFER_BIT

);

glBegin(

GL_TRIANGLE

);

glVertex3fv( v[0] );

glVertex3fv( v[1] );

glVertex3fv( v[2] );

glEnd();

(48)

Idle Callbacks

> Use for animation and continuous

update

~ Can use glutTimerFunc or timed

callbacks for animations

(49)

Idle Callbacks

glutIdleFunc(

idle

);

void

idle

( void )

{

/* change something */

t += dt;

(50)

User Input Callbacks

> Process user input

(51)

User Input Callbacks

glutKeyboardFunc(my_key_events);

void

my_key_events

(char key, int x, int y)

{

switch ( key ) {

case ‘q’ : case ‘Q’ :

exit ( EXIT_SUCCESS);

break;

case ‘r’ : case ‘R’ :

rotate = GL_TRUE;

break;

(52)

Mouse Callbacks

> Captures mouse press and release

events

(53)

Mouse Callbacks

glutMouseFunc( my_mouse );

void

myMouse

(int button, int state, int x,

int y)

{

if (button == GLUT_LEFT_BUTTON && state

== GLUT_DOWN)

{

(54)

Events in OpenGL

Event Example OpenGL Callback Function

Keypress KeyDown KeyUp

glutKeyboardFunc

Mouse leftButtonDown leftButtonUp

glutMouseFunc

Motion With mouse press Without

glutMotionFunc

glutPassiveMotionFunc

Window Moving Resizing

glutReshapeFunc

System Idle Timer

glutIdleFunc glutTimerFunc

(55)

Event Queue

Event queue

Keyboard

Mouse

Window

….

Mouse_callback() {

….

{

Keypress_callback() {

….

{

window_callback() {

….

{

(56)

Cheers,

Old Sport

~

M. Ali Fauzi

Referensi

Dokumen terkait

Catatan : Agar membawa dokumen penawaran asli sesuai yang di-upload lewat aplikasi SPSE2. Demikian undangan dari kami dan atas perhatiannya disampaikan

Menyikapi hal ini, Serikat Petani Indonesia yang tergabung dalam GERAK LAWAN (Gerakan Rakyat Indonesia Melawan Neokolonialisme – Imperialisme) dan Social Movements for an

Pada hari ini Kamis tanggal enam Agustus tahun dua ribu lima belas, Pokja ULP / Panitia Pengadaan Barang dan Jasa Kementerian Lingkungan Hidup dan Kehutanan

Untuk mencapai kompetensi itu mahasiswa diwajibkan mengkaji berbagai konsep pendidikan yang antara lain mencakup: hakikat manusia dan pendidikan, landasan pendidikan,

Universitas Negeri

Singkawang Barat Kota Singkawang Catatan : Undangan ini harus dihadiri oleh direktur atau nama yang tertuang sesuai8. dengan

Berdasarkan pembahasan hasil penelitian dan analisis data tentang penerapan konseling behavioristik dengan teknik self monitoring untuk meningkatkan konsentrasi

Berbagi Strategi P embelajaran Bahasa Dapat Meningkatkan Kemampuan Berbahasa Siswa. Jurnal Bahasa da n