2014-07-22 2 views
-1

현재 나는이 자습서에서 다이렉트 X 11을 배우고 : http://www.rastertek.com/tutdx11.html다이렉트 X 11 확산 조명 문제

자습서는 잘 작동하고 그래서 난 내 작은 엔진에서 일을 구현했습니다. 문제는 directx가 모든 다각형에 확산 라이트를 렌더링하지 않으며 빛의 방향이 잘못되었다는 것입니다. 여기

//The vertex structure 
struct BE_SVERTEX{ 
    BE_SVERTEX(){ 

    } 

    BE_SVERTEX(D3DXVECTOR3 position, D3DXVECTOR2 texture, D3DXVECTOR3 normal){ 
     this->position = position; 
     this->texture = texture; 
     this->normal = normal; 
    } 

    BE_SVERTEX(const BE_SVERTEX &v){ 
     this->normal = v.normal; 
     this->position = v.position; 
     this->texture = v.texture; 
    } 

    D3DXVECTOR3 position; 
    D3DXVECTOR2 texture; 
    D3DXVECTOR3 normal; 
}; 

내가 정점 색인 버퍼를 생성하고 : 여기서 https://www.dropbox.com/s/9m94n1q7oichi2b/DX11Plane.PNG

소스 코드의 일부이다 :

여기

두 다각형 렌더링 평면의 픽처
 aVertices = new BE_SVERTEX[vVertices.size()]; 
     aIndices = new unsigned long[vIndices.size()]; 

     //Filling the vertex and index array with the data from the wavefront file 
     for (int i = 0; i < vVertices.size(); i++){ 
      //To test the normalvector of the plane i have set the normalvectors from all vertices of the plane manually 
      aVertices[i].normal = D3DXVECTOR3{ 0.0f, 0.0f, -1.0f}; 
      aVertices[i].position = vVertices[i].position; 
      aVertices[i].texture = vVertices[i].texture; 
      //The indices for the index buffer. Currently the index buffer is unnecessary because the engine creates for each polygon 3 new vertices. 
      aIndices[i] = vIndices[i]; 
     } 

     //Creating the vertex buffer 
     vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT; 
     vertexBufferDesc.ByteWidth = sizeof(BE_SVERTEX) * vVertices.size(); 
     vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 
     vertexBufferDesc.CPUAccessFlags = 0; 
     vertexBufferDesc.MiscFlags = 0; 
     vertexBufferDesc.StructureByteStride = 0; 

     vertexData.pSysMem = aVertices; 
     vertexData.SysMemPitch = 0; 
     vertexData.SysMemSlicePitch = 0; 

     mesh->vertexBuffer = NULL; 

     result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &mesh->vertexBuffer); 
     if (FAILED(result)){ 
      return BE_SRESULT{ 7, false }; 
     } 
     else{ 
      //Creating the index buffer 
      indexBufferDesc.Usage = D3D11_USAGE_DEFAULT; 
      indexBufferDesc.ByteWidth = sizeof(unsigned long) * vIndices.size(); 
      indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; 
      indexBufferDesc.CPUAccessFlags = 0; 
      indexBufferDesc.MiscFlags = 0; 
      indexBufferDesc.StructureByteStride = 0; 

      indexData.pSysMem = aIndices; 
      indexData.SysMemPitch = 0; 
      indexData.SysMemSlicePitch = 0; 

      mesh->indexBuffer = NULL; 

      result = device->CreateBuffer(&indexBufferDesc, &indexData, &mesh-  >indexBuffer); 
      if (FAILED(result)){ 
       return BE_SRESULT{ 7, false }; 
      } 

쉐이더 생성의 일부 :

,515,
 //Creating the input layout 
     polygonLayout[0].SemanticName = "POSITION"; 
     polygonLayout[0].SemanticIndex = 0; 
     polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; 
     polygonLayout[0].InputSlot = 0; 
     polygonLayout[0].AlignedByteOffset = 0; 
     polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
     polygonLayout[0].InstanceDataStepRate = 0; 

     polygonLayout[1].SemanticName = "TEXCOORD"; 
     polygonLayout[1].SemanticIndex = 0; 
     polygonLayout[1].Format = DXGI_FORMAT_R32G32B32_FLOAT; 
     polygonLayout[1].InputSlot = 0; 
     polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; 
     polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
     polygonLayout[1].InstanceDataStepRate = 0; 

     polygonLayout[2].SemanticName = "NORMAL"; 
     polygonLayout[2].SemanticIndex = 0; 
     polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT; 
     polygonLayout[2].InputSlot = 0; 
     polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; 
     polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
     polygonLayout[2].InstanceDataStepRate = 0; 

     numElements = sizeof(polygonLayout)/sizeof(polygonLayout[0]); 

     result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &layout); 

셰이더의 렌더링 함수 :

//Setting rendering settings. This methode is called every frame before shader rendering. 
BE_SRESULT ObjectElements::BE_OE_Shader::setShaderParameters(D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix, ID3D11ShaderResourceView *texture, D3DXVECTOR3 lightDirection, D3DXVECTOR4 diffuseColor){ 
    HRESULT result; 
    D3D11_MAPPED_SUBRESOURCE mappedResource; 
    unsigned int bufferNumber; 
    MatrixBufferType *dataPtr; 
    LightBufferType *dataPtr2; 

    D3DXMatrixTranspose(&worldMatrix, &worldMatrix); 
    D3DXMatrixTranspose(&viewMatrix, &viewMatrix); 
    D3DXMatrixTranspose(&projectionMatrix, &projectionMatrix); 

    result = immediateDevice->Map(matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 
    if (FAILED(result)){ 
     return BE_SRESULT{ 8, false }; 
    } 
    else{ 

     dataPtr = (MatrixBufferType*)mappedResource.pData; 

     dataPtr->world = worldMatrix; 
     dataPtr->view = viewMatrix; 
     dataPtr->projection = projectionMatrix; 

     immediateDevice->Unmap(matrixBuffer, 0); 

     bufferNumber = 0; 

     immediateDevice->VSSetConstantBuffers(bufferNumber, 1, &matrixBuffer); 
     immediateDevice->PSSetShaderResources(0, 1, &texture); 

     result = immediateDevice->Map(lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 

     if (FAILED(result)){ 
      return BE_SRESULT{ 8, false }; 
     } 
     else{ 

      dataPtr2 = (LightBufferType*)mappedResource.pData; 

      dataPtr2->diffuseColor = diffuseColor; 
      dataPtr2->lightDirection = lightDirection; 
      dataPtr2->padding = 0.0f; 

      immediateDevice->Unmap(lightBuffer, 0); 

      bufferNumber = 0; 

      immediateDevice->PSSetConstantBuffers(bufferNumber, 1, &lightBuffer); 

      return BE_SRESULT{ 0, true }; 
     } 
    } 
} 

    //The render function of the shader 
    void ObjectElements::BE_OE_Shader::render(int indexCount){ 
     immediateDevice->IASetInputLayout(layout); 

     immediateDevice->VSSetShader(vertexShader, NULL, 0); 
     immediateDevice->PSSetShader(pixelShader, NULL, 0); 

     immediateDevice->PSSetSamplers(0, 1, &sampleState); 

     immediateDevice->DrawIndexed(indexCount, 0, 0); 
    } 

광 방향은 {D3DXVECTOR3은 0.0f, 0.0f를, 1.0F}이다.

나는 실패를 찾기 위해 이미 몇 시간 동안 수색을했지만 빛은 여전히 ​​올바르게 작동하지 않습니다. 내가 사용하는 셰이더는 튜토리얼의 셰이더와 같습니다.

누군가가 나를 도울 수 있기를 바랍니다.

답변

0

버텍스 버퍼에는 BE_SVERTEX에 따라 float2 uv가 들어 있지만 입력 레이아웃은 float3으로 선언되며 자동 오프셋을 사용하므로 법선이 잘못 오프셋됩니다. D :이

polygonLayout[1].Format = DXGI_FORMAT_R32G32B32_FLOAT; 

가 되십시오

polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT; 
+0

는 지금 작동 해 주셔서 감사합니다 – Elyptos