250 likes | 396 Views
CSC345: Advanced Graphics & Virtual Environments. Lecture 1: Introduction to OpenGL (1) Patrick Olivier p.l.olivier@ncl.ac.uk 2 nd floor in the Devonshire Building. Course structure. Introduction to OpenGL Visual appearance (fogging & transparency)
E N D
CSC345: Advanced Graphics &Virtual Environments Lecture 1: Introduction to OpenGL (1) Patrick Olivier p.l.olivier@ncl.ac.uk 2nd floor in the Devonshire Building Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Course structure • Introduction to OpenGL • Visual appearance (fogging & transparency) • Discrete Techniques (aliasing & textures) • Advanced lighting & shading (shadows & reflections) • Curves & curved surfaces • Solid object modelling • Visualisation • Procedural modelling • Acceleration algorithms • Intersection and collision detection • Non-photorealistic rendering • Virtual environments Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Required reading (1) Edward Angel: “Interactive Computer Graphics: A Top-Down Approach Using OpenGL” 3rd Edition Addison Wesley, 2002 Required for theory Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Required reading (2) • Put image here Edward Angel: “OpenGL: A Primer” 2nd Edition Addison Wesley, 2004 Compulsory for practicals Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Extra reading… • Put image here Tomas Akeine-Möller & Eric Haines: “Real-time Rendering” 2nd Edition AK Peters, 2002 Advanced topics & for serious graphics types Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Extra reading… • Put image here “OpenGL Programming Guide: The Official Guide to Learning OpenGL” 4th Edition Addison Wesley, 2004 Old version online: see module webpages Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Practical (programming) schedule… • Two-dimensional programming & C (OpenGL primer: ch. 2) • Two-dimensional programming & C (OpenGL primer: ch. 2) • Exercise 1 (worth 3% is due: 13th February) • Interaction & animation (OpenGL primer: ch. 3) • Basic three-dimensional programming (OpenGL primer: ch. 4) • Transformations (OpenGL primer: ch. 5) • Exercise 2 (worth 3% is due: 6th March) • Lights & materials (OpenGL primer: ch. 6) • Images (OpenGL primer: ch. 7) • Texture mapping (OpenGL primer: ch. 8) • Exercise 3 (worth 4% is due: 27th March) • Curves & surfaces (OpenGL primer: ch. 9) • Exercise 4 (worth 15% is due: 12th May) • Exam is worth 80% date to be confirmed Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
OpenGL library • OpenGL core library • OpenGL32 on Windows • GL on most unix/linux systems (libGL.a) • OpenGL Utility Library (GLU) • Provides functionality in OpenGL core but avoids having to rewrite code • Links with window system • GLX for X window systems • WGL for Windows • AGL for Macintosh Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
GLUT library • 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 lacks functionality of a good toolkit for a specific platform • e.g. no slide bars Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
OpenGL architecture Immediate Mode geometry pipeline Per Vertex Operations & Primitive Assembly Polynomial Evaluator DisplayList Per Fragment Operations Frame Buffer CPU Rasterization Texture Memory Pixel Operations Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
OpenGL functions • Primitives • Points • Line Segments • Polygons • Attributes • Transformations • Viewing • Modeling • Control (GLUT) • Input (GLUT) • Query Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
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 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
glVertex3f(x,y,z) x,y,z are floats belongs to GL library function name dimensions glVertex3fv(p) p is a pointer to an array OpenGL function format • OpenGL is not object oriented so that there are multiple functions for a given logical function • glVertex3f • glVertex2i • glVertex3dv • Underlying storage mode is the same • Function format: Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
OpenGL #defines • Most constants are defined in the include files gl.h, glu.h and glut.h • Note #include <GL/glut.h> should automatically include the others • Examples: • glBegin(GL_POLYGON) • glClear(GL_COLOR_BUFFER_BIT) • include files also define OpenGL data types: GLfloat, GLdouble,…. Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
A simple program: simple.c #include <GL/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(); } Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Event loop & defaults • program defines display callback function named mydisplay • every glut program must have a display callback • display callback is executed whenever OpenGL decides the display must be refreshed, (e.g the window is opened) • main function end by entering an event loop • simple.c is too simple • makes heavy use of state variable defaults • viewing / colours / window parameters • next version makes defaults more explicit Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Compilation • Unix/linux • include files usually in …/include/GL • compile with –lglut –lglu –lgl loader flags • may have to add –L flag for X libraries • make file provided on module webpage • Windows (Visual Studio) • get glut.h, glut32.lib and glut32.dll from web • create a console application • add opengl32.lib, glut32.lib, glut32.lib to project settings (under link tab) Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Program structure • most OpenGL programs have same structure: • main() • defines the callback functions • opens one or more windows with the required properties • enters event loop (last executable statement) • init() • sets the state variables (viewing / attributes) • callbacks… • display function • input and window functions • rewrite simple.c that sets colour, view & window… Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
main.c #include <GL/glut.h> int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); init(); glutMainLoop(); } …define window size …define window position …set OpenGL state …enter event loop Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
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 “simple” • glutDisplayFunc display callback • glutMainLoop enter infinite event loop Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
init.c void init() { glClearColor (0.0, 0.0, 0.0, 1.0); /* first 3 values = black, final = opaque */ glColor3f(1.0, 1.0, 1.0); /* set current draw colour to white */ glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); /* bounds of the view volume */ } Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Coordinate systems & cameras • units of glVertex in object coordinates • viewing specifications also in object coordinates • OpenGL converts to camera (eye) coordinates and later to screen coordinates • OpenGL puts camera at the origin pointing in -z direction direction • The default viewing volume is a box centered at the origin of length 2 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
Transformations and viewing • In OpenGL, 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 • alter it with projection matrix that gives the view volume glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
2-D and 3-D viewing • The near & far distances are measured from the camera, in: glOrtho(left,right,bottom,top,near,far) • 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) • In two dimensions, the view or clipping volume becomes a clipping window Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)
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(); } Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)