3d coordinate systems x y z right-hand coordinate system x y z left-hand coordinate system opengl...

25
3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

Upload: arabella-reynolds

Post on 26-Dec-2015

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

3D coordinate systems

X

Y

Z

Right-Hand Coordinate System

X

Y

Z

Left-Hand Coordinate System

OpenGL uses this! Direct3D uses this!

Page 2: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

Visualizing in 3D

X

Y

Z

1.0

z=1.0

x=1.0

y=1.0

A B

CD

E F

GH

Counter-clockwise

Page 3: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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!

A B

CD

E F

GH

Page 4: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 5: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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.

Page 6: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

OpenGL Types

• Basic numeric types– GLdouble = double– GLfloat = float– GLint = int– GLshort = short

• Mostly you’ll use GLdouble and GLfloat

Page 7: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 8: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 9: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 10: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

Moving to 3D

• Camera Configuration

• Parameters

• Tessellation

• Scene Graphs

Page 11: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

Structure of Our Programs

• OnGLDraw()– Clear the buffers– Set up the camera– Position the camera– Configure OpenGL– Render whatever we are rendering– Flush

Page 12: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

. . .

Page 13: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

Camera Configuration

What does it take to describe a camera?

Page 14: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 15: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

Setting up the Camera

Field of View(angle)

Near clipping plane

Far clipping plane

Nothing is rendered that is: Closer than the near clipping plane Farther than the far clipping plane

Aspect ratio = w/h

w

h

Page 16: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 17: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 18: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 19: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

After this

• The camera is:– At the origin– Looking down the -Z axis

Page 20: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 21: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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.

Page 22: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 23: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

Some OpenGL Configurations

// // Some standard parameters //

// Enable depth test glEnable(GL_DEPTH_TEST);

// Cull backfacing polygons glCullFace(GL_BACK); glEnable(GL_CULL_FACE);

Page 24: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

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

Page 25: 3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!

Example: A Cubevoid 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(); . . .

a b

cd

e f

gh

See example program…