gl 01 - cs.brynmawr.edu
TRANSCRIPT
![Page 1: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/1.jpg)
CS312
OpenGL basics
![Page 2: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/2.jpg)
What is openGL?
A low-level graphics library specification.
A small set of geometric primitives: Points Lines Polygons Images Bitmaps
Geometric primitives
Image primitives
![Page 3: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/3.jpg)
OpenGL Libraries
OpenGL core library OpenGL32 on Windows GL/Mesa on most unix/linux systems
OpenGL Utility Library (GLU) Provides functionality in OpenGL core but avoids having to rewrite code
![Page 4: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/4.jpg)
GL is window system independent
Extra libraries are needed to connect GL to the OS GLX – X windows, Unix AGL – Apple Macintosh WGL – Microsoft Windows
![Page 5: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/5.jpg)
GLUT
OpenGL Utility Toolkit (GLUT) Provides functionality common to all
window systems Open a window Get input from mouse and keyboard Menus Event-driven
Code is portable but GLUT is minimal
![Page 6: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/6.jpg)
Software Organization
GLUT
GLU
GL
GLX, AGL or WGL
X, Win32, Mac O/S
software and/or hardware
application program
OpenGL Motif widget or similar
![Page 7: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/7.jpg)
OpenGL Architecture Immediate Mode
Display List
Polynomial Evaluator
Per Vertex Operations &
Primitive Assembly
Rasterization Per Fragment Operations
Texture Memory
CPU
Pixel Operations
Frame Buffer
Geometric pipeline
![Page 8: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/8.jpg)
OpenGL State
OpenGL is a state machine OpenGL functions are of two types
Primitive generating Can cause output if primitive is visible How vertices are processed and appearance
of primitive are controlled by the state State changing
Transformation functions Attribute functions
![Page 9: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/9.jpg)
Typical GL Program Structure
Configure and open a window Initialize GL state Register callback functions
Render Resize Events
Enter infinite event processing loop
![Page 10: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/10.jpg)
Render/Display Draw simple geometric primitives Change states (how GL draws these
primitives) How they are lit or colored How they are mapped from the user's two- or
three-dimensional model space to the two-dimensional screen.
There are also calls to effect direct control of the frame buffer, such as reading and writing pixels.
![Page 11: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/11.jpg)
Header files
#include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h>
![Page 12: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/12.jpg)
Enumerated Types C pref
openGL type C type Data type
b GLbyte signed char 8-bit int s GLshort short 16-bit int i GLint, GLsizei int or long 32-bit int f GLfloat, GLclampf float 32-bit float d GLdouble, GLclampd double 64-bit float ub GLubyte, GLboolean unsigned char 8-bit unsigned int us GLushort unsigned short 16-bit unsigned
int ui GLuint, GLenum,
GLbitfield unsigned int or unsigned long
32-bit unsigned int
![Page 13: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/13.jpg)
OpenGL Function Naming Conventions
Library prefix
Root command
Number of components
Data type Vector b – byte
ub – unsigned byte
s – short
us – unsigned short
i – int
f – float
d - double
glVertex3fv(v)
gl
glu
glut
![Page 14: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/14.jpg)
glVertex*
Capitalizes first letter of each word glVertex{234}{sifd}[v](TYPE coords);
glVertex2i(1, 2); glVertex3f(1.5, -2.0, M_PI); double v[3] = {0.0, 1.5, 3.6};
glVertex3dv(v); Must appear btw glBegin and glEnd
![Page 15: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/15.jpg)
A Simple Program
Generate a square on a solid background
![Page 16: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/16.jpg)
Simple program int main(int argc, char** argv){ glutCreateWindow("simple"); glutDisplayFunc(display); glutMainLoop();
return 0; }
![Page 17: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/17.jpg)
display() void display(){ 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();
}
![Page 18: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/18.jpg)
Event Loop The program defines a display
callback function named display 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
![Page 19: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/19.jpg)
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
![Page 20: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/20.jpg)
Notes on compilation
Unix/linux Include files usually in /usr/include/ Compile with –lglut –lGLU –lGL
loader flags May have to add –l flag for X libraries Mesa implementation included with most
linux distributions
![Page 21: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/21.jpg)
simple.c revisited
In this version, we will see the same output but we have defined relevant state values through function calls with the default values
In particular, we set Colors Window properties
![Page 22: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/22.jpg)
main.c
int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow(argv[0]); glutDisplayFunc(display);
init();
glutMainLoop(); }
define window properties
set OpenGL state
enter event loop
display callback
![Page 23: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/23.jpg)
GLUT functions glutInit allows application to get command line
arguments and initializes system gluInitDisplayMode requests properties for 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 glutDisplayFunc display callback glutMainLoop enter infinite event loop
![Page 24: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/24.jpg)
init.c
void init() { glClearColor (0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0); }
black clear color
draw with white
![Page 25: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/25.jpg)
Specifiying geometric primitives
Each geometric object is described by: A set of vertices Type of the primitive
![Page 26: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/26.jpg)
Specifying geometric primitives glBegin( )
glEnd()
GL_POINTS GL_LINES GL_TRIANGLES GL_QUADS GL_POLYGON
GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUAD_STRIP
![Page 27: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/27.jpg)
OpenGL Primitives
![Page 28: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/28.jpg)
GL geometric primitives
![Page 29: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/29.jpg)
Geometric primitives: examples
glBegin(GL_LINES); [lots of glVertex calls];
glEnd();
glBegin(GL_QUADS); [lots of
glVertex calls];
glEnd();
![Page 30: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/30.jpg)
Polygon Issues OpenGL only correctly displays polygons 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
Triangles satisfy all conditions
nonsimple polygon nonconvex polygon
![Page 31: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/31.jpg)
Attributes Attributes are part of the OpenGL state
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
![Page 32: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/32.jpg)
RGB color Each color component is stored separately in the frame buffer
Usually 8 bits per component in buffer In glColor3f the color values range from 0.0 (none) to 1.0 (all)
![Page 33: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/33.jpg)
RGB: glColor*
glColor3f(0.0, 0.0, 0.0) – black glColor3f(1.0, 0.0, 0.0) – red glColor3f(0.0, 1.0, 0.0) – green glColor3f(0.0, 0.0, 1.0) – blue glColor3f(1.0, 1.0, 0.0) – yellow glColor3f(1.0, 0.0, 1.0) – magenta glColor3f(0.0, 1.0, 1.0) – cyan glColor3f(1.0, 1.0, 1.0) – white
![Page 34: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/34.jpg)
Indexed Color Colors are indices into tables of RGB
values Requires less memory
indices usually 8 bits
![Page 35: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/35.jpg)
GL color models
glutInitDisplayMode() GLUT_RGBA == GLUT_RGB GLUT_INDEX
glClearColor(1.0, 1.0, 1.0, 0.0);
Alpha value – controls transparency Set to 0.0 for now
![Page 36: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/36.jpg)
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
![Page 37: gl 01 - cs.brynmawr.edu](https://reader031.vdocuments.us/reader031/viewer/2022012509/618655bbbb520779a635da67/html5/thumbnails/37.jpg)
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