Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.

Slides:



Advertisements
Similar presentations
Computer Graphics - Graphics Programming -
Advertisements

OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
©Zachary Wartell, UNCC9/28/ :30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights.
OPENGL.
Graphics Pipeline.
Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics (Fall 2008) COMS 4160, Lecture 9: OpenGL 1
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
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,
CS 248 OpenGL Help Session CS248 Presented by Sean Walker
Computer Graphics (Fall 2005) COMS 4160, Lecture 10: OpenGL 1
Interactive 3D Graphics and Virtual Reality Introduction to OpenGL concepts Session 2.
Computer Graphics (Fall 2004) COMS 4160, Lecture 11: OpenGL 2
OpenGL (II). How to Draw a 3-D object on Screen?
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.
OpenGL A Brief Overview. What is OpenGL?  It is NOT a programming language.  It is a Graphics Rendering API consisting of a set of function with a well.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
19/4/ :32 Graphics II Syllabus Selection and Picking Session 1.
OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Lecture 3 OpenGL.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
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.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
OpenGL: Introduction Yanci Zhang Game Programming Practice.
Review of OpenGL Basics
Computer Graphics Bing-Yu Chen National Taiwan University.
Program 2 due 02/01  Be sure to document your program  program level doc  your name  what the program does  each function  describe the arguments.
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.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
Computing & Information Sciences Kansas State University CG Basics 3 of 8: OpenGL Primer 1 CIS 636/736: (Introduction to) Computer Graphics CIS 636 Introduction.
Chap 3 Viewing and Transformation
CSCE 441: Computer Graphics
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
Lecture 7 Midterm Review. OpenGL Libraries gl: Basic OpenGL library, e.g. primitives. glu: OpenGL Utility library, a set of functions to create texture.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
CIS 681 Review: OpenGL. CIS 681 Command Syntax OpenGL commands start with a gl. This is followed by the base command such as Color. Followed by the number.
Introduction to Graphics Programming. Graphics API.
Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics,
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL LAB III.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics Lecture 34. OpenGL Programming II Taqdees A. Siddiqi
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
The Human Visual System vs The Pinhole camera
School of Computer Science
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
גרפיקה ממוחשבת: מבוא ל-OpenGL
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi Many slides courtesy Greg Humphreys

State OpenGL is a big state machine State encapsulates control for operations like: –Lighting –Shading –Texture Mapping Boolean state settings can be turned on and off with glEnable and glDisable Anything that can be set can be queried using glGet

State Management glEnable, glDisable, glIsEnabled glGet Attributes –glPointSize, glFrontFace, glCullFace, Other advanced topics (later) –Vertex Arrays –Display Lists –Curved surfaces

Immediate vs. Retained Mode Two ways of specifying what is to be drawn –Immediate Mode Primitives are sent to the display as soon as they are specified Graphics system has no memory of drawn graphics primitives –Retained Mode Primitives placed in display lists Display lists can be kept on the graphics server Can be redisplayed with different graphics state Almost always a performance win (if you can get away with it)

Event Driven Interaction OpenGL does not dictate any particular model of interaction Applications respond to events generated by devices (i.e., mice) and window system events (i.e., window resized) Events are usually placed in a queue awaiting action Callbacks let you associate a function with a particular type of event –Mouse callback

Usual Interactions Initialization –Open / place / resize window Expose/resize/hide window Mouse –Button click –Button release –Mouse motion –Mouse drag (motion + button) Keyboard –What key was pressed (or released)? –Where was the mouse? –Were any modifier keys pressed? (control, alt, shift)

GLUT OpenGL Utility Toolkit Written by Mark Kilgard Provides basic window system interaction: –Open/close window –Mouse/keyboard callbacks –“Idle” callback for animation –Menus

Typical “main” Function int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); glutCreateWindow( “A window” ); glutDisplayFunc( display ); glutReshapeFunc( reshape ); glutMouseFunc( mouse ); glutIdleFunc( idle ); glutMainLoop(); }

Viewing (Chapter 3) Two parts –Object positioning (GL_MODELVIEW) –Projection (GL_PROJECTION) Transformation stages (pp 98) Perspective, Orthographic transformations Camera always at origin, pointing –z direction Transforms applied to objects –Equivalent to moving camera by inverse… –More details next…

Transformations Object in world coordinates (modelview) –glTranslatef(x,y,z) ; glRotatef(θ,x,y,z) ; glScalef(x,y,z) –Right-multiply current matrix (last is first applied) Matrix Stacks –glPushMatrix, glPopMatrix, glLoad –Useful for Hierarchically defined figures gluLookAt (fromv,atv,upv) –Usually in projection matrix, sometimes in modelview –Concatenate with perspective (orthographic) projection

Sample Code glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( 1, 1, 1 ); glRotatef( 90, 1, 0, 0 ); DrawObject();

Positioning the Camera Use gluLookAt to specify: –Eye location –“Look-at point” –“up” vector gluLookAt( 10, 10, 10, 1, 2, 3, 0, 0, 1 ) –Eye point is (10, 10, 10) –Look at point is (1,2,3) –Up vector is (0,0,1) This is usually done in the GL_PROJECTION matrix, and combined with a perspective matrix

Viewing Volumes Orthographic projection –glOrtho( left, right, bottom, top, front, back ) specifies the boundaries of the parallel viewing volume –Objects are clipped to the specified viewing cube Perspective Projection –glFrustum, gluPerspective –Clipping volume is a frustum Make sure the near and far clipping planes aren’t too far apart Make sure the near plane isn’t too close to the eye

Complete Viewing Example //Projection first glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60, 1, 1, 100 ); gluLookAt( 10, 10, 10, 1, 2, 3, 0, 0, 1 ) //Now object transformations glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( 1, 1, 1 ); glRotatef( 90, 1, 0, 0 ); DrawObject();

Matrix Stacks OpenGL has multiple matrix “stacks” glPushMatrix pushes a copy of the top-of- stack matrix glPopMatrix throws away the top of the stack Very useful for hierarchichally defined figures

Color (Chapter 4) RGBA –8 (or whatever number) bits/color channel –Alpha A important for transparency –32 bits, 16 million colors Color Index –Index into color table, small number of colors (256) Shading model glShadeModel –GL_FLAT, GL_SMOOTH (Goraud) –Issues in smooth shading with color index?

Simple OpenGL Programs

Drawing Primitives Specify primitives using glBegin and glEnd: glBegin( primitive_type ); …specify vertex attributes …draw vertices glEnd(); primitive_type specifies points, lines, triangles, quads, polygons, etc… GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, GL_POLYGON,…

Specifying Vertices glVertex {size}{type}{vector} e.g. glVertex3fv glVertex2f( 1.0f, 1.0f ); glVertex3i( 20, 20, 20 ); float verts[4] = { 1.0, 2.0, 3.0, 1.0 }; glVertex4fv( verts ); Coordinates are passed in an array Coordinate types are float 3 coordinates passed (x,y,z)

Example: A Wireframe Cube GLfloat vertices[][3] = { {-1,-1,-1},{1,-1,1}, {1,1,-1},{-1,1,-1}, {-1,-1,1},{1,-1,1}, {1,1,1},{-1,1,1} }; void cube(void) { polygon( 1,0,3,2 ); polygon( 3,7,6,2 ); polygon( 7,3,0,4 ); polygon( 2,6,5,1 ); polygon( 4,5,6,7 ); polygon( 5,4,0,1 ); }

Example: A Wireframe Cube void polygon( int a, int b, int c, int d ) { glColor3f( 1,1,1 ); glBegin( GL_LINE_LOOP ); glVertex3fv( vertices[a] ); glVertex3fv( vertices[b] ); glVertex3fv( vertices[c] ); glVertex3fv( vertices[d] ); glEnd(); }

Specifying Vertex Attributes Vertex attributes are state settings that are usually applied between a glBegin/glEnd pair Vertex attributes are set using: glColor, glNormal, glTexCoord, glEdgeFlag Vertex attribute routines take a form similar to glVertex: glName {size}{type}{vector} e.g. glColor3us takes 3 unsigned short parameters

Pixel Primitives Provide a way to manipulate rectangles of pixels glDrawPixels, glReadPixels, glCopyPixels move pixel rectangles to and from the framebuffer glBitmap takes a binary image and renders the current color in framebuffer positions corresponding to 1’s in the image. This might be useful for drawing fonts glRasterPos defines where the pixels go in the framebuffer The interaction between glRasterPos and glBitmap is subtle and confusing

Hidden Surface Removal When we draw a fragment, record the z (distance to the eye) in the depth buffer If the z stored in the depth buffer is greater than the z for the fragment about to be drawn, draw it Otherwise, the fragment is behind something that has already been drawn, so throw it away

Hidden Surface Removal When setting up your window, specify a depth buffer: glutInitDisplayMode( GLUT_DEPTH ); When clearing, make sure to: glClear( GL_DEPTH_BUFFER_BIT ); glEnable( GL_DEPTH_TEST ); Set the depth test comparison operation: glDepthFunc( GL_LESS ); (this is the default)

Simple Shading Example: shading a sphere with lighting glShadeModel(GL_FLAT)glShadeModel(GL_SMOOTH) OpenGL will interpolate the colors across the face of a polygon if smooth shading is turned on.