Difference between revisions of "Team Guardian Cube Mapping"

From CDOT Wiki
Jump to: navigation, search
Line 1: Line 1:
<!--[if gte mso 9]>CloudScorpionCloudScorpion219712011-04-14T09:44:00Z2011-04-14T09:44:00Z3104459544913698512.00<![endif]-->
 
 
<!--[if gte mso 9]>CleanCleanfalsefalsefalsefalseEN-CAX-NONEX-NONEMicrosoftInternetExplorer4<![endif]--><!--[if gte mso 9]><![endif]-->
 
 
<!--[if gte mso 10]>
 
 
<![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 9]><![endif]-->
 
 
 
Theory behind the Material Class:
 
Theory behind the Material Class:
  
<![if !supportLists]>-          <![endif]>The material class was originally supposed to encapsulate all material related data, such as textures, colors, reflectivity etc. The intention was to replace a lot of the functionality of graphic, so that an object encapsulates an in world object, graphic encapsulated a static mesh and material described the appearance of a mesh.  This way you could have a material attached to many different meshes, so a similar appearance can be applied amongst many objects. This is similar to the approach taken by 3DS Max.
+
-          The material class was originally supposed to encapsulate all material related data, such as textures, colors, reflectivity etc. The intention was to replace a lot of the functionality of graphic, so that an object encapsulates an in world object, graphic encapsulated a static mesh and material described the appearance of a mesh.  This way you could have a material attached to many different meshes, so a similar appearance can be applied amongst many objects. This is similar to the approach taken by 3DS Max.
  
<![if !supportLists]>-          <![endif]>This approach proved too invasive for others to implement, so now I have made Material a category, which will shift responsibility of drawing the object away from graphic and into Material.
+
-          This approach proved too invasive for others to implement, so now I have made Material a category, which will shift responsibility of drawing the object away from graphic and into Material.
  
 
How to implement Environmental Cube Mapping
 
How to implement Environmental Cube Mapping
  
<![if !supportLists]>1)      <![endif]>Download and include the iMaterial.h, Material.h and Material.cpp into your project.
+
1)      Download and include the iMaterial.h, Material.h and Material.cpp into your project.
  
<![if !supportLists]>2)      <![endif]>Add the cube map code from the provided FX file into your own FX file. If you will be adding there manually, copy the code between //REF_CUBE_MAP_A# and //END REF_CUBE_MAP_A#, there should be 4 sections to copy in total
+
2)      Add the cube map code from the provided FX file into your own FX file. If you will be adding there manually, copy the code between //REF_CUBE_MAP_A# and //END REF_CUBE_MAP_A#, there should be 4 sections to copy in total
  
<![if !supportLists]>3)      <![endif]>Edit the Configuration.h file  and add “MATERIAL” to the Category enumeration
+
3)      Edit the Configuration.h file  and add “MATERIAL” to the Category enumeration
  
<![if !supportLists]>4)      <![endif]>Object is now going to need some adjustments.  I’ll break this down into a few components:
+
4)      Object is now going to need some adjustments.  I’ll break this down into a few components:
  
<![if !supportLists]>·         <![endif]>iObject.h
+
·         iObject.h
  
<![if !supportLists]>                                                              I.      <![endif]>Add ‘class iMaterial’ to the top of the file with the rest of the declarations
+
I.      Add ‘class iMaterial’ to the top of the file with the rest of the declarations
  
<![if !supportLists]>                                                            II.      <![endif]>Add a virtual getting and setter for the Object’s material
+
II.      Add a virtual getting and setter for the Object’s material
  
<![if !supportLists]>                                                          III.      <![endif]>Add two more virtual functions, one called getID, receives an int and returns an int, and second one called resetID which receives an int and returns nothing.
+
III.      Add two more virtual functions, one called getID, receives an int and returns an int, and second one called resetID which receives an int and returns nothing.
  
<![if !supportLists]>·         <![endif]>Object.h
+
·         Object.h
  
<![if !supportLists]>                                                              I.      <![endif]>Add a private iMaterial pointer to the object named rMaterial (you could change it to w/e you want, but it won’t be as easy to copy/paste code if you don’t)
+
I.      Add a private iMaterial pointer to the object named rMaterial (you could change it to w/e you want, but it won’t be as easy to copy/paste code if you don’t)
  
<![if !supportLists]>                                                            II.      <![endif]>Implement the getter and setter in the in the Object header, in the setter, change the category of the object to MATERIAL.
+
II.      Implement the getter and setter in the in the Object header, in the setter, change the category of the object to MATERIAL.
  
<![if !supportLists]>                                                          III.      <![endif]>Remove the braces after Object’s preDraw() function, we are going to need to implement this function in the cpp.
+
III.      Remove the braces after Object’s preDraw() function, we are going to need to implement this function in the cpp.
  
<![if !supportLists]>                                                          IV.      <![endif]>Add a private int array called get cubeMapID, which for this implementation,  it can be an array of 2 items, since reflection depth will not be implemented.
+
IV.      Add a private int array called get cubeMapID, which for this implementation,  it can be an array of 2 items, since reflection depth will not be implemented.
  
<![if !supportLists]>                                                            V.      <![endif]>Implement getID and resetID, get id returns the int at level in the cubeMapID array (return cubeMapID[level];) and resetID sets the int at level in the cubeMapID to -1.
+
V.      Implement getID and resetID, get id returns the int at level in the cubeMapID array (return cubeMapID[level];) and resetID sets the int at level in the cubeMapID to -1.
  
<![if !supportLists]>·         <![endif]>Object.cpp
+
·         Object.cpp
  
<![if !supportLists]>                                                              I.      <![endif]>Add ‘#includeiMaterial.h’ at the top.
+
I.      Add ‘#include iMaterial.h’ at the top.
  
<![if !supportLists]>                                                            II.      <![endif]>In the object constructor add ‘rMaterial = NULL’, just to be safe, also set cubeMapID [0] and [1] to -1.
+
II.      In the object constructor add ‘rMaterial = NULL’, just to be safe, also set cubeMapID [0] and [1] to -1.
  
<![if !supportLists]>                                                          III.      <![endif]>In the object’s = operator, set CubeMapID[0] and [1] to -1.
+
III.      In the object’s = operator, set CubeMapID[0] and [1] to -1.
  
<![if !supportLists]>                                                          IV.      <![endif]>In the Object’s attach function,  check if the category is set to MATERIAL and that rMaterial isn’t NULL, if true, use Material’s setStage, if category is not set to MATERIALS then use the code already there (Remember, if reading this is confusing, you can always copy the code, this is just trying to explain what to do)
+
IV.      In the Object’s attach function,  check if the category is set to MATERIAL and that rMaterial isn’t NULL, if true, use Material’s setStage, if category is not set to MATERIALS then use the code already there (Remember, if reading this is confusing, you can always copy the code, this is just trying to explain what to do)
  
<![if !supportLists]>                                                            V.      <![endif]>Above the draw function, add the implementation of the preDraw, which is simple. Check if the category is set to MATERIAL and rMaterial isn’t NULL, if true, set the cubeMapID (at index of rMaterial->getDepth()) to dthe result of rMaterial->getCubeID(this). Finally call the preDraw function on rMaterial and pass it a reference to the object (this), vertexlist pointer, and scene pointer.
+
V.      Above the draw function, add the implementation of the preDraw, which is simple. Check if the category is set to MATERIAL and rMaterial isn’t NULL, if true, set the cubeMapID (at index of rMaterial->getDepth()) to dthe result of rMaterial->getCubeID(this). Finally call the preDraw function on rMaterial and pass it a reference to the object (this), vertexlist pointer, and scene pointer.
  
<![if !supportLists]>                                                          VI.      <![endif]>Mod the Object’s draw function to test if the category is set to MATERIAL, if it is and  rMaterial isn’t NULL, call the draw function on the material (passing in the object and scene) in place of using the attach and draw code in the function, the report should occur regardless.
+
VI.      Mod the Object’s draw function to test if the category is set to MATERIAL, if it is and  rMaterial isn’t NULL, call the draw function on the material (passing in the object and scene) in place of using the attach and draw code in the function, the report should occur regardless.
  
<![if !supportLists]>                                                        VII.      <![endif]>In the suspend code, check if rMaterial is NULL, if not, call suspend on it. Do the same for release.
+
VII.      In the suspend code, check if rMaterial is NULL, if not, call suspend on it. Do the same for release.
  
<![if !supportLists]>5)      <![endif]>Open Display.cpp,  #include Material.h and call the static function connectDevice from Material, this will give material access to the device and below, call the static function connectEffect, giving Material access to the effect file.
+
5)      Open Display.cpp,  #include Material.h and call the static function connectDevice from Material, this will give material access to the device and below, call the static function connectEffect, giving Material access to the effect file.
  
<![if !supportLists]>6)      <![endif]>The system requires a knowledge of the vertices of the in a mesh, so you have to go to iVertex list and add two virtual functions,  ‘intgetNumOfVerts()’ and ‘Vertex* getVertitices()’. Now you have to go into the VertexList class, implement these functions to return nVertices and Vertex.  You also need to make an implementation for stock mesh, simply give return NULL for both stock mesh implementations. \\ NOTE: If you already have your object’s center and radius calculated or easily accessible, you can skip this step and alter the code in the Material class to use that data instead of calculating the center from the vertex list.
+
6)      The system requires a knowledge of the vertices of the in a mesh, so you have to go to iVertex list and add two virtual functions,  ‘int getNumOfVerts()’ and ‘Vertex* getVertitices()’. Now you have to go into the VertexList class, implement these functions to return nVertices and Vertex.  You also need to make an implementation for stock mesh, simply give return NULL for both stock mesh implementations. \\ NOTE: If you already have your object’s center and radius calculated or easily accessible, you can skip this step and alter the code in the Material class to use that data instead of calculating the center from the vertex list.
  
<![if !supportLists]>7)      <![endif]>Open Design.cpp and #include iMaterial.h.  Add scene->draw(MATERIAL); to Design’s draw function and scene->draw(MATERIAL); to Design’s preDraw.
+
7)      Open Design.cpp and #include iMaterial.h.  Add scene->draw(MATERIAL); to Design’s draw function and scene->draw(MATERIAL); to Design’s preDraw.
  
 
This will give you the ability to use the Material class with your object to give them a nice reflective or refracting effect.
 
This will give you the ability to use the Material class with your object to give them a nice reflective or refracting effect.
Line 71: Line 63:
 
To use a material, follow these few steps:
 
To use a material, follow these few steps:
  
<![if !supportLists]>1)      <![endif]>Create a Material:\\_iMaterial__* mat = CreateMaterial(contextPointer, numberOfStages, numberOfSubsets);\\_NOTES: if the material only have one stage and subset, only pass in context.
+
1)      Create a Material:\\_iMaterial* mat = CreateMaterial(contextPointer, numberOfStages, numberOfSubsets);\\_NOTES: if the material only have one stage and subset, only pass in context.
  
<![if !supportLists]>2)      <![endif]>Setup the material:\\_mat->setup(arrayOfBasicColors, twoDimensionalTextureArray, arrayOfColorsRepresentingReflection, arrayOfRefractionPowers, twoDimensionalArrayOfColorsRepresentingRefraction, ObjectShine, ObjectFlags, ObjectBorderColor);_\\ NOTE: If you specify only one subset/stage, you can simply put in a pointers to this data, but if you specify more than one subset or stage, you  must implement the data in an array OR pass in NULL. The alpha channel determines how strong the effect of the reflection or refraction will be, and they are both over-riding effects, reflection being the most over-riding of the two.  So if refraction’s alpha channel is 1.0, the object’s color and texture will be completely gone, the object will only have the refracted texture on it. If the alpha channel is 0.5, then it will be half refracted texture, half color/diffuse texture. The same works with reflection except that reflection will over-ride refraction, so if an object is 100% refractive and 100% reflective, you will only see the reflection. Where as if the reflection is 50% and refraction is 50%, the distribution of the texture will be 50% reflective, 25% refractive, 25% diffuse. The refractive power float array dictates how much the light bends, so if it is equal to 1.0, the light will not bend at all, where as if it is set to 0.9, it will bend about 10% of the angle of the normal of the fragment.
+
2)      Setup the material:\\_mat->setup(arrayOfBasicColors, twoDimensionalTextureArray, arrayOfColorsRepresentingReflection, arrayOfRefractionPowers, twoDimensionalArrayOfColorsRepresentingRefraction, ObjectShine, ObjectFlags, ObjectBorderColor);_\\ NOTE: If you specify only one subset/stage, you can simply put in a pointers to this data, but if you specify more than one subset or stage, you  must implement the data in an array OR pass in NULL. The alpha channel determines how strong the effect of the reflection or refraction will be, and they are both over-riding effects, reflection being the most over-riding of the two.  So if refraction’s alpha channel is 1.0, the object’s color and texture will be completely gone, the object will only have the refracted texture on it. If the alpha channel is 0.5, then it will be half refracted texture, half color/diffuse texture. The same works with reflection except that reflection will over-ride refraction, so if an object is 100% refractive and 100% reflective, you will only see the reflection. Where as if the reflection is 50% and refraction is 50%, the distribution of the texture will be 50% reflective, 25% refractive, 25% diffuse. The refractive power float array dictates how much the light bends, so if it is equal to 1.0, the light will not bend at all, where as if it is set to 0.9, it will bend about 10% of the angle of the normal of the fragment.
  
<![if !supportLists]>3)      <![endif]>Set the material onto the object:\\_object->setMaterial(mat);_\\ NOTE: this will convert the object to a material object and attach the material.
+
3)      Set the material onto the object:\\_object->setMaterial(mat);_\\ NOTE: this will convert the object to a material object and attach the material.
  
 
A quick example of how to make a glass like cube would be:
 
A quick example of how to make a glass like cube would be:
  
\\_iObject__* boxy = CreateBox(-10,-10, -10, 10, 10, 10, “opaque”, Colour(1.0f, 1.0f, 1.0f,1.0f);\\iMaterial* mat = CreateMaterial(context);\\ float power = 0.9f; //this is just to sidestep the need for an array\\ mat->setup(&Colour(0.7f, 0.7f, 0.7f, 0.7f), NULL, &Colour(1.0f, 1.0f, 1.0f, 0.25), &power, &Colour(1.0f, 1.0f, 1.0f, 1.0f));\\ boxy->setMaterial(mat);_
+
\\_iObject* boxy = CreateBox(-10,-10, -10, 10, 10, 10, “opaque”, Colour(1.0f, 1.0f, 1.0f,1.0f);\\ iMaterial* mat = CreateMaterial(context);\\ float power = 0.9f; //this is just to sidestep the need for an array\\ mat->setup(&Colour(0.7f, 0.7f, 0.7f, 0.7f), NULL, &Colour(1.0f, 1.0f, 1.0f, 0.25), &power, &Colour(1.0f, 1.0f, 1.0f, 1.0f));\\ boxy->setMaterial(mat);_
  
 
I didn’t test the above code so there could be a typo.
 
I didn’t test the above code so there could be a typo.

Revision as of 05:53, 14 April 2011

Theory behind the Material Class:

-          The material class was originally supposed to encapsulate all material related data, such as textures, colors, reflectivity etc. The intention was to replace a lot of the functionality of graphic, so that an object encapsulates an in world object, graphic encapsulated a static mesh and material described the appearance of a mesh.  This way you could have a material attached to many different meshes, so a similar appearance can be applied amongst many objects. This is similar to the approach taken by 3DS Max.

-          This approach proved too invasive for others to implement, so now I have made Material a category, which will shift responsibility of drawing the object away from graphic and into Material.

How to implement Environmental Cube Mapping

1)      Download and include the iMaterial.h, Material.h and Material.cpp into your project.

2)      Add the cube map code from the provided FX file into your own FX file. If you will be adding there manually, copy the code between //REF_CUBE_MAP_A# and //END REF_CUBE_MAP_A#, there should be 4 sections to copy in total

3)      Edit the Configuration.h file  and add “MATERIAL” to the Category enumeration

4)      Object is now going to need some adjustments.  I’ll break this down into a few components:

·         iObject.h

I.      Add ‘class iMaterial’ to the top of the file with the rest of the declarations

II.      Add a virtual getting and setter for the Object’s material

III.      Add two more virtual functions, one called getID, receives an int and returns an int, and second one called resetID which receives an int and returns nothing.

·         Object.h

I.      Add a private iMaterial pointer to the object named rMaterial (you could change it to w/e you want, but it won’t be as easy to copy/paste code if you don’t)

II.      Implement the getter and setter in the in the Object header, in the setter, change the category of the object to MATERIAL.

III.      Remove the braces after Object’s preDraw() function, we are going to need to implement this function in the cpp.

IV.      Add a private int array called get cubeMapID, which for this implementation,  it can be an array of 2 items, since reflection depth will not be implemented.

V.      Implement getID and resetID, get id returns the int at level in the cubeMapID array (return cubeMapID[level];) and resetID sets the int at level in the cubeMapID to -1.

·         Object.cpp

I.      Add ‘#include iMaterial.h’ at the top.

II.      In the object constructor add ‘rMaterial = NULL’, just to be safe, also set cubeMapID [0] and [1] to -1.

III.      In the object’s = operator, set CubeMapID[0] and [1] to -1.

IV.      In the Object’s attach function,  check if the category is set to MATERIAL and that rMaterial isn’t NULL, if true, use Material’s setStage, if category is not set to MATERIALS then use the code already there (Remember, if reading this is confusing, you can always copy the code, this is just trying to explain what to do)

V.      Above the draw function, add the implementation of the preDraw, which is simple. Check if the category is set to MATERIAL and rMaterial isn’t NULL, if true, set the cubeMapID (at index of rMaterial->getDepth()) to dthe result of rMaterial->getCubeID(this). Finally call the preDraw function on rMaterial and pass it a reference to the object (this), vertexlist pointer, and scene pointer.

VI.      Mod the Object’s draw function to test if the category is set to MATERIAL, if it is and  rMaterial isn’t NULL, call the draw function on the material (passing in the object and scene) in place of using the attach and draw code in the function, the report should occur regardless.

VII.      In the suspend code, check if rMaterial is NULL, if not, call suspend on it. Do the same for release.

5)      Open Display.cpp,  #include Material.h and call the static function connectDevice from Material, this will give material access to the device and below, call the static function connectEffect, giving Material access to the effect file.

6)      The system requires a knowledge of the vertices of the in a mesh, so you have to go to iVertex list and add two virtual functions,  ‘int getNumOfVerts()’ and ‘Vertex* getVertitices()’. Now you have to go into the VertexList class, implement these functions to return nVertices and Vertex.  You also need to make an implementation for stock mesh, simply give return NULL for both stock mesh implementations. \\ NOTE: If you already have your object’s center and radius calculated or easily accessible, you can skip this step and alter the code in the Material class to use that data instead of calculating the center from the vertex list.

7)      Open Design.cpp and #include iMaterial.h.  Add scene->draw(MATERIAL); to Design’s draw function and scene->draw(MATERIAL); to Design’s preDraw.

This will give you the ability to use the Material class with your object to give them a nice reflective or refracting effect.

How to use Material:

To use a material, follow these few steps:

1)      Create a Material:\\_iMaterial* mat = CreateMaterial(contextPointer, numberOfStages, numberOfSubsets);\\_NOTES: if the material only have one stage and subset, only pass in context.

2)      Setup the material:\\_mat->setup(arrayOfBasicColors, twoDimensionalTextureArray, arrayOfColorsRepresentingReflection, arrayOfRefractionPowers, twoDimensionalArrayOfColorsRepresentingRefraction, ObjectShine, ObjectFlags, ObjectBorderColor);_\\ NOTE: If you specify only one subset/stage, you can simply put in a pointers to this data, but if you specify more than one subset or stage, you  must implement the data in an array OR pass in NULL. The alpha channel determines how strong the effect of the reflection or refraction will be, and they are both over-riding effects, reflection being the most over-riding of the two.  So if refraction’s alpha channel is 1.0, the object’s color and texture will be completely gone, the object will only have the refracted texture on it. If the alpha channel is 0.5, then it will be half refracted texture, half color/diffuse texture. The same works with reflection except that reflection will over-ride refraction, so if an object is 100% refractive and 100% reflective, you will only see the reflection. Where as if the reflection is 50% and refraction is 50%, the distribution of the texture will be 50% reflective, 25% refractive, 25% diffuse. The refractive power float array dictates how much the light bends, so if it is equal to 1.0, the light will not bend at all, where as if it is set to 0.9, it will bend about 10% of the angle of the normal of the fragment.

3)      Set the material onto the object:\\_object->setMaterial(mat);_\\ NOTE: this will convert the object to a material object and attach the material.

A quick example of how to make a glass like cube would be:

\\_iObject* boxy = CreateBox(-10,-10, -10, 10, 10, 10, “opaque”, Colour(1.0f, 1.0f, 1.0f,1.0f);\\ iMaterial* mat = CreateMaterial(context);\\ float power = 0.9f; //this is just to sidestep the need for an array\\ mat->setup(&Colour(0.7f, 0.7f, 0.7f, 0.7f), NULL, &Colour(1.0f, 1.0f, 1.0f, 0.25), &power, &Colour(1.0f, 1.0f, 1.0f, 1.0f));\\ boxy->setMaterial(mat);_

I didn’t test the above code so there could be a typo.