Open main menu

CDOT Wiki β

Changes

GAM670/DPS905 Weekly Schedule 20121

9,099 bytes added, 19:30, 14 February 2012
This Week
static IDirect3DVertexShader9* vertexShader; // vertex shader
static ID3DXConstantTable* uniformVS; // for vertex shader
</pre>
:: APIBase.cpp - initialize pointers
<pre>
IDirect3DVertexShader9* APIBase::vertexShader = nullptr; // vertex shader
ID3DXConstantTable* APIBase::uniformVS = nullptr; // for vertex shader
</pre>
:: APIDisplay.h
ID3DXConstantTable* APIBase::uniformFS = nullptr; // for fragment shader
</pre>
:: APIDisplay.cpp - setup()
<pre>
LPD3DXBUFFER compiledCodeFS = nullptr;
 
// checks for minimum pixel shader version required
else if (caps.PixelShaderVersion < D3DPS_VERSION(3,0))
error(L"Display::10 Device does not support pixel shader 3_0");
</pre>
<pre>
// compile the fragment shader source code
else if (FAILED(D3DXCompileShaderFromFile(FRAGMENT_SHADER_FILE, NULL,
NULL, FRAGMENT_SHADER_ENTRY_POINT, D3DXGetPixelShaderProfile(d3dd), 0,
&compiledCodeFS, NULL, &uniformFS))) {
release();
error(L"APIDisplay::15 Unable to compile the fragment shader code");
}
// create the pixel shader object
else if (FAILED(d3dd->CreatePixelShader(
(DWORD*)compiledCodeFS->GetBufferPointer(), &fragmentShader))) {
compiledCodeFS->Release();
release();
error(L"APIDisplay::16 Unable to create fragment shader object");
}
else {
compiledCodeFS->Release();
</pre>
<pre>
d3dd->SetPixelShader(fragmentShader);
</pre>
:: APIDisplay.cpp - beginDrawFrame()
<pre>
Colour colour(red, green, blue);
uniformFS->SetFloatArray(d3dd, "ambient", (FLOAT*)&colour, 4);
uniformFS->SetInt(d3dd, "noLights", 4);
</pre>
:: APIDisplay.cpp - set()
<pre>
uniformFS->SetBool(d3dd, "lighting", b);
</pre>
:: APIDisplay.cpp - setReflectivity()
<pre>
uniformFS->SetFloatArray(d3dd, "material.ambient", (FLOAT*)&r.ambient, 4);
uniformFS->SetFloatArray(d3dd, "material.diffuse", (FLOAT*)&r.diffuse, 4);
uniformFS->SetFloatArray(d3dd, "material.specular", (FLOAT*)&r.specular, 4);
uniformFS->SetFloat (d3dd, "material.power", (FLOAT)r.power);
</pre>
:: APIDisplay.cpp - release()
<pre>
}
if (uniformFS) {
uniformFS->Release();
uniformFS = nullptr;
}
if (fragmentShader) {
fragmentShader->Release();
fragmentShader = nullptr;
}
</pre>
:: APIGraphic.cpp - APIXMesh::draw()
<pre>
uniformFS->SetFloatArray(d3dd, "material.ambient", (FLOAT*)&ambient[i], 4);
uniformFS->SetFloatArray(d3dd, "material.diffuse", (FLOAT*)&diffuse[i], 4);
uniformFS->SetFloatArray(d3dd, "material.specular", (FLOAT*)&specular[i], 4);
uniformFS->SetFloat(d3dd, "material.power", (FLOAT)power[i]);
</pre>
:: APILight.cpp - setup()
<pre>
uniformFS->SetInt(d3dd, typ, type);
uniformFS->SetFloatArray(d3dd, amb, (FLOAT*)&ambient, 4);
uniformFS->SetFloatArray(d3dd, dif, (FLOAT*)&diffuse, 4);
uniformFS->SetFloatArray(d3dd, spe, (FLOAT*)&specular, 4);
uniformFS->SetFloatArray(d3dd, pos, (FLOAT*)&zero, 3);
uniformFS->SetFloatArray(d3dd, dir, (FLOAT*)&zero, 3);
uniformFS->SetFloatArray(d3dd, att, (FLOAT*)&attenuation, 3);
uniformFS->SetFloatArray(d3dd, spt, (FLOAT*)&spot, 3);
uniformFS->SetFloat(d3dd, ran, range);
</pre>
:: APILight.cpp - turnOn()
<pre>
uniformFS->SetFloatArray(d3dd, pos, (FLOAT*)&p, 3);
uniformFS->SetFloatArray(d3dd, dir, (FLOAT*)&o, 3);
uniformFS->SetBool(d3dd, constantLightOn, true);
</pre>
<pre>
uniformFS->SetFloatArray(d3dd, pos, (FLOAT*)&p, 3);
uniformFS->SetFloatArray(d3dd, dir, (FLOAT*)&o, 3);
uniformFS->SetBool(d3dd, constantLightOn, true);
</pre>
:: APILight.cpp - update()
<pre>
uniformFS->SetFloatArray(d3dd, pos, (FLOAT*)&p, 3);
uniformFS->SetFloatArray(d3dd, dir, (FLOAT*)&o, 3);
uniformFS->SetBool(d3dd, constantLightOn, true);
</pre>
:: APILight.cpp - turnOff()
<pre>
uniformFS->SetBool(d3dd, constantLightOn, false);
</pre>
:: APITexture.cpp - attach()
<pre>
char str[] = "texOn";
uniformFS->SetBool(d3dd, str, true);
</pre>
:: APITexture.cpp - detach()
<pre>
</pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre><pre></pre> char str[] = "texOn";<pre uniformFS->SetBool(d3dd, str, false);
</pre>
** Device
:: fragmentShader.hlsl - Constant Memory
<pre>
#define MLIGHTS 4
#define MTEXTURES 2
#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
float4 ambient;
float4 diffuse;
float4 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;
};
 
// RawPixel holds the data for a single fragment of the stream
//
struct RawPixel {
float2 texcoord : TEXCOORD0; // texture coordinate at this fragment
float3 position : TEXCOORD1; // fragment position in world space
float3 normal : TEXCOORD2; // lighting normal in world space
float3 toViewer : TEXCOORD3; // direction to viewer in world space
};
 
// Uniform Data (constant for a stream of fragments)
//
float4 ambient; // global ambient light - always on
int noLights; // no of active lights
Light light[MLIGHTS]; // static lights
bool lightOn[MLIGHTS]; // light switch
Material material; // material reflectivity
bool lighting; // lighting calculations on?
 
bool texOn; // texture switch
sampler2D tex; // set by the application
 
</pre>
:: fragmentShader.hlsl - fragmentShader()
<pre>
// The fragment shader receives raw fragment data and returns a pixel colour
//
float4 fragmentShader(RawPixel raw) : COLOR {
 
float4 colour; // 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;
float attenuationFactor, spotFactor, rho;
// normalize the fragment data
normal = normalize(raw.normal);
toViewer = normalize(raw.toViewer);
// perform calculations for each light in turn
for (int i = 0; i < noLights && i < MLIGHTS; i++) {
if (lightOn[i]) {
float diffuseFactor, reflectFactor, factor;
// diffuse and reflection factors
toLightSource = normalize((light[i].type == POINT_LIGHT)?
light[i].position - raw.position : - light[i].direction);
diffuseFactor = saturate(dot(normal, toLightSource));
reflectFactor = saturate(dot(normalize(2 * diffuseFactor *
normal - toLightSource), toViewer));
 
attenuationFactor = 1.0f;
spotFactor = 1.0f;
if (light[i].type == POINT_LIGHT ||
light[i].type == SPOT_LIGHT) {
// detail calcs for attenuationFactor and spotFactor
distance = length(raw.position - light[i].position);
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;
if (light[i].type == SPOT_LIGHT) {
rho = saturate(dot(normalize(light[i].position -
float3(raw.position.x, raw.position.y,
raw.position.z)),
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
attenuationFactor = 0.0f;
}
 
// accumulate ambient, diffuse, and specular elements of light
//
ambientLight += attenuationFactor * spotFactor *
light[i].ambient.xyz;
diffuseLight += attenuationFactor * spotFactor * diffuseFactor *
light[i].diffuse.xyz;
specularLight += attenuationFactor * spotFactor *
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
//
colour.xyz =
saturate(material.ambient.xyz * ambientLight) +
saturate(material.diffuse.xyz * diffuseLight) +
saturate(material.specular.xyz * specularLight);
colour.w = material.diffuse.w;
}
 
// apply texture
//
if (texOn)
colour *= tex2D(tex, raw.texcoord);
 
return colour;
}
</pre>