CSCI E-74. Simulation and Gaming

Similar documents
CSCI E-74. Simulation and Gaming

CSCI E-74. Simulation and Gaming

CSCI E-74. Simulation and Gaming

OpenJSGL Web 3D Rendering Without Plug-Ins Andrés Buriticá Loyola Marymount University Faculty Mentor: John David N. Dionisio

Methodology for Lecture. Importance of Lighting. Outline. Shading Models. Brief primer on Color. Foundations of Computer Graphics (Spring 2010)

Computer Graphics. Illumination and Shading

5.2 Shading in OpenGL

Objectives. Introduce the OpenGL shading Methods 1) Light and material functions on MV.js 2) per vertex vs per fragment shading 3) Where to carry out

Shading and Illumination

Computer Graphics Lighting

Computer Graphics Lighting. Why Do We Care About Lighting?

Shading in OpenGL. Outline. Defining and Maintaining Normals. Normalization. Enabling Lighting and Lights. Outline

Deferred Rendering Due: Wednesday November 15 at 10pm

CS Surface Rendering

Ryan Marcotte CS 499 (Honours Seminar) March 16, 2011

Transformation Pipeline

CSE 167: Lecture #8: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Surface Rendering Methods

Computer graphics MN1

Why we need shading?

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

Input Nodes. Surface Input. Surface Input Nodal Motion Nodal Displacement Instance Generator Light Flocking

Introduction to OpenGL

CGDD 4113 Final Review. Chapter 7: Maya Shading and Texturing

Shading/Texturing. Dr. Scott Schaefer

Computer Graphics I Lecture 11

Exercise Max. Points Total 90

Advanced Lighting Techniques Due: Monday November 2 at 10pm

Overview. Shading. Shading. Why we need shading. Shading Light-material interactions Phong model Shading polygons Shading in OpenGL

Virtual Environments

Full Screen Layout. Main Menu Property-specific Options. Object Tools ( t ) Outliner. Object Properties ( n ) Properties Buttons

CEng 477 Introduction to Computer Graphics Fall

CSE 167: Lecture #8: Lighting. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

3DS Format (.3ds) If you have any additions or comments to this file please me.

Scene Graphs. CS4620/5620: Lecture 7. Announcements. HW 1 out. PA 1 will be out on Wed

Virtual Reality for Human Computer Interaction

6.837 Introduction to Computer Graphics Assignment 5: OpenGL and Solid Textures Due Wednesday October 22, 2003 at 11:59pm

Today s class. Simple shadows Shading Lighting in OpenGL. Informationsteknologi. Wednesday, November 21, 2007 Computer Graphics - Class 10 1

Illumination Model. The governing principles for computing the. Apply the lighting model at a set of points across the entire surface.

CS4202: Test. 1. Write the letter corresponding to the library name next to the statement or statements that describe library.

Parametric description

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

蔡侑庭 (Yu-Ting Tsai) National Chiao Tung University, Taiwan. Prof. Wen-Chieh Lin s CG Slides OpenGL 2.1 Specification

Illumination and Shading

CS451Real-time Rendering Pipeline

Ch 10: Game Event Management. Quiz # 4 Discussion

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

Graphics Hardware and OpenGL

Topic 9: Lighting & Reflection models 9/10/2016. Spot the differences. Terminology. Two Components of Illumination. Ambient Light Source

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

CS 130 Final. Fall 2015

CSE4030 Introduction to Computer Graphics

QUESTION 1 [10] 2 COS340-A October/November 2009

3D GRAPHICS. design. animate. render

CS 432 Interactive Computer Graphics

Topic 9: Lighting & Reflection models. Lighting & reflection The Phong reflection model diffuse component ambient component specular component

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

Maya tutorial. 1 Camera calibration

CSE 167: Introduction to Computer Graphics Lecture #10: View Frustum Culling

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

Reading. Hierarchical Modeling. Symbols and instances. Required: Angel, sections , 9.8. Optional:

Chapter 3: Modeling Transformation

Notes on Computer Graphics and OpenGL. Thomas Strathmann

CHAPTER 1 Graphics Systems and Models 3

Classic Rendering Pipeline

Illumination and Shading

CSE 167: Introduction to Computer Graphics Lecture #7: Lights. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2015

GUERRILLA DEVELOP CONFERENCE JULY 07 BRIGHTON

Visualizing Molecular Dynamics

Surface Graphics. 200 polys 1,000 polys 15,000 polys. an empty foot. - a mesh of spline patches:

Shaders. Slide credit to Prof. Zwicker

Virtual Reality for Human Computer Interaction

Graphics Programming

ttp://

Graphics for VEs. Ruth Aylett

Homework #2 and #3 Due Friday, October 12 th and Friday, October 19 th

Basic Graphics Programming

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

1.2 Basic Graphics Programming

CS Simple Raytracer for students new to Rendering

GL_MODELVIEW transformation

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

Modeling with Transformations

CSC 240 Computer Graphics. Fall 2015 Smith College

CMSC427 Final Practice v2 Fall 2017

- Location: Annenberg Text: Mostly Self-Contained on course Web pages. - Al Barr

CS 591B Lecture 9: The OpenGL Rendering Pipeline

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

CS5620 Intro to Computer Graphics

CSE 167: Introduction to Computer Graphics Lecture #6: Lights. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2014

Illumination and Reflection in OpenGL CS 460/560. Computer Graphics. Shadows. Photo-Realism: Ray Tracing. Binghamton University.

Computer graphics Labs: OpenGL (1/3) Geometric transformations and projections

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Foundations of Computer Graphics (Spring 2012)

CSE Intro to Computer Graphics. ANSWER KEY: Midterm Examination. November 18, Instructor: Sam Buss, UC San Diego

CS 130 Exam I. Fall 2015

Rendering. Illumination Model. Wireframe rendering simple, ambiguous Color filling flat without any 3D information

Open Game Engine Exchange Specification

Transcription:

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