Upload
ivan-barreto
View
222
Download
2
Tags:
Embed Size (px)
DESCRIPTION
algoritmos de visibilidade
Citation preview
VisibilidadeMO603/MC930
Visibilidade
Algoritmos bsicos de visibilidadeAlgoritmo pintorZ-bufferEstilos de ShadingRay-casting
The visibility problem
What is the nearest surface seen at any point in the image?How would YOU solve this problem?
Removendo superfcies ocultas
AB
Removendo superfcies ocultaswhile (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT); draw_3d_object_B(); draw_3d_object_A(); }
Removendo superfcies ocultaswhile (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT); draw_3d_object_A(); draw_3d_object_B(); }
Removendo superfcies ocultas
BA
Image Space Visibility Algorithms
Painters Algorithm
Sort objects by depth (Z) Loop over objects in back-to-front order Project to image scan convert: image[x,y] = shade(x,y)
Sorting Objects by Depth
ZZminABABABSorting Objects by DepthRBGR1R2BG
Painters AlgorithmStrengthsSimplicity: draw objects one-at-a-time, scan convert eachHandles transparency wellDrawbacksSorting can be expensive (slower than linear in the number of objects)Clumsy when ordering is cyclic, because of need to splitInterpenetrating polygons need to be split, tooHard to sort non-polygonal objectsSometimes no need to sortIf objects are arranged in a grid, e.g. triangles in a height field z(x,y), such as a triangulated terrainWho uses it?Postscript interpretersOpenGL, if you dont glEnable(GL_DEPTH_TEST);
Representando objetos por malhas triangularesDivide objeto em faces triangularesA juno de todas as faces forma a casca do objetoTriangulo = estrutura (P1, P2, P3)T1={P1,P2,P3}, T2={P2,P4,P3}T3={P2, P5, P6}, T4={P6, P1, P2}, Objeto = T1, T2, T3, T4,
Backface cullingEach polygon is either front-facing or back-facingA polygon is backfacing if its normal points away from the viewer,i.e. VN >= 0When it worksIf object is closed, back faces are never visible so no need to render themEasy way to eliminate half your polygonsCan be used with both z-buffer and painters algorithmsIf object is convex, backface culling is a complete visibility algorithm!When it doesnt workIf objects are not closed, back faces might be visible
VN1N2
1.bin
Z-Buffer Algorithm Initialization loop over all x,y zbuf[x,y] = infinity Drawing steps loop over all objects scan convert object (loop over x,y) if z(x,y) < zbuf[x,y] compute z of this object at this pixel & test zbuf[x,y] = z(x,y) update z-buffer image[x,y] = shade(x,y) update image (typically RGB)
Z-Buffer Algorithm StrengthsSimple, no sorting or splittingEasy to mix polygons, spheres, other geometric primitivesDrawbacksCant handle transparency wellNeed good Z-buffer resolution or you get depth ordering artifactsIn OpenGL, this resolution is controlled by choice of clipping planes and number of bits for depthChoose ratio of clipping plane depths (zfar/znear) to be as small as possibleWho uses it?OpenGL, if you glEnable(GL_DEPTH_TEST);
Usando o buffer de profundidadeglutInitDisplayMode (GLUT_DEPTH | .... );glEnable(GL_DEPTH_TEST); ... while (1) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); get_viewing_point_from_mouse_position(); draw_3d_object_A(); draw_3d_object_B(); }
Ray Casting A very flexible visibility algorithm loop y loop x shoot ray from eye through pixel (x,y) into scene intersect with all surfaces, find first one the ray hits shade surface point to compute pixel (x,y)s color
Comparing visibility algorithmsPainters:Implementation: moderate to hard if sorting & splitting neededSpeed: fast if objects are pre-sorted, otherwise slowGenerality: sorting & splitting make it ill-suited for general 3-D renderingZ-buffer:Implementation: moderate, it can be implemented in hardwareSpeed: fast, unless depth complexity is highGenerality: good but wont do transparencyRay Casting:Implementation: easy, but hard to make it run fastSpeed: slow if many objects: cost is O((#pixels)(#objects))Generality: excellent, can even do CSG, transparency, shadows
Really Hard Visibility Problems Extremely high scene complexitya building walkthrough (QUAKE)?A fly-by of the Grand Canyon (or any outdoor scene!)Z-buffering requires drawing EVERY triangle for each imageNot feasible in real timeUsually Z-buffering is combined with spatial data structuresBSP trees are common (similar concept to octrees)For really complex scenes, visibility isnt always enoughObjects WAY in the distance are too small to matterMight as well approximate far-off objects with simpler primitivesThis is called geometry simplification
Programa exemplo (esfera)#include #include #include
Programa exemplo (esfera)void init(void) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST);}
Programa exemplo (esfera)void display(void){ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidSphere (1.0, 20, 16); glFlush ();}
Programa exemplo (esfera)int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0;}
Resultado
ComentriosDefinir vetores normais para todos vrtices: glutSolidSphere() faz isso.Criar, posicionar e habilitar uma (ou mais) fontes de luz: glLightfv(), glEnable(), glLight*()Ex: GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position);default: GL_POSITION (0, 0, 1, 0), eixo-Z negativoSelecionar um modelo de iluminao: glLightModel*() Definir propriedades materiais para os objetos na cena
Lembre-seVoce pode usar valores defaults para alguns parmetros, de iluminao; outros devem ser modificadosNo se esquea de habilitar todas as luzes que pretende usar e tambm habilitar o clculo de iluminaoVoce pode usar display lists para maximizar eficincia quando necessrio mudar as condies de iluminao
Computing Z for Z-buffering How to compute Z at each point for z-buffering?Can we interpolate Z?Linear interpolation along edge, along spanNot quite. World space Z does not interpolate linearly in image spaceBut if we linearly interpolate image space Z, it works!Perspective transforms planes to planesNote that image space Z is a nonlinear function of world space Z