首页 新闻 搜索 专区 学院

directx11龙书HillsDemo渲染得不到正确的颜色?

0
悬赏园豆:100 [已关闭问题] 关闭于 2016-05-19 14:46

代码中明明没有给顶点赋任何颜色信息,为什么最后的结果有好多颜色呢?

刚接触directx11,菜鸟一枚,这个问题调试了2天了还没发现问题/(ㄒoㄒ)/~~,哪位大神帮忙看看,感激不尽~~~
shader代码用了effect框架
 
cbuffer cbPerObject
{
float4x4 gWorldViewProj; 
};
 
struct VertexIn
{
float3 Pos  : POSITION;
    float4 Color : COLOR;
};
 
struct VertexOut
{
float4 PosH  : SV_POSITION;
    float4 Color : COLOR;
};
 
VertexOut VS(VertexIn vin)
{
VertexOut vout;
 
// Transform to homogeneous clip space.
vout.PosH = mul(float4(vin.Pos, 1.0f), gWorldViewProj);
 
// Just pass vertex color into the pixel shader.
    vout.Color = vin.Color;
    
    return vout;
}
 
float4 PS(VertexOut pin) : SV_Target
{
    return pin.Color;
}
 
technique11 ColorTech
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
}
 
GeometryGenerator类  创建网格顶点
.h
 
#pragma once
#include<vector>
#include "Dx11DemoBase.h"
 
class GeometryGenerator
{
public:
struct  Vertex 
{
Vertex(){}
Vertex(const XMFLOAT3& p)
: Position(p){}
Vertex(float px, float py, float pz): Position(px, py, pz){}
XMFLOAT3 Position;
};
 
struct MeshData
{
std::vector<Vertex> vertices;
std::vector<UINT> indices;
};
 
void CreateGrid(float width, float depth, UINT m, UINT n, MeshData& meshData);
};
 
.cpp
 
#include "GeometryGenerator.h"
 
void GeometryGenerator::CreateGrid(float width, float depth, UINT m, UINT n, MeshData& meshData)
{
UINT vertexCount = m*n;
UINT faceCount = (m - 1)*(n - 1) * 2;
 
// Create the vertices.
 
float halfWidth = 0.5f*width;
float halfDepth = 0.5f*depth;
 
float dx = width / (n - 1);
float dz = depth / (m - 1);
 
meshData.vertices.resize(vertexCount);
for (UINT i = 0; i < m; ++i)
{
float z = halfDepth - i*dz;
for (UINT j = 0; j < n; ++j)
{
float x = -halfWidth + j*dx;
meshData.vertices[i*n + j].Position = XMFLOAT3(x, 0.0f, z);
}
}
 
// Create the indices.
 
meshData.indices.resize(faceCount * 3); // 3 indices per face
 
UINT k = 0;
for (UINT i = 0; i < m - 1; ++i)
{
for (UINT j = 0; j < n - 1; ++j)
{
meshData.indices[k] = i*n + j;
meshData.indices[k + 1] = i*n + j + 1;
meshData.indices[k + 2] = (i + 1)*n + j;
 
meshData.indices[k + 3] = (i + 1)*n + j;
meshData.indices[k + 4] = i*n + j + 1;
meshData.indices[k + 5] = (i + 1)*n + j + 1;
 
k += 6; // next quad
}
}
}
 
HillsDemo.h 其中LoadContent()是初始化函数,除了基类Dx11DemoBase中初始化设备之类的其他具体代码都放在这个函数中,Update和Render每一帧调用
 
#pragma once
 
#include "Dx11DemoBase.h"
#include "d3dx11effect.h"
#include "GeometryGenerator.h"
 
class HillsDemo : public Dx11DemoBase
{
public:
HillsDemo();
~HillsDemo();
 
bool LoadContent() override;
void UnLoadContent() override;
 
void Update(float dt) override;
void Render() override;
 
void OnMouseDown(WPARAM btnState, int x, int y);
void OnMouseUp(WPARAM btnState, int x, int y);
void OnMouseMove(WPARAM btnState, int x, int y);
private:
ID3D11Buffer *m_pVertexBuffer;
ID3D11Buffer *m_pIndexBuffer;
ID3D11InputLayout *m_pInputLayout;
 
ID3DX11Effect *m_pFx;
ID3DX11EffectTechnique *m_pTechnique;
ID3DX11EffectMatrixVariable *m_pFxWorldViewProj;
XMFLOAT4X4 m_world;
XMFLOAT4X4 m_view;
XMFLOAT4X4 m_proj;
 
UINT m_gridIndexCount;
 
float m_theta;
float m_phi;
float m_radius;
POINT m_lastMousePos;
 
float GetHeight(float x, float z)const;
};
 
HillsDemo.cpp各个函数如下
 
顶点结构
struct Vertex
{
XMFLOAT3 pos;
XMFLOAT4 color;
Vertex(XMFLOAT3 p, XMFLOAT4 c) : pos(p), color(c){}
};
 
bool HillsDemo::LoadContent()
{
HRESULT result;
 
GeometryGenerator::MeshData grid;
GeometryGenerator geoGen;
geoGen.CreateGrid(160.0f, 160.0f, 50, 50, grid);
m_gridIndexCount = grid.indices.size();
 
std::vector<Vertex> vertices(grid.vertices.size(),Vertex(XMFLOAT3(0,0,0),XMFLOAT4(0,0,0,0)));
for (UINT i = 0; i < grid.vertices.size(); ++i)
{
XMFLOAT3 p = grid.vertices[i].Position;
p.y = GetHeight(p.x, p.z);
 
vertices[i].pos = p;
}
 
D3D11_BUFFER_DESC vertexDesc;
ZeroMemory(&vertexDesc, sizeof(vertexDesc));
vertexDesc.Usage = D3D11_USAGE_IMMUTABLE;
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof(Vertex)* grid.vertices.size();
D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory(&resourceData, sizeof(resourceData));
resourceData.pSysMem = &vertices[0];
result = m_pd3dDevice->CreateBuffer(&vertexDesc, &resourceData, &m_pVertexBuffer);
if (FAILED(result))
{
return false;
}
 
 
 
D3D11_BUFFER_DESC indexDesc;
ZeroMemory(&indexDesc, sizeof(indexDesc));
indexDesc.Usage = D3D11_USAGE_IMMUTABLE;
indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexDesc.ByteWidth = sizeof(UINT)* m_gridIndexCount;
 
D3D11_SUBRESOURCE_DATA indexData;
ZeroMemory(&indexData, sizeof(indexData));
indexData.pSysMem = &grid.indices[0];
result = m_pd3dDevice->CreateBuffer(&indexDesc, &indexData, &m_pIndexBuffer);
if (FAILED(result))
{
return false;
}
 
 
 
DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined _DEBUG || defined DEBUG
shaderFlags = D3DCOMPILE_DEBUG;
#endif
 
ID3D10Blob *compiledShader = 0;
ID3D10Blob *compilationMsgs = 0;
result = D3DX11CompileFromFile("SolidColor.fx", 0, 0, 0, "fx_5_0", shaderFlags,
0, 0, &compiledShader, &compilationMsgs, 0);
if (compilationMsgs != 0)
{
MessageBox(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0);
compilationMsgs->Release();
compilationMsgs = 0;
}
if (FAILED(result))
{
MessageBox(0, "载入着色器错误", 0, 0);
return false;
}
 
result = D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(),
0, m_pd3dDevice, &m_pFx);
compiledShader->Release();
if (FAILED(result))
{
MessageBox(0, "载入着色器失败", 0, 0);
return false;
}
m_pTechnique = m_pFx->GetTechniqueByName("ColorTech");
m_pFxWorldViewProj = m_pFx->GetVariableByName("gWorldViewProj")->AsMatrix();
 
 
D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
UINT numLayoutElements = ARRAYSIZE(solidColorLayout);
D3DX11_PASS_DESC passDesc;
m_pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);
 
result = m_pd3dDevice->CreateInputLayout(solidColorLayout, numLayoutElements, passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize, &m_pInputLayout);
 
return true;
}
 
void HillsDemo::Update(float dt)
{
float x = m_radius*sinf(m_phi)*cosf(m_theta);
float z = m_radius*sinf(m_phi)*sinf(m_theta);
float y = m_radius*cosf(m_phi);
 
XMVECTOR pos = XMVectorSet(x, y, z, 1.0f);
XMVECTOR target = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
 
XMMATRIX V = XMMatrixLookAtLH(pos, target, up);
XMStoreFloat4x4(&m_view, V);
XMMATRIX T = XMMatrixPerspectiveFovLH(XM_PIDIV4, m_width / static_cast<float>(m_height),
1.0f, 1000.0f);
XMStoreFloat4x4(&m_proj, T);
 
}
 
 
//地形函数  根据x,z值得到y值
float HillsDemo::GetHeight(float x, float z) const
{
return 0.3f*(z*sinf(0.1f*x) + x*cosf(0.1f*z));
}
 
核心代码只有这些了,到底哪里出错才导致上面图片上的好多颜色呢?
zhangbaochong的主页 zhangbaochong | 初学一级 | 园豆:62
提问于:2016-05-18 17:04
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册