   HLSL issues, not rendering Directx 10
  I am having issues with HLSL code and I am not sure how to fix the issue. I am creating a 3d plane along the z and x axis that is very rough terrain. I want to render lighting across this plane but I run into several issues. Here is the code that generates my terrain mesh along with its indices and surface normalsbool MyGame::CreateObject(){VertexPos vertices[NUM_VERTSX * NUM_VERTSY];for(int z=0; z < NUM_VERTSY; ++z){for(int x = 0; x < NUM_VERTSX; ++x){vertices[x + z * NUM_VERTSX].pos.x = (float)x * CELL_WIDTH;vertices[x + z * NUM_VERTSX].pos.z = (float)z * CELL_HEIGHT;vertices[x + z * NUM_VERTSX].pos.y = (float)(rand() % CELL_HEIGHT);vertices[x + z * NUM_VERTSX].color = D3DXVECTOR4(1.0, 0.0f, 0.0f, 0.0f);}}DWORD indices[NUM_COLS * NUM_ROWS * 6];int curIndex = 0;for(int z=0; z < NUM_ROWS; ++z){for(int x = 0; x < NUM_COLS; ++x){int curVertex = x + (z * NUM_VERTSX);indices[curIndex] = curVertex;indices[curIndex + 1] = curVertex + NUM_VERTSX;indices[curIndex + 2] = curVertex + 1;indices[curIndex + 3] = curVertex + 1;indices[curIndex + 4] = curVertex + NUM_VERTSX;indices[curIndex + 5] = curVertex + NUM_VERTSX + 1;curIndex += 6;}}modelObject.numIndices = sizeof(indices) / sizeof(DWORD);// compute normals for each face in the modelfor (unsigned int i = 0; i < modelObject.numIndices; i+=3){D3DXVECTOR3 v0 = vertices[indices].pos;D3DXVECTOR3 v1 = vertices[indices].pos;D3DXVECTOR3 v2 = vertices[indices].pos;D3DXVECTOR3 normal;D3DXVECTOR3 cross;D3DXVec3Cross(&cross, &D3DXVECTOR3(v2 - v0), &D3DXVECTOR3(v1 - v0));D3DXVec3Normalize(&normal, &cross);// assign the computed normal to each vertex in this facevertices[indices<span style="font-weight:bold;">].normal= normal;vertices[indices].normal = normal;vertices[indices].normal = normal;}//Create LayoutD3D10_INPUT_ELEMENT_DESC layout[] = {{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT, 0 , 0, D3D10_INPUT_PER_VERTEX_DATA, 0},{"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 12, D3D10_INPUT_PER_VERTEX_DATA, 0},{"NORMAL",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 28, D3D10_INPUT_PER_VERTEX_DATA, 0}};UINT numElements = (sizeof(layout)/sizeof(layout[0]));modelObject.numVertices = sizeof(vertices)/sizeof(VertexPos);//Create buffer descD3D10_BUFFER_DESC bufferDesc;bufferDesc.Usage = D3D10_USAGE_DEFAULT;bufferDesc.ByteWidth = sizeof(VertexPos) * modelObject.numVertices;bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;bufferDesc.CPUAccessFlags = 0;bufferDesc.MiscFlags = 0;D3D10_SUBRESOURCE_DATA initData;initData.pSysMem = vertices;//Create the bufferHRESULT hr = mpD3DDevice-&gt;CreateBuffer(&bufferDesc, &initData, &modelObject.pVertexBuffer);if(FAILED(hr))return false;bufferDesc.ByteWidth = sizeof(DWORD) * modelObject.numIndices;bufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;initData.pSysMem = indices;hr = mpD3DDevice-&gt;CreateBuffer(&bufferDesc, &initData, &modelObject.pIndicesBuffer);if(FAILED(hr))return false;///////////////////////////////////////////////////////////////////////////////Set up fx filesLPCWSTR effectFilename = L"effect.fx";modelObject.pEffect = NULL; hr = D3DX10CreateEffectFromFile(effectFilename,NULL,NULL,"fx_4_0",D3D10_SHADER_ENABLE_STRICTNESS,0,mpD3DDevice,NULL,NULL,&modelObject.pEffect,NULL,NULL);if(FAILED(hr))return false;pProjectionMatrixVariable = modelObject.pEffect-&gt;GetVariableByName("Projection")-&gt;AsMatrix();pTimeVariable = modelObject.pEffect-&gt;GetVariableByName("TimeStep")-&gt;AsScalar();//Dont sweat the technique. Get it!LPCSTR effectTechniqueName = "Render";modelObject.pTechnique = modelObject.pEffect-&gt;GetTechniqueByName(effectTechniqueName);if(modelObject.pTechnique == NULL)return false;//Create Vertex layoutD3D10_PASS_DESC passDesc;modelObject.pTechnique-&gt;GetPassByIndex(0)-&gt;GetDesc(&passDesc);hr = mpD3DDevice-&gt;CreateInputLayout(layout, numElements,passDesc.pIAInputSignature,passDesc.IAInputSignatureSize,&modelObject.pVertexLayout);if(FAILED(hr))return false;return true;}</pre>I debugged the above code above and checked some of my surface normals and it seems they all recieve values are normalized. everything is fine above . the problem consist below<pre>// PS_INPUT - input variables to the pixel shader// This struct is created and fill in by the // vertex shadercbuffer Variables{matrix Projection;matrix World;float TimeStep;};struct PS_INPUT{float4 Pos : SV_POSITION;    float4 Color : COLOR0;float3 Normal : TEXCOORD0;float3 ViewVector : TEXCOORD1;};float specpower = 80.0f;float3 camPos = float3(0.0f, 9.0, -256.0f);float3 DirectLightColor  = float3(1.0f, 1.0f, 1.0f);float3 DirectLightVector = float3(0.0f, 0.602f, 0.70f);float3 AmbientLightColor = float3(1.0f, 1.0f, 1.0f);/**************************************** Lighting functions***************************************//********************************** CalculateAmbient - * inputs - *vKa material's reflective color*lightColor - the ambient color of the lightsource* output - ambient color*********************************/float3 CalculateAmbient(float3 vKa, float3 lightColor){float3 vAmbient = vKa * lightColor;return vAmbient;}/********************************** CalculateDiffuse - * inputs - *material color*   The color of the direct light*the local normal*   the vector of the direct light* output - difuse color*********************************/float3 CalculateDiffuse(float3 baseColor, float3 lightColor, float3 normal, float3 lightVector){float3 vDiffuse = baseColor * lightColor * saturate(dot(normal, lightVector));return vDiffuse;}/********************************** CalculateSpecular - * inputs - *viewVector*the direct light vector*   the normal* output - specular highlight*********************************/float CalculateSpecular(float3 viewVector, float3 lightVector, float3 normal){float3 vReflect = reflect(lightVector, normal);float fSpecular = saturate(dot(vReflect, viewVector));fSpecular = pow(fSpecular, specpower);return fSpecular;}/********************************** LightingCombine - * inputs - *ambient component*diffuse component*   specualr component* output - phong color color*********************************/float3 LightingCombine(float3 vAmbient, float3 vDiffuse, float fSpecular){float3 vCombined = vAmbient + vDiffuse + fSpecular.xxx;return vCombined;}////////////////////////////////////////////////// Vertex Shader - Main Function///////////////////////////////////////////////PS_INPUT VS(float4 Pos : POSITION, float4 Color : COLOR, float3 Normal : NORMAL){PS_INPUT psInput;float4 newPosition;newPosition = Pos;newPosition.y = sin((newPosition.x * TimeStep) + (newPosition.z / 3.0f)) * 5.0f;// Pass through both the position and the color    psInput.Pos = mul(newPosition , Projection );psInput.Color = Color;psInput.ViewVector = normalize(camPos - psInput.Pos);    return psInput;}///////////////////////////////////////////////// Pixel Shader/////////////////////////////////////////////////Anthony!!!!!!!!!!! Find out how color works when multiplying themfloat4 PS(PS_INPUT psInput) : SV_Target{float3 normal = -normalize(psInput.Normal);float3 vAmbient = CalculateAmbient(psInput.Color, AmbientLightColor);float3 vDiffuse = CalculateDiffuse(psInput.Color, DirectLightColor, normal, DirectLightVector);float fSpecular = CalculateSpecular(psInput.ViewVector, DirectLightVector, normal);float4 outColor;outColor.rgb = LightingCombine(vAmbient, vDiffuse, fSpecular);outColor.a = 1.0f;//Below is where the error begins    //return outColor; //&lt;——-it throws a error when I return this    return psInput.Color;   //works fine here}// Define the techniquetechnique10 Render{    pass P0    {   SetVertexShader( CompileShader( vs_4_0, VS() ) );   SetGeometryShader( NULL );   SetPixelShader( CompileShader( ps_4_0, PS() ) );    }}</pre>In the above code everything appears to be working fine until I get to my pixel shader. If I return the output color (I commented out) I get an error. If I return the same color that was put into the pixel shader, everything works fine. &#111;nly problem is my mesh is a solid red color.What is the error ??? I dont know. when I run the program and return outColor, it immediately closes without display a thing.  I hope I provided enough and if anyone got any further questions. please let me know.
  You don't write the Normal field of the PS_INPUT structure that you return from the vertex shader. This causes a mismatch between the VS output and PS input, thus preventing the effect to compile.Also, you mix float3's and float4's in several places. For example, see line 107. The logic doesn't make sense to me - do you really want to normalize the difference of world-space camera position and post-projection vertex position?Anyway, you can use the fxc tool (supplied with the SDK) to compile shaders and effects. To compile your effect file, I used the following basic syntax:fxc /Tfx_4_0 temp.fxNote that when compiling effects, you don't specify an entrypoint name.   ;  As for the immediately exiting application, I assume that you have handled the case of failed effect compilation in a way somewhat like following://HRESULT hr = result of the compilationif (FAILED(hr) || !pEffect)    return;This kind of code would indeed end your program prematurely. It is a good practice to handle exceptions but doing it like in the above example is not very robust. As a quick fix, you can put breakpoints in places like this (on the "return" row in this example) so you can seamlessly debug the issues. A good long-term solution is to log the exceptions somewhere.   ;Quote:Original post by Nik02You don't write the Normal field of the PS_INPUT structure that you return from the vertex shader. This causes a mismatch between the VS output and PS input, thus preventing the effect to compile.Also, you mix float3's and float4's in several places. For example, see line 107. The logic doesn't make sense to me - do you really want to normalize the difference of world-space camera position and post-projection vertex position?Anyway, you can use the fxc tool (supplied with the SDK) to compile shaders and effects. To compile your effect file, I used the following basic syntax:fxc /Tfx_4_0 temp.fxNote that when compiling effects, you don't specify an entrypoint name.keep in mind that I am a complete newb, so theres alot of stuff you might find that does not make sense. But I will take you guy's help into consideration and let you guy's know how it went. thanks   ;Quote:Original post by Nik02You don't write the Normal field of the PS_INPUT structure that you return from the vertex shader. This causes a mismatch between the VS output and PS input, thus preventing the effect to compile..Ok, so I changed that and now it compiles correctly without turning off on me. The bad news is that it's not shading my plane correctly still. Here is the updated hlsl code// PS_INPUT - input variables to the pixel shader// This struct is created and fill in by the // vertex shadercbuffer Variables{matrix Projection;matrix World;float TimeStep;};struct PS_INPUT{float4 Pos : SV_POSITION;    float4 Color : COLOR0;float3 Normal : TEXCOORD0;float3 ViewVector : TEXCOORD1;};float specpower = 80.0f;float3 camPos = float3(0.0f, 9.0, -256.0f);float3 DirectLightColor  = float3(1.0f, 1.0f, 1.0f);float3 DirectLightVector = float3(0.0f, 0.602f, 0.70f);float3 AmbientLightColor = float3(1.0f, 1.0f, 1.0f);/**************************************** Lighting functions***************************************//********************************** CalculateAmbient - * inputs - *vKa material's reflective color*lightColor - the ambient color of the lightsource* output - ambient color*********************************/float3 CalculateAmbient(float3 vKa, float3 lightColor){float3 vAmbient = vKa * lightColor;return vAmbient;}/********************************** CalculateDiffuse - * inputs - *material color*   The color of the direct light*the local normal*   the vector of the direct light* output - difuse color*********************************/float3 CalculateDiffuse(float3 baseColor, float3 lightColor, float3 normal, float3 lightVector){float3 vDiffuse = baseColor * lightColor * saturate(dot(normal, lightVector));return vDiffuse;}/********************************** CalculateSpecular - * inputs - *viewVector*the direct light vector*   the normal* output - specular highlight*********************************/float CalculateSpecular(float3 viewVector, float3 lightVector, float3 normal){float3 vReflect = reflect(lightVector, normal);float fSpecular = saturate(dot(vReflect, viewVector));fSpecular = pow(fSpecular, specpower);return fSpecular;}/********************************** LightingCombine - * inputs - *ambient component*diffuse component*   specualr component* output - phong color color*********************************/float3 LightingCombine(float3 vAmbient, float3 vDiffuse, float fSpecular){float3 vCombined = vAmbient + vDiffuse + fSpecular.xxx;return vCombined;}////////////////////////////////////////////////// Vertex Shader - Main Function///////////////////////////////////////////////PS_INPUT VS(float4 Pos : POSITION, float4 Color : COLOR, float3 Normal : NORMAL){PS_INPUT psInput;float4 newPosition;newPosition = Pos;newPosition.y = sin((newPosition.x * TimeStep) + (newPosition.z / 3.0f)) * 5.0f;// Pass through both the position and the color    psInput.Pos = mul(newPosition , Projection );psInput.Color = Color;psInput.ViewVector = normalize(camPos - psInput.Pos);psInput.Normal = Normal;    return psInput;}///////////////////////////////////////////////// Pixel Shader/////////////////////////////////////////////////Anthony!!!!!!!!!!! Find out how color works when multiplying themfloat4 PS(PS_INPUT psInput) : SV_Target{float3 normal = -normalize(psInput.Normal);float3 vAmbient = CalculateAmbient(psInput.Color, AmbientLightColor);float3 vDiffuse = CalculateDiffuse(psInput.Color, DirectLightColor, normal, DirectLightVector);float fSpecular = CalculateSpecular(psInput.ViewVector, DirectLightVector, normal);float4 outColor;outColor.rgb = LightingCombine(vAmbient, vDiffuse, fSpecular);outColor.a = 1.0f;//Below is where the error begins    return outColor;}// Define the techniquetechnique10 Render{    pass P0    {   SetVertexShader( CompileShader( vs_4_0, VS() ) );   SetGeometryShader( NULL );   SetPixelShader( CompileShader( ps_4_0, PS() ) );    }}Quote:Also, you mix float3's and float4's in several places. For example, see line 107. The logic doesn't make sense to me - do you really want to normalize the difference of world-space camera position and post-projection vertex position?Alot of the hlsl code I got out of a book. In Visual Studio's, it does not number my lines so I don't know precisely where line 107 is. could you point that out.Quote:Anyway, you can use the fxc tool (supplied with the SDK) to compile shaders and effects. To compile your effect file, I used the following basic syntax:fxc /Tfx_4_0 temp.fxNote that when compiling effects, you don't specify an entrypoint name.I am not an expert on visual studio's and I am pretty new to c++. where would I put fxc /Tfx_4_0 temp.fx to compile my shader.;Quote:Original post by Nik02As for the immediately exiting application, I assume that you have handled the case of failed effect compilation in a way somewhat like following://HRESULT hr = result of the compilationif (FAILED(hr) || !pEffect)    return;This kind of code would indeed end your program prematurely. It is a good practice to handle exceptions but doing it like in the above example is not very robust. As a quick fix, you can put breakpoints in places like this (on the "return" row in this example) so you can seamlessly debug the issues. A good long-term solution is to log the exceptions somewhere.I think you are right. I am pretty new to c++ so I havnt really got all into that. alot of the material I am getting is from a book. So you can somewhat say I learning c++ as I am learning directx. But it seems add a breakpoint would be the least hardest to implement. thanks   ;Quote:Also, you mix float3's and float4's in several places. For example, see line 107. The logic doesn't make sense to me - do you really want to normalize the difference of world-space camera position and post-projection vertex position?Nevermind, I was able to get the number lines turned on. But still, I am not sure what you mean. The code I am looking off of does the same thing. it uses a float4 for pos and a float3 for campos.   ;  Hi again, had some sleep here!Quote:Original post by numerical25The bad news is that it's not shading my plane correctly still. Here is the updated hlsl code...So there is a logic error somewhere. Without trying to run your code, one thing sticks in my eye: in your vertex declaration, you assign R/G/B/A32F (which is a float4) to the normal element (which should be float3). I wouldn't be surprised if this caused a data alignment issue.Quote:I am not an expert on visual studio's and I am pretty new to c++. It would be best to first learn the basics of the language and the tools before tackling D3D - all of these are relatively complex topics. To put this into perspective, imagine that you are a swimming beginner jumping in the deep end of a pool and subsequently wondering why you tend to drown [smile]Quote:where would I put fxc /Tfx_4_0 temp.fx to compile my shader.Fxc is a command-line tool that you can run via the "DirectX command prompt"; this prompt differs from an ordinary command prompt only in that it registers the SDK tool path as an environment variable so you don't have to type the tool path itself. As a command-line tool, it can also be run as part of a pre-build step when you build your project so you can compile your effects and shaders conveniently alongside the rest of your code.I created a file called "temp.fx" from the effect code you posted. The file name is arbitrary. Based on your code, you seem to have named the file as "effect.fx".Quote:The code I am looking off of does the same thing. it uses a float4 for pos and a float3 for campos.In all practical use I've seen, the vector |vertex-camera| uses a vertex transformed by world and view matrices, not projection. In some cases, however, that might be intentional. The results are similar but not exactly the same in either case, because projection transform does not usually introduce drastic rotation or translation of the scene.   ;  well what resources would you suggest for learning IDE tools. cause I got the language down. I am pretty good at c and c++. I just get lost when it comes to running compilers via dos and IDE tools. Cause the books I read don't cover this type of stuff.   ;  I changed the D3D10_INPUT_ELEMENT_DESC for the normal to a r32g32b32 datatype but still does not work. I don't know. I do appreciate the help and effort. below is the code that my code is based off of..cpp file/***************************************************************** example2* this application shows how to use lighting within a scene.* The DirectX d3d10.lib and d3dx10.lib libraries are required * to be linked for this example to work.****************************************************************/#include <windows.h>#include <tchar.h>#include "resource.h"#include <d3d10.h>#include <d3dx10.h>// Globals //////////////////////////////////////////////////////HINSTANCE hInst;HWND mainhWnd;int  windowWidth  = 640;int  windowHeight = 480;// Direct3D global vars /////////////////////////////////////////ID3D10Device* pD3DDevice = NULL;IDXGISwapChain*    pSwapChain = NULL;ID3D10RenderTargetView* pRenderTargetView = NULL;// World MatrixD3DXMATRIX   WorldMatrix;// View MatrixD3DXMATRIX   ViewMatrix;// Projection MatrixD3DXMATRIX   ProjectionMatrix;ID3D10EffectMatrixVariable* pProjectionMatrixVariable = NULL;ID3D10EffectMatrixVariable* pWorldMatrixVariable = NULL;ID3D10EffectScalarVariable* pTimeVariable = NULL;// Triangle Varsstruct VertexPosColorNormalStruct{    D3DXVECTOR3 Pos;D3DXVECTOR4 Color;D3DXVECTOR3 Normal;};typedef struct{    ID3D10Effect* pEffect;    ID3D10EffectTechnique*  pTechnique;    // Vertex information    ID3D10Buffer* pVertexBuffer;ID3D10Buffer* pIndexBuffer;UINTnumVertices;UINTnumIndices;    ID3D10InputLayout* pVertexLayout;}ModelObject;// The single model objectModelObject gModelObject = {0};// timer variablesLARGE_INTEGER timeStart;LARGE_INTEGER timeEnd;LARGE_INTEGER timerFreq;double currentTime;float    anim_rate;// Variable to hold how long since last frame changefloat    lastElaspedFrame = 0;// How long should the frames lastfloat    frameDuration = 0.5;// Grid Information#define NUM_COLS 16#define NUM_ROWS 16#define NUM_VERTSX (NUM_COLS + 1)#define NUM_VERTSY (NUM_ROWS + 1)// The width and height of each cell in the grid#define CELL_WIDTH 32#define CELL_HEIGHT 32// Forward declarations //////////////////////////////////////////bool InitWindow(HINSTANCE hInstance, int width, int height);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);// Direct3D functions ///////////////////////////////////////////bool InitDirect3D(HWND hWnd, int width, int height);void ShutdownDirect3D();void Render();bool InitTerrain(ModelObject *modelObject);void PrepareModelObject(const ModelObject *modelObject);void DrawModelObject(const ModelObject *modelObject);/******************************************************************** WinMain*******************************************************************/int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){// save off the instance handlehInst = hInstance;// Set up the application windowif (!InitWindow(hInstance, windowWidth, windowHeight)){return 0;}    // Initialize Direct3Dif (!InitDirect3D(mainhWnd, windowWidth, windowHeight)){return 0;}// Init the grid for the terrainif (!InitTerrain(&gModelObject)){return 0;}// The raster description structure. This allows you // to change the filling and culling mode. D3D10_RASTERIZER_DESC rasterDescription;rasterDescription.FillMode = D3D10_FILL_SOLID;rasterDescription.CullMode = D3D10_CULL_NONE;    rasterDescription.FrontCounterClockwise = true;    rasterDescription.DepthBias = false;    rasterDescription.DepthBiasClamp = 0;    rasterDescription.SlopeScaledDepthBias = 0;    rasterDescription.DepthClipEnable = false;    rasterDescription.ScissorEnable = false;    rasterDescription.MultisampleEnable = false;    rasterDescription.AntialiasedLineEnable = false;// Create a new raster stateID3D10RasterizerState *g_pRasterState;pD3DDevice->CreateRasterizerState( &rasterDescription, &g_pRasterState );// Set the new raster statepD3DDevice->RSSetState(g_pRasterState);// Get the timer frequencyQueryPerformanceFrequency(&timerFreq);float freqSeconds = 1.0f / timerFreq.QuadPart;// set the frame change count to 0lastElaspedFrame = 0;// Main message loop    MSG msg = {0};    while (WM_QUIT != msg.message)    {// Process Windows messages first   while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)   {  TranslateMessage(&msg);  DispatchMessage(&msg);   }    // Get the start timer countQueryPerformanceCounter(&timeStart);currentTime += anim_rate;Render();// Get the end timer countQueryPerformanceCounter(&timeEnd);anim_rate = ( (float)timeEnd.QuadPart - (float)timeStart.QuadPart ) / timerFreq.QuadPart;float temp = (timeEnd.QuadPart - timeStart.QuadPart) * freqSeconds;int ter = 3;    }// Clean up the resources we allocatedShutdownDirect3D();return (int) msg.wParam;}/******************************************************************** InitWindow* Inits and creates and main app window* Inputs - application instance - HINSTANCE   Window width - int   Window height - int* Outputs - true if successful, false if failed - bool*******************************************************************/bool InitWindow(HINSTANCE hInstance, int width, int height){    // Register class    WNDCLASSEX wcex;    wcex.cbSize = sizeof(WNDCLASSEX);wcex.style= CS_HREDRAW | CS_VREDRAW;    wcex.lpfnWndProc    = WndProc;    wcex.cbClsExtra= 0;    wcex.cbWndExtra= 0;    wcex.hInstance = hInstance;    wcex.hIcon= 0;    wcex.hCursor   = LoadCursor(NULL, IDC_ARROW);    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);    wcex.lpszMenuName   = NULL;    wcex.lpszClassName  = TEXT("DirectXExample");    wcex.hIconSm   = 0;    if(!RegisterClassEx(&wcex)){   return false;}    // Create window    RECT rect = { 0, 0, width, height };    AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);// create the window from the class above    mainhWnd = CreateWindow(TEXT("DirectXExample"), TEXT("DirectXExample"), WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL);    if(!mainhWnd){   return false;}    ShowWindow(mainhWnd, SW_SHOW);UpdateWindow(mainhWnd);  return true;}/******************************************************************** WndProc* The main window procedure for the application* Inputs - application window handle - HWND   message sent to the window - UINT   wParam of the message being sent - WPARAM   lParam of the message being sent - LPARAM* Outputs - LRESULT*******************************************************************/LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    switch (message){// Allow the user to press the escape key to end the application   case WM_KEYDOWN:switch(wParam){// Check if the user hit the escape keycase VK_ESCAPE:PostQuitMessage(0);break;}   break;// The user hit the close button, close the application   case WM_DESTROY:  PostQuitMessage(0);  break;    }    return DefWindowProc(hWnd, message, wParam, lParam);}/******************************************************************** InitDirect3D* Initializes Direct3D* Inputs - Parent window handle - HWND,   Window width - int   Window height - int* Outputs - true if successful, false if failed - bool*******************************************************************/bool InitDirect3D(HWND hWnd, int width, int height){// Create the clear the DXGI_SWAP_CHAIN_DESC structureDXGI_SWAP_CHAIN_DESC swapChainDesc;    ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));// Fill in the needed values    swapChainDesc.BufferCount = 1;    swapChainDesc.BufferDesc.Width = width;    swapChainDesc.BufferDesc.Height = height;    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;    swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    swapChainDesc.OutputWindow = hWnd;swapChainDesc.SampleDesc.Count = 1;    swapChainDesc.SampleDesc.Quality = 0;    swapChainDesc.Windowed = TRUE;// Create the D3D device and the swap chain    HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,D3D10_DRIVER_TYPE_HARDWARE, NULL, 0,D3D10_SDK_VERSION, &swapChainDesc,&pSwapChain, &pD3DDevice);// Error checking. Make sure the device was created    if (FAILED(hr)){   MessageBox(hWnd, TEXT("A DX10 Compliant Video Card is Required"), TEXT("ERROR"), MB_OK);return false;}    // Get the back buffer from the swapchain    ID3D10Texture2D *pBackBuffer;    hr = pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);    if (FAILED(hr)){   return false;}// create the render target view    hr = pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);// release the back buffer    pBackBuffer->Release();// Make sure the render target view was created successfullyif (FAILED(hr)){   return false;}// set the render target    pD3DDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);    // create and set the viewport    D3D10_VIEWPORT viewPort;    viewPort.Width = width;    viewPort.Height = height;    viewPort.MinDepth = 0.0f;    viewPort.MaxDepth = 1.0f;    viewPort.TopLeftX = 0;    viewPort.TopLeftY = 0;    pD3DDevice->RSSetViewports(1, &viewPort);// Set up the World MatrixD3DXMatrixIdentity(&WorldMatrix);// Set up the view matrixD3DXMatrixLookAtLH(&ViewMatrix, new D3DXVECTOR3(0.0f, 96, -300.0f),new D3DXVECTOR3(0.0f, 0.0f, 1.0f),new D3DXVECTOR3(0.0f, 1.0f, 0.0f));// Set up the projection matrix    D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, (float)D3DX_PI * 0.5f, (float)width/(float)height, 0.0f, 500.0f);return true;}/******************************************************************** ShutdownDirect3D* Closes down and releases the resources for Direct3D* Inputs - void* Outputs - void*******************************************************************/void ShutdownDirect3D(){// release the effect objectif (gModelObject.pEffect){gModelObject.pEffect->Release();}// release the vertex bufferif (gModelObject.pVertexBuffer){gModelObject.pVertexBuffer->Release();}// Release the vertex layoutif (gModelObject.pVertexLayout){gModelObject.pVertexLayout->Release();}// release the rendertargetif (pRenderTargetView) {pRenderTargetView->Release();}// release the swapchain    if (pSwapChain){pSwapChain->Release();}// release the D3D Device    if (pD3DDevice) {pD3DDevice->Release();}}/******************************************************************** Render* All drawing happens in the Render function* Inputs - void* Outputs - void*******************************************************************/void Render(){if (pD3DDevice != NULL){// clear the target bufferpD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(1.0f, 1.0f, 1.0f, 0.0f));// Prepare the pipeline to draw the model objectPrepareModelObject(&gModelObject);// Draw a single model objectDrawModelObject(&gModelObject);// display the next item in the swap chainpSwapChain->Present(0, 0);}}/******************************************************************** InitTerrain* Initializes a ModelObject struct with the data to draw a single*triangle* Inputs - ModelObject *modelObject - the modelObject struct to fill* Outputs - bool - true if successful*******************************************************************/bool InitTerrain(ModelObject *modelObject){    // Create the effectHRESULT hr = D3DX10CreateEffectFromFile(L".\\simple.fx", NULL, NULL, "fx_4_0",D3D10_SHADER_ENABLE_STRICTNESS, 0, pD3DDevice, NULL, NULL, &modelObject->pEffect, NULL,NULL);    if (FAILED(hr))    {   return false;    }// Obtain the techniquemodelObject->pTechnique = modelObject->pEffect->GetTechniqueByName("Render");// Bind the projection matrix to the Projection variable in the shaderpProjectionMatrixVariable = modelObject->pEffect->GetVariableByName("Projection")->AsMatrix();pWorldMatrixVariable = modelObject->pEffect->GetVariableByName("World")->AsMatrix();pTimeVariable   = modelObject->pEffect->GetVariableByName("TimeStep")->AsScalar();    // The vertex input layout    D3D10_INPUT_ELEMENT_DESC layout[] =    {   { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },  { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },{ "NORMAL", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0 },};// Calculate the number of elements in the layout array    UINT numElements = (sizeof(layout) / sizeof(layout[0]));    // Create the vertex input layout    D3D10_PASS_DESC PassDesc;    modelObject->pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc);    hr = pD3DDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &modelObject->pVertexLayout);    if(FAILED(hr)){   return false;}// create the vertices array large enough to hold all those neededVertexPosColorNormalStruct vertices[NUM_VERTSX * NUM_VERTSY];// Fill the vertices array with the terrain valuesfor(int z=0; z < NUM_VERTSY; ++z){for(int x=0; x < NUM_VERTSX; ++x){vertices[x + z * NUM_VERTSX].Pos.x = (float)x * CELL_WIDTH;vertices[x + z * NUM_VERTSX].Pos.z = (float)z * CELL_HEIGHT;// Allow the height of the cell to be randomly decidedvertices[x + z * NUM_VERTSX].Pos.y = float(rand() % 64);// Create the default colorvertices[x + z * NUM_VERTSX].Color = D3DXVECTOR4(0.0f, 0.0f, 0.5f, 0.0f);// create a default normalvertices[x + z * NUM_VERTSX].Normal = D3DXVECTOR3(0.0f, 0.0f, 0.0f);}}// Calculate the number of vertices in the arraymodelObject->numVertices = sizeof(vertices) / sizeof(VertexPosColorNormalStruct);// Create the indices arrayDWORD indices[NUM_COLS * NUM_ROWS * 6];// The index counterint curIndex = 0;// Fill the indices array to create the triangles needed for the terrain// The triangles are created in a counter-clockwise directionfor (int z=0; z < NUM_ROWS; z++){for (int x=0; x < NUM_COLS; x++){// The current vertex to build off ofint curVertex = x + (z * NUM_VERTSX);// Create the indices for the first triangleindices[curIndex]   = curVertex;indices[curIndex+1] = curVertex + NUM_VERTSX;indices[curIndex+2] = curVertex + 1;// Create the indices for the second triangleindices[curIndex+3] = curVertex + 1;indices[curIndex+4] = curVertex + NUM_VERTSX;indices[curIndex+5] = curVertex + NUM_VERTSX + 1;// increment curIndex by the number of vertices for the two trianglescurIndex += 6;}}// determine the number of incicesmodelObject->numIndices = sizeof(indices) / sizeof(DWORD);// compute normals for each face in the modelfor (unsigned int i = 0; i < modelObject->numIndices; i+=3){D3DXVECTOR3 v0 = vertices[indices].Pos;D3DXVECTOR3 v1 = vertices[indices].Pos;<br>D3DXVECTOR3 v2 = vertices[indices].Pos;<br><br>D3DXVECTOR3 normal;<br>D3DXVECTOR3 cross;<br>D3DXVec3Cross(&cross, &D3DXVECTOR3(v2 - v0), &D3DXVECTOR3(v1 - v0));<br>D3DXVec3Normalize(&normal, &cross);<br><br>// assign the computed normal to each vertex in this face<br>vertices[indices<span style="font-weight:bold;">].Normal= normal;<br>vertices[indices].Normal = normal;<br>vertices[indices].Normal = normal;<br>}<br><br>// create the vertex buffer<br>D3D10_BUFFER_DESC bd;<br>    bd.Usage = D3D10_USAGE_DEFAULT;<br>bd.ByteWidth = sizeof(VertexPosColorNormalStruct) * modelObject-&gt;numVertices;<br>    bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;<br>    bd.CPUAccessFlags = 0;<br>    bd.MiscFlags = 0;<br>    <br>D3D10_SUBRESOURCE_DATA InitData;<br>    InitData.pSysMem = vertices;<br><br>// Do the creation of the actual vertex buffer<br>    hr = pD3DDevice-&gt;CreateBuffer(&bd, &InitData, &modelObject-&gt;pVertexBuffer);<br>    if(FAILED(hr))<br>{<br>   return false;<br>}<br><br>// create the index buffer<br>bd.Usage = D3D10_USAGE_DEFAULT;<br>    bd.ByteWidth = sizeof(DWORD) * modelObject-&gt;numIndices;<br>    bd.BindFlags = D3D10_BIND_INDEX_BUFFER;<br>    bd.CPUAccessFlags = 0;<br>    bd.MiscFlags = 0;<br>    InitData.pSysMem = indices;<br>   <br>// Create the index buffer<br>hr = pD3DDevice-&gt;CreateBuffer(&bd, &InitData, &modelObject-&gt;pIndexBuffer);<br>    if (FAILED(hr))<br>{<br>   return false;<br>}<br><br>return true;<br>}<br><br>/*******************************************************************<br>* PrepareModelObject<br>* All drawing happens in the Render function<br>* Inputs - const ModelObject *modelObject - the ModelObject to <br>*draw<br>* Outputs - void<br>*******************************************************************/<br>void PrepareModelObject(const ModelObject *modelObject)<br>{<br>// Prepare and Send the final matrix to the shader<br>D3DXMATRIX translationMatrix;<br>D3DXMatrixTranslation(&translationMatrix, -256, 0, -256);<br><br>pWorldMatrixVariable-&gt;SetMatrix(translationMatrix);<br><br>D3DXMATRIX finalMatrix = (translationMatrix * ViewMatrix * ProjectionMatrix);<br>    pProjectionMatrixVariable-&gt;SetMatrix((float*)&finalMatrix);<br><br>pTimeVariable-&gt;SetFloat((float)currentTime);<br><br>    // Set the input layout<br>    pD3DDevice-&gt;IASetInputLayout(modelObject-&gt;pVertexLayout);<br><br>    // Set vertex buffer<br>    UINT stride = sizeof(VertexPosColorNormalStruct);<br>    UINT offset = 0;<br>    pD3DDevice-&gt;IASetVertexBuffers(0, 1, &modelObject-&gt;pVertexBuffer, &stride, &offset);<br><br>// Set index buffer<br>pD3DDevice-&gt;IASetIndexBuffer(modelObject-&gt;pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);<br><br>    // Set primitive topology<br>    pD3DDevice-&gt;IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);<br><br>}<br><br>/*******************************************************************<br>* DrawModelObject<br>* Draws a single model object<br>* Inputs - const ModelObject *modelObject<br>* Outputs - void<br>*******************************************************************/<br>void DrawModelObject(const ModelObject *modelObject)<br>{<br>// make sure modelObject is valid<br>if (modelObject == NULL)<br>{<br>return;<br>}<br><br>    // Render a model object<br>    D3D10_TECHNIQUE_DESC techniqueDescription;<br>    modelObject-&gt;pTechnique-&gt;GetDesc(&techniqueDescription);<br><br>// Loop through the technique passes<br>    for(UINT p=0; p &lt; techniqueDescription.Passes; ++p)<br>    {<br>   modelObject-&gt;pTechnique-&gt;GetPassByIndex(p)-&gt;Apply(0);<br>pD3DDevice-&gt;DrawIndexed(modelObject-&gt;numIndices, 0, 0);<br>    }<br>}<br><br></pre><br><br><br><br>Then it has a .fxh file<br><br><pre><br>//Calculate Ambient///////////////////////////////////////<br>float3 CalculateAmbient(float3 vKa, float3 lightColor)<br>{<br>float3 vAmbient = vKa * lightColor;<br>return vAmbient;<br>}<br>/////////////////////////////////////////////<br><br><br>float3 CalculateDiffuse(float3 baseColor, float3 lightColor, float3 normal, float3 lightVector)<br>{<br>float3 vDiffuse = baseColor * lightColor * saturate(dot(normal, lightVector));<br>return vDiffuse;<br>}<br><br>float CalculateSpecular(float3 viewVector, float3 lightVector, float3 normal)<br>{<br>float3 vReflect = reflect(lightVector, normal);<br><br>float fSpecular = saturate(dot(vReflect, viewVector));<br>fSpecular = pow(fSpecular, specpower);<br><br>return fSpecular;<br>}<br><br>float3 LightingCombine(float3 vAmbient, float3 vDiffuse, float fSpecular)<br>{<br>float3 vCombined = vAmbient + vDiffuse + fSpecular.xxx;<br><br>return vCombined;<br>}<br></pre><br><br><br>Then here is the actually fx file<br><br><pre><br>// PS_INPUT - input variables to the pixel shader<br>// This struct is created and fill in by the <br>// vertex shader<br>cbuffer Variables<br>{<br>matrix Projection;<br>matrix World;<br>float TimeStep;<br>};<br><br>struct PS_INPUT<br>{<br>float4 Pos : SV_POSITION;<br>    float4 Color : COLOR0;<br>float3 Normal : TEXCOORD0;<br>float3 ViewVector : TEXCOORD1;<br>};<br><br>float specpower = 80.0f;<br>float3 camPos = float3(0.0f, 9.0, -256.0f);<br>float3 DirectLightColor  = float3(1.0f, 1.0f, 1.0f);<br>float3 DirectLightVector = float3(0.0f, 0.602f, 0.70f);<br>float3 AmbientLightColor = float3(1.0f, 1.0f, 1.0f);<br><br>/***************************************<br>* Lighting functions<br>***************************************/<br><br>/*********************************<br>* CalculateAmbient - <br>* inputs - <br>*vKa material's reflective color<br>*lightColor - the ambient color of the lightsource<br>* output - ambient color<br>*********************************/<br>float3 CalculateAmbient(float3 vKa, float3 lightColor)<br>{<br>float3 vAmbient = vKa * lightColor;<br>return vAmbient;<br>}<br><br>/*********************************<br>* CalculateDiffuse - <br>* inputs - <br>*material color<br>*   The color of the direct light<br>*the local normal<br>*   the vector of the direct light<br>* output - difuse color<br>*********************************/<br>float3 CalculateDiffuse(float3 baseColor, float3 lightColor, float3 normal, float3 lightVector)<br>{<br>float3 vDiffuse = baseColor * lightColor * saturate(dot(normal, lightVector));<br><br>return vDiffuse;<br>}<br><br><br>/*********************************<br>* CalculateSpecular - <br>* inputs - <br>*viewVector<br>*the direct light vector<br>*   the normal<br>* output - specular highlight<br>*********************************/<br>float CalculateSpecular(float3 viewVector, float3 lightVector, float3 normal)<br>{<br>float3 vReflect = reflect(lightVector, normal);<br><br>float fSpecular = saturate(dot(vReflect, viewVector));<br>fSpecular = pow(fSpecular, specpower);<br><br>return fSpecular;<br>}<br><br>/*********************************<br>* LightingCombine - <br>* inputs - <br>*ambient component<br>*diffuse component<br>*   specualr component<br>* output - phong color color<br>*********************************/<br>float3 LightingCombine(float3 vAmbient, float3 vDiffuse, float fSpecular)<br>{<br>float3 vCombined = vAmbient + vDiffuse + fSpecular.xxx;<br><br>return vCombined;<br>}<br><br>////////////////////////////////////////////////<br>// Vertex Shader - Main Function<br>///////////////////////////////////////////////<br>PS_INPUT VS(float4 Pos : POSITION, float4 Color : COLOR, float3 Normal : NORMAL)<br>{<br>PS_INPUT psInput;<br>float4 newPosition;<br>newPosition = Pos;<br><br>newPosition.y = sin((newPosition.x * TimeStep) + (newPosition.z / 3.0f)) * 5.0f;<br><br>// Pass through both the position and the color<br>    psInput.Pos = mul(newPosition , Projection );<br>psInput.Color = Color;<br>psInput.ViewVector = normalize(camPos - psInput.Pos);<br>psInput.Normal = Normal;<br><br>    return psInput;<br>}<br><br><br>///////////////////////////////////////////////<br>// Pixel Shader<br>///////////////////////////////////////////////<br>//Anthony!!!!!!!!!!! Find out how color works when multiplying them<br><br>float4 PS(PS_INPUT psInput) : SV_Target<br>{<br>float3 normal = -normalize(psInput.Normal);<br>float3 vAmbient = CalculateAmbient(psInput.Color, AmbientLightColor);<br>float3 vDiffuse = CalculateDiffuse(psInput.Color, DirectLightColor, normal, DirectLightVector);<br><br>float fSpecular = CalculateSpecular(psInput.ViewVector, DirectLightVector, normal);<br><br>float4 outColor;<br>outColor.rgb = LightingCombine(vAmbient, vDiffuse, fSpecular);<br>outColor.a = 1.0f;<br>//Below is where the error begins<br><br>    return outColor;<br>}<br><br><br><br>// Define the technique<br>technique10 Render<br>{<br>    pass P0<br>    {<br>   SetVertexShader( CompileShader( vs_4_0, VS() ) );<br>   SetGeometryShader( NULL );<br>   SetPixelShader( CompileShader( ps_4_0, PS() ) );<br>    }<br>}<br><br></pre>