csc345: advanced graphics & virtual environments

25
30/1/200 6 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 1 CSC345: Advanced Graphics & Virtual Environments Lecture 1: Introduction to OpenGL (1) Patrick Olivier [email protected] 2 nd floor in the Devonshire Building

Upload: bryga

Post on 12-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

CSC345: Advanced Graphics & Virtual Environments. Lecture 1: Introduction to OpenGL (1) Patrick Olivier [email protected] 2 nd floor in the Devonshire Building. Course structure. Introduction to OpenGL Visual appearance (fogging & transparency) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC345: Advanced Graphics & Virtual Environments

30/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 1

CSC345: Advanced Graphics &

Virtual Environments

Lecture 1: Introduction to OpenGL (1)

Patrick [email protected]

2nd floor in the Devonshire Building

Page 2: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 2

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

Page 3: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 3

Required reading (1)

Edward Angel:“Interactive Computer Graphics: A Top-Down Approach Using OpenGL”3rd EditionAddison Wesley, 2002Required for theory

Page 4: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 4

Required reading (2) Put image

here Edward Angel:“OpenGL: A Primer”2nd EditionAddison Wesley, 2004Compulsory for practicals

Page 5: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 5

Extra reading… Put image

here Tomas Akeine-Möller & Eric Haines:“Real-time Rendering”2nd EditionAK Peters, 2002Advanced topics & for serious graphics types

Page 6: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 6

Extra reading… Put image

here “OpenGL Programming Guide: The Official Guide to Learning OpenGL”4th EditionAddison Wesley, 2004Old version online: see module webpages

Page 7: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 7

Practical (programming) schedule…1. Two-dimensional programming & C (OpenGL primer: ch. 2)2. Two-dimensional programming & C (OpenGL primer: ch. 2)

Exercise 1 (worth 3% is due: 13th February)3. Interaction & animation (OpenGL primer: ch. 3)4. Basic three-dimensional programming (OpenGL primer: ch.

4)5. Transformations (OpenGL primer: ch. 5)

Exercise 2 (worth 3% is due: 6th March)6. Lights & materials (OpenGL primer: ch. 6)7. Images (OpenGL primer: ch. 7)8. Texture mapping (OpenGL primer: ch. 8)

Exercise 3 (worth 4% is due: 27th March)9. Curves & surfaces (OpenGL primer: ch. 9)

Exercise 4 (worth 15% is due: 12th May) Exam is worth 80% date to be confirmed

Page 8: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 8

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

Page 9: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 9

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

Page 10: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 10

OpenGL architectureImmediate Mode

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

Operations

TextureMemory

CPU

PixelOperations

FrameBuffer

geometry pipeline

Page 11: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 11

OpenGL functions Primitives

Points Line Segments Polygons

Attributes Transformations

Viewing Modeling

Control (GLUT) Input (GLUT) Query

Page 12: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 12

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 13: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 13

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:

OpenGL function format

function name

glVertex3f(x,y,z)

belongs to GL library x,y,z are floatsdimensions

glVertex3fv(p)

p is a pointer to an array

Page 14: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 14

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,….

Page 15: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 15

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();

}

Page 16: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 16

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

Page 17: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 17

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)

Page 18: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 18

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…

Page 19: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 19

#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();}

main.c

…define window size…define window position

…set OpenGL state…enter event loop

Page 20: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 20

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

Page 21: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 21

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 */

}

Page 22: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 22

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

Page 23: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 23

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);

Page 24: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 24

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

Page 25: CSC345: Advanced Graphics & Virtual Environments

31/1/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 25

mydisplay.cvoid 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();

}