CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming Fall term 2017 Gianluca De Novi, PhD Lesson 7 Multi Texturing
Data Structures in a 3D Engine Vertices/Vectors Segments Matrices Polygons Surfaces Materials Objects (Generic) Super categories of Objects Lights Camera Motion Data Lattices Skeletons Textures Particles CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 2
Data Structure TEngineVR TScene TObject TSurface TPolygon TVertex TObject TSurface TPolygon TVertex TObject TSurface TPolygon TVertex TMaterial TTexture TMaterial TTexture TMaterial TTexture Cameras Motion Data Lights CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 3
Data Hierarchy TObject TextureList TextureListLen TSurface VertexList NormalList TexCoords TMaterial Specular Diffuse Ambient Emissive Shininess Smoothing Doublesided Wireframe Visible TextureLevelList TextureOps TextureIDListLen TTexture Image Data [r,g,b,a] ID Name Width Height Interpolation CromaKey KeyColor CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 4
Texture Drawing Workflow V1(x,y,z) T1(u,v) 0,0 1,0 u V0(x,y,z) T0(u,v) v 0,1 1,1 V2(x,y,z) T2(u,v) Usually square with side lengths power of 2 ( 2x2, 4x4, 8x8, 16x16, 32x32, 64x64, 128x128, 256x256, 512x512, 1024x1024, etc.) CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 5
Textured Operations Avg + CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 6
Textured Levels Combination glenable(gl_texture_2d); for(j=0;j<materiallist[material].texturelevellistlen;j++) { glbindtexture(gl_texture_2d, Material.Texture); gltexenvf(gl_texture_env,gl_texture_env_mode, EnvMode); glenable(gl_blend); glblendfunc(blendsrc, BlendDst); glbegin(gl_triangles); glnormal3f(n.x, n.y, n.z); gltexcoord2f(t0.u, t0.v); glvertex3f(v0.x, v0.y, v0.z); gltexcoord2f(t1.u, t1.v); glvertex3f(v1.x, v1.y, v1.z); gltexcoord2f(t2.u, t0.v); glvertex3f(v2.x, v2.y, v2.z); glend(); gldisable(gl_blend); } gldisable(gl_texture_2d); V0(x,y,z) T0(u,v) V1(x,y,z) T1(u,v) The combination of these parameters determine the final operation and color of the texel This is not an optimized approach, there are hardware supported methods for multi texturing V2(x,y,z) T2(u,v) CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 7
Blending Operation glblendfunc(blendsrc, BlendDst); Final Color = Source Color * Source Factor + Destination Color * Destination Factor CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 8
TMaterials Class typedef struct { unsigned int TextureIndex; unsigned int EnvMode; unsigned int MappingType; unsigned int Blend; unsigned int BlendSrc; unsigned int BlendDst; unsigned int Visible; }TextureLevel; // Texture index in the object // Environment Combination mode // Mapping mode, UV or spherical mapping // Blending enable/disable // Blending Source Factor // Blending Destination Factor // Visible flag class TMaterial { float Ambient [4]; float Specular[4]; float Diffuse [4]; float Emissive[4]; float Color [4]; float Shinines; short Smoothing; short Visible; short Doublesided; short Wireframe; short Visible; TextureLevel TextureLevelList[ ]; unsigned int TextureLevelListLen; void LoadDefaultMaterial(); void LoadMaterial(); int AddTextureLevel(unsigned int TexID, unsigned int Operation); void RemoveTextureLevel(unsigned int Level); }; CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 9
Texture Coordinates UV Can different texture level use different UV coordinate sets? Yes it is possible to use different UV sets The majority of the 3D engines use only one set UV and the other are dynamically generated like spherical or cube mapping. CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 10
Method 5: Cube Mapping It represents an improved version of the reflection spherical mapping The cube map can be updated in real-time CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 11
Examples of Materials with Multi-texturing It represents an improved version of the reflection spherical mapping Mapping Type : UV Blend : OFF Blend Src:-- Blend Dst:-- Mapping Type : Spherical Blend : ON Blend Src: SRC COLOR Blend Dst: 1-SRC COLOR Gloss Gray Plastic CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 12
Examples of Materials with Multi-texturing It represents an improved version of the reflection spherical mapping Mapping Type : UV Blend : OFF Blend Src:-- Blend Dst:-- Mapping Type : Spherical Blend : ON Blend Src: SRC COLOR Blend Dst: DST COLOR Chrome CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 13
Examples of Materials with Multi-texturing It represents an improved version of the reflection spherical mapping Mapping Type : UV Blend : OFF Blend Src:-- Blend Dst:-- Mapping Type : Spherical Blend : ON Blend Src: SRC COLOR Blend Dst: DST COLOR Mapping Type : UV Blend : ON Blend Src: SRC COLOR Blend Dst: 1-SRC ALPHA Rusted Chrome CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 14
Examples of Materials with Multi-texturing It represents an improved version of the reflection spherical mapping Mapping Type : UV EnvMode : Blend Blend : OFF Blend Src:-- Blend Dst:-- Mapping Type : Spherical EnvMode : Blend Blend : ON Blend Src: SRC COLOR Blend Dst: DST COLOR Mapping Type : UV Blend : ON Blend Src: SRC ALPHA Blend Dst: 1-SRC ALPHA Oxidized Copper CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 15
Examples of Materials with Multi-texturing It represents an improved version of the reflection spherical mapping Mapping Type : UV Blend : OFF Blend Src:-- Blend Dst:-- Mapping Type : Spherical Blend : ON Blend Src: SRC COLOR Blend Dst: 1-SRC COLOR Mapping Type : UV Blend : ON Blend Src: SRC COLOR Blend Dst: 1-SRC COLOR Wood CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 16
Background Texture In some scenes it might be nice to have a background image, how is it done? Draw a polygon of the size of the viewport Use a texture Disable the Depth Test CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 17
Background Texture glmatrixmode(gl_projection); glpushmatrix(); glloadidentity(); glortho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); glmatrixmode(gl_modelview); glpushmatrix(); glloadidentity(); gldisable(gl_lighting); gldisable(gl_depth_test); gldisable(gl_color_material); glenable(gl_texture_2d); glbindtexture(gl_texture_2d,backgroundtexture); gltexenvi(gl_texture_env,gl_texture_env_mode,gl_replace); glbegin(gl_quads); gltexcoord2f(0,0); glvertex2f(0,0); gltexcoord2f(1,0); glvertex2f(1,0); gltexcoord2f(1,1); glvertex2f(1,1); gltexcoord2f(0,1); glvertex2f(0,1); glend(); gldisable(gl_texture_2d); glmatrixmode(gl_projection); glpopmatrix(); glmatrixmode(gl_modelview); glpopmatrix(); Save and re-initialize the Projection Matrix Set and orthographic matrix for the clipping planes Save and re-initialize the Model View Matrix Disables Lighting, Depth Test and Colors Selects a texture Use replacement settings Draws a 2D quad with the view coordinate Pops the matrices Re-enable the depth test glenable(gl_depth_test); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 18
Scene Class class TScene { public: Tcamera Camera; TObject OList[ ]; //List of objects unsigned int OListLen; //Number of objects int Visible; //Visibility Flag }; void InitScene(); //Initialize Scene Geometry void BuildSceneMatrices();//builds the scene matrices; void Draw(); //draw scene unsigned int EnableBkgTexture; TTexture Background; void DrawBackground(); void TScene::DrawBackground() { glmatrixmode(gl_projection); glpushmatrix(); glloadidentity(); glortho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); glmatrixmode(gl_modelview); glpushmatrix(); glloadidentity(); } gldisable(gl_lighting); gldisable(gl_depth_test); gldisable(gl_color_material); glenable(gl_texture_2d); glbindtexture(gl_texture_2d,background.texture); gltexenvi(gl_texture_env,gl_texture_env_mode,gl_replace); //gltexenvf(gl_texture_env,gl_texture_env_color,gl_replace) glcolor4f(1.0f,1.0f,1.0f,1.0f); glbegin(gl_quads); gltexcoord2f(0,0); glvertex2f(0,0); gltexcoord2f(1,0); glvertex2f(1,0); gltexcoord2f(1,1); glvertex2f(1,1); gltexcoord2f(0,1); glvertex2f(0,1); glend(); gldisable(gl_texture_2d); glmatrixmode(gl_projection); glpopmatrix(); glmatrixmode(gl_modelview); glpopmatrix(); glenable(gl_depth_test); For example NB: This model doesn t consider the vertex optimization CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 19
Rendering inside a Texture Is it possible to transfer the final rendering inside a texture? Move the Camera where desired Set the ViewPort with the same size of the texture Render the frame without flipping the frame buffer Copy the image inside the texture Reset the ViewPort Size to the original one It is mostly used to simulate reflective surfaces CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 20
EngineVR Class class TEngineVR { public: unsigned int ViewPortWidth; unsigned int ViewPortHeight; Tscene SCList[ ]; //Available Scenes unsigned int SCListLen; //Number of Scenes unsigned int CurrentScene; //Selected Scene void InitScene(); //Initializes selected Scene void RenderScene();//draw scene void EngineVR::RenderToTexture(TTexture * t) { if(t && t->texture) { glviewport(0,0,t->width,t->height); ClearBuffers(); RenderScene(); glbindtexture(gl_texture_2d,t->texture); glcopytexsubimage2d(gl_texture_2d,0,0,0,0,0,t->width,t->height); glviewport(0,0,viewportwidth,viewportheight); } } }; void RenderToTexture(TTexture * t); For example NB: This model doesn t consider the vertex optimization CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 21