Difference between revisions of "Team Guardian Cube Mapping"

From CDOT Wiki
Jump to: navigation, search
 
Line 1: Line 1:
 +
<div class="WordSection1">
 +
 
Theory behind the Material Class:
 
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.
+
-<span style="font: 7.0pt &quot;Times New Roman&quot;">          </span>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.
+
-<span style="font: 7.0pt &quot;Times New Roman&quot;">          </span>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
  
1)      Download and include the iMaterial.h, Material.h and Material.cpp into your project.
+
1)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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
+
2)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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
+
3)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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:
+
4)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>Object is now going to need some adjustments.  I’ll break this down into a few components:
  
·         iObject.h
+
<font face="Symbol">·<span style="font: 7.0pt &quot;Times New Roman&quot;">         </span></font>iObject.h
  
I.      Add ‘class iMaterial’ to the top of the file with the rest of the declarations
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                              </span>I.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                            </span>II.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                          </span>III.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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
+
<font face="Symbol">·<span style="font: 7.0pt &quot;Times New Roman&quot;">         </span></font>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)
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                              </span>I.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                            </span>II.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                          </span>III.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                          </span>IV.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                            </span>V.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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
+
<font face="Symbol">·<span style="font: 7.0pt &quot;Times New Roman&quot;">         </span></font>Object.cpp
  
I.      Add ‘#include iMaterial.h’ at the top.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                              </span>I.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                            </span>II.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                          </span>III.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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)
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                          </span>IV.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                            </span>V.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                          </span>VI.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
<span style="font: 7.0pt &quot;Times New Roman&quot;">                                                        </span>VII.<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
5)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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.
+
6)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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. <br /> 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.
+
7)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>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 63: Line 65:
 
To use a material, follow these few steps:
 
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.
+
1)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>Create a Material:<br />''iMaterial* mat = CreateMaterial(contextPointer, numberOfStages, numberOfSubsets);<br />''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.
+
2)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>Setup the material:<br />''mat->setup(arrayOfBasicColors, twoDimensionalTextureArray, arrayOfColorsRepresentingReflection, arrayOfRefractionPowers, twoDimensionalArrayOfColorsRepresentingRefraction, ObjectShine, ObjectFlags, ObjectBorderColor);''<br /> 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.
+
3)<span style="font: 7.0pt &quot;Times New Roman&quot;">      </span>Set the material onto the object:<br />''object->setMaterial(mat);''<br /> 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);_
+
<br />''iObject* boxy = CreateBox(-10,-10, -10, 10, 10, 10, “opaque”, Colour(1.0f, 1.0f, 1.0f,1.0f);<br /> iMaterial* mat = CreateMaterial(context);<br /> float power = 0.9f; //this is just to sidestep the need for an array<br /> 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));<br /> 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.
 +
 +
</div>

Latest revision as of 06:05, 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.