460 likes | 663 Views
Texture and lighting. Mark Nelson mjas@itu.dk. Purpose of today ’ s lecture. Introduction to the main conceptual elements of T&L Theoretical basis Practical approximations. Types of rendering. Photorealistic Ultimate goal is an accurate simulation of light physics/optics
E N D
Texture and lighting • Mark Nelson • mjas@itu.dk Fall 2013 www.itu.dk
Purpose of today’s lecture Introduction to the mainconceptual elements of T&L Theoretical basis Practical approximations
Types of rendering Photorealistic Ultimate goal is an accurate simulation of light physics/optics Realistic goal is a visually acceptable approximation Non-photorealistic In principle can be anything Render in an interesting way, not necessarily physically accurate Most common example is cel-shading (”cartoon-style”) Will focus on photorealistic
Two basic elements Light and light propagation Surfaces with properties (And howthoseinteract)
Light interacting with surfaces Fourbasicthings it can do Absorption Reflection Transmission (possibly with refraction) Diffraction (Diffraction is almostalwaysignored.)
Absorption, reflection, and color Light striking a surface can be absorbed (not reflected) If uniformly, surface looks dimmer/lighter If some colors more than others, gives surface its color Reflection direction/scattering can vary Surfaces have a BDRF – bidirectional reflectance distribution function BDRF is relative to a surface normal
Global illumination Assume we have light sources and BDRFs for all surfaces Can theoretically compute L(x,ω) for every surface point Luminence from that point in each direction
Rendering equation L(x,ω) equals: Light emitted directly (e.g. glowing), plus For each incoming direction ω’ (integrate), The amount of incoming light from that direction, times The proportion reflected towards direction ω (given by the BDRF), times (-ω · n), which attenuates light based on incident angle
Global illumination problems Impossible to actually compute! Can approximate in some ways High-quality but slow: raytracing Trace many incident rays, see where they go, add up the results
More global illumination problems Doesn’t fit the pipelined rasterization model Want to texture & light locally, not globally
Approximation 1: Normal-based local lighting Vertex is lit based on its distance and angle to a light source Angle effects: Flat-on vertices are fully lit Tilted vertices are less lit Perpendicular or facing-the-wrong way vertices not lit Vertex lighting is sum of contribution from each light
Approximation 1 assumptions Purely diffuse reflections ”Lambertian surface” Angle dependence is ”Lambert’s cosine law” Independent local lighting
Approximation 1 features Purely diffuse lighting No shiny reflections No ambient light No other fancy features also No shadows No mirror-type reflections Some of these are easier to fix than others
Re-introduce a simplified BRDF No longer a full function giving reflection angles Approximate with two parts Diffuse reflections spread out in all directions Specular reflections bounce in a direction (but are not ”true” reflections; can’t see yourself in the mirror) Surfaces are a mix of Lambertian and reflective
Phong lighting model Diffuseand specularreflections, plus ambient light
The shadow question Convex objects do self-shadowing appropriately Normals on the ”other side” are facing away, so don’t get lit Standard OpenGL-style lighting doesn’t do real shadows Several advanced techniques Shadow mapping Volumetric shadows
Where does this leave us? Decent approximation to real lighting Albeit without shadows Decent approximation to flat surfaces What about more complex surfaces?
Textures Make a flat surface look textured Two main reasons (related): Capture elements of the surface look not captured in the lighting model Simulate 3d elements too complex to spend polygons on (can’t spend polys on every brick in a brick wall)
Texture space A single texture goes from (0,0) to (1,1) Texture is addressed in texture coordinates This is texture space
Texture mapping A textured triangle is texture-mapped Each vertex is mapped into a point (u,v) in texture space The texture is then drawn on the rasterized triangle’s face
Texture repetition see glTexParameter
Texturing in OpenGL Enable textures Load textures Create a new texture ID Bind texture ID Set up textures Store texture ID When rendering an object Store texture IDs that it uses Bind texture ID
Enable texturing glEnable(GL_TEXTURE_2D) glDisable(GL_TEXTURE_2D)
Create textures void *data = LoadBitmap(textureName); GLuint textureId; glGenTextures(1, &textureId); glBindTexture(GL_TEXTURE_2D, textureId); glTextImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); // then set other parameters
Render textures glBindTexture(GL_TEXTURE_2D, textureId); glBegin(GL_TRIANGLES) for( int i=0 ; i < numTriangles ; i++ ) { glTexCoord2f( v0.u(), v0.v() ); glVertex3f( v0.x(), v0.y(), v0.z() ); glTexCoord2f( v1.u(), v1.v() ); glVertex3f( v1.x(), v1.y(), v1.z() ); glTexCoord2f( v2.u(), v2.v() ); glVertex3f( v2.x(), v2.y(), v2.z() ); } glEnd();
Unwraps (UV-mapping) To texture complex meshes, unwrap into texture space 3d editors will typically generate these Paint in the textures on the unwrap Load the (u,v) coordinate map with the mesh
Texture filtering and texel density A texel is a pixel from a texture mapped into the scene Rarely matches 1-to-1 to screen pixels Texture filtering upsamples or downsamples as needed Texel density is the density of texels in the world E.g. texels/meter, if world coordinates are meters
Texel density Typically want approximately constant texel density But, may have higher-resolution texture in certain places Character faces Book sitting on a table (so it can be read)
Texture filtering Nearest Neighbor (GL_NEAREST) (bi)linear (GL_LINEAR) Mipmapping (GL_NEAREST_MIPMAP_*)
Mipmapping Several versions of a texture of reduced size ”Texture pyramid” Efficiency (pre-filtered) Avoids some aliasing problems
Isotropic filtering Standard texture filtering is ”isotropic” Texture is resized in texture space, like resizing a 2d image in Photoshop Gives poor results at steep viewing angles Very different texel density in width versus height Ends up with blurry textures in the distance
Anisotropic filtering Rip mapping Like mip mapping but in one more dimension Pyramid of textures at different res. and aspect ratios (viewing angles) On modern hardware, ”proper” anisotropic filtering GL_TEXTURE_MAX_ANISOTROPY_EXT
Pre-baking things into textures Can bake rendered intotextures Light-mapping: pre-lit textures Oftenused to fake non-movingshadows Pre-bakedlightingcanbe made exclusivew/ GL_REPLACE (instead of the default GL_MODULATE) Reflectionmapping: pre-rendered distant reflections
“3d” texturing This lecture was essentially about ”2d” texturing Can also use textures to fake 3d effects Rough surfaces, bricks with some depth, etc. Bump mapping Parallax mapping
Recap Should understand: Global illumination theory Phong approximation Texture mapping Texture filtering