Changes

Jump to: navigation, search

GAM670/DPS905 Weekly Schedule 20121

23,319 bytes added, 08:00, 21 March 2012
This Week
*** [http://msdn.microsoft.com/en-us/library/bb509617%28v=vs.85%29.aspx length()] - length of a vector
*** [http://msdn.microsoft.com/en-us/library/bb509645%28v=vs.85%29.aspx saturate()] - clamp scalar, vector, or matrix to [0, 1]
* effects.fx- for unlit vertices and fragments: Uniform data<syntaxhighlight lang="cpp">#define MLIGHTS 4#define POINT_LIGHT 0#define SPOT_LIGHT 1#define DIRECTIONAL_LIGHT 2 // Types//// Light holds the data for a single static light in world space//struct Light { int type; // POINT_LIGHT, SPOT_LIGHT, DIRECTIONAL_LIGHT float3 ambient; float3 diffuse; float3 specular; float3 direction; // in world space float3 position; // in world space float3 attenuation; // .xyz for 1.0f/ (.x + .y * d + .z * d * d) float3 spot; // .x = cos(phi/2), .y = cos(theta/2), .z = falloff float range; // where attenuation becomes 0}; // Material holds the reflectivity properties of the material//struct Material { float4 ambient; float4 diffuse; float4 specular; float power;}; // Uniform Data (constant for the stream of vertices)//// Geometryfloat4x4 viewProjection; // view * projection transformationfloat4x4 world; // world transformationfloat4 viewPoint; // camera viewpoint for specular calcsfloat3 heading; // camera heading for specular calcsint noLights; // no of static lightsLight light[MLIGHTS]; // static lightsbool lightOn[MLIGHTS]; // light switch // Uniform Data (constant for a stream of fragments)//float4 ambient; // global ambient light - always onMaterial material; // material reflectivity bool texOn; // texture switchtexture texMap;sampler2D tex = sampler_state { texture = <texMap>; Filter = MIN_MAG_MIP_LINEAR; AddressU = MIRROR; AddressV = MIRROR;};</syntaxhighlight>: Varying Data - simple unlit objects<syntaxhighlight lang="cpp">//-------------------------------- Simple Unlit Objects -----------------------//// UnlitInput holds the original data for a vertex in the stream//struct UnlitVSInput {  float3 position : POSITION; // position in local space float3 normal : NORMAL; // normal in local space float2 texCoord : TEXCOORD0; // texture coordinates}; // UnlitOutput holds the transformed data for the vertex//struct UnlitVSOutput {  float4 position : POSITION; // position in homogeneous clip space float2 texCoord : TEXCOORD; // texture coordinates float3 normal : TEXCOORD1; // lighting normal in world space float3 toViewer : TEXCOORD2; // direction to viewer in world space float3 toLight[MLIGHTS] : TEXCOORD3; // light vector}; // UnlitFSInput holds the input data for a single fragment of the stream//struct UnlitFSInput { float2 texCoord : TEXCOORD0; // texture coordinate at this fragment float3 normal : TEXCOORD1; // lighting normal in world space float3 toViewer : TEXCOORD2; // direction to viewer in world space float3 toLight[MLIGHTS] : TEXCOORD3; // direction from fragment to light i};</syntaxhighlight>: Vertex Shader<syntaxhighlight lang="cpp">// unlitVShader transforms vertex and lighting data for simple unlit objects//UnlitVSOutput unlitVShader(UnlitVSInput input) {  UnlitVSOutput output; // result returned by this function  // Transform the vertex coordinates to homogeneous clip coordinates // // A more efficient algorithm would accept the world*view*projection // tranformation as one uniform matrix and avoid the 2-stage product // This will require a bit of restructuring of the application code. // output.position = mul(float4(input.position, 1.0), world); // local to world output.position = mul(output.position, viewProjection); //... to clip // Determine the vector from this vertex to the viewer output.toViewer = viewPoint.xyz - input.position;  // Determine the vector from this vertex to light source i in local space // (assumes that light position and direction are specified in local space) for (int i = 0; i < noLights; i++) if (light[i].type == DIRECTIONAL_LIGHT) output.toLight[i] = - light[i].direction; else output.toLight[i] = light[i].position - input.position; // pass the normal and texture coordinates along unaltered // output.normal = input.normal; output.texCoord = input.texCoord;  return output;} </syntaxhighlight>: Fragment Shader<syntaxhighlight lang="cpp">// This fragment shader processes lighting for noLights lights in object space// and returns a pixel colour//float4 unlitFShader(UnlitFSInput input) : COLOR {  float4 output; // result returned by this function float3 normal; // normal to the fragment float3 toViewer; // from fragment to the camera float3 toLightSource; // from fragment to current light source // lighting contribution accumulators float3 ambientLight = ambient.xyz; float3 diffuseLight = (float3)0; float3 specularLight = (float3)0; // lighting calculation factors float diffuseFactor, reflectFactor, distance, factor; float attenuationFactor, spotFactor, rho; // normalize the fragment input normal = normalize(input.normal); toViewer = normalize(input.toViewer); // perform calculations for each light in turn for (int i = 0; i < noLights && i < MLIGHTS; i++) { if (lightOn[i]) { // diffuse and reflection factors toLightSource = normalize(input.toLight[i]); diffuseFactor = saturate(dot(normal, toLightSource)); reflectFactor = saturate(dot(normalize(2 * diffuseFactor * normal - toLightSource), toViewer));  // attenuation if (light[i].type != DIRECTIONAL_LIGHT) { distance = length(input.toLight[i]); if (distance < light[i].range) { attenuationFactor = light[i].attenuation.x + light[i].attenuation.y * distance + light[i].attenuation.z * distance * distance; attenuationFactor = 1.0f / attenuationFactor; } else attenuationFactor = 0.0f; } else attenuationFactor = 1.0f;  // spot if (light[i].type == SPOT_LIGHT) { rho = saturate(dot(toLightSource, normalize(-light[i].direction))); if (rho <= light[i].spot.x) spotFactor = 0.0f; else if (rho <= light[i].spot.y) spotFactor = pow(abs( (rho - light[i].spot.x)/ (light[i].spot.y - light[i].spot.x)), light[i].spot.z); else spotFactor = 1.0f; } else spotFactor = 1.0;  factor = attenuationFactor * spotFactor;  // accumulate ambient, diffuse, and specular elements of light i ambientLight += factor * light[i].ambient.xyz; diffuseLight += factor * diffuseFactor * light[i].diffuse.xyz; specularLight += factor * light[i].specular.xyz * pow(reflectFactor, material.power); } } // apply material reflectivity to each accumulated element of light // to obtain the colour of the lit fragment // output.xyz = saturate(material.ambient.xyz * ambientLight) + saturate(material.diffuse.xyz * diffuseLight) + saturate(material.specular.xyz * specularLight); output.w = material.diffuse.w;  // sample the texture // if (texOn) output *= tex2D(tex, input.texCoord);  return output;}</syntaxhighlight>: techniques - opaque and translucent<syntaxhighlight lang="cpp">technique opaque { pass { AlphaBlendEnable = false; VertexShader = compile vs_3_0 unlitVShader(); PixelShader = compile ps_3_0 unlitFShader(); }}</syntaxhighlight> <syntaxhighlight lang="cpp">technique translucent { pass { AlphaBlendEnable = true; VertexShader = compile vs_3_0 unlitVShader(); PixelShader = compile ps_3_0 unlitFShader(); }}</syntaxhighlight>  === To Do ====== Resources ===  == Week 10 - Mar 18 ===== This Week ===* Texturing - Identification: connection between Design.cpp and effects.fx** iTexture.h:: add texture and texture state identifier<syntaxhighlight lang="cpp">iTexture* CreateTexture(const wchar_t* file, const char* str, const char* isOn);</syntaxhighlight>** Texture.h:<syntaxhighlight lang="cpp"> Texture(const wchar_t*, const char*, const char*);</syntaxhighlight>** Texture.cpp:<syntaxhighlight lang="cpp">// constructor initializes the texture identifier//Texture::Texture(const wchar_t* file, const char* str, const char* isOn) : filter(0u) {  if (file) { int len = strlen(file); this->file = new wchar_t[len + 1]; strcpy(this->file, file, len); } else this->file = nullptr;  tex = nullptr; #if PIPELINE == FIXED_FUNCTION #elif PIPELINE == PROGRAMMABLE #elif PIPELINE == PROGRAMMABLE_EFFECT textureHandle = effect->GetParameterByName(0, str); textureonHandle = effect->GetParameterByName(0, isOn); #endif}</syntaxhighlight>:<syntaxhighlight lang="cpp"> effect->SetTexture(textureHandle, tex); effect->SetBool(textureonHandle, true);</syntaxhighlight>** iAPITexture.h:: add texture and texture state identifier<syntaxhighlight lang="cpp">iAPITexture* CreateAPITexture(const wchar_t* file, const char* str, const char* isOn);</syntaxhighlight>** APITexture.h:<syntaxhighlight lang="cpp"> D3DXHANDLE textureHandle; // points to texture D3DXHANDLE textureonHandle; // points to texture on statuspublic: APITexture(const wchar_t*, const char*, const char*);</syntaxhighlight>** APITexture.cpp:<syntaxhighlight lang="cpp">// constructor initializes the texture identifier//APITexture::APITexture(const wchar_t* file, const char* str, const char* isOn, AddressMode m) : filter(0u), mode(m) {  if (file) { int len = strlen(file); this->file = new wchar_t[len + 1]; strcpy(this->file, file, len); } else this->file = nullptr;  tex = nullptr; target = nullptr; #if PIPELINE == FIXED_FUNCTION #elif PIPELINE == PROGRAMMABLE #elif PIPELINE == PROGRAMMABLE_EFFECT textureHandle = effect->GetParameterByName(0, str); textureonHandle = effect->GetParameterByName(0, isOn); #endif}</syntaxhighlight>:<syntaxhighlight lang="cpp"> effect->SetTexture(textureHandle, tex); effect->SetBool(textureonHandle, true);</syntaxhighlight>* Texturing - Tiling: [http://msdn.microsoft.com/en-us/library/windows/desktop/bb206245%28v=vs.85%29.aspx Texture Coordinates]** iGraphic.h:: add u v parameters, both of which default to 1<syntaxhighlight lang="cpp">iGraphic* CreateBox(float, float, float, float, float, float, float = 1, float = 1);iGraphic* CreateIBox(float, float, float, float, float, float, float = 1, float = 1);iGraphic* CreateRectangleList(float, float, float, float, float = 1, float = 1);iGraphic* CreateIRectangleList(float, float, float, float, float = 1, float = 1);iGraphic* CreateMeshBox(float, float, float, float, float, float, float = 1, float = 1);</syntaxhighlight>** Graphic.cpp:: extend add functions to include u v parameters<syntaxhighlight lang="cpp">//-------------------------------- Graphic Structures -------------------------//// prototypes for add() function used by the Create...() functionsvoid add(VertexList<Vertex>*, const Vector&, const Vector&, const Vector&, const Vector&, const Vector&, float = 1, float = 1);void add(IndexedList<Vertex>*, const Vector&, const Vector&, const Vector&, const Vector&, const Vector&, float = 1, float = 1);void add(CustomMesh<Vertex>*, const Vector&, const Vector&, const Vector&, const Vector&, const Vector&, float = 1, float = 1); // CreateBox builds a triangle vertex list for a brick-like box from two// extreme points one face at a time with all faces having the same attributes//iGraphic* CreateBox(float minx, float miny, float minz, float maxx, float maxy, float maxz, float u, float v) { VertexList<Vertex>* vertexList = (VertexList<Vertex>*)CreateVertexList<Vertex>(TRIANGLE_LIST, 12);  float x = (minx + maxx) / 2; float y = (miny + maxy) / 2; float z = (minz + maxz) / 2; minx -= x; miny -= y; minz -= z; maxx -= x; maxy -= y; maxz -= z; // bounding sphere float max; max = maxx > maxy ? maxx : maxy; max = maxz > max ? maxz : max; vertexList->setRadius(1.73205f * max); // locate centroid at origin Vector p1 = Vector(minx, miny, minz), p2 = Vector(minx, maxy, minz), p3 = Vector(maxx, maxy, minz), p4 = Vector(maxx, miny, minz), p5 = Vector(minx, miny, maxz), p6 = Vector(minx, maxy, maxz), p7 = Vector(maxx, maxy, maxz), p8 = Vector(maxx, miny, maxz); add(vertexList, p1, p2, p3, p4, Vector(0, 0, -1), u, v); // front add(vertexList, p4, p3, p7, p8, Vector(1, 0, 0), u, v); // right add(vertexList, p8, p7, p6, p5, Vector(0, 0, 1), u, v); // back add(vertexList, p6, p2, p1, p5, Vector(-1, 0, 0), u, v); // left add(vertexList, p1, p4, p8, p5, Vector(0, -1, 0), u, v); // bottom add(vertexList, p2, p6, p7, p3, Vector(0, 1, 0), u, v); // top  return vertexList;} iGraphic* CreateIBox(float minx, float miny, float minz, float maxx, float maxy, float maxz, float u, float v) { IndexedList<Vertex>* indexedList = (IndexedList<Vertex>*) CreateIndexedList<Vertex, unsigned short>(TRIANGLE_LIST, 12);  float x = (minx + maxx) / 2; float y = (miny + maxy) / 2; float z = (minz + maxz) / 2; minx -= x; miny -= y; minz -= z; maxx -= x; maxy -= y; maxz -= z; // bounding sphere float max; max = maxx > maxy ? maxx : maxy; max = maxz > max ? maxz : max; indexedList->setRadius(1.73205f * max); // locate centroid at origin Vector p1 = Vector(minx, miny, minz), p2 = Vector(minx, maxy, minz), p3 = Vector(maxx, maxy, minz), p4 = Vector(maxx, miny, minz), p5 = Vector(minx, miny, maxz), p6 = Vector(minx, maxy, maxz), p7 = Vector(maxx, maxy, maxz), p8 = Vector(maxx, miny, maxz); add(indexedList, p1, p2, p3, p4, Vector(0, 0, -1), u, v); // front add(indexedList, p4, p3, p7, p8, Vector(1, 0, 0), u, v); // right add(indexedList, p8, p7, p6, p5, Vector(0, 0, 1), u, v); // back add(indexedList, p6, p2, p1, p5, Vector(-1, 0, 0), u, v); // left add(indexedList, p1, p4, p8, p5, Vector(0, -1, 0), u, v); // bottom add(indexedList, p2, p6, p7, p3, Vector(0, 1, 0), u, v); // top  return indexedList;} // CreateRectangleList builds a triangle list in the x-y plane from its two// extreme points//iGraphic* CreateRectangleList(float minx, float miny, float maxx, float maxy, float u, float v) { VertexList<Vertex>* vertexList = (VertexList<Vertex>*)CreateVertexList<Vertex>(TRIANGLE_LIST, 2);  float x = (minx + maxx) / 2, y = (miny + maxy) / 2; minx -= x; miny -= y; maxx -= x; maxy -= y; // bounding sphere float max; max = maxx > maxy ? maxx : maxy; vertexList->setRadius(1.73205f * max); // locate centroid at origin Vector p1 = Vector(minx, miny, 0), p2 = Vector(minx, maxy, 0), p3 = Vector(maxx, maxy, 0), p4 = Vector(maxx, miny, 0); add(vertexList, p1, p2, p3, p4, Vector(0, 0, -1), u, v); return vertexList;} // CreateIRectangleList builds an indexed triangle list in the x-y plane from// its two extreme points//iGraphic* CreateIRectangleList(float minx, float miny, float maxx, float maxy, float u, float v) { IndexedList<Vertex>* indexedList = (IndexedList<Vertex>*) CreateIndexedList<Vertex, unsigned short>(TRIANGLE_LIST, 2);  float x = (minx + maxx) / 2, y = (miny + maxy) / 2; minx -= x; miny -= y; maxx -= x; maxy -= y; // bounding sphere float max; max = maxx > maxy ? maxx : maxy; indexedList->setRadius(1.73205f * max); // locate centroid at origin Vector p1 = Vector(minx, miny, 0), p2 = Vector(minx, maxy, 0), p3 = Vector(maxx, maxy, 0), p4 = Vector(maxx, miny, 0); add(indexedList, p1, p2, p3, p4, Vector(0, 0, -1), u, v); return indexedList;} // CreateIRectangleList builds an indexed triangle list in the x-y plane from// its two extreme points//iGraphic* CreateBRectangleList(float minx, float miny, float maxx, float maxy, float u, float v) { IndexedList<BumpVertex>* indexedList = (IndexedList<BumpVertex>*) CreateIndexedList<BumpVertex, unsigned short>(TRIANGLE_LIST, 2);  float x = (minx + maxx) / 2, y = (miny + maxy) / 2; minx -= x; miny -= y; maxx -= x; maxy -= y; // bounding sphere float max; max = maxx > maxy ? maxx : maxy; indexedList->setRadius(1.73205f * max); // locate centroid at origin Vector p1 = Vector(minx, miny, 0), p2 = Vector(minx, maxy, 0), p3 = Vector(maxx, maxy, 0), p4 = Vector(maxx, miny, 0); add(indexedList, p1, p2, p3, p4, Vector(0, 0, -1), u, v); return indexedList;} // CreateMeshBox builds a mesh for a brick-like box from two extreme points one// face at a time with each faces having distinct attributes//iGraphic* CreateMeshBox(float minx, float miny, float minz, float maxx, float maxy, float maxz, float u, float v) {  unsigned attribute[] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5};  CustomMesh<>* mesh = (CustomMesh<>*) CreateCustomMesh<Vertex, unsigned short>(attribute, 12, 36, 24, 6);  float x = (minx + maxx) / 2, y = (miny + maxy) / 2, z = (minz + maxz) / 2; minx -= x; miny -= y; minz -= z; maxx -= x; maxy -= y; maxz -= z; // bounding sphere float max; max = maxx > maxy ? maxx : maxy; max = maxz > max ? maxz : max; mesh->setRadius(1.73205f * max); // one face at a time Vector p1 = Vector(minx, miny, minz), p2 = Vector(minx, maxy, minz), p3 = Vector(maxx, maxy, minz), p4 = Vector(maxx, miny, minz), p5 = Vector(minx, miny, maxz), p6 = Vector(minx, maxy, maxz), p7 = Vector(maxx, maxy, maxz), p8 = Vector(maxx, miny, maxz); add(mesh, p1, p2, p3, p4, Vector(0, 0, -1), u, v); // front add(mesh, p4, p3, p7, p8, Vector(1, 0, 0), u, v); // right add(mesh, p8, p7, p6, p5, Vector(0, 0, 1), u, v); // back add(mesh, p6, p2, p1, p5, Vector(-1, 0, 0), u, v); // left add(mesh, p1, p4, p8, p5, Vector(0, -1, 0), u, v); // bottom add(mesh, p2, p6, p7, p3, Vector(0, 1, 0), u, v); // top  return mesh;} void add(VertexList<Vertex>* vertexList, const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, const Vector& n, float u, float v) {  vertexList->add(Vertex(p1, n, 0, v)); vertexList->add(Vertex(p2, n, 0, 0)); vertexList->add(Vertex(p3, n, u, 0)); vertexList->add(Vertex(p1, n, 0, v)); vertexList->add(Vertex(p3, n, u, 0)); vertexList->add(Vertex(p4, n, u, v));} void add(IndexedList<Vertex>* indexedList, const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, const Vector& n, float u, float v) {  unsigned v1 = indexedList->add(Vertex(p1, n, 0, v)); unsigned v2 = indexedList->add(Vertex(p2, n, 0, 0)); unsigned v3 = indexedList->add(Vertex(p3, n, u, 0)); unsigned v4 = indexedList->add(Vertex(p4, n, u, v)); indexedList->add(v1); indexedList->add(v2); indexedList->add(v3); indexedList->add(v1); indexedList->add(v3); indexedList->add(v4);} void add(CustomMesh<Vertex>* mesh, const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, const Vector& n, float u, float v) {  unsigned v1 = mesh->add(Vertex(p1, n, 0, v)); unsigned v2 = mesh->add(Vertex(p2, n, 0, 0)); unsigned v3 = mesh->add(Vertex(p3, n, u, 0)); unsigned v4 = mesh->add(Vertex(p4, n, u, v)); mesh->add(v1); mesh->add(v2); mesh->add(v3); mesh->add(v1); mesh->add(v3); mesh->add(v4);} </syntaxhighlight>* Texturing - Address Modes: [http://msdn.microsoft.com/en-us/library/windows/desktop/bb206239%28v=vs.85%29.aspx Direct3D9 Addressing Modes]** APITexture.h:<syntaxhighlight lang="cpp"> D3DXHANDLE textureMode; // points to texture mode</syntaxhighlight>** APITexture.cpp:<syntaxhighlight lang="cpp"> effect->SetInt(textureMode, (int)mode);</syntaxhighlight>** effects.fx - uniform variables::<syntaxhighlight lang="cpp">// Addressing Modes#define TEX_WRAP 0#define TEX_CLAMP 1#define TEX_MIRROR 2 //... bool tex_1_On; // texture switchint tex_mode; // addressing modetexture tex_1; sampler2D tex_1_ww = sampler_state { texture = <tex_1>; Filter = MIN_MAG_MIP_LINEAR; AddressU = WRAP; AddressV = WRAP;}; sampler2D tex_1_mm = sampler_state { texture = <tex_1>; Filter = MIN_MAG_MIP_LINEAR; AddressU = MIRROR; AddressV = MIRROR;}; sampler2D tex_1_cc = sampler_state { texture = <tex_1>; Filter = MIN_MAG_MIP_LINEAR; AddressU = CLAMP; AddressV = CLAMP;}; </syntaxhighlight>** effects.fx - fragment shader:<syntaxhighlight lang="cpp"> // sample the texture // if (tex_1_On) { if (tex_mode == TEX_CLAMP) output *= tex2D(tex_1_cc, input.texCoord); else if (tex_mode == TEX_WRAP) output *= tex2D(tex_1_ww, input.texCoord); else if (tex_mode == TEX_MIRROR) output *= tex2D(tex_1_mm, input.texCoord); }</syntaxhighlight>* Texturing - Multi-Texturing** effects.fx - uniform variables:<syntaxhighlight lang="cpp">bool tex_2_On; // texture switchtexture tex_2; sampler2D tex_2_ww = sampler_state { texture = <tex_2>; Filter = MIN_MAG_MIP_LINEAR; AddressU = WRAP; AddressV = WRAP;}; sampler2D tex_2_mm = sampler_state { texture = <tex_2>; Filter = MIN_MAG_MIP_LINEAR; AddressU = MIRROR; AddressV = MIRROR;}; sampler2D tex_2_cc = sampler_state { texture = <tex_2>; Filter = MIN_MAG_MIP_LINEAR; AddressU = CLAMP; AddressV = CLAMP;}; </syntaxhighlight>** effects.fx - fragment shader:<syntaxhighlight lang="cpp"> if (tex_2_On) { if (tex_mode == TEX_CLAMP) output *= tex2D(tex_2_cc, input.texCoord); else if (tex_mode == TEX_WRAP) output *= tex2D(tex_2_ww, input.texCoord); else if (tex_mode == TEX_MIRROR) output *= tex2D(tex_2_mm, input.texCoord); }</syntaxhighlight> === To Do ====== Resources ===  <!--== Week 11 - Mar 25 ===== This Week === <syntaxhighlight lang="cpp"></syntaxhighlight> === To Do ====== Resources ===--><!--== Week 12 - Apr 1 ===== This Week === <syntaxhighlight lang="cpp"></syntaxhighlight>
=== To Do ===
=== Resources ===
-->

Navigation menu