Your First OpenGL Program

  GRAFIKA

KOMPUTER

  Your First

OpenGL Program

  Inisialisasi Simple

  Update logika Program Architecture

  Rendering Keluar ? RECALL

Recall OpenGL performing can’t

windowing tasks or obtaining

user input, but GLUT

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

  The Header

#include <windows.h>

#include <GL/glut.h>

  // For MS Windows The Header #include <windows.h> #include <GL/glut.h>

  // GLUT, includes glu.h and gl.h /* Handler for window-repaint event. The Main 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

  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

  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

  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

  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

  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

  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 Display

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

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

  The Display

  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); The Display

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

  The Display

  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); The Display

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

  The Display

  OPENGL

COMMAND SYNTAX Command Syntax OpenGL commands use the prefix gl and initial capital

letters for each word making

up the command name (recall

  Command Syntax OpenGL defined constants begin with GL_ , use all capital

letters , and use underscores to

separate words (for example,

  Command Syntax

You might also have noticed

some seemingly extraneous

letters appended to some command names (for

  Command Syntax gl Vertex f ()

  2 Data Type Number of Component f - float

  • – (x, y)

  2 i – integer

  • – (x, y, z)

  3 Command Syntax

  Command Syntax

Thus, the two commands are

equivalent glVertex2i(1, 3);

  

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 Command Syntax

Thus, the two commands are

equivalent glVertex2i(1, 3);

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

Command Syntax

  glColor3f(1.0, 0.0, 0.0);

GLfloat color_array[] = {1.0, 0.0, 0.0};

glColor3fv(color_array);

  OPENGL AS A

STATE MACHINE Command Syntax OpenGL is a state machine , particularly if you’re using the

fixed-function pipeline. You put it

into various states (or modes)

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

  The Display

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

  The Display

  

GLUT CALLBACK FUNCTIONS Callback Func > Your OpenGL program will be in infinite loop > Event-driven: Programs that use windows

  ~ Input/Output Callback Func > Your OpenGL program will be in infinite loop > Event-driven: Programs that use windows

  ~ Input/Output Callback Func key press, mouse button

  Events –

press and release, window resize,

etc.

  Callback Func

Callback function : Routine to call when an event happens

  ~ Window resize or redraw ~ User input (mouse, keyboard) ~ Animation (render many frames) Callback Func “Register” callbacks with GLUT

glutDisplayFunc( my_display_func );

glutIdleFunc( my_idle_func ); glutKeyboardFunc(my_key_events_func); glutMouseFunc (my_mouse_events_func);

  Callback Func

  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

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

Rendering Callbacks

  > Callback function where all our drawing is done

> Every GLUT program must have a

display callback

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

  Idle Callbacks

> Use for animation and continuous

update

  ~ Can use glutTimerFunc or timed callbacks for animations

  

Idle Callbacks

glutIdleFunc( idle ); void idle ( void ) { /* change something */ t += dt; glutPostRedisplay(); }

  User Input Callbacks > Process user input

glutKeyboardFunc(my_key_events);

  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’ :

  Mouse Callbacks

> Captures mouse press and release

events glutMouseFunc( my_mouse );

  Mouse Callbacks glutMouseFunc( my_mouse ); void myMouse (int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {

  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 glutReshapeFunc

  Event Queue Event queue

  Keyboard Mouse Window ….

  Mouse_callback() Keypress_callback() window_callback()

  MainLoop()

  Cheers, Old Sport

Dokumen yang terkait

Sunu Puguh Hayu Triono Program Studi Manajemen Sekolah Tinggi Ilmu Ekonomi Indonesia Membangun Jl. Sukarno Hatta No.448 Bandung e-mail: sunu.puguhymail.com ABSTRAK - View of PENGARUH ENTREPRENEURIAL SELF EFFICACY DAN PERSONAL NETWORKS TERHADAP NIAT MAHASI

0 0 25

Henny Utarsih Program Studi Manajemen Sekolah Tinggi Ilmu (STIE) Ekuitas, Jl. PH.H. Mustofa No.31 Bandung Email : henny.utarsihgmail.com ABSTRAK - View of PENGARUH EXPERIENTAL MARKETING, CUSTOMER RELATIONSHIP MARKETING, DAN CUSTOMER SATISFACTION TERHADAP

0 0 24

(Studi Kasus pada Perusahaan yang Go Publik di Bursa Efek Indonesia) Dedi Gumilar Program Studi Manajemen Sekolah Tinggi Ilmu Ekonomi Indonesia Membangun Jl. Sukarno Hatta No.448 Bandung Email : goo.meelargmail.com ABSTRAK - View of Beta: Tinjauan atas Op

0 0 17

Program Studi Magister Manajemen Sekolah Tinggi Ilmu Ekonomi Indonesia Membangun Jl. Soekarno Hatta No. 448 Bandung Email : gurawandayonayahoo.co.id ridhaagus86gmail.com ABSTRAK - View of PENGARUH KOMPENSASI TIDAK LANGSUNG DAN LINGKUNGAN KERJA FISIK TERHA

0 0 22

Program Studi Magister Manajemen Sekolah Tinggi Ilmu Ekonomi Indonesia Membangun Jl. Soekarno Hatta No. 448 Bandung Email : ade.salmangmail.com NDC_75yahoo.com ABSTRAK - View of ANALISA CAPITAL BUDGETING SEBAGAI ALAT UNTUK MENILAI KELAYAKAN RENCANA INVEST

1 1 20

View of PENGARUH PENGHARGAAN FINANSIAL DAN PERTIMBANGAN PASAR KERJA TERHADAP MINAT MAHASISWA AKUNTANSI UNTUK BERKARIR MENJADI AKUNTAN PUBLIK (Studi Pada Mahasiswa Program Studi Akuntansi STIE INABA Bandung)

0 0 23

Program Studi S1 Administrasi Bisnis, Fakultas Komunikasi dan Bisnis, Universitas Telkom Email : geniamegaymail.com Email : yahyaarwiyahtelkomuniversity.ac.id ABSTRAK - View of PENGARUH NILAI-NILAI RELIGIUS PEGAWAI DALAM MENDUKUNG PENINGKATAN KINERJA PEGA

0 0 13

Manajemen Pembiayaan dan Pelaporan Keuangan Program Microfinance Syariah Berbasis Masyrakat (MISYKAT) di Lembaga Keuangan Mikro Syariah Baitul Mal Aceh

0 0 7

IKI 30320: Sistem Cerdas Kuliah 12: First Order Logic

0 0 42

Program Studi Teknik Informatika ITB

0 2 46