Computer Graphics (Fall 2004) COMS 4160, Lecture 11: OpenGL 2

Slides:



Advertisements
Similar presentations
Using GLU/GLUT Objects GLU/GLUT provides very simple object primitives glutWireCube glutWireCone gluCylinder glutWireTeapot.
Advertisements

Graphics Pipeline.
Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
1 Understanding of OpenGL TA: Dong Hyun Jeong Instructor : Dr. Kalpathi Subramanian Texture Mapping.
OpenGL Texture Mapping
MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Today: - Make sure everyone is set up with an OpenGL environment - OpenGL basics:
Computer Graphics (Fall 2008) COMS 4160, Lecture 9: OpenGL 1
Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics (Fall 2005) COMS 4160, Lecture 10: OpenGL 1
1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry.
CS 4731: Computer Graphics Lecture 17: Texturing Emmanuel Agu.
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.
19/4/ :32 Graphics II Syllabus Selection and Picking Session 1.
CAP4730: Computational Structures in Computer Graphics Introduction to OpenGL.
3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!
Texture Mapping. To add surface details… World of Warcraft, Blizzard Inc. More polygons (slow and hard to handle small details) Less polygons but with.
Computer Graphics Texture Mapping Eriq Muhammad Adams
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.
Geometric transformations The Pipeline
Hank Childs, University of Oregon
2IV60 Computer Graphics set 10: Texture mapping Jack van Wijk TU/e.
Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
ECSE-4750 Computer Graphics Fall 2004 Prof. Michael Wozny TA. Abhishek Gattani TA. Stephen
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
Lecture 27: Texture Mapping Li Zhang Spring 2008
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.
CG1 Labs Wei Li. Back Face Culling // enable back-face culling glEnable( GL_CULL_FACE ); // orientation of front-facing polygons glFrontFace( GL_CCW );
Computer Graphics Bing-Yu Chen National Taiwan University.
Texture Mapping Drawing Pictures on Polygons. Texture Mapping.
Modeling with OpenGL Practice with OpenGL transformations.
TEXTURES & OTHER GOODIES Computer Graphics. glTexCoord2f(...); + =
Computer Science Term 1, 2006 Tutorial 2 Assignment 3 – The Virtual World.
OpenGL Selection. Three Selection Methods Color coding (OpenGL) Selection mode (OpenGL) Selection ray (generic)
Review of OpenGL Basics
2 COEN Computer Graphics I Evening’s Goals n Discuss displaying and reading image primitives n Describe texture mapping n Discuss OpenGL modes and.
MAE152 Computer Graphics for Scientists and Engineers Fall 03 Display Lists.
CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2.
Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 8: OpenGL 2
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.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall.
1 Angel: Interactive Computer Graphics5E © Addison- Wesley 2009 Image Formation Fundamental imaging notions Fundamental imaging notions Physical basis.
Chap 3 Viewing and Transformation
Lecture 2 Review OpenGL Libraries Graphics Overview Rendering Pipeline OpenGL command structure.
CS552: Computer Graphics Lecture 6: Viewing in 2D.
Lecture 7 Midterm Review. OpenGL Libraries gl: Basic OpenGL library, e.g. primitives. glu: OpenGL Utility library, a set of functions to create texture.
Details of Texture Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, December 1, 2003.
1 Geometric Transformations-II Modelling Transforms By Dr.Ureerat Suksawatchon.
Introduction to Graphics Programming. Graphics API.
Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics,
OpenGL LAB III.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
OpenGL: The Open Graphics Language Technology and Historical Overview By Ricardo Veguilla.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Texture Mapping CEng 477 Introduction to Computer Graphics.
OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360/4360.
CSE 167 [Win 17], Lecture 8: OpenGL 2 Ravi Ramamoorthi
School of Computer Science
Reference1. [OpenGL course slides by Rasmus Stenholt]
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Assignment 3b Q&A.
Introduction to OpenGL
Programming Textures Lecture 15 Fri, Sep 28, 2007.
Presentation transcript:

Computer Graphics (Fall 2004) COMS 4160, Lecture 11: OpenGL 2

To Do  Start working on HW 3. Milestones due in 1 week.  Can leverage many sources (Red book, excellent online documentation, see links class website)  And programs shown in class (try reading, compiling, understanding source code)  It is a good idea to copy (and modify) relevant segments  (Very) tough to get started, but lots of fun afterwards

Methodology for Lecture  Make demo from last lecture more ambitious  Questions on some changes and potential problems  I will run through sequence of steps with demos  Demo opengl2\opengl2-orig.exeopengl2\opengl2-orig.exe

Outline  Review of demo from last lecture  Display lists (extend init for pillars)  Matrix stacks and transforms (draw 4 pillars)  Depth testing or z-buffering  Animation (moving teapot)  Texture mapping (wooden floor) Best source for OpenGL is the redbook (in this lecture, chapters 3, 7 and early part of 9). Of course, this is more a reference manual than a textbook, and you are better off implementing rather reading end to end. Though if you do have time, the book is actually quite readable

Review of Last Demo  Changed floor to all white, added global for display lists (talked about next) and included some new files  Demo 0 (in Visual Studio) #include #include // ** NEW ** for loading the texture #include #include // ** NEW ** for errors int mouseoldx, mouseoldy ; // For mouse motion GLdouble eyeloc = 2.0 ; // Where to look from; initially 0 -2, 2 GLuint pillar ; // ** NEW ** For the display list for the pillars

Immediate vs. Retained Mode Immediate Mode  Primitives sent to display as soon as specified (default)  Graphics system has no memory of drawn 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 We will add 4 pillars using a display list for a single pillar, with changed attributes (transform, color)

Display List Initialization (in init) // This uses gluCylinder. The glu primitives are // sometimes useful. // The GLU library is described in chapter 11. We need only // a small part of it. cyl = gluNewQuadric() ; /* This part sets up a display list for the pillars. Refer to chapter 7 for more details */ pillar = glGenLists(1) ; glNewList(pillar, GL_COMPILE) ; gluCylinder (cyl, 0.1, 0.1,.5, 10, 10) ; glEndList() ;

Outline  Review of demo from last lecture  Display lists (extend init for pillars)  Matrix stacks and transforms (draw 4 pillars)  Depth testing or z-buffering  Animation (moving teapot)  Texture mapping (wooden floor) Red Book, Chapter 3

Summary OpenGL Vertex Transforms Object coords (x y z w) t vertex Modelview matrix [Object Transforms and gluLookAt] Projection matrix [3D to 2D, usually gluPerspective] Eye coordinates (used for lighting) Perspective Divide (Dehomogenization) Clip coordinates Viewport Transform (glViewport) Normalized Device Coordinates Window Coords

Transformations Matrix Stacks  glPushMatrix, glPopMatrix, glLoad, glMultMatrixf  Useful for Hierarchically defined figures, placing pillars Transforms  glTranslatef(x,y,z) ; glRotatef(θ,x,y,z) ; glScalef(x,y,z)  Right-multiply current matrix (last is first applied) Also gluLookAt, gluPerspective  Remember gluLookAt just matrix like any other transform, affecting modelview  Must come before in code, after in action to other transfs  Why not usually an issue for gluPerspective?

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

Drawing Pillars 1 (in display) /* Note the use of matrix stacks and push and pop */ glMatrixMode(GL_MODELVIEW) ; /* Draw first pillar by Translating */ glPushMatrix() ; glTranslatef(0.4, 0.4, 0.0) ; glColor3f(1.0, 1.0, 0.0) ; glCallList(pillar) ; glPopMatrix() ; /* Draw second pillar by Translating */ glPushMatrix() ; glTranslatef(-0.4, 0.4, 0.0) ; glColor3f(1.0, 0.0, 0.0) ; glCallList(pillar) ; glPopMatrix() ;

Drawing Pillars 2 /* Draw third pillar by Translating */ glPushMatrix() ; glTranslatef(-0.4, -0.4, 0.0) ; glColor3f(0.0, 1.0, 0.0) ; glCallList(pillar) ; glPopMatrix() ; /* Draw fourth pillar by Translating */ glPushMatrix() ; glTranslatef(0.4, -0.4, 0.0) ; glColor3f(0.0, 0.0, 1.0) ; glCallList(pillar) ; glPopMatrix() ;

Demo  Demo 1 (in visual studio)  Does order of drawing matter?  What if I move floor after pillars in code?  Is this desirable? If not, what can I do about it?

Outline  Review of demo from last lecture  Display lists (extend init for pillars)  Matrix stacks and transforms (draw 4 pillars)  Depth testing or z-buffering (state management too)  Animation (moving teapot)  Texture mapping (wooden floor)

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

Turning on Depth test (Z-buffer) OpenGL use a Z-buffer for depth tests  For each pixel, store nearest Z value (to camera) so far  If new fragment is closer, it replaces old z, color  Simple technique to get accurate visibility  (Be sure you know what fragments and pixels are) Changes in main fn, display to Z-buffer glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); In init function glEnable(GL_DEPTH_TEST) ; glDepthFunc(GL_LESS) ; // The default option

Demo  Demo 2 (in visual studio)  Does order of drawing matter any more?  What if I change near plane to 0?  Is this desirable? If not, what can I do about it?

Outline  Review of demo from last lecture  Display lists (extend init for pillars)  Matrix stacks and transforms (draw 4 pillars)  Depth testing or z-buffering  Animation (moving teapot)  Texture mapping (wooden floor)

Demo  Demo 3 (in visual studio)  Notice how teapot cycles around  And that I can pause and restart animation  And do everything else (zoom etc.) while teapot moves in background

Drawing Teapot (in display) GLdouble teapotloc = -0.5 ; // global variable set before /* ** NEW ** Put a teapot in the middle that animates */ glColor3f(0.0,1.0,1.0) ; glPushMatrix() ; /* I now transform by the teapot translation for animation */ glTranslatef(teapotloc, 0.0, 0.0) ; /* The following two transforms set up and center the teapot */ /* Remember that transforms right-multiply the stack */ glTranslatef(0.0,0.0,0.1) ; glRotatef(90.0,1.0,0.0,0.0) ; glutSolidTeapot(0.15) ; glPopMatrix() ;

Simple Animation routine void animation(void) { teapotloc = teapotloc ; if (teapotloc > 0.5) teapotloc = -0.5 ; glutPostRedisplay() ; }

Keyboard callback (p to pause) GLint animate = 0 ; // ** NEW ** whether to animate or not void keyboard (unsigned char key, int x, int y) { switch (key) { case 27: // Escape to quit exit(0) ; break ; case 'p': // ** NEW ** to pause/restart animation animate = !animate ; if (animate) glutIdleFunc(animation) ; else glutIdleFunc(NULL) ; break ; default: break ; }

Double Buffering  New primitives draw over (replace) old objects  Can lead to jerky sensation  Solution: double buffer. Render into back (offscreen) buffer. When finished, swap buffers to display entire image at once.  Changes in main and display glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutSwapBuffers() ; glFlush ();

Outline  Review of demo from last lecture  Display lists (extend init for pillars)  Matrix stacks and transforms (draw 4 pillars)  Depth testing or z-buffering  Animation (moving teapot)  Texture mapping (wooden floor) Initial part of GL chapter 9, Demo 4

Texture Mapping  Textures are images applied to objects  Texture modifies the color assignment to a fragment  Texture color can modify the material color used in the shading model, or it can be a decal  Use glTexCoord to assign a texture coordinate to a vertex

Texture Mapping Example glBegin( GL_QUADS ); glTexCoord2f( 0, 0 ); glVertex3f( a, b, c ); glTexCoord2f( 1, 0 ); glVertex3f( a, b, d ); glTexCoord2f( 1, 1 ); glVertex3f( a, e, d ); glTexCoord2f( 0, 1 ); glVertex3f( a, e, c ); glEnd();

Specifying the Texture Image  glTexImage2D( target, level, components, width height, border, format, type, data )  target is GL_TEXTURE_2D  level is (almost always) 0  components = 3 or 4 (RGB/RGBA)  width/height MUST be a power of 2  border = 0 (usually)  format = GL_RGB or GL_RGBA (usually)  type = GL_UNSIGNED_BYTE, GL_FLOAT, etc…

More on Texture (very briefly)  Optimizations for efficiency  Mipmapping  Filtering  Texture Coordinate generation  Texture Matrix  Environment Mapping If very ambitious, read all of chapter 9

Setting up texture (in init) /* ** New for demo 2 ** setup for textures */ /* First, read this simple ppm file in */ assert(fp = fopen("wood.ppm","rb")) ; fscanf(fp,"%*s %*d %*d %*d%*c") ; for (i = 0 ; i < 256 ; i++) for (j = 0 ; j < 256 ; j++) for (k = 0 ; k < 3 ; k++) fscanf(fp,"%c",&(woodtexture[i][j][k])) ; fclose(fp) ; /* Now, set up all the stuff for texturing, per page 368 */ glGenTextures(1, &texName) ; glBindTexture(GL_TEXTURE_2D, texName) ; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) ; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) ; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) ; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) ; glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, woodtexture) ;

Rendering with texture (in display) /* As a final step, I modify this for texture mapping * NEW * */ /* Consult chapter 9 for the explanation of the various options */ /* Note addition of texture coordinates, and the glue to add texturing */ /* Also note some effort to find the error if any */ glEnable(GL_TEXTURE_2D) ; glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) ; glBindTexture(GL_TEXTURE_2D, texName) ; glColor3f(1.0,1.0,1.0) ; err = glGetError() ; assert(err == GL_NO_ERROR) ; glBegin(GL_POLYGON) ; glTexCoord2f(1.0, 1.0) ; glVertex3f (0.5, 0.5, 0.0); glTexCoord2f(0.0,1.0) ; glVertex3f (-0.5, 0.5, 0.0); glTexCoord2f(0.0,0.0); glVertex3f (-0.5, -0.5, 0.0); glTexCoord2f(1.0,0.0) ; glVertex3f (0.5, -0.5, 0.0); glEnd() ; err = glGetError() ; assert(err == GL_NO_ERROR) ; glDisable(GL_TEXTURE_2D) ;