GRAFIKA KOMPUTERmalifauzi.lecture.ub.ac.id/files/2016/02/Your-First-OpenGL-Program.pdf · GRAFIKA...

Post on 05-Sep-2020

18 views 0 download

Transcript of GRAFIKA KOMPUTERmalifauzi.lecture.ub.ac.id/files/2016/02/Your-First-OpenGL-Program.pdf · GRAFIKA...

GRAFIKA KOMPUTER~ M. Ali Fauzi

Your First OpenGL Program

Simple Program Architecture

Inisialisasi

Update logika

Rendering

De-Inisialisasi

Keluar ?

RECALL

Recall

OpenGL can’t performing windowing tasks or obtaining user input, but GLUT

THE CODE

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

}

#include <windows.h>

#include <GL/glut.h>

The Header

// For MS Windows

#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 to be re-painted. */

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;

}

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;

}

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 – RGB or color Index, single or double Buffer

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

// Create a window with the given title// Set the window's initial width & height

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

// Position the window's initial top-left corner

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

// Register display callback handler for window re-paint

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

// Enter the infinitely event-processing loop

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

// Set background color to black and opaque

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

// Clear the color buffer

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

// Draw a Red 1x1 Square centered at origin

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

// Red

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

// Each set of 4 vertices form a quad

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

// Render

OPENGL COMMAND SYNTAX

Command Syntax

OpenGL commands use the prefix gl and initial capital letters for each word making up the command name (recall glClearColor(), for example)

Command Syntax

OpenGL defined constants begin with GL_, use all capital letters, and use underscores to separate words (for example,GL_SINGLE).

Command Syntax

You might also have noticed some seemingly extraneous letters appended to some command names (forexample, the 3f in glColor3f()and 2f in glVertex2f()).

Command Syntax

glVertex2f()

Number of Component

2 – (x, y)

3 – (x, y, z)

4 – (x, y, z, w)

Data Typef - floati – integerd – doubleEtc.

Command Syntax

Command Syntax

Thus, the two commands are equivalent

glVertex2i(1, 3);

glVertex2f(1.0, 3.0);

Command Syntax

> When only x and y are

specified, z defaults to 0.0and w defaults to 1.0.

> When x, y, and z are

specified, w defaults to 1.0.

Command Syntax

Thus, the two commands are equivalent

glVertex2i(1, 3);

glVertex3f(1.0, 3.0, 0.0);

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 individual arguments.

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) that then remain in effect until you change them.

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

// Red

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

// Green

GLUT CALLBACK FUNCTIONS

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 according to the user’s input

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 according to the user’s input

Callback Func

Events – key press, mouse button 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);

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

// Register display callback handler for window re-paint

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;

}

Callback Func

Rendering Callbacks

> Callback function where all our drawing is done> Every GLUT program must have a display callback

glutDisplayFunc( my_display_func );

/* this part is in main.c */

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

}

Idle Callbacks

> Use for animation and continuous update

~ Can use glutTimerFunc or timed callbacks for animations

glutIdleFunc( idle );

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’ :rotate = GL_TRUE;break;

}}

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 OpenGLEvent 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

Software What to draw glutDisplayFunc

Event Queue

Event queue

Keyboard

Mouse

Window

….

Mouse_callback() {….{

Keypress_callback() {….{

window_callback() {….{

MainLoop()

Cheers, Old Sport~ M. Ali Fauzimoch.ali.fauzi@gmail.com