0% found this document useful (0 votes)
52 views

Open GLTutorial

1. The document describes the basic OpenGL architecture and pipeline. It shows how OpenGL sits between the CPU and graphics hardware to perform geometric processing and rendering. 2. It explains the different OpenGL primitive types like points, lines, triangles, and polygons that are used to define geometric shapes. 3. It provides examples of basic OpenGL functions and commands for initializing OpenGL, defining a display callback, setting colors and viewport, and drawing geometric primitives.

Uploaded by

anggi ramadan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Open GLTutorial

1. The document describes the basic OpenGL architecture and pipeline. It shows how OpenGL sits between the CPU and graphics hardware to perform geometric processing and rendering. 2. It explains the different OpenGL primitive types like points, lines, triangles, and polygons that are used to define geometric shapes. 3. It provides examples of basic OpenGL functions and commands for initializing OpenGL, defining a display callback, setting colors and viewport, and drawing geometric primitives.

Uploaded by

anggi ramadan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 93

Software Organization

application program

OpenGL Motif
widget or similar GLUT
GLX, AGL
or WGL GLU

X, Win32, Mac O/S GL

software and/or hardware

204481 Foundation of Computer Graphics December 19, 2019 1


OpenGL Architecture
Immediate Mode Geometric
pipeline
Per Vertex
Polynomial Operations &
Evaluator Primitive
Assembly

Display Per Fragment Frame


CPU List
Rasterization
Operations Buffer

Texture
Memory
Pixel
Operations

204481 Foundation of Computer Graphics December 19, 2019 2


OpenGL Command Notation (2/2)

glVertex3fv( ... )

Number of Data Type Vector


components b - byte omit “v” for
ub - unsigned byte
2 - (x,y) s - short
scalar form
3 - (x,y,z) us - unsigned short
4 - (x,y,z,w) i - int glVertex2f( x, y )
ui - unsigned int
f - float
d - double

204481 Foundation of Computer Graphics December 19, 2019 3


Preliminaries
Header files
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
GL enumerated types
 for platform independence
GLbyte, GLshort, GLushort, GLint, GLuint,
GLsizei, GLfloat, GLdouble, GLclampf,
GLclampd, GLubyte, GLboolean, GLenum,
GLbitfield

204481 Foundation of Computer Graphics December 19, 2019 4


OpenGL #defines
Most constants are defined in the include
files gl.h, glu.h and glut.h
Note #include <glut.h> should
automatically include the others
Examples
glBegin(GL_PLOYGON)

glClear(GL_COLOR_BUFFER_BIT)

includefiles also define OpenGL data types:


Glfloat, Gldouble,….

204481 Foundation of Computer Graphics December 19, 2019 5


A Simple Program
Generate a square on a solid background

204481 Foundation of Computer Graphics December 19, 2019 6


simple.c
#include <glut.h>
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
int main(int argc, char** argv){
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
204481 Foundation of Computer Graphics December 19, 2019 7
Event Loop
Note that the program defines a display
callback function named mydisplay
Every glut program must have a display
callback
The display callback is executed whenever
OpenGL decides the display must be refreshed,
for example when the window is opened
The main function ends with the program
entering an event loop

204481 Foundation of Computer Graphics December 19, 2019 8


Defaults
simple.c is too simple
Makes heavy use of state variable default
values for
Viewing
Colors
Window parameters
Next version will make the defaults more
explicit

204481 Foundation of Computer Graphics December 19, 2019 9


main.c
#include <GL/glut.h> includes gl.h

int main(int argc, char** argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0); define window properties
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
display callback
init(); set OpenGL state

glutMainLoop();
} enter event loop

204481 Foundation of Computer Graphics December 19, 2019 10


GLUT functions
glutInit allows application to get command line
arguments and initializes system
gluInitDisplayMode requests properties of the
window (the rendering context)
RGB color
Single buffering
Properties logically ORed together
glutWindowSize in pixels
glutWindowPosition from top-left corner of display
glutCreateWindow create window with title “simple”
glutDisplayFunc display callback
glutMainLoop enter infinite event loop

204481 Foundation of Computer Graphics December 19, 2019 11


glutInit()
 The arguments allows application to get
command line arguments (argc and argv) and
initializes system
 This procedure must be called before any others.
 It processes (and removes) command-line
arguments that may be of interest to GLUT and
the window system and does general initialization
of GLUT and OpenGL.

204481 Foundation of Computer Graphics December 19, 2019 12


glutInitWindowSize()
 This command specifies the desired width and
height of the graphics window. The general form
is:
glutInitWindowSize(int width, int height)
 The values are given in numbers of pixels.

204481 Foundation of Computer Graphics December 19, 2019 13


glutInitPosition()
 This command specifies the location of the upper
left corner of the graphics window. The form is
glutInitWindowPosition(int x, int y)
 where the (x, y) coordinates are given relative to
the upper left corner of the display. Thus, the
arguments (0, 0) places the window in the upper
left corner of the display.

204481 Foundation of Computer Graphics December 19, 2019 14


glutCreateWindow()
 This command actually creates the graphics
window. The general form of the command is
glutCreateWindowchar(*title)
 where title is a character string. Each window has
a title, and the argument is a string which
specifies the window’s title.

204481 Foundation of Computer Graphics December 19, 2019 15


init.c

black clear color


void init()
opaque window
{
glClearColor (0.0, 0.0, 0.0, 1.0);

glColor3f(1.0, 1.0, 1.0); fill with white

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
viewing volume

204481 Foundation of Computer Graphics December 19, 2019 16


Orthographic Viewing
In the default orthographic view, points are
projected forward along the z axis onto the
plane z=0

z=0

z=0

204481 Foundation of Computer Graphics December 19, 2019 17


Transformations and Viewing
In OpenGL, the projection is carried out by a
projection matrix (transformation)
There is only one set of transformation functions
so we must set the matrix mode first
glMatrixMode (GL_PROJECTION)
 Transformation functions are incremental so we
start with an identity matrix and alter it with a
projection matrix that gives the view volume
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

204481 Foundation of Computer Graphics December 19, 2019 18


Two- and three-dimensional viewing
In glOrtho(left, right, bottom, top,
near, far) the near and far distances are
measured from the camera
Two-dimensional vertex commands place all
vertices in the plane z=0
If the application is in two dimensions, we can use
the function
gluOrtho2D(left, right,bottom,top)
Intwo dimensions, the view or clipping volume
becomes a clipping window
204481 Foundation of Computer Graphics December 19, 2019 19
mydisplay.c

void mydisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}

204481 Foundation of Computer Graphics December 19, 2019 20


OpenGL Primitives

GL_POINTS GL_POLYGON
GL_LINES GL_LINE_STRIP

GL_LINE_LOOP

GL_TRIANGLES
GL_QUAD_STRIP

GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

204481 Foundation of Computer Graphics December 19, 2019 21


Polygon Issues
OpenGL will only display polygons correctly that are
Simple: edges cannot cross
Convex: All points on line segment between two points
in a polygon are also in the polygon
Flat: all vertices are in the same plane
User program must check if above true
Triangles satisfy all conditions

nonsimple polygon nonconvex polygon

204481 Foundation of Computer Graphics December 19, 2019 22


Attributes
Attributes are part of the OpenGL and
determine the appearance of objects
Color (points, lines, polygons)
Size and width (points, lines)
Stipple pattern (lines, polygons)
Polygon mode
Display as filled: solid color or stipple pattern
Display edges

204481 Foundation of Computer Graphics December 19, 2019 23


Constructive Primitives
 The glBegin() / glEnd() Wrappers
 all OpenGL descriptions of primitives start
with glBegin(xxx), where xxx is an OpenGL-
defined constant that identifies the OpenGL
primitive.

204481 Foundation of Computer Graphics December 19, 2019 24


Specifying Primitives
 Primitives are described by their vertices
 Vertex is a point in space which is used in
the construction of a geometric primitive
 Described by a homogenous coordinate

x y z w

204481 Foundation of Computer Graphics December 19, 2019 25


Specifying an OpenGL Vertex
 Recall OpenGL specifies geometric
primitives by its vertices
glVertex3f( x, y, z );
 Different primitives require different
numbers of vertices

204481 Foundation of Computer Graphics December 19, 2019 26


Drawing Points
glBegin(GL_POINTS); // selection points as
the primitive
glVertex3f(0.0f, 0.0f, 0.0f); // Specify a point
glVertex3f(50.0f, 50.0f, 50.0f); // Specify another
point
glEnd(); // Done drawing points

204481 Foundation of Computer Graphics December 19, 2019 27


Setting the Point Size
void glPointSize(Glfloat size);

GLFloat sizes[2]; // Store supported point size range


GLFloat step; // Store supported point size
increments

// Get supported point size range and step size


glGetFloatv(GL_POINT_SIZE_RANGE, sizes);
glGetFloatv(GL_POINT_SIZE_GRANULARITY, &step);

204481 Foundation of Computer Graphics December 19, 2019 28


Actually Drawing Something ...
 Here’s an OpenGL sequence to draw a
square centered around the origin

glBegin( GL_QUADS );
glVertex2f( -0.8, -0.8 );
glVertex2f( 0.8, -0.8 );
glVertex2f( 0.8, 0.8 );
glVertex2f( -0.8, 0.8 );
glEnd();
GL_QUADS

204481 Foundation of Computer Graphics December 19, 2019 29


Adding Personality to Primitives
 State ( or Attributes )
 data required for computing colors for
primitives
 Examples
 color
 reflectivity
 surface texture

204481 Foundation of Computer Graphics December 19, 2019 30


Specifying a Vertex’s Color
 Use the OpenGL color command

glColor3f( r, g, b );
 Where you specify the color determines
how the primitive is shaded
 points only get one color

204481 Foundation of Computer Graphics December 19, 2019 31


Opening a Window Using GLUT
void main( int argc, char** argv )
{
glutInitWindowSize( 512, 512 );
glutInitDisplayMode( GLUT_RGBA );
glutCreateWindow( “my window” );

init();

glutDisplayFunc( drawScene );

glutMainLoop();
}

204481 Foundation of Computer Graphics December 19, 2019 32


OpenGL Initalization
 We’ll use the init() routine for our one-time
OpenGL state initialization
 call after window has been created, but before
first rendering call

void init( void )


{
glClearColor( 1.0, 0.0, 0.0, 1.0 );
}

204481 Foundation of Computer Graphics December 19, 2019 33


Drawing Lines in 3D
glBegin(GL_LINE_STRIP)
glVertex3f(0.0f, 0.0f, 0.0f); // v0
glVertex3f(50.0f, 50.0f, 50.0f); // v1
glEnd();
Y

v1
X
v0

204481 Foundation of Computer Graphics December 19, 2019 34


Draw Line in 3D
glBegin(GL_LINE_STRIP) Y
v2
glVertex3f(0.0f, 0.0f, 0.0f); // v0
glVertex3f(50.0f, 50.0f, 0.0f); // v1
v1
glVertex3f(50.0f, 100.0f, 0.0f); // v2
glEnd(); v0 X

glBegin(GL_LINE_LOOP) Y
glVertex3f(0.0f, 0.0f, 0.0f); // v0 v2
glVertex3f(50.0f, 50.0f, 0.0f); // v1
glVertex3f(50.0f, 100.0f, 0.0f); // v2 v1
glEnd();
v0 X

204481 Foundation of Computer Graphics December 19, 2019 35


Setting the Line Width
void glLineWidth( GLFloat width);
GLFloat sizes[2]; // Store supported line width range
GLFloat step; // Store supported line width
increments

// Get supported line width range and step size


glGetFloatv(GL_LINE_WIDTH_RANGE, sizes);
glGetFLoatv(GL_LINE_WIDTH_GRANULARITY,
&step);

204481 Foundation of Computer Graphics December 19, 2019 36


Drawing Triangles in 3D
glBegin(GL_TRIANGLES) Y
glVertex2f(0.0f, 0.0f); // v0
glVertex2f(25.0f, 25.0f); // v1
glVertex2f(50.0f, 0.0f); // v2 v1
glEnd();
X
v0 v2

Choose the Fastest Primitives for Performance Tip


Most 3D accelerated hardware is highly optimized for the drawing
of triangles.

204481 Foundation of Computer Graphics December 19, 2019 37


Winding
 The combination of order and direction in
which the vertices are specified
Y

v5

v1

X
v3 v4 v0 v2

Counterclockwise Clockwise winding


winding (Front Facing) (Back facing)

204481 Foundation of Computer Graphics December 19, 2019 38


Winding (cont)
 OpenGL by default considers polygons that have
counterclockwise winding to be front facing
Why so important?
 you can hide the back of a polygon altogether, or
give it a different color and reflective property as
well

GLFrontFace(GL_CW); // change default winding to clockwise


GLFrontFace(GL_CCW); // change back to counterclockwise

204481 Foundation of Computer Graphics December 19, 2019 39


Triangle Strips
 GL_TRIANGLE_STRIP

v4

v2
v2 v2
v3 v3

v0 v1 v0 v1 v0 v1

204481 Foundation of Computer Graphics December 19, 2019 40


Triangle Fans
GL_TRIANLGLE_FAN
v1
2
v2
v1 v1
2 1
v2 v0 v3
v2
1 1
2 3 2
3
v0 v4
v0 3 v3

204481 Foundation of Computer Graphics December 19, 2019 41


Setting Polygon Colors
 Colors are specified per vertex, not per polygon
 glShadeModel(GL_FLAT)
 glShadeModel(GL_SMOOTH)

 GL_FLAT tells the OpenGL to fill the polygon with the


solid color that was current when the polygon’s last
vertex was specified.
 GL_SMOOTH tells the OpenGl to shade the triangle
smoothly from each vertex, attempting to interpolate the
colors between those specified for each vertex

204481 Foundation of Computer Graphics December 19, 2019 42


Four-Sided Polygons: Quads

v1 2 v3 v1 2 v3 v1 v3 2 v5

1 3 1 3 1 3

v0 4 v2 v0 4 v2 v0 v2 4 v4
Example of GL_QUAD Progression of GL_QUAD_STRIP

204481 Foundation of Computer Graphics December 19, 2019 43


General Polygon
GL_POLYGON

v0 v1

v2

v3
v4

204481 Foundation of Computer Graphics December 19, 2019 44


Simple lighting
Enable lighting:
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
Specify light sources parameters:
 glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient);
 glLightfv(GL_LIGHT0,GL_POSITION, light_pos);
 glLightfv(GL_LIGHT0,GL_DIFFUSE, light_dif);
Plus global ambient light:
 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,
l_ambient);

204481 Foundation of Computer Graphics December 19, 2019 45


Specifying materials (1/2)
One function call for each property:
 glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,
mat_amb);
 glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,
mat_diff);
 glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULA
R,matspec);
 glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS
,100.0);
 glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION
,mat_emi);

204481 Foundation of Computer Graphics December 19, 2019 46


Specifying materials (2/2)
Also possible using glColor();
 glColorMaterial(GL_FRONT,GL_DIFFUSE);
 glEnable(GL_COLOR_MATERIAL);
 glColor3f(0.14,0.33,0.76);

204481 Foundation of Computer Graphics December 19, 2019 47


Color

OpenGL supports two colors model


 RGBA mode
 color-index mode

violet blue green yellow orange red

390 nm 720 nm

204481 Foundation of Computer Graphics December 19, 2019 48


The Color Cube
Green

Yellow
(0,255,0) (255,255,0)
Cyan
(0,255,255) White
(255,255,255)

Black
(0,0,0) Red

Magenta
(255,0,255)
Blue

204481 Foundation of Computer Graphics December 19, 2019 49


Color and Shading
 Color in RGBA mode is set by specifying
the red, green, blue and alpha intensities.
 alpha = 1 //opaque
 alpha = 0 // transparent

204481 Foundation of Computer Graphics December 19, 2019 50


RGB color
Each color component stored separately in the frame
buffer
Usually 8 bits per component in buffer
Note in glColor3f the color values range from 0.0
(none) to 1.0 (all), while in glColor3ub the values range
from 0 to 255

204481 Foundation of Computer Graphics December 19, 2019 51


Indexed Color
Colors are indices into tables of RGB values
Requires less memory
indices usually 8 bits
not as important now
Memory inexpensive

Need more colors for shading

204481 Foundation of Computer Graphics December 19, 2019 52


Color and State
The color as set by glColor becomes part of
the state and will be used until changed
Colors and other attributes are not part of the
object but are assigned when the object is
rendered
We can create conceptual vertex colors by
code such as
glColor
glVertex
glColor
glVertex
204481 Foundation of Computer Graphics December 19, 2019 53
Setting of color attribute
 glClearColor(1.0, 1.0, 1.0, 0.0); //Clear
color
 glColor3f(1.0f, 0.0f, 0.0f); // red

204481 Foundation of Computer Graphics December 19, 2019 54


Example of setting color
glColor3f(1.0f, 0.0f, 0.0f); // no alpha value form
glBegin( GL_TRIANGLEs);
glVertex3f( -1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();

• Note that the glColor*() function can be placed inside a


glBegin()/glEnd() pair. Therefore you can specify individual colors for
each individual vertex

204481 Foundation of Computer Graphics December 19, 2019 55


2 vertices of different color

What happen if 2 vertices have different colors?

glBegin( GL_TRIANGLEs);
glColor3f(1.0f, 0.0f, 0.0f); // red
glVertex3f( -1.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex3f(1.0f, 0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // blue
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();

204481 Foundation of Computer Graphics December 19, 2019 56


Shading
Green
 What color is the
interior if we specify a
different color for each
vertex of a primitive?
Red
 Smooth shading causes
the color vary as they
do through the color Blue (128,128,128)
Medium Grey
cube from one point to
the other

(0,0,0) (255,255,255)
black White

204481 Foundation of Computer Graphics December 19, 2019 57


Shading Model
 glShadeModel(GL_SMOOTH);
 glShadeModel(GL_FLAT);

 Flat shading means that no shading


calculations are performed on the interior of
primitives.
 Generally the color specify by the last vertex
except the GL_POLYGON primitive, the color
specify by the first vertex.

204481 Foundation of Computer Graphics December 19, 2019 58


Smooth Color
Default is smooth shading
OpenGL interpolates vertex colors across
visible polygons
Alternative is flat shading
Color of first vertex
determines fill color
glShadeModel
(GL_SMOOTH)
or GL_FLAT

204481 Foundation of Computer Graphics December 19, 2019 59


Flat Shading in OpenGL
 If you issue only one glColor() command
per primitive

glColor3f( r, g, b );
glBegin( GL_TRIANGLES );
glVertex3fv( v1 );
glVertex3fv( v2 );
glVertex3fv( v3 );
glEnd();

204481 Foundation of Computer Graphics December 19, 2019 60


Flat Shading in OpenGL

glColor3f( r, g, b );
glBegin( GL_TRIANGLES );
glVertex3fv( v1 );
glVertex3fv( v2 );
glVertex3fv( v3 );
glEnd();

204481 Foundation of Computer Graphics December 19, 2019 61


Gouraud Shading in OpenGL
 However, to get Gouraud, issue a color per
vertex

glBegin( GL_TRIANGLES );
glColor3fv( c1 );
glVertex3fv( v1 );
glColor3fv( c2 );
glVertex3fv( v2 );
glColor3fv( c3 );
glVertex3fv( v3 );
glEnd();

204481 Foundation of Computer Graphics December 19, 2019 62


Gouraud Shading in OpenGL

glBegin( GL_TRIANGLES );
glColor3fv( c1 );
glVertex3fv( v1 );
glColor3fv( c2 );
glVertex3fv( v2 );
glColor3fv( c3 );
glVertex3fv( v3 );
glEnd();

204481 Foundation of Computer Graphics December 19, 2019 63


Viewports
Do not have use the entire window for the
image: glViewport(x,y,w,h)
Values in pixels (screen coordinates)

204481 Foundation of Computer Graphics December 19, 2019 64


Event-Driven Programming & Callbacks
 Virtually all interactive graphics programs are
event driven.
 Therefore, a graphics program must be prepared
at any time for input from any number of sources,
including the mouse, or keyboard, or other
graphics devises such as trackballs and
joysticks.
 In OpenGL, this is done through the use of
callbacks.

204481 Foundation of Computer Graphics December 19, 2019 65


Callbacks
 The callbacks are used when the graphics
program instructs the system to invoke a
particular procedure or functions whenever an
event of interest occurs, say, the mouse button is
clicked.
 The graphics program indicates its interest, or
registers, for various events. This involves telling
the window system which event type we are
interested in, and passing it the name of a
procedure we have written to handle the event.

204481 Foundation of Computer Graphics December 19, 2019 66


Types of callback
 Callbacks are used for two purposes:
1. User input events
2. System events
User input events: include things such as mouse
clicks,the motion of the mouse (without clicking) that is
also called passive motion and keyboard hits.

NB: The program is only signaled about events that


happen to its window. For example, entering text into
another window’s dialogue box will not generate a
keyboard event for the program.
204481 Foundation of Computer Graphics December 19, 2019 67
Types of callback (1/2)
System event: There are a number of different
events that are generated by the system such as
display event, reshape event, idle event and
timer event.
display event : a special event that every OpenGL
program must handle. A display event is invoked
when the system senses that the contents of the
window need to be redisplayed, either because:
the graphics window has completed its initial
creation an obscuring window has moved away,
thus revealing all or part of the graphics window

204481 Foundation of Computer Graphics December 19, 2019 68


Types of callback (2/2)
 The program explicitly requests redrawing,
by calling glutPostRedisplay() procedure.

204481 Foundation of Computer Graphics December 19, 2019 69


Types of callback
 Reshape event: Happens whenever the
window shape is altered.The callback
provides information on the new size of the
window.
 Idle event: Happens every time the system
has nothing to do

 Timer event: Happens when after the


waiting period is over

204481 Foundation of Computer Graphics December 19, 2019 70


Callbacks

Table: Common callbacks and the associated registration functions

204481 Foundation of Computer Graphics December 19, 2019 71


Callback Setup
int main(int argc, char** argv)
{
...
glutDisplayFunc(myDraw); // set up the callbacks
glutReshapeFunc(myReshape);
glutMouseFunc(myMouse);
glutKeyboardFunc(myKeyboard);
glutTimerFunc(20, myTimeOut, 0);
...
}

204481 Foundation of Computer Graphics December 19, 2019 72


Callback Functions
 Callback functions depend on the function
definition that we create.

204481 Foundation of Computer Graphics December 19, 2019 73


Examples of Callback Functions for SE.
void myDraw() { // called to display window
// ...insert your drawing code here ...
}
void myReshape(int w, int h) { // called if reshaped
windowWidth = w; // save new window size
windowHeight = h;
// ...may need to update the projection ...
glutPostRedisplay(); // request window redisplay
}
void myTimeOut(int id) { // called if timer event
// ...advance the state of animation incrementally...
glutPostRedisplay(); // request redisplay
glutTimerFunc(20, myTimeOut, 0); // request next timer event
}
204481 Foundation of Computer Graphics December 19, 2019 74
Callback Function for SE
 From the example, both the timer and reshape
callback invoke the function
glutPostRedisplay().

 This function informs OpenGL that the state of


the scene has changed and should be redrawn

204481 Foundation of Computer Graphics December 19, 2019 75


Example of Callback Functions for User Input
Events
void myMouse(int b, int s, int x, int y) {
switch (b) { // b indicates the button
case GLUT_LEFT_BUTTON:
if (s == GLUT_DOWN) // button pressed
// ...
else if (s == GLUT_UP) // button released
// ...
break;
// ... // other button events
}
}

204481 Foundation of Computer Graphics December 19, 2019 76


GLUT Parameters

GLUT parameter names associated with mouse events

204481 Foundation of Computer Graphics December 19, 2019 77


Example of Callback Functions for User Input
Events

// called if keyboard key hit


void myKeyboard(unsigned char c, int x, int y) {
switch (c) { // c is the key that is hit
case ’q’: // ’q’ means quit
exit(0);
break;
// ... // other keyboard events
}
}

204481 Foundation of Computer Graphics December 19, 2019 78


Source :

Pradondet Nilagupta
Department of Computer Engineering
Kasetsart University

204481 Foundation of Computer Graphics December 19, 2019 79


Menggambar Titik :

glBegin(GL_POINTS)

glVertex2f(x,y) A(0.5,0.5) B(0.5,-0.5) dst....


glVertex2i(x,y) A(0,0) B(0,5)... Dst

glVertex2d(x,y) A(5.1e09, 7.3e12)


glEnd( );

Void drawDot ( int x, int y)


{
glBegin(GL_POINTS):
glVertex2i(x,y):
glEnd( );
} 204481 Foundation of Computer Graphics December 19, 2019 80
Menggambar Titik :

Void drawDot ( int x, int y)


{
glBegin(GL_POINTS):
glVertex2i(x,y):
glEnd( );
}

Void drawDot ( float x, float y)


{
glBegin(GL_POINTS):
glVertex2f(x,y):
glEnd( );
}

204481 Foundation of Computer Graphics December 19, 2019 81


Menggambar Garis : Tipe Integer

glBegin(GL_LINES) ;
glVertex2i(100,100);
glVertex2i(200,150);
glEnd( );

Void drawLine ( int x1, int y1, int x2, int y2)
{
glBegin(GL_LINES):
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd( );
}

204481 Foundation of Computer Graphics December 19, 2019 82


Menggambar Garis : Tipe Float

glBegin(GL_LINES) ;
glVertex2f(100,100);
glVertex2f(200,150);
glEnd( );

Void drawLine ( float x1, float y1, float x2, float y2)
{
glBegin(GL_LINES):
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd( );
}

204481 Foundation of Computer Graphics December 19, 2019 83


Menggambar Garis : Tipe Double

glBegin(GL_LINES) ;
glVertex2d(100,100);
glVertex2d(200,150);
glEnd( );

Void drawLine ( double x1, double y1, double x2, double y2)
{
glBegin(GL_LINES):
glVertex2d(x1,y1);
glVertex2d(x2,y2);
glEnd( );
}

204481 Foundation of Computer Graphics December 19, 2019 84


Menggambar PolyLine : Tipe Integer

glBegin(GL_LINE_STRIP) ;
glVertex2i(100,100);
glVertex2i(200,150);
glVertex2i(300,50);
glEnd( );

Void drawLine ( int x1, int y1, int x2, int y2, int x3, int y3)
{
glBegin(GL_LINE_STRIP):
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glVertex2i(x3,y3);
glEnd( );
}
204481 Foundation of Computer Graphics December 19, 2019 85
Menggambar PolyLine : Tipe float 4 titik

glBegin(GL_LINE_STRIP) ;
glVertex2f(100,100);
glVertex2f(200,150);
glVertex2f(300,50);
glVertex2f(400,200);

glEnd( );
Void drawLine ( float x1, float y1, float x2, float y2, float x3, float y3, float x4,
float y4)
{
glBegin(GL_LINE_STRIP):
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd( );
204481 Foundation of Computer Graphics December 19, 2019 86
Menggambar PolyLine : Tipe double 5 titik
glBegin(GL_LINE_STRIP) ;
glVertex2d(100,100);
glVertex2d(200,150);
glVertex2d(300,50);
glVertex2d(400,200);
glVertex2d(250, 250);
glEnd( );

Void drawLine ( double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4, double x5, double y5)
{
glBegin(GL_LINE_STRIP):
glVertex2d(x1,y1);
glVertex2d(x2,y2);
glVertex2d(x3,y3);
glVertex2d(x4,y4);
glVertex2d(x5,y5);
glEnd( );
} 204481 Foundation of Computer Graphics December 19, 2019 87
Menggambar Polygon : Tipe float 4 titik

glBegin(GL_LINE_LOOP) ;
glVertex2f(100,100);
glVertex2f(200,150);
glVertex2f(300,50);
glVertex2f(400,200);

glEnd( );
Void drawLine ( float x1, float y1, float x2, float y2, float x3, float y3, float x4,
float y4)
{
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd( );
204481 Foundation of Computer Graphics December 19, 2019 88
Menggambar Polygon : Tipe integer 3 titik

glBegin(GL_LINE_LOOP) ;
glVertex2i(100,100);
glVertex2i(200,150);
glVertex2i(300,50);

glEnd( );
Void drawLine ( int x1, int y1, int x2, int y2, int x3, int y3)
{
glBegin(GL_LINE_LOOP);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glVertex2i(x3,y3);
glEnd( );
}

204481 Foundation of Computer Graphics December 19, 2019 89


Menggambar Polygon : Tipe double 5 titik

glBegin(GL_LINE_LOOP) ;
glVertex2d(100,100);
glVertex2d(200,150);
glVertex2d(300,50);
glVertex2d(400,200);
glVertex2d(250,250);
glEnd( );

Void drawLine ( double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4, double x5, double y5)
{
glBegin(GL_LINE_LOOP);
glVertex2d(x1,y1);
glVertex2d(x2,y2);
glVertex2d(x3,y3);
glVertex2d(x4,y4);
glVertex2d(x5,y5);
glEnd( );
} 204481 Foundation of Computer Graphics December 19, 2019 90
Menggambar PolyLine :
drawPolyline(nama_objek2D, jumlah_titik)

Void drawPolyline(point2D_i pnt [ ], int i)


{
int i;
glBegin(GL_LINE_STRIP);
for (i=0; i < n ; i=i+1 ) /* for (i=0; i , n ; i++ ) */
{
glVertex2i( pnt[i].x, pnt[i].y ) ;

}
glEnd ( );
}

Void userdraw ( void )


{
point2D_i segitiga[3]={{0,0}, {200,0}, {100,100}};
setColor(1,0,0); //Red
drawPolyline(segitiga, 3);
}

204481 Foundation of Computer Graphics December 19, 2019 91


Menggambar Polygon :
drawPolygone(nama_objek2D, jumlah_titik)

Void drawPolygon(point2D_i pnt [ ], int n)


{
int n;
glBegin(GL_LINE_LOOP);
for (n=0; n < m, n=n+1 ) /* for (n=0; n < m ; n++ ) */
{
glVertex2i( pnt[n].x, pnt[n].y ) ;

}
glEnd ( );
}

void userdraw ( void )


{
point2D_i segitiga[3]={{0,0}, {200,0}, {100,100}};
setColor(1,0,0); //Red
drawPolylgon(segitiga, 3);
}

204481 Foundation of Computer Graphics December 19, 2019 92


Memberi warna pada Polygon :
drawPolygone(nama_objek2D, jumlah_titik)

Void fillPolygon(point2D_i pnt [ ], int m, color_t color )


{
int n;
setColor(color);
glBegin(GL_POLYGON);
for (n=0; n < m, n=n+1 ) /* for (n=0; n < m ; n++ ) */
{

glVertex2i( pnt[n].x, pnt[n].y ) ;

}
glEnd ( );
}

void userdraw ( void )


{
point2D_i segitiga[3]={{0,0}, {200,0}, {100,100}};
color_t kuning = {1, 1, 0};
fillPolygon (segitiga, 3, kuning); // polygon berwarna kuning
setColor(1,0,0);
drawPolylgon(segitiga, 3); // warna garis merah
} 204481 Foundation of Computer Graphics December 19, 2019 93

You might also like