Team Guardian Cube Mapping
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head> <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> <meta name=ProgId content=Word.Document> <meta name=Generator content="Microsoft Word 12"> <meta name=Originator content="Microsoft Word 12"> <link rel=File-List href="How%20to%20implement%20Environmental%20Cube%20Mapping_files/filelist.xml"> <link rel=themeData href="How%20to%20implement%20Environmental%20Cube%20Mapping_files/themedata.thmx"> <link rel=colorSchemeMapping href="How%20to%20implement%20Environmental%20Cube%20Mapping_files/colorschememapping.xml"> <style> </style> </head>
<body lang=EN-CA style='tab-interval:36.0pt'>
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.
<![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.
How to implement Environmental Cube Mapping
<![if !supportLists]>1) <![endif]>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
<![if !supportLists]>3) <![endif]>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:
<![if !supportLists]>· <![endif]>iObject.h
<![if !supportLists]> I. <![endif]>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
<![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.
<![if !supportLists]>· <![endif]>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)
<![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.
<![if !supportLists]> III. <![endif]>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.
<![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.
<![if !supportLists]>· <![endif]>Object.cpp
<![if !supportLists]> I. <![endif]>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.
<![if !supportLists]> III. <![endif]>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)
<![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.
<![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.
<![if !supportLists]> VII. <![endif]>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.
<![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, ‘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.
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:
<![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.
<![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.
<![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.
<o:p> </o:p>
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);<o:p></o:p>
I didn’t test the above code so there could be a typo.
</body>
</html>