3D游戏编程OpenGL纹理和缓存.pdf
《3D游戏编程OpenGL纹理和缓存.pdf》由会员分享,可在线阅读,更多相关《3D游戏编程OpenGL纹理和缓存.pdf(59页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、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
2、 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 g
3、eometric 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
4、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
5、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 af
6、fect 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
7、 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 tex
8、ture 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 coord
9、inate(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
10、 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,thi
11、s 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 Gam
12、e 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 A
13、lso 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:Sph
14、ere 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
15、 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 Po
16、int 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
17、 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 Det
18、ailed 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 te
19、xture 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,
20、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 bo
21、rder,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 Program
22、ming 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 mo
23、des 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,Typ
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 游戏 编程 OpenGL 纹理 和缓
限制150内