UNIT 10-OpenGL
UNIT 10-OpenGL
com
1
https://genuinenotes.com
Introduction
• OpenGL (Open Graphics Library) is the computer industry's
standard application program interface ( API ) for defining 2-D
and 3-D graphic images.
• Prior to OpenGL, any company developing a graphical
application typically had to rewrite the graphics part of it for
2
https://genuinenotes.com
Introduction…
• OpenGL specifies a set of "commands" or immediately executed
functions.
• Each command directs a drawing action or causes special effects. A
list of these commands can be created for repetitive effects.
• OpenGL is independent of the windowing characteristics of each
Introduction…
callback functions
• A callback function is basically a function pointer that you can
set that GLFW can call at an appropriate time. One of those
callback functions that we can set is the KeyCallback function,
which should be called whenever the user interacts with the
keyboard.
The key input function takes a GLFWwindow as its first argument, an integer
that specifies the key pressed, an action that specifies if the key is pressed or
released and an integer representing some bit flags to tell you if shift, control, 5
alt or super keys have been pressed. Whenever a user pressed a key, GLFW
calls this function and fills in the proper arguments for you to process.
https://genuinenotes.com
callback functions
callback function
• A callback function is a function which the library (GLUT) calls
when it needs to know how to proccess something.
• e.g. when glut gets a key down event it uses the
glutKeybourdFunc callback routine to find out what to do with
a key press.
callback function
• GLUT supports a number of callbacks to respond to events.
There are three types of callbacks: window, menu, and global.
Window callbacks indicate when to redisplay or reshape a
window, when the visibility of the window changes, and when
input is available for the window. The menu callback is set by
Color commands
• There are many ways to specify a color in computer graphics,
but one of the simplest and most widely used methods of
describing a color is the RGB color model. RGB stands for the
colors red, green and blue: the additive primary colors. Each of
these colors is given a value, in OpenGL usually a value
9
https://genuinenotes.com
Color commands
• For instance, pure red is represented as (1, 0, 0) and full blue is
(0, 0, 1). White is the combination of all three, denoted (1, 1,
1), while black is the absence of all three, (0, 0, 0). Yellow is
the combination of red and green, as in (1, 1, 0). Orange is
yellow with slightly less green, represented as (1, 0.5, 0).
Using glColor3f
• glColor3f() takes 3 arguments: the red, green and blue
components of the color you want. After you use glColor3f,
everything you draw will be in that color. For example,
consider
void this display
display() { function:
glClear(GL_COLOR_BUFFER_BIT |
glBegin(GL_QUADS);
glVertex2f(-0.75, 0.75);
glVertex2f(-0.75, -0.75);
glVertex2f(0.75, -0.75);
glVertex2f(0.75, 0.75);
glEnd(); 11
glutSwapBuffers();
}
https://genuinenotes.com
Giving Individual Vertices Different
Colors
• glColor3f can be called in between glBegin and glEnd. When it
is used this way, it can be used to give each vertex its own
color. The resulting rectangle is then shaded with an attractive
color gradient, as shown on the right.
Output
Drawings pixels
• OpenGL provides only the lowest level of support for drawing
strings of characters and manipulating fonts.
• The commands glRasterPos*() and glBitmap() position and
draw a single bitmap on the screen.
• In addition, through the display-list mechanism, you can use a
15
https://genuinenotes.com
Drawing lines
glBegin(GL_LINES);
glVertex2f(.25,0.25);
glVertex2f(.75,.75);
glEnd();
Drawing lines
/* Draws two horizontal lines */
glBegin(GL_LINES);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
17
https://genuinenotes.com
Loop of lines
/* Draws a square */
glBegin(GL_LINE_LOOP);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
18
https://genuinenotes.com
Connected lines
/* Draws a 'C' */
glBegin(GL_LINE_STRIP);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
19
https://genuinenotes.com
22
https://genuinenotes.com
23
https://genuinenotes.com
24
https://genuinenotes.com
Chapter 10