Your First OpenGL Program
GRAFIKA
KOMPUTER
Your First
OpenGL Program
Inisialisasi Simple
Update logika Program Architecture
Rendering Keluar ? RECALL
Recall OpenGL performing can’twindowing 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 (forCommand 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 to0.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 callbackRendering 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 glutKeyboardFuncMouse 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