程序代写代做代考 OpenGL Basics

OpenGL Basics
Lecture: 2
Fall 2016
Computer Graphics (CS3388) Department of Computer Science
University of Western Ontario

OpenGL
Outline
Basics, functionalities, data types Input-Output
Projection
Viewing
Transformation
Camera metaphor
(materials from Stu Pomerantz (Pittsburgh), “OpenGL Red book”, Torsten M ̈oller (SFU), Zahid Hossain (Stanford))
1

What is OpenGL?
A low level software interface to graphics hardware. You can access graphics hardware directly Independent to window system, cross platform
High level enough, so it’s independent to graphics hardware Industry standard
2

What is OpenGL?
Hardware and Operating System independent. No commands for performing windowing tasks No commands for obtaining input
A State Machine
Put OpenGL into a state (or mode) & that state remains in effect until
you change it
Each OpenGL window has its own separate state
3

What is OpenGL?
An open standard
The OpenGL language is not controlled by a single company but rather
steered by the Architecture Review Board (ARB).
Extensible
OpenGL language has an extension mechanism defined in it. Venders can expose non-standard OpenGL features this way
4

What is OpenGL?
Is often called as API Window based programming Event driven programming
callback function
event loop
register callback function
5

OpenGL pipeline (simplified)
OpenGL API calls → OpenGL command buffer → Transform & lighting → Rasterization → Frame buffer
6

What OpenGL provides?
Draw with points, lines, and polygons Matrix Operations (Transformations) Hidden Surface Removal (Z-Buffer) Lighting (Phong model)
Shading
Texture mapping
7

Libraries
Basic GL
Fundamental OpenGL library
OpenGL32 on Windows
GL on most UNIX/Linux systems
GLUT
GL utility toolkit
Open a window, get input, etc.
GLU
GL utility library
Provides high level routines
8

OpenGL data types
9

OpenGL primitives
10

OpenGL primitives
11

OpenGL function format
12

OpenGL syntax
All OpenGL are prefixed by the letters gl Defined constants begin with GL Commands may be postfixed by
A number {2, 3, 4} indicating the number of arguments (e.g. glVertex2i())
A letter {i , f , d , ub} indicating the data type of the arguments (e.g. glRectf())
A letter v indicating the argument is a vector (array) (e.g. glVertex3fv())
Most constants are defined in the include files gl.h, glu.h and glut.h # include should automatically include the others
13

Some syntax examples
Example: Setting the current color using glColor:
Colors may have 3 components: RGB, or 4 components: RGBA.
(Think of A as opacity)
Floating point – color component values range from 0 to 1
glColor3f(0.0,0.5,1.0);
(0% Red, 50% Green, 100% Blue)
glColor4f(0.0,0.5,1.0,0.3);
(0% Red, 50% Green, 100% Blue, 30% opacity)
GLfloat color[4] = {0.0,0.5,1.0,0.3}; glColor4fv(color);
(0% Red, 50% Green, 100% Blue, 30% opacity)
14

Some syntax examples
Unsigned byte: color component values range from 0 to 255: glColor3ub(0,127,255);
(0% Red, 50% Green, 100% Blue)
glColor4f(0,127,255,76);
(0% Red, 50% Green, 100% Blue, 30% opacity)
GLubyte color[4] = {0,127,255,76}; glColor4ubv(color);
(0% Red, 50% Green, 100% Blue, 30% opacity)
15

OpenGL clear colour
The background is specified using glClearColor(); Set blue colour: glClearColor(0.0,0.0,1.0,1.0); Notice that the colours are always floating point
Reset the colour buffer: glClear(GL COLOR BUFFER BIT); at the top of display() function
16

Getting started
17

Inputs
Keyboard input: glutKeyboardFunc(keyboard) Ex: keyboard(unsigned char key, int x, int y);
Mouse input: glutMouseFunc(mouse)
Ex: mouse(int button, int state, int mousex, int mousey);
18

OpenGL geometry
The fundamental primitive is the point. Points are also called vertices, a single point is a vertex
Points are used to build more complex geometry There are many ways to specify points:
2D: glVertex2f(1.0, 2.0); 2D: glVertex2i(4, -1);
3D: glVertex3f(10, 20, 30);
glVertex*() must be within glBegin(constant) and glEnd() commands
19

OpenGL geometry examples
glBegin(GL POINTS); glVertex2i(0,0); glVertex2i(1,0) ; glVertex2i(1,1) ; glVertex2i(0,1) ; glEnd() ;
This code draws 4 points
20

OpenGL geometry examples
glBegin(GL LINES); glVertex2i(0,0); glVertex2i(1,0) ; glVertex2i(1,1) ; glVertex2i(0,1) ; glEnd() ;
This code draws 2 lines: The first one from (0,0) to (1,0) and the second one from (1,1) to (0,1)
21

OpenGL geometry examples
glBegin(GL LINE LOOP); glVertex2i(0,0); glVertex2i(1,0) ; glVertex2i(1,1) ; glVertex2i(0,1) ;
glEnd() ;
This code draws a square
22

OpenGL geometry examples
glBegin(GL QUADS); glVertex2i(0,0); glVertex2i(1,0) ; glVertex2i(1,1) ; glVertex2i(0,1) ; glEnd() ;
This code draws a filled square, or quad
23

OpenGL geometry
The order that vertices is important
Each glBegin(constant) expects a certain ordering of vertices in order
to produce the expected result
Polygon (e.g. triangle) vertices are usually specified counter clockwise.
24

OpenGL camera
OpenGL places a camera at the origin in object space pointing in the
negative z direction
25

Orthographic projection
Representing 3D object into 2D
By default orthographic view, points are projected forward along the z axis onto the plane z=0
26

Viewport
Viewport is a rectangular area of the window in which OpenGL drawing takes place.
Syntax: glViewport(lowerx, lowery, width, height);
27

Viewport
Values in pixels (window coordinates) Usually put in glutReshapeFunc();
28

OpenGL pipeline
Transformation: Vertices are rotated & translated into place Projection: The projection of vertices onto the image plane is
calculated
Clipping: Vertices outside the image plane are clipped away
29

OpenGL camera metaphor
Imagine that OpenGL has a (virtual) camera which is taking a picture of the (virtual) world
Must determine the part of the world that the camera will see. Can’t fit the entire world inside the computer
Can’t fit the entire world on the screen
Must specify how the camera sees the world Cameras can have different lenses
30

OpenGL camera metaphor
Must specify the position and orientation of the camera
Must also specify position and orientation of objects in the world
that the camera will be “photographing”
OpenGL projection transformation is concerned with:
determining the part of the world that the camera will see How the camera sees the world
OpenGL modelview transformation is concerned with: The position and orientation of the camera
The position and orientation of objects in the world
31

OpenGL projection transformation
Defines how a point, or vertex, is transformed from world space to the 2D plane of the screen, or screen space.
World space could be an 5×10 sheet of paper in a 2D drawing program, or the solar system in a space flight simulator
World space is the two- or three-dimension space which you are modelling in the computer
World coordinate numbers (vertices) are unitless.
glVertex2i(2,3) purposely does not say whether the 2 is in mm, cm, inch, m, km, miles, or light years
32

OpenGL projection transformation
OpenGL represents the projection transform as a 4×4 matrix (we’ll see details later in the course):
x11 x12 x13 x21 x22 x23 x31 x32 x33
x14  x24  x34 
x41 x42 x43 x44
There is special support for two commonly used transformations
Orthographic Perspective
33

OpenGL Orthographic transformation
An orthographic viewing volume:
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
GLdouble zNear, GLdouble zFar ) ;
34

OpenGL Perspective transformation
An perspective viewing volume:
void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar ) ;
35

OpenGL general transformation
An general viewing volume:
void glFrustum(GLdouble fovy, GLdouble aspect, GLdouble bottom, GLdouble top
GLdouble zNear, GLdouble zFar ) ;
36

OpenGL projection transformation
What happens when glOrtho() gluPerspective() or glFrustum is executed?
A matrix which produces a projection transformation for the requested viewing volume is generated
That matrix is multiplied into the current OpenGL state
37

OpenGL modelview transformation
Recall that the modelview transformation is concerned with: The position and orientation of the camera
The position and orientation of objects in the world
Like the projection transformation, the modelview transformation is a 4×4 matrix
38

OpenGL modelview transformation
But Wait!!!
Why not use two separate transformation matrices, one for the camera (a view transformation) and one for the objects in the world (a model transformation) instead of one composite modelview transformation?
39

OpenGL modelview transformation
Try this thought experiment:
Suppose the camera, or eye, and the object the camera is to view are in the same location, the origin.
In order to view the object either the camera or the object must be moved
40

OpenGL modelview transformation
Moving the camera away from the origin:
Moving the object away from the origin:
41

OpenGL modelview transformation
No Difference !!!
The programmer can think of the modelview matrix as transforming the view or the models that are being viewed
Hence the name modelview
OpenGL’s default state is modelview mode
42

gluLookAt()
43

gluLookAt()
44

gluLookAt()
Example 1: void gluLookAt( 0,0,-5, 0,0,0, 0,1,0 ) ; Camera is at -5 on z-axis
looking at the origin
coincident with the positive y-axis
Example 2: void gluLookAt( 10,0,0, 0,0,0, 0,0,1 ) ; Camera is at 10 on x-axis
looking at the origin
coincident with the positive z-axis
45

Specifying the view
In order to specify a view to OpenGL:
1 Set the viewport
2 Set the projection transformation
3 Set the modelview transformation
In order to do these tasks:
Set the transformation state for the projection mode and the modelview mode back to a default ’no transformation’ state.
Change between the projection and modelview states
46

Specifying the view
glMatrixMode(GL PROJECTION) ;
Tells OpenGL we want to set the projection transformation
glMatrixMode(GL MODELVIEW) ;
Tells OpenGL we want to set the modelview transformation
47

Specifying the view
Return to the default state: glLoadIdentity() ;
1 0 0 0 0 1 0 0 0 0 1 0 0001
Which sets the transformation to the identity matrix
48

Specifying the view
The default projection is
glMatrixMode(GL PROJECTION) ; glLoadIdentity() ;
which is equivalent to:
glMatrixMode(GL PROJECTION) ; glOrtho(-1,1, -1,1, 1,1) ;
A common example:
glMatrixMode(GL PROJECTION) ; glLoadIdentity() ; gluPerspective(45,1,5,100) ;
49

Specifying the view
The default modelview is
glMatrixMode(GL MODELVIEW) ; glLoadIdentity() ;
which is equivalent to:
glMatrixMode(GL MODELVIEW) ; gluLookAt(0,0,0, 0,0,-1, 0,1,0) ;
A common example:
glMatrixMode(GL MODELVIEW) ; glLoadIdentity() ;
gluLookAt(0,0,0, 0,0,-1, 0,1,0) ;
50

Specifying the view
Putting everything together in a reshape callback example:
void reshape(int x, int y){ glViewPort(x,y) ;
glMatrixMode(GL PROJECTION) ; glLoadIdentity() ; gluPerspective(45,1,5,100) ;
glMatrixMode(GL MODELVIEW) ; glLoadIdentity() ; gluLookAt(0,0,10, 0,0,-1, 0,1,0) ; }
51

Modelview Transformations
Transformations in OpenGL are not drawing commands. They are retained as part of the graphics state
Transformation calls in OpenGL multiply a matrix which represents the transformation into the current OpenGL state
This approach could easily get out of hand if there were no way to save and restore the modelview matrix state. Fortunately, there is.
52

Modelview Transformations
Translation: moves the origin of the coordinate system: x′ =x+tx,y′ =y+ty,z′ =z+tz glTranslatef(tx,ty,tz);
Rotation: rotates by angle θ (in degrees) about an axis direction (x,y,z)
glRotatef(theta,x,y,z);
glRotatef(30.0, 0.0, 1.0, 0.0) rotates by 30 degree about y-axis
Scaling: multiply coordinates by scale factor s x′ =sx ∗x,y′ =sy ∗y,z′ =sz ∗z glScalef(sx,sy,sz);
The order of transform is important!
53

Modelview Transformations
glPushMatrix(): saves the modelview state
pushes the current matrix on to a stack, duplicating the current matrix. That is, after a glPushMatrix call, the matrix on top of the stack is identical to the one below it
glPopMatrix(): restores the modelview state
pops the current matrix stack, replacing the current matrix with the one below it on the stack
54

Modelview Transformations
Example:
55

Modelview Transformations
Translation: glTranslatef(GLfloat x, GLfloat y, GLfloat z) (using gluLookAt(0,0,7, 0,0,0, 0,1,0))
56

Modelview Transformations
Rotation: glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) (using gluLookAt(0,0,7, 0,0,0, 0,1,0))
57

Modelview Transformations
Translation followed by Rotation: (using gluLookAt(0,0,7, 0,0,0, 0,1,0))
58

Modelview Transformations
Rotation followed by Translation: (using gluLookAt(0,0,7, 0,0,0, 0,1,0))
59

Modelview Transformations
Translation and Rotation are order dependent!
60