460 likes | 1.13k Views
3D coordinate systems. Y. Y. Z. X. X. Z. Right-Hand Coordinate System. Left-Hand Coordinate System. Direct3D uses this!. OpenGL uses this!. Visualizing in 3D. Y. y=1.0. G. H. Counter- clockwise. C. D. F. E. X. x=1.0. 1.0. B. A. z=1.0. Z. Rendering a Box in OpenGL.
E N D
3D coordinate systems Y Y Z X X Z Right-Hand Coordinate System Left-Hand Coordinate System Direct3D uses this! OpenGL uses this!
Visualizing in 3D Y y=1.0 G H Counter-clockwise C D F E X x=1.0 1.0 B A z=1.0 Z
Rendering a Box in OpenGL • We render the 6 faces as polygons • Polygons are specified as a list of vertices • Vertices are specified in counterclockwise order looking at the surface of the face! H G C D E F A B
OpenGL Polygon Rendering GLdouble size = 1.0; glBegin(GL_POLYGON); // front face glVertex3d(0.0, 0.0, size); glVertex3d(size, 0.0, size); glVertex3d(size, size, size); glVertex3d(0.0, size, size); glEnd();
OpenGL Conventions • C library • All function names start with gl • OpenGL is a retained mode graphics system • It has a state • glBegin(GL_POLYGON) puts us into a polygon rendering state.
OpenGL Types • Basic numeric types • GLdouble = double • GLfloat = float • GLint = int • GLshort = short • Mostly you’ll use GLdouble and GLfloat
Function suffixes • Many functions have alternatives • Alternatives are specified by the suffix • glVertex2d • 2 double parameters • void glVertex2d(GLdouble x, GLdouble y); • glVertex3f • 3 float parameters • void glVertex3f(GLfloat x, GLfloat y, GLfloat z); • glVertex3fv • void glVertex3fv(const GLfloat *v);
All of dem… • glVertex2d, glVertex2f, glVertex2i, glVertex2s, glVertex3d, glVertex3f, glVertex3i, glVertex3s, glVertex4d, glVertex4f, glVertex4i, glVertex4s, glVertex2dv, glVertex2fv, glVertex2iv, glVertex2sv, glVertex3dv, glVertex3fv, glVertex3iv, glVertex3sv, glVertex4dv, glVertex4fv, glVertex4iv, glVertex4sv
Specifying a color (no lighting) • glColor3f(red, green, blue); • Most of the same suffixes apply… GLdouble size = 1.0; glColor3d(1.0, 0.0, 0.0); // red glBegin(GL_POLYGON); // front face glVertex3d(0.0, 0.0, size); glVertex3d(size, 0.0, size); glVertex3d(size, size, size); glVertex3d(size, 0.0, size); glEnd();
Moving to 3D • Camera Configuration • Parameters • Tessellation • Scene Graphs
Structure of Our Programs • OnGLDraw() • Clear the buffers • Set up the camera • Position the camera • Configure OpenGL • Render whatever we are rendering • Flush
Clear the Buffers void CChildView::OnGLDraw(CDC *pDC) { // Clear to black... glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); . . .
Camera Configuration What does it take to describe a camera?
Describing a Camera • Where it is in space • Eye location • Which way it’s pointing • Or what it’s pointing at • Which way is up • Zoom characteristics • Field of view angle
Setting up the Camera Far clipping plane Near clipping plane Field of View (angle) h w Nothing is rendered that is: • Closer than the near clipping plane • Farther than the far clipping plane Aspect ratio = w/h
gluPerspective() • void gluPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear,GLdouble zFar); • Fovy = Field of view angle • Degrees, usually less than 90 • Aspect = Ratio of width/height of window • zNear = distance to near clipping plane • zFar = distance to far clipping plane glu = GL Utility
Important • What do the numbers zFar, zNear represent? • Always select some unit for your application • Inches, Feet, Meters, etc. • In my sample application, I used inches
Using gluPerspective() // // Set up the camera // glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Determine the screen size so // we can determine the aspect ratio int width, height; GetSize(width, height); GLdouble aspectratio = GLdouble(width) / GLdouble(height); // Set the camera parameters gluPerspective(25., // Field of view. aspectratio, // The aspect ratio. 10., // Near clipping 200.); // Far clipping
After this • The camera is: • At the origin • Looking down the -Z axis
Positioning the Camera • gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz); • Eye – Where the camera is located • At – What the camera is looking at • Up – Which direction is up • Can’t be the looking direction
Matrix Modes • GL_PROJECTION • 3D to 2D conversion • Camera parameters • GL_MODELVIEW • Translation, rotation, etc. of Graphical Models • gluLookAt is a rotation and translation of your graphical model • the camera is really at the origin and looking down the z axis.
Using gluLookAt() // Set the camera location glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(50., 50., 50., // eye x,y,z 0., 0., 0., // center x,y,z 0., 1., 0.); // Up direction . . . Render from here on . . .
Some OpenGL Configurations // // Some standard parameters // // Enable depth test glEnable(GL_DEPTH_TEST); // Cull backfacing polygons glCullFace(GL_BACK); glEnable(GL_CULL_FACE);
Example: Showing the coordinate axis… if(m_showaxis) { glColor3d(0., 1., 1.); glBegin(GL_LINES); glVertex3d(0., 0., 0.); glVertex3d(12., 0., 0.); glVertex3d(0., 0., 0.); glVertex3d(0., 12., 0.); glVertex3d(0., 0., 0.); glVertex3d(0., 0., 12.); glEnd(); }
Example: A Cube void CChildView::Cube(GLdouble size) { GLdouble a[] = {0., 0., size}; GLdouble b[] = {size, 0., size}; GLdouble c[] = {size, size, size}; GLdouble d[] = {0., size, size}; GLdouble e[] = {0., 0., 0.}; GLdouble f[] = {size, 0., 0.}; GLdouble g[] = {size, size, 0.}; GLdouble h[] = {0., size, 0.}; glColor3d(0.8, 0., 0.); glBegin(GL_POLYGON); // Front glVertex3dv(a); glVertex3dv(b); glVertex3dv(c); glVertex3dv(d); glEnd(); . . . h g c d e f a b See example program…