programming4us
programming4us
MULTIMEDIA

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us