MULTIMEDIA

Programming with DirectX : Implementing Per-Pixel Lighting (part 2) - The Main Source File

1/21/2014 12:57:52 AM

The Main Source File

In the main source file, two new objects are added to the global section: one to pass the light position to the shaders and one to pass the camera position. The end of the global section with the new variables in the Lighting demo are shown in Listing 1. In the InitializeDemo() function, these effect variables are obtained so we can use them to actually pass data to the shaders. The InitializeDemo() function is partially shown in Listing 2. The only new code in it is the effect variable calls that obtain access so we can set the light and camera position.

Listing 1. Partial Look at the Lighting Demo’s Global Variables


ID3D10Effect *g_shader = NULL;
ID3D10EffectTechnique *g_lightingTech = NULL;
ID3D10EffectVectorVariable *g_lightPosEffectVar = NULL;
ID3D10EffectVectorVariable *g_eyePosEffectVar = NULL;
ID3D10EffectMatrixVariable *g_worldEffectVar = NULL;
ID3D10EffectMatrixVariable *g_viewEffectVar = NULL;
ID3D10EffectMatrixVariable *g_projEffectVar = NULL;

D3DXMATRIX g_worldMat, g_viewMat, g_projMat;


// Scene rotations.
float g_xRot = 0.0f;
float g_yRot = 0.0f;

Listing 2. Partial Look at the InitializeDemo() Function
bool InitializeDemo()
{
// Load the shader.

DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;

#if defined( DEBUG ) || defined( _DEBUG )
shaderFlags |= D3D10_SHADER_DEBUG;
#endif

ID3D10Blob *errors = NULL;

HRESULT hr = D3DX10CreateEffectFromFile("LambertBlinnPhong.fx",
NULL, NULL, "fx_4_0", shaderFlags, 0, g_d3dDevice, NULL,
NULL, &g_shader, &errors, NULL);

if(errors != NULL)
{
MessageBox(NULL, (LPCSTR)errors->GetBufferPointer(),
"Error in Shader!", MB_OK);

errors->Release()
}

if(FAILED(hr))
return false;

g_lightingTech = g_shader->GetTechniqueByName(
"BlinnPhongSpecular");

g_worldEffectVar = g_shader->GetVariableByName(
"World")->AsMatrix();

g_viewEffectVar = g_shader->GetVariableByName(
"View")->AsMatrix();

g_projEffectVar = g_shader->GetVariableByName(
"Projection")->AsMatrix();

g_lightPosEffectVar = g_shader->GetVariableByName(
"lightPos")->AsVector();

g_eyePosEffectVar = g_shader->GetVariableByName(
"eyePos")->AsVector();



return true;
}


In the Update() function the world matrix is rotated along the X and Y axes so we can see the specular lighting changes as the orientation moves in real time. In the RenderScene() function the only new code is the code that passes the light and camera positions to the shaders. The light position is located 5 units along the negative Z axis, while the camera position is located at the origin. The object itself is positioned 6 units along the Z axis, which can be seen in the Update() function.

The Update() and RenderScene() functions are shown in Listing 3. Figure 1 shows a screenshot from the running demo.

Listing 3. The Update() and RenderScene() Functions from the Lighting Demo
void Update()
{
g_xRot += 0.0001f;
g_yRot += 0.0002f;

if(g_xRot < 0) g_xRot = 359;
else if(g_xRot >= 360) g_xRot = 0;

if(g_yRot < 0) g_yRot = 359;
else if(g_yRot >= 360) g_yRot = 0;

D3DXMATRIX trans, rotX, rotY;

D3DXMatrixRotationX(&rotX, g_xRot);
D3DXMatrixRotationY(&rotY, g_yRot);
D3DXMatrixTranslation(&trans, 0, 0, 6);

g_worldMat = (rotX * rotY) * trans;
}


void RenderScene()
{
float col[4] = { 0, 0, 0, 1 };

g_d3dDevice->ClearRenderTargetView(g_renderTargetView, col);
g_d3dDevice->ClearDepthStencilView(g_depthStencilView,
D3D10_CLEAR_DEPTH, 1.0f, 0);

g_d3dDevice->IASetInputLayout(g_layout);
g_d3dDevice->IASetPrimitiveTopology(
D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

D3D10_TECHNIQUE_DESC techDesc;
g_lightingTech->GetDesc(&techDesc);

unsigned int stride = sizeof(DX10Vertex);
unsigned int offset = 0;

float lightPos[4] = { 0, 0, -5, 1 };
float eyePos[4] = { 0, 0, 0, 1 };

g_worldEffectVar->SetMatrix((float*)&g_worldMat);
g_lightPosEffectVar->SetFloatVector(lightPos);
g_eyePosEffectVar->SetFloatVector(eyePos);

for(int m = 0; m < (int)g_meshes.size(); m++)
{
g_d3dDevice->IASetVertexBuffers(0, 1,
&g_meshes[m].m_vertices, &stride, &offset);

for(unsigned int i = 0; i < techDesc.Passes; i++)
{
g_lightingTech->GetPassByIndex(i)->Apply(0);
g_d3dDevice->Draw(g_meshes[m].m_totalVerts, 0);
}
}

g_swapChain->Present(0, 0);

Update();
}


Figure 1. Screenshot from the Lighting demo.
Other  
  •  Programming with DirectX : Basic Lighting Information - Diffuse Light , Specular Light , Blinn-Phong Specular Model
  •  Programming with DirectX : Light Types - Directional Lights,Point Lights,Spot Lights, Area Lights
  •  Programming with DirectX : Overview of Lighting - Pros and Cons of Per-Vertex Lighting, Per-Pixel Lighting
  •  What Will 2014 Bring To Tech Lovers?
  •  DirectX 10 Game Programming : Direct3D - Textures and Details (part 3) - Drawing the Textured Object
  •  DirectX 10 Game Programming : Direct3D - Textures and Details (part 1) - Adding Support for Texture Mapping - Texturing Coordinates and Sampling
  •  DirectX 10 Game Programming : Direct3D - Textures and Details (part 1) - Adding Support for Texture Mapping - The Vertex Format
  •  DirectX 10 Game Programming : Shaders and Effects - Geometry Shaders
  •  Adobe Photoshop CS5 : Managing Color from Monitor to Print - Working with Rendering Intents
  •  Adobe Photoshop CS5 : Managing Color from Monitor to Print - Changing from Additive (RGB) to Subtractive (CMYK) Color
  •  
    Top 10
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8