OpenGL and Projections2

advertisement
OpenGL and Projections
David E. Johnson
Goals
• Quick overview of OpenGL
• Some derivation of projections and
transformations
OpenGL
• OpenGL is nothing more than a set of
functions you call from your program
– Most functions map to GPU hardware
• Hides the details of the display adapter,
operating system, etc.
• Has grown to become a flexible general
purpose compute platform
• As a programmer, you need to
– Specify the location/parameters of camera.
– Specify the geometry (and appearance).
– Specify the lights.
• OpenGL will compute the resulting 2D image!
Basic
Pipeline
Basic
Pipeline
OpenGL Hierarchy
• Several levels of abstraction are provided
• GL
– Lowest level: vertex, matrix manipulation
– glVertex3f(point.x, point.y, point.z)
• GLU
– Helper functions for shapes, transformations
– gluPerspective( fovy, aspect, near, far )
• GLUT
– Highest level: Window and interface management
– glutSwapBuffers()
• All fairly raw – most people have their own point
class which gets transformed to GL arrays as
needed.
OpenGL Implementations
• OpenGL is an API
– #include <GL/gl.h>
– #include <GL/glu.h>
– #include <GL/glut.h>
• Windows, Linux, UNIX, etc. all provide a
platform specific implementation.
• Windows: opengl32.lib glu32.lib glut32.lib
• Linux: -l GL -l GLU –l GLUT
OpenGL Conventions
• OpenGL is largely state based
– Calls persist
– This instead of having functions with 100s of options
– Transformations live on stacks
• Layers of transformations possible
• Work is done on buffers (color, depth,…)
• Many functions have multiple forms:
– glVertex2f, glVertex3i, glVertex4dv, etc.
• Number indicates number of arguments
• Letters indicate type
– f: float, d: double, v: pointer, etc.
• Programs tend to be event based
A simple GLUT program
// A simple OpenGL and glut program
#include <GL/gl.h>
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(512,512);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH );
glutCreateWindow("The glut hello world program");
glutDisplayFunc(display);
glClearColor(0.0, 0.0, 0.0, 1.0);
glutMainLoop(); // Infinite event loop
return 0;
}
OpenGL: Camera
• Two things to specify:
– Physical location of camera in the scene
• Where is the camera?
• Which direction is it pointing?
• What is the orientation of the camera?
– Projection properties of the camera
• Depth of field?
• Field of view in the x and y directions?
The OpenGL
Camera
• Initially the object and camera frames are the same
–Default model-view matrix is an identity
• The camera is located at origin and points in the
negative z direction
• OpenGL also specifies a default view volume that
is a cube with sides of length 2 centered at the
origin
–Default projection matrix is an identity
Moving Camera Back
frames after translation by –d
d>0
default frames
Moving the Camera
• We can move the camera to any desired
position by a sequence of rotations and
translations
• Example: side view
–Rotate the camera
–Move it away from origin
–Model-view matrix C = RT
OpenGL code
• Remember that last transformation
specified is first to be applied
glMatrixMode(GL_MODELVIEW)
glLoadIdentity();
glRotatef(90.0, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, -d);
The LookAt Function
• The GLU library contains the function gluLookAt to form
the required modelview matrix through a simple
interface
• Note the need for setting an up direction
• Still need to initialize
–Can concatenate with modeling transformations
• Example: isometric view of cube aligned with axes
glMatrixMode(GL_MODELVIEW):
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., 1.0. 0.0);
gluLookAt
glLookAt(eyex, eyey, eyez, atx, aty, atz, upx,
upy, upz)
Projections
• See notes
Download