3D游戏编程OpenGL纹理和缓存.pdf
OpenGL:Texture&Buffers Yanci Zhang Game Programming IIGame Programming II Concept of texture mapping Difficulties and solutions in texture mapping Texture mapping in OpenGL Buffers in OpenGL Game Programming IIGame Programming II Outline How to add more details to surfaces?Using more triangles Using texture mapping without increasing geometry complexity Game Programming IIGame Programming II Texture Mapping Shading Texture Mapping Advanced texture mapping techniques can add more details!Game Programming IIGame Programming II Texture Mapping Uses of texture mapping Simulate materials Reducing geometric complexity Image warping Reflection Game Programming IIGame Programming II Texture Mapping What we can do now Using vertex color to specify object color Using light sources and object materials to produce more realistic result But these are far from enough!Real objects have lots of details Texture mapping can Add details to surfaces Not increase#of vertices Game Programming IIGame Programming II Texture Mapping A texture is a bitmap of pixel colors that give an object the appearance of texture Mapping 2D images to 3D surfaces Texture can be 1D,2D even 3D Key question:how to build the connection between 2D texture coordinates and 3D object coordinates Game Programming IIGame Programming II What is Texture Mapping 1/2 Game Programming IIGame Programming II What is Texture Mapping 2/2 Images and geometry flow through separate pipelines that join at the rasterizer Textures do NOT affect geometric complexity Every vertex has its own texture coordinate Texture coordinates of fragments are interpolated from vertices in rasterizztion stage Texture lookups are executed in fragment processing stage Game Programming IIGame Programming II Texture Mapping in Pipeline Screen Coordinates Where the final image is produced World Coordinates Conceptually,where the mapping takes place Texture coordinates Used to identify points in the image to be mapped Parametric coordinates May be used to model curved surfaces Game Programming IIGame Programming II Coordinate Systems The map from texture coordinates to geometric coordinates may be a complex function or may have undesirable properties Prefer the inverse map from screen coordinates to texture coordinates Antialiasing problem Game Programming IIGame Programming II Difficulties Forward mapping Texture coordinate(s,t)geometric coordinate(x,y,z,w)Such mapping maybe hard to be built in practice Backward mapping Texture coordinate(s,t)geometric coordinate(x,y,z,w)s=s(x,y,z,w),t=t(x,y,z,w)Game Programming IIGame Programming II Forward and Backward Mapping Two approaches:Mapping texture onto parametric surface Two-part mapping Game Programming IIGame Programming II Solutions to Mapping Problem The 3D coordinates of surface point p is given in terms of some parameters u,v Game Programming IIGame Programming II Parametric Surface Map texel coordinates(s,t)onto the surface parameters(u,v)Often use linear map Simple If ae!=bd,this mapping is invertible;May distort the texture shape Game Programming IIGame Programming II Mapping Texture First Step(S Mapping)Mapping texture to a simple 3D intermediate surface(Cylinder,box,sphere)Second Step(O Mapping)Mapping texture values on the intermediate object to the desired surface Game Programming IIGame Programming II Two-Part Mapping Parametric representation Maps rectangle in u,v space to cylinder of radius r and height h in world coordinates Map from(u,v)to(s,t)Game Programming IIGame Programming II S Mapping Example:Cylinder Easy to use with simple orthographic projection Also used in environmental maps Game Programming IIGame Programming II S Mapping Example:Box Parametric representation In a similar manner to the cylinder but have to decide where to put the distortion Spheres are used in environmental maps Game Programming IIGame Programming II S Mapping Example:Sphere Three possible strategies Intermediate surface normal Object normal Object centroid Game Programming IIGame Programming II O Mapping May introduce undesired texture distortions if the intermediate surface differs much from the destination surface Game Programming IIGame Programming II Problem of Two-Part Mapping A single screen pixel can correspond to anything from a tiny portion of a texel(magnification)to a large collection of texels(minification)Point Sampling vs.Area Averaging Game Programming IIGame Programming II Aliasing Problem Game Programming IIGame Programming II Artifacts in Point Sampling Texture image Game Programming IIGame Programming II Another Example A better but slower option is to use area averaging Game Programming IIGame Programming II Area Averaging Pixel size texel size Game Programming IIGame Programming II Minification Game Programming IIGame Programming II Mip Mapping Game Programming IIGame Programming II Applying Textures Three steps Specify texture Read or generate image Assign to texture Enable texturing Assign texture coordinates to vertices Specify texture parameters Warping,filtering Game Programming IIGame Programming II Applying Textures Detailed steps specify textures in texture objects set texture filter set texture function set texture wrap mode set optional perspective correction hint bind texture object enable texturing supply texture coordinates for vertex coordinates can also be generated One image per texture object Generate texture names glGenTextures(GLsizei n,GLuint*textures);Returns n texture names in the textures parameter Texture names are not necessarily a contiguous set of integers Game Programming IIGame Programming II Texture Objects Create texture objects with texture data and state glBindTexture(GLenum target,GLuint texture);Bind textures before using glBindTexture(GLenum target,GLuint texture);Game Programming IIGame Programming II Texture Objects Define a texture image from an array of texels in CPU memory glTexImage2D(GLenum target,GLint level,GLint internalformat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid*pixels );Game Programming IIGame Programming II Specify Texture Image Contains 1-4 components(s,t,r,q)The range of each component is normally 0,1 glTexCoord*()specified at each vertex glTexCoord*()has to be called in a pair of glBegin()/glEnd()Game Programming IIGame Programming II Texture Coordinates s t 1,1 0,1 0,0 1,0(s,t)=(0.2,0.8)(0.4,0.2)(0.8,0.4)A B C a b c Texture Space Object Space OpenGL has a variety of parameters that determine how texture is applied Wrapping parameters determine what happens if s and t are outside the(0,1)range Filter modes allow us to use area averaging instead of point samples Mipmapping allows us to use textures at multiple resolutions Environment parameters determine how texture mapping interacts with shading Game Programming IIGame Programming II Texture Parameters glTexParameter(GLenum target,GLenum pname,Type param)target:GL_TEXTURE_1D,GL_TEXTURE2D,GL_TEXTURE_3D pname:name of texture parameter param:value of texture parameter Game Programming IIGame Programming II Specifying Texture Parameters Wrap mode determines what should happen if a texture coordinate lies outside of the 0,1 range Warp mode can be configured for each component of texture coordinate separately Two modes in OpenGL GL_CLAMP:value at the extreme(either 0 or 1)is used GL_REPEAT:the integer is ignored and only the fractional value is used Game Programming IIGame Programming II Wrapping Mode How to sample texture images to avoid artifacts?Two different situations Magnification:One texel covers multiple pixels Nearest sampling Bilinear sampling Minification:One pixel covers multiple texels Game Programming IIGame Programming II Filter Modes Texture Polygon Magnification Minification Polygon Texture Mipmap allows for prefiltered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Declare mipmap level during texture definition glTexImage*D(GL_TEXTURE_*D,level,)Game Programming IIGame Programming II Mipmapped Textures Specifying magnification or minification GL_TEXTURE_MAG_FILTER GL_TEXTURE_MIN_FILTER Specifying filter modes GL_NEAREST GL_LINEAR Mipmap(used for GL_TEXTURE_MIN_FILTER only)Using the same level GL_NEAREST_MIPMAP_NEAREST GL_LINEAR_MIPMAP_NEAREST Using two levels GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_LINEAR Game Programming IIGame Programming II Specifying Filter Mode How texels and fragment colors are combined?glTexEnvfiv(GLenum target,GLenum pname,Type param)target:Must be GL_TEXTURE_ENV pname:Name of texture environment parameter.Accepted values are GL_TEXTURE_ENV_MODE,GL_TEXTURE_ENV_COLOR param GL_MODULATE:multiply texel and fragment color GL_BLEND:linearly blend texel,fragment,env color GL_REPLACE:replace fragments color with texel Game Programming IIGame Programming II Texture Environment Automatically generate texture coordinates glEnable(GL_TEXTURE_GEN_STRQ)glTexGenifdv(GLenum coord,GLenum pname,TYPE param)Configure the generation mode for s,t,r,q coordinates separately Generation modes GL_OBJECT_LINEAR GL_EYE_LINEAR GL_SPHERE_MAP Game Programming IIGame Programming II Texture Coordinates Generation Query largest dimension of texture image typically largest square texture doesnt consider internal format size glGetIntegerv(GL_MAX_TEXTURE_SIZE,&size)Texture proxy will memory accommodate requested texture size?no image specified;placeholder if texture wont fit,texture state variables set to 0 doesnt know about other textures only considers whether this one texture will fit all of memory Game Programming IIGame Programming II Is There Room for a Texture?Tricking eye into seeing smooth animation of rendered scenes Two buffers:front buffer and back buffer The front buffer is displayed while the application renders into the back buffer When application completes rendering,it requests hardware to swap the roles of the buffers Game Programming IIGame Programming II Double Buffering Request a double buffered color buffer glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);Clear color buffer Render scene Request swap of front and back buffer glutSwapBuffers();Game Programming IIGame Programming II Double Buffering Determine which primitives in scene are occluded by other primitives Store pixels depth values(distance from camera)in depth buffer Only when a pixels depth is less than the stored value,its depth and color is written to buffers Depth value range from 0,1 After perspective projection Game Programming IIGame Programming II Depth Buffer if(pixel-z z)depthBuffer(x,y)-z=pixel-z;colorBuffer(x,y)-color=pixel-color;Incoming fragments have to pass the depth test before they are wrote to buffers Enable depth test:glEnable(GL_DEPTH_TEST)Determine whether writing depth value to buffer if fragment passes depth test glDepthMask(GLboolean flag)Game Programming IIGame Programming II Depth Test 1/2 Specify depth comparison function glDepthFunc(GLenum func)Game Programming IIGame Programming II Depth Test 2/2 Buffers are often cleared at the start of a frame The clear value can be specified by glClearColor(),glClearDepth()Clear the specified buffers by glClear()Game Programming IIGame Programming II Clearing Buffers Fragments generated in rasterization stage have to pass a serious of tests before wrote to buffers Fragment process and test Alpha test Depth test Alpha blending Game Programming IIGame Programming II Fragment Test Measure of opacity Valid range:0,1 Value 0:totally transparent Value 1:totally opaque Usages Simulating translucent objects like glass,water,etc.Blending and compositing images Antialiasing geometric primitives Game Programming IIGame Programming II Alpha:the 4th Color Component Reject pixels based on their alpha value Enable alpha test:glEnable(GL_ALPHA_TEST)Specify alpha function glAlphaFunc(GLenum func,GLclampf ref)Game Programming IIGame Programming II Alpha Test Cloud rendering in computer games A popular solution:texture mapping+billboard+alpha test Similar technique is adopted in plants rendering Game Programming IIGame Programming II Alpha Test:Application Check a cool picture Game Programming IIGame Programming II Alpha Blending Blend color of incoming fragment with value stored in color buffer,and write the blended color back to buffer Enable alpha blending:glEnable(GL_BLEND)How to blend?Source:incoming RGB Destination:RGB that are already in the framebuffer Game Programming IIGame Programming II Alpha Blending in OpenGL 1/2 Ccomposite=src*Csrc+dst*Cdst Specify the blending factor glBlendFunc(GLenum sfactor,GLenum dfactor)Source factor:GL_ZERO,GL_ONE,GL_DST_COLOR,GL_ONE_MINUS_DST_COLOR,GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_DST_ALPHA,GL_ONE_MINUS_DST_ALPHA,and GL_SRC_ALPHA_SATURATE Destination factor:GL_ZERO,GL_ONE,GL_SRC_COLOR,GL_ONE_MINUS_SRC_COLOR,GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_DST_ALPHA,and GL_ONE_MINUS_DST_ALPHA Game Programming IIGame Programming II Alpha Blending in OpenGL 2/2 Render several transparent objects Steps Objects have to been rendered from back to front in camera space Sort objects before rendering How to implement it?Blending factor Source factor:GL_SRC_ALPHA Destination factor:GL_ONE_MINUS_SRC_ALPHA Game Programming IIGame Programming II Application 1/2 Render several objects and opaque objects together Steps:Render all opaque objects Depth test has to be enabled Render transparent objects Render objects from back to front in camera space Keep depth test on but disable depth write Blending factor Source factor:GL_SRC_ALPHA Destination factor:GL_ONE_MINUS_SRC_ALPHA Game Programming IIGame Programming II Application 2/2 Target:Render several objects and opaque objects together Render 5 teapots 4 transparent teapots locating at the corner of a square 1 opaque teapot in the center of square Deadline:2011-11-6 Please submit your assignment to teach assistants by email Game Programming IIGame Programming II Assignment