Grafica Computazionale lezione34 Informatica e Automazione, "Roma Tre" May 27, 2010
Grafica Computazionale: Lezione 33 OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
Contents OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
OpenGL examples source Main reference:
OpenGL examples source Main reference: http://www.opengl.org
OpenGL examples source Main reference: http://www.opengl.org http://www.opengl.org/resources/code/samples/redbook
OpenGL examples source Main reference: http://www.opengl.org http://www.opengl.org/resources/code/samples/redbook Geometric Programming for Computer Aided Design
OpenGL examples source Main reference: http://www.opengl.org http://www.opengl.org/resources/code/samples/redbook Geometric Programming for Computer Aided Design Nate Robins OpenGL Tutors
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming Go to the Tutors site and explore it
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming Go to the Tutors site and explore it download the [zipped archive]
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming Go to the Tutors site and explore it download the [zipped archive] compile the whole
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming Go to the Tutors site and explore it download the [zipped archive] compile the whole
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming Go to the Tutors site and explore it download the [zipped archive] compile the whole 1. unzip the archive
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming Go to the Tutors site and explore it download the [zipped archive] compile the whole 1. unzip the archive 2. $ cd (to it from terminal)
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming Go to the Tutors site and explore it download the [zipped archive] compile the whole 1. unzip the archive 2. $ cd (to it from terminal) 3. $ make (compile and build)
Nate Robins OpenGL Tutors Interactive (simple) programs to make experiments with the main topics of OpenGL programming Go to the Tutors site and explore it download the [zipped archive] compile the whole 1. unzip the archive 2. $ cd (to it from terminal) 3. $ make (compile and build) 4. $./name (play with the executables :o)
Contents OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
Your first perspective (cube.py) from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def init(): glclearcolor(o.o, 0.0, 0.0, 0.0) glshademodel(gl FLAT) def display ( ) : glclear(gl COLOR BUFFER BIT) gicolor3f(i.0, 1 0, 1.0) giloadldentity() #/* clear the matrix */ #/* viewing transformation */ glulookat(o.o, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) giscalef(l.o, 2.0, 1.0) #/* modeling transformation */ glutwirecube(l.o) glflush( ) def reshape(w, h): giviewport(o, 0, w, h) glmatrixmode(gl PROJECTION) glloadidentity() glfrustum(-l.o, 1.0, -1.0, 1.0, 1.5, 20.0) glmatrixmode(gl MODELVIEW) # Main program entry point if name ",,,, " main " : -- -- glutlnit() glutlnitdisplaymode(glut_single I GLUT RGB) glutlnitwindowsize(soo, 500) glutlnitwindowposition(loo, 100) glutcreatewindow( "cube") init() glutdisplayfunc(display) glutreshapefunc(reshape) glutmainloop( )
Play with the resulting window
Play with the resulting window One-point perspective projection
Play with the resulting window One-point perspective projection
Play with the resulting window One-point perspective projection notice that the Viewport transformation, from Normalized device coordinates to Window coordinates:
Play with the resulting window One-point perspective projection notice that the Viewport transformation, from Normalized device coordinates to Window coordinates: is Non-isomorphic
glulookat glulookat(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz ) None Parameters eyex, eyey, eyez Specifies the position of the eye point. centerx, centery, centerz Specifies the position of the reference point. upx, upy, upz Specifies the direction of the up vector.
glulookat Description glulookat creates a viewing matrix derived from an eye point, a reference point indicating the center of the scene, and an UP vector. The matrix maps the reference point to the negative z axis and the eye point to the origin
glulookat Description glulookat creates a viewing matrix derived from an eye point, a reference point indicating the center of the scene, and an UP vector. The matrix maps the reference point to the negative z axis and the eye point to the origin When a typical projection matrix is used, the center of the scene therefore maps to the center of the viewport
glulookat Description glulookat creates a viewing matrix derived from an eye point, a reference point indicating the center of the scene, and an UP vector. The matrix maps the reference point to the negative z axis and the eye point to the origin When a typical projection matrix is used, the center of the scene therefore maps to the center of the viewport Similarly, the direction described by the UP vector projected onto the viewing plane is mapped to the positive y axis so that it points upward in the viewport
glulookat Description glulookat creates a viewing matrix derived from an eye point, a reference point indicating the center of the scene, and an UP vector. The matrix maps the reference point to the negative z axis and the eye point to the origin When a typical projection matrix is used, the center of the scene therefore maps to the center of the viewport Similarly, the direction described by the UP vector projected onto the viewing plane is mapped to the positive y axis so that it points upward in the viewport The UP vector must not be parallel to the line of sight from the eye point to the reference point.
Perspective projections What viewing model (COP, VRP, VUV) when setting (function glulookat) for:
Perspective projections What viewing model (COP, VRP, VUV) when setting (function glulookat) for: a one-point projection?
Perspective projections What viewing model (COP, VRP, VUV) when setting (function glulookat) for: a one-point projection? a two-point projection?
Perspective projections What viewing model (COP, VRP, VUV) when setting (function glulookat) for: a one-point projection? a two-point projection? a three-point projection?
Perspective projections What viewing model (COP, VRP, VUV) when setting (function glulookat) for: a one-point projection? a two-point projection? a three-point projection?
Perspective projections What viewing model (COP, VRP, VUV) when setting (function glulookat) for: a one-point projection? a two-point projection? a three-point projection? PLEASE experiment with...!!
glfrustum glfrustum(left, right, bottom, top, znear, zfar) None Parameters left, right Specify the coordinates for the left and right vertical clipping planes. bottom, top Specify the coordinates for the bottom and top horizontal clipping planes. nearval, farval Specify the distances to the near and far depth clipping planes. Both distances must be positive.
glfrustum Description (deprecated since 3.0) glfrustum describes a perspective matrix that produces a perspective projection. The current matrix (see glmatrixmode) is multiplied by this matrix and the result replaces the current matrix
glfrustum Description (deprecated since 3.0) glfrustum describes a perspective matrix that produces a perspective projection. The current matrix (see glmatrixmode) is multiplied by this matrix and the result replaces the current matrix Typically, the matrix mode is GL_PROJECTION
glfrustum Description (deprecated since 3.0) glfrustum describes a perspective matrix that produces a perspective projection. The current matrix (see glmatrixmode) is multiplied by this matrix and the result replaces the current matrix Typically, the matrix mode is GL_PROJECTION (left bottom, nearval) and (right top, nearval) specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, assuming that the eye is located at (0, 0, 0).
glfrustum Description (deprecated since 3.0) glfrustum describes a perspective matrix that produces a perspective projection. The current matrix (see glmatrixmode) is multiplied by this matrix and the result replaces the current matrix Typically, the matrix mode is GL_PROJECTION (left bottom, nearval) and (right top, nearval) specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, assuming that the eye is located at (0, 0, 0). farval specifies the location of the far clipping plane. Both nearval and farval must be positive.
Contents OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
Your first axonometric projection (cube1.py) from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def init(): glclearcolor(o.o, 0.0, 0.0, 0.0) glshademodel(gl FLAT) def display(): glclear(gl_color_buffer_bit) glcolor3f (1.0, 1.0, 1.0) glloadidentity() #/* clear the matrix */ #/* viewing transformation */ glulookat(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) glscalef(l.0, 2.0, 1.0) #/* modeling transformation */ glutwirecube(l.0) glflush( ) def reshape(w, h): glviewport(o, 0, w, h) glmatrixmode(gl PROJECTION) glloadidentity() glortho(-4.0, 4.0, -4.0, 4.0, 1.5, 20.0) glmatrixmode(gl MODELVIEW) # Main program entry point if name " main ": glutinit( ) glutinitdisplaymode(glut SINGLE glutinitwindowsize(400, 400) glutinitwindowposition(940, 0) glutcreatewindow( "cube") ini t () glutdisplayfunc(display) glutreshapefunc(reshape) glutmainloop( ) I GLUT_RGB)
Play with the window...
Play with the window... isometric parallel projection
Play with the window... isometric parallel projection
Play with the window... isometric parallel projection notice AGAIN that the Viewport transformation, from Normalized device coordinates to Window coordinates:
Play with the window... isometric parallel projection notice AGAIN that the Viewport transformation, from Normalized device coordinates to Window coordinates: is Non-isomorphic
Contents OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
"Isomorphic" viewport transformation def display(): glclear(gl_color_buffer_bit) glcolor3f(1.0, 1.0, 1.0) glloadidentity() #/* clear the matrix */ #/* viewing transformation */ glulookat(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) glutwirecube(1.0) glflush() def reshape(w, h): glviewport(o, 0, w, h) aspectratio = float(w) / float(h) if (w <= h): u,v = 1,1/aspectRatio else: u,v = aspectratio, 1 glmatrixmode(gl PROJECTION) glloadidentity() glortho(-1.0*u, 1.0*u, -1.0*v, 1.0*v, 0.01, 20.0) glmatrixmode(gl MODELVIEW) Unit cube in isometric parallel projection
The Viewport transformation is now isomorphic...
The Viewport transformation is now isomorphic... play with the window aspect ratio...
The Viewport transformation is now isomorphic... play with the window aspect ratio...
The Viewport transformation is now isomorphic... play with the window aspect ratio... isometric parallel projection
Parallel projections What viewing model (DOP, VRP, VUV) when setting (function glulookat) for:
Parallel projections What viewing model (DOP, VRP, VUV) when setting (function glulookat) for: isometric orthogonal projection?
Parallel projections What viewing model (DOP, VRP, VUV) when setting (function glulookat) for: isometric orthogonal projection? (standard) dimetric orthogonal projection?
Parallel projections What viewing model (DOP, VRP, VUV) when setting (function glulookat) for: isometric orthogonal projection? (standard) dimetric orthogonal projection? (standard) three-metric orthogonal projection?
Parallel projections What viewing model (DOP, VRP, VUV) when setting (function glulookat) for: isometric orthogonal projection? (standard) dimetric orthogonal projection? (standard) three-metric orthogonal projection?
Parallel projections What viewing model (DOP, VRP, VUV) when setting (function glulookat) for: isometric orthogonal projection? (standard) dimetric orthogonal projection? (standard) three-metric orthogonal projection? PLEASE experiment with...!!
Parallel projections PROBLEM What about the OBLIQUE projections?
Parallel projections PROBLEM What about the OBLIQUE projections? isometric cavalier projections?
Parallel projections PROBLEM What about the OBLIQUE projections? isometric cavalier projections? dimetric cabinet projections?
Parallel projections PROBLEM What about the OBLIQUE projections? isometric cavalier projections? dimetric cabinet projections?
Parallel projections PROBLEM What about the OBLIQUE projections? isometric cavalier projections? dimetric cabinet projections? PLEASE look the web (better: just THINK) and try with...!!
Contents OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
Multiple viewport rendering (cube2.py) (1/3) from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def init(): glclearcolor(o.o, 0.0, 0.0, 0.0) glshademodel(gl FLAT) glenable(gl_scissor_test) def scenemodel(): glscalef(3.0, 3.0, 3.0) #/* modeling transformation */ glutwirecube(l.o) def display(): global viewl,view2,view3,view4 global winwidth,winheigth glcolor3f(1.0, 1.0, 1.0) glloadidentity() #/* clear the matrix */ #/* viewing transformation */ w,h = winwidth,winheigth glscissor(o, 0, w/2, h/2) glclear(gl COLOR BUFFER BIT) glviewport(o, 0,-w/2, h72) glloadidentity() glulookat(*viewl) scenemodel()
Multiple viewport rendering (2/3) glscissor(w/2, 0, w/2, h/2) glclear(gl COLOR BUFFER BIT) glviewport(w/2, 0, w/2,-h/2) glmatrixmode(gl PROJECTION) glpushmatrix() glloadidentity() glortho(-4.0, 4.0, -4.0, 4.0, 1.5, 100.0) glmatrixmode(gl MODELVIEW) glloadidentity() glulookat(*view2) scenemodel() glmatrixmode(gl PROJECTION) glpopmatrix() glmatrixmode(gl MODELVIEW) glloadidentity() glscissor(o, h/2, w/2, h/2) glclear(gl COLOR BUFFER BIT) glviewport(o, h/2, w/2,-h/2) glloadidentity() glulookat(*view3) scenemodel() glscissor(w/2, h/2, w/2, h/2) glclear(gl COLOR BUFFER BIT) glviewport(w/2, h/2, w/2, h/2) glloadidentity() glulookat(*view4) scenemodel() glfiush()
Multiple viewport rendering (3/3) glscissor(w/2, h/2, w/2, h/2) glclear(gl COLOR BUFFER BIT) glviewport(w/2, h/2, w/2, h/2) glloadidentity() glulookat(*view4) scenemodel ( ) glflush( ) def reshape(w, h): global viewl,view2,view3,view4 global winwidth,winheigth winwidth,winheigth = w,h glmatrixmode(gl PROJECTION) glloadidentity() glfrustum( -1. 0, 1. 0, -1. 0, 1. 0, 1. 5, 100.0) glmatrixmode(gl MODELVIEW) view3 - (0.0, 0.0, 6.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) view4 = (3.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) viewl = (3.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) view2 = (5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) winwidth, winheigth = 0, 0 # Main program entry point if name " main " : glutinit( ) glutinitdisplaymode(glut SINGLE I GLUT_RGB) glutinitwindowsize(soo, 500) glutinitwindowposition(940, 0) glutcreatewindow( "cube") ini t ( ) glutdisplayfunc(display) glutreshapefunc(reshape) glutmainloop( )
Multiple viewport rendering (non-isomorphic)
glscissor glscissor(x, y, width, height) None Definethe scissorbox Parameters x, y Specify the lower left corner of the scissor box. Initially (0, 0). width, height Specify the width and height of the scissor box. When a GL context is first attached to a window, width and height are set to the dimensions of that window.
glscissor Description glscissor defines a rectangle, called the scissor box, in window coordinates. The first two arguments, x and y, specify the lower left corner of the box. width and height specify the width and height of the box. To enable and disable the scissor test, call glenable and gldisable with argument GL_SCISSOR_TEST.
glscissor Description glscissor defines a rectangle, called the scissor box, in window coordinates. The first two arguments, x and y, specify the lower left corner of the box. width and height specify the width and height of the box. To enable and disable the scissor test, call glenable and gldisable with argument GL_SCISSOR_TEST.
glscissor Description glscissor defines a rectangle, called the scissor box, in window coordinates. The first two arguments, x and y, specify the lower left corner of the box. width and height specify the width and height of the box. To enable and disable the scissor test, call glenable and gldisable with argument GL_SCISSOR_TEST. The test is initially disabled. While the test is enabled, only pixels that lie within the scissor box can be modified by drawing commands.
glscissor Description glscissor defines a rectangle, called the scissor box, in window coordinates. The first two arguments, x and y, specify the lower left corner of the box. width and height specify the width and height of the box. To enable and disable the scissor test, call glenable and gldisable with argument GL_SCISSOR_TEST. The test is initially disabled. While the test is enabled, only pixels that lie within the scissor box can be modified by drawing commands. glscissor(0,0,1,1) allows modification of only the lower left pixel in the window, and glscissor(0,0,0,0) doesn t allow modification of any pixels in the window.
glscissor Description glscissor defines a rectangle, called the scissor box, in window coordinates. The first two arguments, x and y, specify the lower left corner of the box. width and height specify the width and height of the box. To enable and disable the scissor test, call glenable and gldisable with argument GL_SCISSOR_TEST. The test is initially disabled. While the test is enabled, only pixels that lie within the scissor box can be modified by drawing commands. glscissor(0,0,1,1) allows modification of only the lower left pixel in the window, and glscissor(0,0,0,0) doesn t allow modification of any pixels in the window. When the scissor test is disabled, it is as though the scissor box includes the entire window.
isomorphic multi-viewport (1/2) cube3.py... def init(): glclearcolor(1.0, 1.0, 1.0, 1.0) glcolor3f(0.0, 0.0, 0.0) glshademodel(gl_flat) glenable(gl_scissor_test) def scenemodel() : glscalef(3.0, 3.0, 3.0) #/* modeling transformation */ glutwirecube(1.0) def display(): global view1,view2,view3,view4 global winwidth,winheigth w,h = winwidth,winheigth aspectratio = float(w) / float(h) if (w <= h): u,v = 1,1/aspectRatio else: u,v = aspectratio, 1 glmatrixmode(gl_projection) glloadldentity() glfrustum(-1.0*u, 1.0*u, -1.0*v, 1.0*v, 1.5, 100.0) glmatrixmode(gl_modelview) glloadldentity()...
isomorphic multi-viewport (2/2)... glmatrixmode(gl PROJECTION) glpushmatrix( ) glloadidentity() glortho(-4.0*u, 4.0*u, -4.0*v, 4.0*v, 1.5, 100.0) glmatrixmode(gl MODELVIEW) glloadidentity() glulookat(*view2) scenemodel ( ) glmatrixmode(gl PROJECTION) glpopmatrix( ) glmatrixmode(gl MODELVIEW) glloadidentity() glflush( ) def reshape(w, h): global viewl,view2,view3,view4 global winwidth,winheigth winwidth,winheigth = w,h...
isomorphic multi-viewport...
Contents OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
Color primitives (1/2) cube4.py def scenemodel(): glscalef(1.5, 1.5, 1.5) #/* modeling transformation */ glbegin(gl QUADS); glcolor3f(0.0,1.0,0.0) glvertex3f( 1.0, 1.0,-1.0) glvertex3f(-1.0, 1.0,-1.0) glvertex3f(-1.0, 1.0, 1.0) glvertex3f( 1.0, 1.0, 1.0) glcolor3f(1.0,0.5,0.0) glvertex3f( 1.0,-1.0, 1.0) glvertex3f(-1.0,-1.0, 1.0) glvertex3f(-1.0,-1.0,-1.0) glvertex3f( 1.0,-1.0,-1.0) glcolor3f(1.0,0.0,0.0) glvertex3f( 1.0, 1.0, 1.0) glvertex3f(-1.0, 1.0, 1.0) glvertex3f(-1.0,-1.0, 1.0) glvertex3f( 1.0,-1.0, 1.0) glcolor3f(1.0,1.0,0.0) glvertex3f( 1.0,-1.0,-1.0) glvertex3f(-1.0,-1.0,-1.0) glvertex3f(-1.0, 1.0,-1.0) glvertex3f( 1.0, 1.0,-1.0) glcolor3f(0.0,0.0,1.0) glvertex3f(-1.0, 1.0, 1.0) glvertex3f(-1.0, 1.0,-1.0) glvertex3f(-1.0,-1.0,-1.0) glvertex3f(-1.0,-1.0, 1.0) glcolor3f(1.0,0.0,1.0) glvertex3f( 1.0, 1.0,-1.0) glvertex3f( 1.0, 1.0, 1.0) glvertex3f( 1.0,-1.0, 1.0) glvertex3f( 1.0,-1.0,-1.0) glend()
Color primitives (2/2) glpushmatrix() glscalef(2, 2, 2) glcolor3f(1.0, 1.0, 1.0) glutwirecube(1.0) glpopmatrix() glutswapbuffers() def display(): global view1,view2,view3,view4 global winwidth,winheigth w,h = winwidth,winheigth aspectratio = float(w) / float(h) if (w <= h): u,v = 1,1/aspectRatio else: u,v = aspectratio, 1 glmatrixmode(gl PROJECTION) glloadidentity() glfrustum(-1.0*u, 1.0*u, -1.0*v, 1.0*v, 1.5, 10.0) glmatrixmode(gl MODELVIEW) glloadidentity() glscissor(o, 0, w/2, h/2) glclear(gl COLOR BUFFER BIT) glviewport(o, 0,-w/2, h72) glloadidentity() glulookat(*view1) scenemodel()
Color primitives (no depth-ordering of quads) Notice that at least one of the faces is covered by the others
Contents OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
Hidden surface removal (cube5.py) def init(): glclearcolor(1.0, loo, loo, 1.0) glcolor3f(0.o, 0.0, 1.0) glshademodel(gl_flat) glenable(gl_scissor_test) glenable(gl_depth_test) glscissor(o, 0, w/2, h/2) glclear(gl COLOR BUFFER BIT I glviewport(o, 0,-w/2, h72) glloadidentity() glulookat(*viewl) scenemodel() GL_DEPTH_BUFFER_BIT) # Main program entry point if name ",,,, " main " : - - glutlnit( ) glutlnitdisplaymode(glut DOUBLE I GLUT RGB I GLUT_DEPTH) glutlnitwindowsize(500, 500) glutlnitwindowposition(940, 0) glutcreatewindow( "cube")
Hidden surface removal The only visible faces are drawn (z-buffer algorithm)
Contents OpenGL examples Perspective projection Parallel projection Isomorphic viewport transformation Multiple viewports Color primitives Hidden surface removal Modeling transformation
Interactive model rotation via arrow keys (1/2) cube5.py
Interactive model rotation via arrow keys (2/2)
Modeling transformations Let us play with arrow key rotations... :o)
Exercises PROBLEM 1 add interactive model scaling to the above PROBLEM 2 build a more complex structured world (a room with table and chairs?) PROBLEM 3 create an articulated lamp model PROBLEM 4 Define a viewport transformation that maps isomorphically the biggest possible image (of any world and with any projection) within the window