Introduction to Computer Graphics CS 445 / 645 Lecture 4 OpenGL OpenGL OpenGL Programming Guide.

Slides:



Advertisements
Similar presentations
Computer Graphics - Viewing -
Advertisements

March 1, 2009Dr. Muhammed Al-Mulhem1 ICS 415 Computer Graphics OpenGL Dr. Muhammed Al-Mulhem March 1, 2009 Dr. Muhammed Al-Mulhem March 1, 2009.
02/17/05CISC640/440 OpenGL Tutorial1 OpenGL Tutorial CISC 640/440 Computer Graphics TA: Qi Li/Mani Thomas
OpenGL: Simple Use Open a window and attach OpenGL to it Set projection parameters (e.g., field of view) Setup lighting, if any Main rendering loop –Set.
MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Goals of the course: 1. to introduce real-time 3D graphics programming with openGL.
Using GLU/GLUT Objects GLU/GLUT provides very simple object primitives glutWireCube glutWireCone gluCylinder glutWireTeapot.
David Luebke5/16/2015 Administrivia l Back on track: canceling OpenGL lecture 2 l Assignment 1 –Greg Yukl found an alternate XForms site:
Based on slides created by Edward Angel
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
OpenGL and Projections
Objectives Learn to build arbitrary transformation matrices from simple transformations Learn to build arbitrary transformation matrices from simple transformations.
OpenGL (II). How to Draw a 3-D object on Screen?
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
More OpenGL CS 551/645 - Introduction to Computer Graphics.
Computer Graphics, KKU. Lecture 131 Transformation and Viewing in OpenGL.
OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360.
TWO DIMENSIONAL GEOMETRIC TRANSFORMATIONS CA 302 Computer Graphics and Visual Programming Aydın Öztürk
The Viewing Pipeline (Chapter 4) 5/26/ Overview OpenGL viewing pipeline: OpenGL viewing pipeline: – Modelview matrix – Projection matrix Parallel.
Image Synthesis Rabie A. Ramadan, PhD 2. 2 Java OpenGL Using JOGL: Using JOGL: Wiki: You can download JOGL from.
Geometric transformations The Pipeline
UBI 516 Advanced Computer Graphics Introduction to OpenGL OpenGL Programming Guide.
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
CS 445 / 645 Introduction to Computer Graphics Lecture 4 OpenGL Intro Assignment 1 Lecture 4 OpenGL Intro Assignment 1.
Introduction to OpenGL 1. 2 OpenGL A Graphics rendering API introduced in 1992 by Silicon Graphics Inc Provide the low-level functions to access graphics.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Computer Graphics Bing-Yu Chen National Taiwan University.
Computer Graphics I, Fall 2010 Computer Viewing.
Modeling with OpenGL Practice with OpenGL transformations.
Intro to OpenGL Transformations CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
OpenGL Viewing and Modeling Transformation Geb Thomas Adapted from the OpenGL Programming Guidethe OpenGL Programming Guide.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 OpenGL Transformations.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
The Camera Analogy ► Set up your tripod and point the camera at the scene (viewing transformation) ► Arrange the scene to be photographed into the desired.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke
Chap 3 Viewing and Transformation
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Learning Objectives Affine transformations Affine transformations Translation Translation Rotation Rotation Scaling Scaling Reflection Reflection Shear.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
1 Geometric Transformations-II Modelling Transforms By Dr.Ureerat Suksawatchon.
Introduction to Graphics Programming. Graphics API.
CSC Graphics Programming Budditha Hettige Department of Statistics and Computer Science.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL LAB III.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Computer Graphics Lecture 41 Viewing Using OpenGL Taqdees A. Siddiqi
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Administrivia Back on track: canceling OpenGL lecture 2 Assignment 1
Programming with OpenGL Part 2: Complete Programs
OpenGL API 2D Graphic Primitives
Advanced Graphics Algorithms Ying Zhu Georgia State University
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Geometric Transformations
Lecture 08 and 09 View & Projection
Computer Graphics, KKU. Lecture 13
Projection in 3-D Glenn G. Chappell
Introduction to OpenGL
Geometric Transformations
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Advanced Graphics Algorithms Ying Zhu Georgia State University
Geometric Transformations
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Introduction to Computer Graphics CS 445 / 645 Lecture 4 OpenGL OpenGL OpenGL Programming Guide

Announcement Visit discussion forum to keep up to dateVisit discussion forum to keep up to date Red Book is good source for OpenGLRed Book is good source for OpenGLAnnouncement Visit discussion forum to keep up to dateVisit discussion forum to keep up to date Red Book is good source for OpenGLRed Book is good source for OpenGL

Intro to OpenGL OpenGL operates as an infinite loop Put things in the scene (points, colored lines, textured polys)Put things in the scene (points, colored lines, textured polys) Describe the camera (location, orientation, field of view)Describe the camera (location, orientation, field of view) Listen for keyboard eventsListen for keyboard events Render – draw the sceneRender – draw the scene OpenGL operates as an infinite loop Put things in the scene (points, colored lines, textured polys)Put things in the scene (points, colored lines, textured polys) Describe the camera (location, orientation, field of view)Describe the camera (location, orientation, field of view) Listen for keyboard eventsListen for keyboard events Render – draw the sceneRender – draw the scene

Intro to OpenGL OpenGL has a “state” There are a lot of ways your OpenGL program can be configuredThere are a lot of ways your OpenGL program can be configured The current configuration is stored in OpenGL’s stateThe current configuration is stored in OpenGL’s state Be aware that OpenGL commands affect the program’s state rather than redirect its logical executionBe aware that OpenGL commands affect the program’s state rather than redirect its logical execution OpenGL has a “state” There are a lot of ways your OpenGL program can be configuredThere are a lot of ways your OpenGL program can be configured The current configuration is stored in OpenGL’s stateThe current configuration is stored in OpenGL’s state Be aware that OpenGL commands affect the program’s state rather than redirect its logical executionBe aware that OpenGL commands affect the program’s state rather than redirect its logical execution

Intro to OpenGL OpenGL uses matrices Matrix describes camera typeMatrix describes camera type Matrix describes current configuration of the 3D spaceMatrix describes current configuration of the 3D space –Explanation… OpenGL uses matrices Matrix describes camera typeMatrix describes camera type Matrix describes current configuration of the 3D spaceMatrix describes current configuration of the 3D space –Explanation…

Intro to OpenGL OpenGL coordinate system right-handedright-handed –Hold out your right hand and hold your thumb, index, and middle fingers orthogonal to one another –Call your thumb the x-axis, index = y-axis, and middle = axis –This is the OpenGL coordinate system The camera defaults to look down negative z-axisThe camera defaults to look down negative z-axis OpenGL coordinate system right-handedright-handed –Hold out your right hand and hold your thumb, index, and middle fingers orthogonal to one another –Call your thumb the x-axis, index = y-axis, and middle = axis –This is the OpenGL coordinate system The camera defaults to look down negative z-axisThe camera defaults to look down negative z-axis

Intro to OpenGL So… X-axis = thumb = 1, 0, 0X-axis = thumb = 1, 0, 0 Y-axis = index = 0, 1, 0Y-axis = index = 0, 1, 0 Z-axis = middle = 0, 0, 1Z-axis = middle = 0, 0, 1 Camera defaults to look down negative z-axisCamera defaults to look down negative z-axis Let’s say we want it to look down x-axisLet’s say we want it to look down x-axisSo… X-axis = thumb = 1, 0, 0X-axis = thumb = 1, 0, 0 Y-axis = index = 0, 1, 0Y-axis = index = 0, 1, 0 Z-axis = middle = 0, 0, 1Z-axis = middle = 0, 0, 1 Camera defaults to look down negative z-axisCamera defaults to look down negative z-axis Let’s say we want it to look down x-axisLet’s say we want it to look down x-axis

Intro to OpenGL Coordinate system transformation so camera looks down x-axis If x-axis  negative z-axisIf x-axis  negative z-axis –x  -z –y  y –z  x Coordinate system transformation so camera looks down x-axis If x-axis  negative z-axisIf x-axis  negative z-axis –x  -z –y  y –z  x

Intro to OpenGL The a  i matrix defines the transformation Why store the transformation matrix and not the final desired matrix? The a  i matrix defines the transformation Why store the transformation matrix and not the final desired matrix? =

Intro to OpenGL The transformation will be applied to many points If the following transformation moves the axesIf the following transformation moves the axes The same transformation moves verticesThe same transformation moves vertices –Example: (1, 1, -1)  (-1, 1, -1) The transformation will be applied to many points If the following transformation moves the axesIf the following transformation moves the axes The same transformation moves verticesThe same transformation moves vertices –Example: (1, 1, -1)  (-1, 1, -1) = 

Intro to OpenGL This important matrix is stored as the MODELVIEW matrix The MODELVIEW matrix is so important OpenGL maintains a stack of these matricesThe MODELVIEW matrix is so important OpenGL maintains a stack of these matrices You have control of this stack with the glPushMatrix and glPopMatrix commandsYou have control of this stack with the glPushMatrix and glPopMatrix commands (The matrix is actually 4x4, but we will study the details to understand why in the future)(The matrix is actually 4x4, but we will study the details to understand why in the future) Note OpenGL preserves a similar matrix to describe the camera type and this is called the PROJECTION_MATRIX This important matrix is stored as the MODELVIEW matrix The MODELVIEW matrix is so important OpenGL maintains a stack of these matricesThe MODELVIEW matrix is so important OpenGL maintains a stack of these matrices You have control of this stack with the glPushMatrix and glPopMatrix commandsYou have control of this stack with the glPushMatrix and glPopMatrix commands (The matrix is actually 4x4, but we will study the details to understand why in the future)(The matrix is actually 4x4, but we will study the details to understand why in the future) Note OpenGL preserves a similar matrix to describe the camera type and this is called the PROJECTION_MATRIX

Introduction to OpenGL What’s hard about this assignment? Managing the MODELVIEW stackManaging the MODELVIEW stack We’ll use:We’ll use: –glLoadIdentity(); –glTranslatef(GLfloat x, y, z); –glRotatef(GLfloat degrees, x, y, z); –glScalef (GLfloat x, y, z); –glPushMatrix(), glPopMatrix(); What’s hard about this assignment? Managing the MODELVIEW stackManaging the MODELVIEW stack We’ll use:We’ll use: –glLoadIdentity(); –glTranslatef(GLfloat x, y, z); –glRotatef(GLfloat degrees, x, y, z); –glScalef (GLfloat x, y, z); –glPushMatrix(), glPopMatrix();

Modeling Transformations glTranslate (x, y, z) Post-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-valuesPost-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-values glRotate (theta, x, y, z) Post-multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z)Post-multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z) glTranslate (x, y, z) Post-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-valuesPost-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-values glRotate (theta, x, y, z) Post-multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z)Post-multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z)

Modeling Transformations glScale (x, y, z) Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axesPost-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes glScale (x, y, z) Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axesPost-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes

Modeling Transformations It is important that you understand the order in which OpenGL concatenates matrices glMatrixMode (MODELVIEW); glLoadIdentity();glMultMatrix(N);glMultMatrix(M);glMultMatrix(L);glBegin(POINTS);glVertex3f(v);glEnd(); glLoadIdentity();glMultMatrix(N);glMultMatrix(M);glMultMatrix(L);glBegin(POINTS);glVertex3f(v);glEnd(); Modelview matrix successively contains: I(dentity), N, NM, NML The transformed vertex is: NMLv = N(M(Lv))

Manipulating Matrix Stacks Observation: Certain model transformations are shared among many models We want to avoid continuously reloading the same sequence of transformations glPushMatrix ( ) push all matrices in current stack down one level and copy topmost matrix of stackpush all matrices in current stack down one level and copy topmost matrix of stack glPopMatrix ( ) pop the top matrix off the stackpop the top matrix off the stack Observation: Certain model transformations are shared among many models We want to avoid continuously reloading the same sequence of transformations glPushMatrix ( ) push all matrices in current stack down one level and copy topmost matrix of stackpush all matrices in current stack down one level and copy topmost matrix of stack glPopMatrix ( ) pop the top matrix off the stackpop the top matrix off the stack

Matrix Manipulation - Example Drawing a car with wheels and lugnuts draw_wheel( ); for (j=0; j<5; j++) { glPushMatrix (); glRotatef(72.0*j, 0.0, 0.0, 1.0); glTranslatef (3.0, 0.0, 0.0); draw_bolt ( ); glPopMatrix ( ); }

Matrix Manipulation – Example Grand, fixed coordinate system draw_wheel( ); for (j=0; j<5; j++) { glPushMatrix (); glRotatef(72.0*j, 0.0, 0.0, 1.0); glTranslatef (3.0, 0.0, 0.0); draw_bolt ( ); glPopMatrix ( ); Global – Bottom Up Start Rot Trans R RT RTv

Matrix Manipulation – Example Local coordinate system draw_wheel( ); for (j=0; j<5; j++) { glPushMatrix (); glRotatef(72.0*j, 0.0, 0.0, 1.0); glTranslatef (3.0, 0.0, 0.0); draw_bolt ( ); glPopMatrix ( ); Local – Top Down Start Rot Trans R RT RTv

OpenGL: Conventions Functions in OpenGL start with gl Most functions just gl (e.g., glColor() )Most functions just gl (e.g., glColor() ) Functions starting with glu are utility functions (e.g., gluLookAt() )Functions starting with glu are utility functions (e.g., gluLookAt() ) Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c) Functions in OpenGL start with gl Most functions just gl (e.g., glColor() )Most functions just gl (e.g., glColor() ) Functions starting with glu are utility functions (e.g., gluLookAt() )Functions starting with glu are utility functions (e.g., gluLookAt() ) Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)

OpenGL: Conventions Function names indicate argument type and number Functions ending with f take floatsFunctions ending with f take floats Functions ending with i take intsFunctions ending with i take ints Functions ending with b take bytesFunctions ending with b take bytes Functions ending with ub take unsigned bytesFunctions ending with ub take unsigned bytes Functions that end with v take an array.Functions that end with v take an array.Examples glColor3f() takes 3 floatsglColor3f() takes 3 floats glColor4fv() takes an array of 4 floatsglColor4fv() takes an array of 4 floats Function names indicate argument type and number Functions ending with f take floatsFunctions ending with f take floats Functions ending with i take intsFunctions ending with i take ints Functions ending with b take bytesFunctions ending with b take bytes Functions ending with ub take unsigned bytesFunctions ending with ub take unsigned bytes Functions that end with v take an array.Functions that end with v take an array.Examples glColor3f() takes 3 floatsglColor3f() takes 3 floats glColor4fv() takes an array of 4 floatsglColor4fv() takes an array of 4 floats

OpenGL: Conventions Variables written in CAPITAL letters Example: GLUT_SINGLE, GLUT_RGBExample: GLUT_SINGLE, GLUT_RGB usually constantsusually constants use the bitwise or command (x | y) to combine constantsuse the bitwise or command (x | y) to combine constants Variables written in CAPITAL letters Example: GLUT_SINGLE, GLUT_RGBExample: GLUT_SINGLE, GLUT_RGB usually constantsusually constants use the bitwise or command (x | y) to combine constantsuse the bitwise or command (x | y) to combine constants

OpenGL: Simple Use Open a window and attach OpenGL to it Set projection parameters (e.g., field of view) Setup lighting, if any Main rendering loop Set camera pose with gluLookAt()Set camera pose with gluLookAt() –Camera position specified in world coordinates Render polygons of modelRender polygons of model –Simplest case: vertices of polygons in world coordinates Open a window and attach OpenGL to it Set projection parameters (e.g., field of view) Setup lighting, if any Main rendering loop Set camera pose with gluLookAt()Set camera pose with gluLookAt() –Camera position specified in world coordinates Render polygons of modelRender polygons of model –Simplest case: vertices of polygons in world coordinates

OpenGL: Simple Use Open a window and attach OpenGL to it glutCreateWindow() or FLTK window methodglutCreateWindow() or FLTK window method Open a window and attach OpenGL to it glutCreateWindow() or FLTK window methodglutCreateWindow() or FLTK window method

OpenGL: Perspective Projection Set projection parameters (e.g., field of view) Typically, we use a perspective projection Distant objects appear smaller than near objectsDistant objects appear smaller than near objects Vanishing point at center of screenVanishing point at center of screen Defined by a view frustum (draw it)Defined by a view frustum (draw it) Other projections: orthographic, isometric Set projection parameters (e.g., field of view) Typically, we use a perspective projection Distant objects appear smaller than near objectsDistant objects appear smaller than near objects Vanishing point at center of screenVanishing point at center of screen Defined by a view frustum (draw it)Defined by a view frustum (draw it) Other projections: orthographic, isometric

Setting up Camera glMatrixMode(GL_MODELVIEW);glLoadIdentity(); gluLookAt(eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); eye[XYZ]: camera position in world coordinateseye[XYZ]: camera position in world coordinates look[XYZ]: a point centered in camera’s viewlook[XYZ]: a point centered in camera’s view up[XYZ]: a vector defining the camera’s verticalup[XYZ]: a vector defining the camera’s vertical Creates a matrix that transforms points in world coordinates to camera coordinates Camera at originCamera at origin Looking down -Z axisLooking down -Z axis Up vector aligned with Y axisUp vector aligned with Y axisglMatrixMode(GL_MODELVIEW);glLoadIdentity(); gluLookAt(eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); eye[XYZ]: camera position in world coordinateseye[XYZ]: camera position in world coordinates look[XYZ]: a point centered in camera’s viewlook[XYZ]: a point centered in camera’s view up[XYZ]: a vector defining the camera’s verticalup[XYZ]: a vector defining the camera’s vertical Creates a matrix that transforms points in world coordinates to camera coordinates Camera at originCamera at origin Looking down -Z axisLooking down -Z axis Up vector aligned with Y axisUp vector aligned with Y axis

OpenGL: Perspective Projection In OpenGL: Projections implemented by projection matrixProjections implemented by projection matrix gluPerspective() creates a perspective projection matrix:gluPerspective() creates a perspective projection matrix:glSetMatrix(GL_PROJECTION); glLoadIdentity(); //load an identity matrix gluPerspective(vfov, aspect, near, far); Parameters to gluPerspective() : vfov : vertical field of viewvfov : vertical field of view aspect : window width/heightaspect : window width/height near, far : distance to near & far clipping planesnear, far : distance to near & far clipping planes In OpenGL: Projections implemented by projection matrixProjections implemented by projection matrix gluPerspective() creates a perspective projection matrix:gluPerspective() creates a perspective projection matrix:glSetMatrix(GL_PROJECTION); glLoadIdentity(); //load an identity matrix gluPerspective(vfov, aspect, near, far); Parameters to gluPerspective() : vfov : vertical field of viewvfov : vertical field of view aspect : window width/heightaspect : window width/height near, far : distance to near & far clipping planesnear, far : distance to near & far clipping planes

OpenGL: Lighting Setup lighting, if any Simplest option: change the current color between polygons or vertices glColor() sets the current colorglColor() sets the current color Or OpenGL provides a simple lighting model: Set parameters for light(s)Set parameters for light(s) –Intensity, position, direction & falloff (if applicable) Set material parameters to describe how light reflects from the surfaceSet material parameters to describe how light reflects from the surface Won’t go into details now; check the red book if interested Setup lighting, if any Simplest option: change the current color between polygons or vertices glColor() sets the current colorglColor() sets the current color Or OpenGL provides a simple lighting model: Set parameters for light(s)Set parameters for light(s) –Intensity, position, direction & falloff (if applicable) Set material parameters to describe how light reflects from the surfaceSet material parameters to describe how light reflects from the surface Won’t go into details now; check the red book if interested

OpenGL: Specifying Geometry Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() A simple example: telling GL to render a triangleA simple example: telling GL to render a triangleglBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glEnd(); Usage: glBegin( geomtype ) where geomtype is:Usage: glBegin( geomtype ) where geomtype is: –Points, lines, polygons, triangles, quadrilaterals, etc... Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() A simple example: telling GL to render a triangleA simple example: telling GL to render a triangleglBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glEnd(); Usage: glBegin( geomtype ) where geomtype is:Usage: glBegin( geomtype ) where geomtype is: –Points, lines, polygons, triangles, quadrilaterals, etc...

Primitive Types GL_POINTSGL_LINE {S | _STRIP | _LOOP}{S | _STRIP | _LOOP}GL_TRIANGLE {S | _STRIP | _FAN}{S | _STRIP | _FAN}GL_QUAD {S | _STRIP}{S | _STRIP}GL_POLYGONGL_POINTSGL_LINE {S | _STRIP | _LOOP}{S | _STRIP | _LOOP}GL_TRIANGLE {S | _STRIP | _FAN}{S | _STRIP | _FAN}GL_QUAD {S | _STRIP}{S | _STRIP}GL_POLYGON

GL_POLYGON List of vertices defines polygon edges Polygon must be convex List of vertices defines polygon edges Polygon must be convex

Non-planar Polygons Imagine polygon with non-planar vertices Some perspectives will be rendered as concave polygons These concave polygons may not rasterize correctly Imagine polygon with non-planar vertices Some perspectives will be rendered as concave polygons These concave polygons may not rasterize correctly

OpenGL: More Examples Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); This type of operation is called immediate-mode rendering; each command happens immediatelyThis type of operation is called immediate-mode rendering; each command happens immediately Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); This type of operation is called immediate-mode rendering; each command happens immediatelyThis type of operation is called immediate-mode rendering; each command happens immediately

OpenGL: Drawing Triangles You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];...glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle What do the triangles drawn above look like?What do the triangles drawn above look like? How much redundant computation is happening?How much redundant computation is happening? You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];...glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle What do the triangles drawn above look like?What do the triangles drawn above look like? How much redundant computation is happening?How much redundant computation is happening?

OpenGL: Triangle Strips An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);glVertex3fv(v3);glVertex3fv(v4);glVertex3fv(v5);glEnd(); triangle 0 is v0, v1, v2triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?)triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)triangle 3 is v4, v3, v5 (again, not v3, v4, v5) An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);glVertex3fv(v3);glVertex3fv(v4);glVertex3fv(v5);glEnd(); triangle 0 is v0, v1, v2triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?)triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)triangle 3 is v4, v3, v5 (again, not v3, v4, v5) v0v0 v2v2 v1v1 v3v3 v4v4 v5v5

OpenGL: More Examples Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); This type of operation is called immediate-mode rendering; each command happens immediatelyThis type of operation is called immediate-mode rendering; each command happens immediately Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); This type of operation is called immediate-mode rendering; each command happens immediatelyThis type of operation is called immediate-mode rendering; each command happens immediately

OpenGL: Front/Back Rendering Each polygon has two sides, front and back OpenGL can render the two differently The ordering of vertices in the list determines which is the front side: When looking at the front side, the vertices go counterclockwiseWhen looking at the front side, the vertices go counterclockwise –This is basically the right-hand rule –Note that this still holds after perspective projection Each polygon has two sides, front and back OpenGL can render the two differently The ordering of vertices in the list determines which is the front side: When looking at the front side, the vertices go counterclockwiseWhen looking at the front side, the vertices go counterclockwise –This is basically the right-hand rule –Note that this still holds after perspective projection

OpenGL: Drawing Triangles You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];...glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle What do the triangles drawn above look like?What do the triangles drawn above look like? How much redundant computation is happening?How much redundant computation is happening? You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];...glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle What do the triangles drawn above look like?What do the triangles drawn above look like? How much redundant computation is happening?How much redundant computation is happening?

OpenGL: Triangle Strips An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);glVertex3fv(v3);glVertex3fv(v4);glVertex3fv(v5);glEnd(); triangle 0 is v0, v1, v2triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?)triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)triangle 3 is v4, v3, v5 (again, not v3, v4, v5) An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);glVertex3fv(v3);glVertex3fv(v4);glVertex3fv(v5);glEnd(); triangle 0 is v0, v1, v2triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?)triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)triangle 3 is v4, v3, v5 (again, not v3, v4, v5) v0v0 v2v2 v1v1 v3v3 v4v4 v5v5

Double Buffering Avoids displaying partially rendered frame buffer OpenGL generates one raster image while another raster image is displayed on monitor glxSwapBuffers (Display *dpy, Window, w) glutSwapBuffers (void) Avoids displaying partially rendered frame buffer OpenGL generates one raster image while another raster image is displayed on monitor glxSwapBuffers (Display *dpy, Window, w) glutSwapBuffers (void)

Learn OpenGL by example robot.c from the OpenGL Programming Guide

Learn OpenGL by example Two bodies Upper armUpper arm Lower armLower arm Major tasks PositionPosition OrientationOrientation Two bodies Upper armUpper arm Lower armLower arm Major tasks PositionPosition OrientationOrientation

Learn OpenGL by example Both bodies originally at origin

Learn OpenGL by example Headers #include #include Headers

Learn OpenGL by example int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMainLoop(); return 0; } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMainLoop(); return 0; }

Learn OpenGL by example void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); }

Learn OpenGL by example void display(void){ glClear (GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslatef (-1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();Continued… void display(void){ glClear (GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslatef (-1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();Continued…

Learn OpenGL by example glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();glPopMatrix();glutSwapBuffers();} glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 1.0); glutWireCube (1.0); glPopMatrix();glPopMatrix();glutSwapBuffers();}