CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming Fall term 2017 Gianluca De Novi, PhD Lesson 5 Basic Lighting and Materials
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 TObject TObject Vectors Vertices Segments Vectors Vertices Segments Vectors Vertices Segments Materials Materials Materials Cameras Motion Data Lights CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 3
Data Hierarchy Object Surfaces Polygons Vectors Vertices Segments Character : Object Surfaces Polygons Vectors Vertices Segments Skeleton Motion Data Materials CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 4
OpenGL Lighting Enable the OpenGL Lighting void glenable(gl_lighting); Enables the light number i void glenable(gl_lighti); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 5
OpenGL Lighting gllightfv(gl_light0+lindex,gl_ambient, Ambient); gllightfv(gl_light0+lindex,gl_diffuse, Diffuse); gllightfv(gl_light0+lindex,gl_specular,specular); //Ambient Light //Diffuse Light //Specular Diffuse Specular Ambient CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 6
OpenGL Lighting gllightfv(gl_light0+lindex,gl_position, Position); //position of the light gllightfv(gl_light0+lindex,gl_spot_direction, Direction); //direction of the center of the light projection gllightfv(gl_light0+lindex,gl_spot_cutoff,&cutoff); // cone cutoff angle gllightfv(gl_light0+lindex,gl_spot_exponent,&exponent); // spot profile gllightfv(gl_light0+lindex,gl_constant_attenuation,&constantattenuation); // constant attenuation factor gllightfv(gl_light0+lindex,gl_linear_attenuation,&linearattenuation); // linear attenuation factor gllightfv(gl_light0+lindex,gl_quadratic_attenuation,&quadraticattenuation); // quadratic attenuation factor CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 7
OpenGL Lighting Enable the OpenGL Lighting void TLight::EnableLight(int enable) if(enable) glenable(gl_light0+index); else gldisable(gl_light0+index); Class TLight int Index; float Ambient[4]; float Diffuse[4]; float Specular[4]; float Position[3]; float Direction[3]; float Specular[4]; float Cutoff); float SpotProfile; float ConstantAttenuation; float LinearAttenuation; float QuadraticAttenuation; TMatrix4x4 T; //Model View Matrix void EnableLight(int enable); void SetAmbientColor(float r, float g, float b, float a); void BuildMatrix(); ; void TLight::SetAmbientColor(float r, float g, float b, float a) Ambient[0]=r; Ambient[1]=g; Ambient[2]=b; Ambient[3]=a; gllightfv(gl_light0+lindex,gl_ambient, Ambient); void Tlight::BuildMatrix() glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(position[0],position[1],position[2]); gllightfv(gl_light0+lightid,gl_position,l->position); gllightfv(gl_light0+lightid,gl_spot_direction,l->direction); glgetfloatv( GL_MODELVIEW_MATRIX, TR); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 8
Flat Shading void glshademodel(gl_flat); L( l n) = ( l n) if l n 0 0 l n 1 0 0 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 9
Flat Shading glbegin(gl_triangles); glnormal3f(p[i].n.x, p[i].n.y, p[i].n.z); glvertex3f(vlist[p[i].v1].x, Vlist[p[i].v1].y, Vlist[p[i].v1].z); glvertex3f(vlist[p[i].v2].x, Vlist[p[i].v2].y, Vlist[p[i].v2].z); glvertex3f(vlist[p[i].v3].x, Vlist[p[i].v3].y, Vlist[p[i].v3].z); glend(); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 10
Smooth Shading void glshademodel(gl_smooth); L( l n) = ( l n) if l n 0 0 n n v v = n 1 + n 2 n 1 n 2 l n Normals interpolation CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 11
Smooth Shading glbegin(gl_triangles); glnormal3f(nlist[p[i].v1].x, Nlist[p[i].v1].y, Nlist[p[i].v1].z); glvertex3f(vlist[p[i].v1].x, Vlist[p[i].v1].y, Vlist[p[i].v1].z); glnormal3f(nlist[p[i].v2].x, Nlist[p[i].v2].y, Nlist[p[i].v2].z); glvertex3f(vlist[p[i].v2].x, Vlist[p[i].v2].y, Vlist[p[i].v2].z); glnormal3f(nlist[p[i].v3].x, Nlist[p[i].v3].y, Nlist[p[i].v3].z); glvertex3f(vlist[p[i].v3].x, Vlist[p[i].v3].y, Vlist[p[i].v3].z); glend(); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 12
OpenGL Materials 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; void LoadDefaultMaterial(); void LoadMaterial(); ; void TMaterial::LoadDefaultMaterial() Ambient [0]=0.0f; Ambient [1]=0.0f; Ambient [2]=0.0f; Ambient [3]=1.0f; Diffuse [0]=0.0f; Diffuse [1]=0.0f; Diffuse [2]=0.0f; Diffuse [3]=1.0f; Specular[0]=0.0f; Specular[1]=0.0f; Specular[2]=0.0f; Specular[3]=1.0f; Emissive[0]=0.0f; Emissive[1]=0.0f; Emissive[2]=0.0f; Emissive[3]=1.0f; Color[0]=0.0f; Color[0]=0.0f; Color[0]=0.0f; Color[0]=1.0f; Smoothing = 0; Visible = 1; Doublesided = 0; Wireframe = 0; Visible = 1; void TMaterial::LoadMaterial() glmaterialfv(gl_front_and_back,gl_ambient,ambient); glmaterialfv(gl_front_and_back,gl_diffuse,diffuse); glmaterialfv(gl_front_and_back,gl_specular,specular); glmaterialfv(gl_front_and_back,gl_emission,emissive); glmaterialfv(gl_front_and_back,gl_shininess,&shininess); if(doublesided) gldisable(gl_cull_face); else glenable (GL_CULL_FACE); if(wireframe) else if(smoothing) else glpolygonmode(gl_front_and_back, GL_LINE); glpolygonmode(gl_front_and_back, GL_FILL); glshademodel(gl_smooth); glshademodel(gl_flat); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 13
Object Class class TObject public: TPose Pose; //Object Pose Tvector Scale; //Object Scale TMatrix4x4 TRS; //Model View Matrix TMaterial Mlist[ ]; //List of available materials ; unsigned int MListLen; //Number of materials available TSurface SList[ ]; //Surfaces of the Object unsigned int SListLen; //Number of Surfaces int Visible; //Visibility Flag void CalcNormals(); //calculate Normal void BuildjMatrix(); //Builds the TRS Matrix void Draw(); //draw void TObject::CalcNormal() int i; for(i=0;i<slistlen;i++) Slist[i].CalcNormal(); void TObject::Draw() if(visible) int i; for(i=0;i<slistlen;i++) Slist[i].Draw(); void TObject:: BuildMatrix() glloadidentity(); // loads the identity matrix gltranslatef(pose.x,pose.y,pose.z); // translate the object to the desired position glrotatef (Pose.yaw,0.0f,1.0f,0.0f); // rotation around Y axis glrotatef (Pose.pitch,1.0f,0.0f,0.0f); // rotation around X axis glrotatef (Pose.roll,0.0f,0.0f,1.0f); // rotation around Z axis glscalef (Scale.x,Scale.y,Scale.z); // Scale object glgetfloatv( GL_MODELVIEW_MATRIX, TRS); For example NB: This model doesn t consider the vertex optimization CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 14
Surface Class class TSurface public: TVertex VList[ ]; //Vertices on the surface unsigned int VListLen; //Number of Vertices TVector NList[ ]; //Normals on the surface TPolygon Plist[ ]; //Polygons on the surface unsigned int PListLen; //Number of Polygons on the surface TMaterial * Material; //Surface color int Visible; //Visibility Flag ; void CalcNormals(); //calculate Normal void Flip(); //flip the normal orientation void Draw(); //draw //assigns the material for the surface void SetSurfaceMaterial(TMaterial * M); void TSurface::Flip() int i; for(i=0;i<plistlen;i++) Plist[i].Flip(); void TSurface::SetSurfaceMaterial(TMaterial * M); Material = M; for(i=0;i<plistlen;i++) Plist[i].Material = M; void TSurface::Draw() if(visible && Material->Visible) int i; Material->LoadMaterial(); for(i=0;i<plistlen;i++) Plist[i].Draw(); NB: This model doesn t consider the vertex optimization CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 15
Surface Class class TSurface public: TVertex VList[ ]; //Vertices on the surface unsigned int VListLen; //Number of Vertices TVector NList[ ]; //Normals on the surface TPolygon PList[ ]; //Polygons on the surface unsigned int PListLen; //Number of Polygons on the surface TMaterial * Material; //Surface color int Visible; //Visibility Flag ; void CalcNormals(); //calculate Normal void Flip(); //flip the normal orientation void Draw(); //draw //assigns the material for the surface void SetSurfaceMaterial(Tmaterial * M); void TSurface::CalcNormal() int i,j; // calculate normals per polygon for(i=0;i<plistlen;i++) Plist[i].CalcNormal(); // calculate normals per vertex Tvector N; N.vector(0,0,0); for(i=0;i<vlistlen;i++) for(j=0;j<plistlen;j++) if(plist[j].v1==i PList[j].v2==i PList[j].v3==i) N+=PList[j].Normal; N.Normalize(); NB: This model doesn t consider the vertex optimization CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 16
Polygon Class class TPolygon public: int v1,v2,v3; //indices to the 3 vertices int Visible; //visibility flag TMaterial* Material; //Material Pointer TVector Normal; //Normal vector TVertex* Vlist; //List of Vertices TVector* Nlist; //List of Normal per Vertex void CalcNormal(); //calculate Normal void Flip(); //flip the normal orientation void Draw(); //draw ; void TPolygon::CalcNormal() Vector t,r,n; t = Vlist[v2]-Vlist[v1]; r = Vlist[v3]-Vlist[v1]; Normal = t^r; Normal.Normalize(); void TPolygon::Flip() int a = v2; v2 = v3; v3 = v2; CalcNormal(); void TPolygon::Draw() if(visible) if(material->smoothing) glbegin(gl_triangles); glnormal3f(nlist[p[i].v1].x, Nlist[p[i].v1].y, Nlist[p[i].v1].z); glvertex3f(vlist[p[i].v1].x, Vlist[p[i].v1].y, Vlist[p[i].v1].z); glnormal3f(nlist[p[i].v2].x, Nlist[p[i].v2].y, Nlist[p[i].v2].z); glvertex3f(vlist[p[i].v2].x, Vlist[p[i].v2].y, Vlist[p[i].v2].z); glnormal3f(nlist[p[i].v3].x, Nlist[p[i].v3].y, Nlist[p[i].v3].z); glvertex3f(vlist[p[i].v3].x, Vlist[p[i].v3].y, Vlist[p[i].v3].z); glend(); else glbegin(gl_triangles); glnormal3f(normal.x, Normal.y, Normal.z); glvertex3f(vlist[v1].x, Vlist[v1].y, Vlist[v1].z); glvertex3f(vlist[v2].x, Vlist[v2].y, Vlist[v2].z); glvertex3f(vlist[v3].x, Vlist[v3].y, Vlist[v3].z); glend(); NB: This model doesn t consider the vertex optimization CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 17
Limitation of the OpenGL Lighting OpenGL Lighting, doesn t create Shadows Expected OpenGL CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 18
Loading a 3DS (3D Studio file) 0x4D4D // Main Chunk 0x0002 // M3D Version 0x3D3D // 3D Editor Chunk 0x4000 // Object Block 0x4100 // Triangular Mesh 0x4110 // Vertices List 0x4120 // Faces Description 0x4130 // Faces Material 0x4150 // Smoothing Group List 0x4140 // Mapping Coordinates List 0x4160 // Local Coordinates System 0x4600 // Light 0x4610 // Spotlight 0x4700 // Camera 0xAFFF // Material Block 0xA000 // Material Name 0xA010 // Ambient Color 0xA020 // Diffuse Color 0xA030 // Specular Color 0xA200 // Texture Map 1 0xA230 // Bump Map 0xA220 // Reflection Map /* Sub Chunks For Each Map */ 0xA300 // Mapping Filename 0xA351 // Mapping Parameters 0xB000 // Keyframer Chunk 0xB002 // Mesh Information Block 0xB007 // Spot Light Information Block 0xB008 // Frames (Start and End) 0xB010 // Object Name 0xB013 // Object Pivot Point 0xB020 // Position Track 0xB021 // Rotation Track 0xB022 // Scale Track 0xB030 // Hierarchy Position int TObject::Load3DSObject (char * filename) unsigned short l_chunk_id; unsigned int l_chunk_length; unsigned char l_char; unsigned short l_qty; unsigned short l_face_flags; FILE * fp = fopen(filename,"rb"); if(fp) while (ftell (fp) < filelength (fileno (fp))) //Loop to scan the whole file fread (&l_chunk_id, 2, 1, fp); //Read the chunk header fread (&l_chunk_length, 4, 1, fp); //Read the length of the chunk switch (l_chunk_id) case 0x4d4d:... break; case 0x3d3d:... break; case 0x4000:... break;... case 0xB030:... break; CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 19
Scene Class class TScene public: TCamera Camera; TLight LList[ ]; unsigned int LListLen; 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 void Load3DSObject(char * filename); // Load new object ; CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 20
EngineVR Class class TEngineVR public: Tscene SCList[ ]; //Available Scenes unsigned int SCListLen; //Number of Scenes unsigned int CurrentScene; //Selected Scene void InitScene(); //Initializes selected Scene void RenderScene();//draw scene ; Now we can get to this point CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 21