Team Guardian Cube Mapping

From CDOT Wiki
Revision as of 05:42, 14 April 2011 by CloudScorpion (talk | contribs) (Created page with '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…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.