Download - OpenGL
![Page 1: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/1.jpg)
OpenGLA Brief Overview
![Page 2: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/2.jpg)
What is OpenGL? It is NOT a programming language. It is a Graphics Rendering API consisting of a
set of function with a well defined interface. Whenever we say that a program is
OpenGL-based or OpenGL applications, we mean that it is written in some programming language (such as C/C++ or Java) that makes calls to one or more of OpenGL libraries.
2
![Page 3: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/3.jpg)
Useful Websites and Books Official Site
http://www.opengl.org Non official sites
http://nehe.gamedev.net/http://google.com/
BOOKS OpenGL Red Book & OpenGL Blue Book
3
![Page 4: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/4.jpg)
Three Views of OpenGL Programmer’s view
Specify a set of objects to render Describe the properties of these objects Define how these objects should be viewed
State machine States determine how the inputs are processed. Change a state (such as color) by using state
changing functions OpenGL uses Rendering Pipeline Model
Models -> Transformer -> Clipper -> Projector -> Rasterizer -> Image
4
![Page 5: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/5.jpg)
OpenGL API Functions OpenGL contains over 200 functions
Primitive functions: define the elements (e.g. points, lines, polygons, etc.)
Attribute functions: control the appearance of primitives (e.g. colors, line types, light source, textures, etc.)
Viewing functions: determine the properties of camera. Handle transformations.
Windowing functions: not part of core OpenGL Other functions
5
![Page 6: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/6.jpg)
Libraries and HeadersLibrary Name Library File Header File NoteOpenGL opengl32.lib
(Win)-lgl (UNIX)
gl.h “core” library
Auxiliary library
glu32.lib (Win)-lglu (UNIX)
glu.h handles a variety of accessory functions
Utility toolkit glut32.lib (Win)-lglut (UNIX)
glut.hglaux.h
window management & others
6
![Page 7: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/7.jpg)
Function Naming ConventionsglColor3f(…)
library name, command name, # of arguments, argument type
gl: OpenGLglu: GLUglut: GLUT
f: the argument is float typei: the argument is integer typev: the argument requires a vector
7
![Page 8: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/8.jpg)
A Sample Programvoid main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutCreateWindow (“My First Program");
myinit ();
glutDisplayFunc ( display );
glutReshapeFunc ( resize );
glutKeyboardFunc ( key );
glutMainLoop ();
}
2
3
4
1
8
![Page 9: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/9.jpg)
1. Initialize & Create Window
void main (int argc, char **argv)
{
glutInit (&argc, argv); // GLUT initialization
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // display model
glutInitWindowSize (500, 500); // set window size
glutCreateWindow (“My First Program"); // create window
……
}
9
![Page 10: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/10.jpg)
2. Initialize OpenGL Statevoid myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0); // background color
glColor3f(1.0, 0.0, 0.0); // line color
glMatrixMode(GL_PROJECTION); // set up viewing:
glLoadIdentity(); // load identity matrix
gluOrtho2D(0.0, 500.0, 0.0, 500.0); // specify Orthographic view
glMatrixMode(GL_MODELVIEW); // go back to MV matrix
}
10
![Page 11: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/11.jpg)
3. Register Callback Functionsvoid main (int argc, char **argv)
{
……
glutDisplayFunc ( display ); // display callback
glutReshapeFunc ( resize ); // window resize callback
glutKeyboardFunc ( key ); // keyboard callback
……
}
11
![Page 12: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/12.jpg)
(side note) GLUT Callback Functions• Contents of window need to be refreshed
glutDisplayFunc() • Window is resized or moved
glutReshapeFunc()• Key action
glutKeyboardFunc()• Mouse button action
glutMouseFunc()• Mouse moves while a button is pressed
glutMotionFunc()• Mouse moves regardless of mouse button state glutPassiveMouseFunc()• Called when nothing else is going on
glutIdleFunc()
12
![Page 13: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/13.jpg)
3.1 Rendering Callbackvoid display( void )
{
int k;
glClear(GL_COLOR_BUFFER_BIT);
for( k=0; k<5000; k++)
……
}
13
![Page 14: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/14.jpg)
3.2 Window Resize Callbackvoid resize(int w, int h)
{
……
display();
}
14
![Page 15: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/15.jpg)
3.3 Keyboard Input Callbackvoid key( char mkey, int x, int y ){ switch( mkey ) { case ‘q’ : exit( EXIT_SUCCESS ); break; …… }}
15
![Page 16: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/16.jpg)
4. Event Process Loop This is where your application receives
events, and schedules when callback functions are calledvoid main (int argc, char **argv)
{
……
glutMainLoop();
}
16
![Page 17: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/17.jpg)
2D Geometric Primitives Primitives – fundamental entities such
as point and polygons Basic types of geometric primitives
Points Line segments Polygons
17
![Page 18: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/18.jpg)
GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP
GL_POLYGON GL_QUADS GL_TRIANGLES GL_TRIANGLE_FAN
All geometric primitives are specified by vertices
2D Geometric Primitives
18
![Page 19: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/19.jpg)
glBegin( type ); glVertex*(…); …… glVertex*(…);glEnd();
type determines how vertices are combined
Specifying Geometric Primitives
19
![Page 20: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/20.jpg)
Examplevoid drawSquare (GLfloat *color){
glColor3fv( color ); // sets the color of the square glBegin(GL_POLYGON); glVertex2f ( 0.0, 0.0 ); glVertex2f ( 1.0, 0.0 ); glVertex2f ( 1.1, 1.1 ); glVertex2f ( 0.0, 1.0 ); glEnd(); glFlush() // force the renderer to output the results }
20
![Page 21: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/21.jpg)
21
2D Viewing• Where do we draw the 2D object?
– Assume that we draw objects on an infinite sheet of paper
– Then we need to specify clipping region and project these 2D objects to the screen
void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);
![Page 22: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/22.jpg)
22
Coordinate Systems and Transformation• Identify which matrix we wish to alter• Set the matrix to an identity matrix• Alter the matrix to form the desired matrix
glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
![Page 23: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/23.jpg)
23
Coordinate Systems and TransformationglMatrixMode(GLenum mode);
– Identify which matrix we wish to alter – The mode is usually GL_MODELVIEW, GL_PROJECTION
glLoadIdentity();– Set the current matrix to an identity matrix
![Page 24: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/24.jpg)
24
Enabling GL Features• Features – lighting, hidden-surface
removal, texture mapping, etc…• Each feature will slow down the
rendering process.• We can enable/disable each feature
individuallyvoid glEnable(GLenum feature)void glDisable(GLenum feature) E.g. glEnable(GL_LIGHTING);
![Page 25: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/25.jpg)
25
Enabling GL FeaturesGL_ALPHA_TEST
If enabled, do alpha testing. GL_AUTO_NORMAL
If enabled, generate normal vectors when either GL_MAP2_VERTEX_3 or GL_MAP2_VERTEX_4 is used to generate vertices. See glMap2.
GL_BLEND If enabled, blend the incoming RGBA color values with the values in the color buffers..
GL_CLIP_PLANE i If enabled, clip geometry against user-defined clipping plane i..
GL_COLOR_LOGIC_OP If enabled, apply the currently selected logical operation to the incoming RGBA color and color buffer values.
GL_COLOR_MATERIAL If enabled, have one or more material parameters track the current color.
GL_COLOR_TABLE If enabled, preform a color table lookup on the incoming RGBA color values.
GL_CONVOLUTION_1D If enabled, perform a 1D convolution operation on incoming RGBA color values.
GL_CONVOLUTION_2D If enabled, perform a 2D convolution operation on incoming RGBA color values.
……
![Page 26: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/26.jpg)
26
Saving the State• State changing functions – overwrites the
state variables• We can store previous state values for later
use– Matrix stacks
void glPushMatrix()void glPopMatrix()
– Attribute stacksvoid glPishAttrib(GLbitfield mask)void glPopAttrib()
![Page 27: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/27.jpg)
27
Saving the StateglMatrix(GL_PROJECTION)// set projection matrix// draw sceneglPushMatrix();// change the project matrix//draw sceneglPopMatrix();
![Page 28: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/28.jpg)
28
Double Buffering• Use two buffers: front buffer and back buffer
to guarantee the displaying of a fully redrawn butter image
glutSwapBuffers();– Replace glFlush() by glutSwapBuffer() in
the display callback if using double buffering
![Page 29: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/29.jpg)
29
Transformations and Camera Analogy• Modeling transformation
– Positioning and moving the model.• Viewing transformation
– Positioning and aiming camera in the world.• Projection transformation
– Adjusting the lens of the camera.• Viewport transformation
– Enlarging or reducing the physical photograph.
![Page 30: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/30.jpg)
30
Transformations in OpenGL
• Transformations are specified by matrix operations. Desired transformation can be obtained by a sequence of simple transformations that can be concatenated together.
• Transformation matrix is usually represented by 4x4 matrix (homogeneous coordinates).
• Provides matrix stacks for each type of supported matrix to store matrices.
![Page 31: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/31.jpg)
31
Specifying Operations (1)• Translation
glTranslate {fd} (TYPE x, TYPE y, TYPE z)
Multiplies the current matrix by a matrix that translates an object by the given x, y, z.
![Page 32: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/32.jpg)
32
Specifying Operations (2)• Scaling
glScale {fd} (TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a
matrix that scales an object by the given x, y, z.
![Page 33: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/33.jpg)
33
Specifying Operations (3)• Rotation
glRotate {fd} (TYPE angle, TYPE x, TYPE y,
TYPE z) Multiplies the current matrix by a matrix
that rotates an object in a counterclockwise direction about the ray from origin through the point by the given x, y, z. The angle parameter specifies the angle of rotation in degrees.
![Page 34: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/34.jpg)
34
Order of Transformations• The transformation matrices appear in
reverse order to that in which the transformations are applied.
• In OpenGL, the transformation specified most recently is the one applied first.
![Page 35: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/35.jpg)
35
Viewing-Modeling Transformation
• If given an object, and we want to render it from a viewpoint, what information do we have to have?
– Viewing position– Which way I am looking at– Which way is “up”…..
![Page 36: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/36.jpg)
36
Default Viewing
+X
+Z
+YBy default, the camera is at the origin, looking down the negative z-axis
![Page 37: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/37.jpg)
37
Where are we and what are we looking at?
x
y
z
x
y
zEyepoint
(eyex, eyey, eyez)
Model
View-up vector
(upx, upy, upz)
Loot at
(atx, aty, atz)
![Page 38: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/38.jpg)
38
Viewing in OpenGL• Look-At Function
gluLookAt (eyex, eyey, eyez, atx, aty, atz, upx, upy, upz )
Defines a viewing matrix and multiplies the current matrix by it. Alters the ModelView matrix.
![Page 39: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/39.jpg)
39
Projection Transformation• Projection & Viewing Volume• Projection Transformation• Viewpoint Transformation
![Page 40: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/40.jpg)
40
Perspective Projection Volume
Far-plane: zFarNear-plane: zNear
Viewing volume
hw
aspect ratio = w/hy
z
x
fovy
![Page 41: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/41.jpg)
41
Perspective Projection CommandsglFrustum( left, right, bottom, top, zNear, zFar )
Creates a matrix for a perspective viewing frustum and multiplies the current matrix by it. Alters the Projection matrix.
![Page 42: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/42.jpg)
42
gluPerspective( fovy, aspect, zNear, zFar )
Alternative to glFrustum(..). Creates a matrix for an perspective viewing frustum and multiplies the current matrix by it. Alters the Projection matrix.
Note: fovy is the field of view (fov) angle between the top and bottom planes of the clipping volume. aspect is the aspect ratio
Perspective Projection Commands
![Page 43: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/43.jpg)
43
Viewport• Viewport
– The region within the window that will be used for drawing the clipping area
– By default, it is set to the entire rectangle of the window that is opened
– Measured in the window coordinates, which reflect the position of pixels on the screen related to the lower-left corner of the window
![Page 44: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/44.jpg)
44
Viewport Transformation
A viewpoint is defined as the same size as the window
A viewpoint is defined as half the size of the window
h
w h
w
![Page 45: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/45.jpg)
45
Viewport Commands• glViewport( x, y, width, height )
Defines an area of the window into which the final image is mapped
(x, y) specifies the lower-left corner of the viewport
(width, height) specifies the size of the viewport rectangle
![Page 46: OpenGL](https://reader034.vdocument.in/reader034/viewer/2022051821/56816731550346895ddbdcf2/html5/thumbnails/46.jpg)
Advanced Topics Lighting Texture Mapping Plotting Implicit Functions Shadows Fog Picking (object selection) GUI (glut pop-up menus, glui library)
46
Reference links given on slide 3