   Issues Shading my object. Object appears but is not shaded with directional light
  I have a simple cube and I am trying to shade it based on directional lighting. For some reason it compiles but does not shade. I have yet to find a solution. Here is my code along with my .fx fileThe issue is very basic. Its a simple cube that gets shaded based on the location of a light. I provided an image of the cube. it compiles fine but does not shade. I also tried downloading the debugview and compiling but that does not show anything either and yes I turned debug to forced on in the direct x configurations.Theres enough code that provides me creating the vertex buffer, the vertex layout and also setting the normals. Again, I appreciate any feedback and any help will be greatly appreciated thanks!#mygame.cpp#include "MyGame.h"#include "OneColorCube.h"typedef struct {ID3D10Effect* pEffect;ID3D10EffectTechnique* pTechnique;//vertex informationID3D10Buffer* pVertexBuffer;ID3D10Buffer* pIndicesBuffer;ID3D10InputLayout* pVertexLayout;UINT numVertices;UINT numIndices;}ModelObject;ModelObject modelObject;// World MatrixD3DXMATRIX   WorldMatrix;// View MatrixD3DXMATRIX   ViewMatrix;// Projection MatrixD3DXMATRIX   ProjectionMatrix;ID3D10EffectMatrixVariable* pProjectionMatrixVariable = NULL;ID3D10EffectVectorVariable* pLightVarible = NULL;bool MyGame::InitDirect3D(){if(!DX3dApp::InitDirect3D()){return false;}D3D10_RASTERIZER_DESC rastDesc;rastDesc.FillMode = D3D10_FILL_WIREFRAME;rastDesc.CullMode = D3D10_CULL_FRONT;rastDesc.FrontCounterClockwise = true;rastDesc.DepthBias = false;rastDesc.DepthBiasClamp = 0;rastDesc.SlopeScaledDepthBias = 0;rastDesc.DepthClipEnable = false;rastDesc.ScissorEnable = false;rastDesc.MultisampleEnable = false;rastDesc.AntialiasedLineEnable = false;ID3D10RasterizerState *g_pRasterizerState;mpD3DDevice->CreateRasterizerState(&rastDesc, &g_pRasterizerState);//mpD3DDevice->RSSetState(g_pRasterizerState);// Set up the World MatrixD3DXMatrixIdentity(&WorldMatrix);D3DXMatrixLookAtLH(&ViewMatrix, new D3DXVECTOR3(0.0f, 10.0f, -20.0f), new D3DXVECTOR3(0.0f, 0.0f, 0.0f), new D3DXVECTOR3(0.0f, 1.0f, 0.0f));// Set up the projection matrix    D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, (float)D3DX_PI * 0.5f, (float)mWidth/(float)mHeight, 0.1f, 100.0f);if(!CreateObject()){return false;}return true;}//These are actions that take place after the clearing of the buffer and before the presentvoid MyGame::GameDraw(){static float rotationAngleY = 15.0f;static float rotationAngleX = 0.0f;static D3DXMATRIX rotationXMatrix;static D3DXMATRIX rotationYMatrix;// create the rotation matrix using the rotation angleD3DXMatrixRotationY(&rotationYMatrix, rotationAngleY);D3DXMatrixRotationX(&rotationXMatrix, rotationAngleX);    //rotationAngleY += (float)D3DX_PI * 0.002f;//rotationAngleX += (float)D3DX_PI * 0.001f;WorldMatrix = rotationYMatrix * rotationXMatrix;    // Set the input layoutmpD3DDevice->IASetInputLayout(modelObject.pVertexLayout);    // Set vertex buffer    UINT stride = sizeof(VertexPos);    UINT offset = 0;    mpD3DDevice->IASetVertexBuffers(0, 1, &modelObject.pVertexBuffer, &stride, &offset);    // Set primitive topologympD3DDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);//ViewMatrix._43 += 0.005f;// Combine and send the final matrix to the shaderD3DXMATRIX finalMatrix = (WorldMatrix * ViewMatrix * ProjectionMatrix);    pProjectionMatrixVariable->SetMatrix((float*)&finalMatrix);// make sure modelObject is valid    // Render a model object    D3D10_TECHNIQUE_DESC techniqueDescription;    modelObject.pTechnique->GetDesc(&techniqueDescription);// Loop through the technique passes    for(UINT p=0; p < techniqueDescription.Passes; ++p)    {   modelObject.pTechnique->GetPassByIndex(p)->Apply(0);// draw the cube using all 36 vertices and 12 trianglesmpD3DDevice->Draw(36,0);    }}//Render actually incapsulates Gamedraw, so you can call data before you actually clear the buffer or after you //present datavoid MyGame::Render(){DX3dApp::Render();}bool MyGame::CreateObject(){//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 , 24, D3D10_INPUT_PER_VERTEX_DATA, 0}};UINT numElements = (sizeof(layout)/sizeof(layout[0]));modelObject.numVertices = sizeof(vertices)/sizeof(VertexPos);for(int i = 0; i < modelObject.numVertices; i += 3){D3DXVECTOR3 out;D3DXVECTOR3 v1 = vertices[0 + i].pos;D3DXVECTOR3 v2 = vertices[1 + i].pos;D3DXVECTOR3 v3 = vertices[2 + i].pos;D3DXVECTOR3 u = v2 - v1;D3DXVECTOR3 v = v3 - v1;D3DXVec3Cross(&out, &u, &v);D3DXVec3Normalize(&out, &out);vertices[0 + i].normal = out;vertices[1 + i].normal = out;vertices[2 + i].normal = out;}//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->CreateBuffer(&bufferDesc, &initData, &modelObject.pVertexBuffer);if(FAILED(hr))return false;/*//Create indicesDWORD indices[] ={0,1,3,1,2,3};ModelObject.numIndices = sizeof(indices)/sizeof(DWORD);bufferDesc.ByteWidth = sizeof(DWORD) * ModelObject.numIndices;bufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;initData.pSysMem = indices;hr = mpD3DDevice->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->GetVariableByName("Projection")->AsMatrix();pLightVarible = modelObject.pEffect->GetVariableByName("lightSource")->AsVector();//Dont sweat the technique. Get it!LPCSTR effectTechniqueName = "Render";D3DXVECTOR3 vLight(10.0f, 10.0f, 10.0f);pLightVarible->SetFloatVector(vLight);modelObject.pTechnique = modelObject.pEffect->GetTechniqueByName(effectTechniqueName);if(modelObject.pTechnique == NULL)return false;//Create Vertex layoutD3D10_PASS_DESC passDesc;modelObject.pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);hr = mpD3DDevice->CreateInputLayout(layout, numElements,passDesc.pIAInputSignature,passDesc.IAInputSignatureSize,&modelObject.pVertexLayout);if(FAILED(hr))return false;return true;}#effect.fxmatrix Projection;float3 lightSource;float4 lightColor = {0.5, 0.5, 0.5, 0.5};// PS_INPUT - input variables to the pixel shader// This struct is created and fill in by the // vertex shaderstruct PS_INPUT{float4 Pos : SV_POSITION;   float4 Color : COLOR0;float4 Normal : NORMAL;};////////////////////////////////////////////////// Vertex Shader - Main Function///////////////////////////////////////////////PS_INPUT VS(float4 Pos : POSITION, float4 Color : COLOR, float4 Normal : NORMAL){PS_INPUT psInput;// Pass through both the position and the color    psInput.Pos = mul( Pos, Projection );psInput.Color = Color;psInput.Normal = Normal;    return psInput;}///////////////////////////////////////////////// Pixel Shader///////////////////////////////////////////////float4 PS(PS_INPUT psInput) : SV_Target{float4 finalColor = 0; finalColor = saturate(dot(lightSource, psInput.Normal) * lightColor);return finalColor;}// Define the techniquetechnique10 Render{    pass P0    {   SetVertexShader( CompileShader( vs_4_0, VS() ) );   SetGeometryShader( NULL );   SetPixelShader( CompileShader( ps_4_0, PS() ) );    }}In a nutshell, alot of the code is in the createobject() function. I set the surface normals and store them in the vertices. I then pass the lighting values to the shader varible.Below is a screen shotHere is my vertex structstruct VertexPos{D3DXVECTOR3 pos;D3DXVECTOR4 color;D3DXVECTOR3 normal;};#endif[Edited by - numerical25 on May 27, 2010 1:13:14 PM]
  O come on guys. Is there something I am not providing ??? I have got 0 feedback from this site so far. O well, guess I got to find a better forum. Anyone know of any good direct x forums??   ;Well, I guess it's safe to say that many folks here would really like to help, but you're not really making it easy for us to do so. Your problem description "does not shade" tells us very little. Is the object visible or not? Are the colors off? Is the lighting going wrong? A screenshot or at least some more detailed description would have been very helpful to enable us to help you.Instead you post a long code listing and seem to expect us to go figure it out. I happened to check out a few of your earlier topics and you seem to be often posting such listings. As much as we'd like to help, it's a bit of a chore to go reading all the way through it or setting up a test project with your code ourselves, just to figure out what the problem actually might be in the first place. Dumping your code on us like that comes across as a bit lazy and while I'm sure that's not your intent, it might put people off who could have helped you out.Anyway, since you have some working code around, it might be a good idea to toy around with it a bit yourself. Simplify the pixel shader to return some fixed color to see if that's behaving. If that works, try returning the normals as color to see if they look somewhat correct. Then return the dotproduct to see if the lighting calculation is correct and so on. Going about it like this should help you isolate the problem.;  Yes! feedback! My bad, I didn't know dumping alot of code was bad. I just wanted to make sure I provided enough code. Not sure if you actually looked at the code, but in any case, I added the code that shows me creating the vertex buffer, adding the normals , and added the effect.fx file as well as adding the variables and updating it frame by frame. I am a newb, so I am not familuar with how to setup pix, I would like to some day, but right now I just want to shade this cube based on light.As mentioned before, its a simple cube that has 1 directional light and it shades the cube based on it's position. But it does not shade at all.. I am trying to get the angle (dot product ) of the the 2 lights and shade the light according to angle. If you look at the pixel shader you will see the formula for that. I am trying to debug this issue, ive been doing so for about 2 weeks, sadly. If your conception is of me dumping all this on you guys and not caring to do it myself, then I would suspect you have that conception on everyone who makes a post.Ive updated my original post, hopefully that will help.   ;  here's few suggestions1. try to ouput normals in pixel shader[to see if they actually are here]2. check Input Layout[especially offsets] you have error there, you have format RGBA32[4*4=16][and you set 12 which is 3*4 not 4*4]3. you don't supply the color for your vertices in bool MyGame::CreateObject()best guess from what u provided is bad normals   ;Quote:Original post by e3d_ALiVEhere's few suggestions1. try to ouput normals in pixel shader[to see if they actually are here]2. check Input Layout[especially offsets] you have error there, you have format RGBA32[4*4=16][and you set 12 which is 3*4 not 4*4]3. you don't supply the color for your vertices in bool MyGame::CreateObject()best guess from what u provided is bad normalsHi e3d_ALiVE , thanks for the feed back I appreciate it. But I figured it out. And to a lot of you this is probably a very newbish error. but I had no clue cause I am a newb. on the cpu side I supplied light source locationD3DXVECTOR3 vLight(10.0f, 10.0f, 10.0f);As you can see, this is not valid. I had to normalize the directionD3DXVECTOR3 vLight(1.0f, 1.0f, 1.0f);Then it finally shaded correctly. I did supply this in the code above. And yes possibly I should of supplied the vertices definition. But this was not the issue. as I demonstrated above. Thanks all.;  i'm glad it worked;  Are you learning DirectX with the book "Beginning DirectX 10 Game Programming" of Wendy Jones (I recognize the ModelObject struct...)?I also started with that one, but I threw it quickly away, it really sucks.She doesn't even use a depth buffer during the whole book I think.. . The only thing you learn is how to draw a cube and some ugly terrainy thing.   