GRAFIKA
KOMPUTER
Your First
Simple
Program
Architecture
Inisialisasi
Update logika
Rendering
Recall
OpenGL
can’t
performing
windowing tasks or obtaining
#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
#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
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
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 Main
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
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
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
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
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
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
OPENGL
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
example, the
3f
in
glColor3f()
Command Syntax
gl
Vertex
2
f
()
Number of Component
2
– (x, y)3
– (x, y, z)4
– (x, y, z, w)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
values, rather than a series of
Command Syntax
glColor3f(1.0, 0.0, 0.0);
OPENGL AS A
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
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
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
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
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
Callback Func
Callback Func
Callback function : Routine to
call
when an event happens
~ Window resize or redraw
~ User input (mouse, keyboard)
Callback Func
“Register” callbacks with GLUT
glutDisplayFunc( my_display_func );
glutIdleFunc( my_idle_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);
glutMouseFunc (my_mouse_events_func);
glutMainLoop(); return 0;
}
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] );
glVertex3fv( v[1] );
glVertex3fv( v[2] );
glEnd();
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;
User Input Callbacks
> Process user input
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
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 Resizing
glutReshapeFunc
System Idle Timer
glutIdleFunc glutTimerFunc
Event Queue
Event queue
Keyboard
Mouse
Window
….
Mouse_callback() {
….
{
Keypress_callback() {
….
{
window_callback() {
….
{