gpt4 book ai didi

c++ - Directx11 项目不会显示图形输出

转载 作者:行者123 更新时间:2023-11-28 04:43:04 27 4
gpt4 key购买 nike

我是 Directx 的新手,所以我几乎不知道这里发生了什么。

我已确认窗口和 DirectX API 的初始设置和激活已成功。然而,尽管绘图和更新功能似乎可以完全运行,但它们仍然无法成功显示输出。我已经检查了链接器、我的对象类、我的效果文件,基本上整个项目都检查了十几遍。请帮忙!

立方体.h

#pragma once
#pragma comment(lib,"d3d11.lib")

#include <d3d11.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
#include <string>
#include <WICTextureLoader.h>
#include <d3d11_2.h>
#include <vector>
using namespace DirectX;

struct Vertex {
XMFLOAT3 pos;
XMFLOAT2 texCoord;
XMFLOAT3 normal;
};

using namespace DirectX;
class Cube
{
private:
Vertex vertices[24];
XMMATRIX RotX, RotZ, RotY,trans;
double scale;
XMFLOAT3 loc;
DWORD indices[36];
ID3D11DeviceContext* d3dDevCon;
ID3D11Device*d3dDev;
ID3D11ShaderResourceView*CubeTexture;
XMMATRIX cubeWorld;

public:
Cube();
Cube(double,double,double, double, XMFLOAT3,ID3D11DeviceContext*,ID3D11Device*,std::string );
~Cube();
Vertex* getVertices();
void Rotate(double,double,double);
void Move(double,double,double);
void Draw();
void Update();
XMMATRIX getWorld();
DWORD* getIndices();
ID3D11ShaderResourceView*getCubeTexture();

};

立方体.cpp

#include "stdafx.h"
#include "Cube.h"

using namespace DirectX;

Cube::Cube()
{
RotX = XMMatrixRotationAxis(XMVectorSet(1.0, 0.0, 0.0, 0.0), 0);
RotY = XMMatrixRotationAxis(XMVectorSet(0.0, 1.0, 0.0, 0.0), 0);
RotZ = XMMatrixRotationAxis(XMVectorSet(0.0, 0.0, 1.0, 0.0), 0);
scale = 1;
loc = XMFLOAT3(0.0, 0.0, 0.0);

Vertex v[] =
{ //remember that structs do not have constructors unless defined!
// Front Face
{ { -1.0f, -1.0f, -1.0f },{ 0.0f, 1.0f },{ -1.0f, -1.0f, -1.0f } },
{ { -1.0f, 1.0f, -1.0f },{ 0.0f, 0.0f },{ -1.0f, 1.0f, -1.0f } },
{ { 1.0f, 1.0f, -1.0f },{ 1.0f, 0.0f } ,{ 1.0f, 1.0f, -1.0f } },
{ { 1.0f, -1.0f, -1.0f },{ 1.0f, 1.0f } ,{ 1.0f, -1.0f, -1.0f } },

// Back Face
{ { -1.0f, -1.0f, 1.0f },{ 1.0f, 1.0f } ,{ -1.0f, -1.0f, 1.0f } },
{ { 1.0f, -1.0f, 1.0f },{ 0.0f, 1.0f } ,{ 1.0f, -1.0f, 1.0f } },
{ { 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f } ,{ 1.0f, 1.0f, 1.0f } },
{ { -1.0f, 1.0f, 1.0f },{ 1.0f, 0.0f },{ -1.0f, 1.0f, 1.0f } },

// Top Face
{ { -1.0f, 1.0f, -1.0f },{ 1.0f, 1.0f },{ -1.0f, 1.0f, -1.0f } },
{ { -1.0f, 1.0f, 1.0f },{ 0.0f, 1.0f },{ -1.0f, 1.0f, 1.0f } },
{ { 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
{ { 1.0f, 1.0f, -1.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f, -1.0f } },

// Bottom Face
{ { -1.0f, -1.0f, -1.0f },{ 1.0f, 1.0f },{ -1.0f, -1.0f, -1.0f } },
{ { 1.0f, -1.0f, -1.0f },{ 0.0f, 1.0f },{ 1.0f, -1.0f, -1.0f } },
{ { 1.0f, -1.0f, 1.0f },{ 0.0f, 0.0f },{ 1.0f, -1.0f, 1.0f } },
{ { -1.0f, -1.0f, 1.0f },{ 1.0f, 0.0f },{ -1.0f, -1.0f, 1.0f } },

// Left Face
{ { -1.0f, -1.0f, 1.0f },{ 0.0f, 1.0f },{ -1.0f, -1.0f, 1.0f } },
{ { -1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f },{ -1.0f, 1.0f, 1.0f } },
{ { -1.0f, 1.0f, -1.0f },{ 1.0f, 0.0f },{ -1.0f, 1.0f, -1.0f } },
{ { -1.0f, -1.0f, -1.0f },{ 1.0f, 1.0f },{ -1.0f, -1.0f, -1.0f } },

// Right Face
{ { 1.0f, -1.0f, -1.0f },{ 0.0f, 1.0f },{ 1.0f, -1.0f, -1.0f } },
{ { 1.0f, 1.0f, -1.0f },{ 0.0f, 0.0f },{ 1.0f, 1.0f, -1.0f } },
{ { 1.0f, 1.0f, 1.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
{ { 1.0f, -1.0f, 1.0f },{ 1.0f, 1.0f },{ 1.0f, -1.0f, 1.0f } }

};
for (int i = 0; i < 24; i++) {
vertices[i] = v[i];
}
DWORD ind[] = {
// Front Face
0, 1, 2,
0, 2, 3,

// Back Face
4, 5, 6,
4, 6, 7,

// Top Face
8, 9, 10,
8, 10, 11,

// Bottom Face
12, 13, 14,
12, 14, 15,

// Left Face
16, 17, 18,
16, 18, 19,

// Right Face
20, 21, 22,
20, 22, 23
};
for (int s = 0; s < 36; s++) {
indices[s] = ind[s];
}
}

Cube::Cube(double rotx,double roty,double rotz, double scale, XMFLOAT3 loc,ID3D11DeviceContext*devcon, ID3D11Device*dev,std::string name ) {
RotX = XMMatrixRotationAxis(XMVectorSet(1.0, 0.0, 0.0, 0.0), rotx);
RotY = XMMatrixRotationAxis(XMVectorSet(0.0, 1.0, 0.0, 0.0), roty);
RotZ = XMMatrixRotationAxis(XMVectorSet(0.0, 0.0, 1.0, 0.0), rotz);
this->scale = scale;
this->loc = loc;

d3dDevCon = devcon;
d3dDev = dev;

CreateWICTextureFromFile(d3dDev, L"gray.jpg", NULL, &CubeTexture, 0);
Vertex v[] =
{ //remember that structs do not have constructors unless defined!
// Front Face
{ { -1.0f, -1.0f, -1.0f },{ 0.0f, 1.0f },{ -1.0f, -1.0f, -1.0f } },
{ { -1.0f, 1.0f, -1.0f },{ 0.0f, 0.0f },{ -1.0f, 1.0f, -1.0f } },
{ { 1.0f, 1.0f, -1.0f },{ 1.0f, 0.0f } ,{ 1.0f, 1.0f, -1.0f } },
{ { 1.0f, -1.0f, -1.0f },{ 1.0f, 1.0f } ,{ 1.0f, -1.0f, -1.0f } },

// Back Face
{ { -1.0f, -1.0f, 1.0f },{ 1.0f, 1.0f } ,{ -1.0f, -1.0f, 1.0f } },
{ { 1.0f, -1.0f, 1.0f },{ 0.0f, 1.0f } ,{ 1.0f, -1.0f, 1.0f } },
{ { 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f } ,{ 1.0f, 1.0f, 1.0f } },
{ { -1.0f, 1.0f, 1.0f },{ 1.0f, 0.0f },{ -1.0f, 1.0f, 1.0f } },

// Top Face
{ { -1.0f, 1.0f, -1.0f },{ 1.0f, 1.0f },{ -1.0f, 1.0f, -1.0f } },
{ { -1.0f, 1.0f, 1.0f },{ 0.0f, 1.0f },{ -1.0f, 1.0f, 1.0f } },
{ { 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
{ { 1.0f, 1.0f, -1.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f, -1.0f } },

// Bottom Face
{ { -1.0f, -1.0f, -1.0f },{ 1.0f, 1.0f },{ -1.0f, -1.0f, -1.0f } },
{ { 1.0f, -1.0f, -1.0f },{ 0.0f, 1.0f },{ 1.0f, -1.0f, -1.0f } },
{ { 1.0f, -1.0f, 1.0f },{ 0.0f, 0.0f },{ 1.0f, -1.0f, 1.0f } },
{ { -1.0f, -1.0f, 1.0f },{ 1.0f, 0.0f },{ -1.0f, -1.0f, 1.0f } },

// Left Face
{ { -1.0f, -1.0f, 1.0f },{ 0.0f, 1.0f },{ -1.0f, -1.0f, 1.0f } },
{ { -1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f },{ -1.0f, 1.0f, 1.0f } },
{ { -1.0f, 1.0f, -1.0f },{ 1.0f, 0.0f },{ -1.0f, 1.0f, -1.0f } },
{ { -1.0f, -1.0f, -1.0f },{ 1.0f, 1.0f },{ -1.0f, -1.0f, -1.0f } },

// Right Face
{ { 1.0f, -1.0f, -1.0f },{ 0.0f, 1.0f },{ 1.0f, -1.0f, -1.0f } },
{ { 1.0f, 1.0f, -1.0f },{ 0.0f, 0.0f },{ 1.0f, 1.0f, -1.0f } },
{ { 1.0f, 1.0f, 1.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f, 1.0f } },
{ { 1.0f, -1.0f, 1.0f },{ 1.0f, 1.0f },{ 1.0f, -1.0f, 1.0f } }

};
for (int i = 0; i < 24; i++) {
vertices[i] = v[i];
}
DWORD ind[] = {
// Front Face
0, 1, 2,
0, 2, 3,

// Back Face
4, 5, 6,
4, 6, 7,

// Top Face
8, 9, 10,
8, 10, 11,

// Bottom Face
12, 13, 14,
12, 14, 15,

// Left Face
16, 17, 18,
16, 18, 19,

// Right Face
20, 21, 22,
20, 22, 23
};
for (int s = 0; s < 36; s++) {
indices[s] = ind[s];
}
}
Cube::~Cube()
{
}

void Cube::Rotate(double rotx, double roty, double rotz) {
RotX = XMMatrixRotationAxis(XMVectorSet(1.0, 0.0, 0.0, 0.0), rotx);
RotY = XMMatrixRotationAxis(XMVectorSet(0.0, 1.0, 0.0, 0.0), roty);
RotZ = XMMatrixRotationAxis(XMVectorSet(0.0, 0.0, 1.0, 0.0), rotz);
}

void Cube::Move(double x,double y, double z) {
trans = XMMatrixTranslation(x, y, z);
}

void Cube::Update() {
cubeWorld = XMMatrixIdentity();
cubeWorld = trans*RotX*RotY*RotZ;
}

void Cube::Draw() {
return;
}

XMMATRIX Cube::getWorld() {
return cubeWorld;
}

Vertex* Cube::getVertices() {
return vertices;
}

DWORD* Cube::getIndices() {
return indices;
}

ID3D11ShaderResourceView* Cube::getCubeTexture() {
return CubeTexture;
}

标题

#pragma comment(lib,"D3D11.lib")
#pragma comment(lib,"d3dcompiler.lib")
#pragma comment(lib,"DXGI.lib")
#pragma comment(lib,"dwrite.lib")
#pragma comment(lib,"dinput8.lib")
#pragma comment(lib,"dxguid.lib")

#include "stdafx.h"
#include "Cube.h"
#include "D3DIndependentExperimentation.h"
#include <d3d11.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
#include <dinput.h>
#include <vector>
#include <iostream>
#include <d3dcompiler.h>

绘制场景()

void DrawScene(std::vector<Cube> cubelist) { // performs actual rendering
//clear backbuffer
float bgColor[4] = { 0.0, 0.0, 0.0, 0.0f };

d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);
//clear depth stencil
d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0, 0.0);
//set default blend state(no blending)
d3d11DevCon->OMSetBlendState(0, 0, 0xffffff);
World = XMMatrixIdentity();
d3d11DevCon->PSSetConstantBuffers(0, 1, &cbPerFrameBuffer);
d3d11DevCon->VSSetShader(VS, 0, 0);
d3d11DevCon->PSSetShader(PS, 0, 0);
constBufferPerFrame.light = light;
d3d11DevCon->UpdateSubresource(cbPerFrameBuffer, 0, NULL, &constBufferPerFrame, 0, 0);



d3d11DevCon->OMSetRenderTargets(1, &renderTargetView, depthStencilView);

d3d11DevCon->IASetIndexBuffer(IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
//set buffer data
UINT stride = sizeof(Vertex);//size of each Vertex
UINT offset = 0;// how far from the buffer beginning we start
d3d11DevCon->IASetVertexBuffers(0, 1, &VertexBuffer, &stride, &offset);
//TODO: everything
XMMATRIX cubeWorld = XMMatrixIdentity();
for (int i = 0; i < cubelist.size(); i++) {
Cube active = cubelist.at(i);
cubeWorld = active.getWorld();
WVP = cubeWorld*camView*camProjection;
cbPerObj.WVP = XMMatrixTranspose(WVP);
cbPerObj.World = XMMatrixTranspose(cubeWorld);
d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);

d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);
ID3D11ShaderResourceView* temp = active.getCubeTexture();
d3d11DevCon->PSSetShaderResources(0, 1, &temp);

d3d11DevCon->PSSetSamplers(0, 1, &CubesTexSamplerState);
d3d11DevCon->RSSetState(NOcullMode);
d3d11DevCon->DrawIndexed(36, 36 * i, 24 * i);
}

SwapChain->Present(0, 0);
}

初始化场景()

bool InitScene(std::vector<Cube> cubelist) {
HRESULT hr;
//Compiling Shaders
hr = D3DCompileFromFile(L"effects.fx", 0, 0, "VS", "vs_5_0", 0, 0, &VS_Buffer, 0);
hr = D3DCompileFromFile(L"effects.fx", 0, 0, "PS", "ps_5_0", 0, 0, &PS_Buffer, 0);
//Creating Shaders
hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);
//Setting Shaders
d3d11DevCon->VSSetShader(VS, NULL, NULL);
d3d11DevCon->PSSetShader(PS, NULL, NULL);

//Creating and populating Vertex Buffers
//Buffer description
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));

vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT; //describes how buffer is used
vertexBufferDesc.ByteWidth = sizeof(Vertex)*cubelist.size() *24; // specifies the size of buffer; dependent on amount of vertices passed and size of vertices
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;//Specifies that this is a vertex buffer
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;

//Specifies what kind of data is placed in buffer
D3D11_SUBRESOURCE_DATA vertexBufferData;
ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
std::vector<Vertex> cubeVertices;
for (int i = 0; i < cubelist.size(); i++) {
Vertex *point = cubelist.at(i).getVertices();
cubeVertices.insert(cubeVertices.end(), point, point + 24);
}

vertexBufferData.pSysMem = &cubeVertices;
hr = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &VertexBuffer);

//Buffer description is mostly the same as vertex buffer
D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));

indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(DWORD) * 36*cubelist.size();
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
std::vector<short> cubeIndices;
D3D11_SUBRESOURCE_DATA indexBufferData;
ZeroMemory(&indexBufferData, sizeof(indexBufferData));

for (int i = 0; i < cubelist.size(); i++) {
DWORD*point = cubelist.at(i).getIndices();
cubeIndices.insert(cubeIndices.end(), point, point + 36);
}
indexBufferData.pSysMem = &cubeIndices;
d3d11Device->CreateBuffer(&indexBufferDesc, &indexBufferData, &IndexBuffer);

//set input layout
hr = d3d11Device->CreateInputLayout(layout, NUMELEMENTS, VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &vertLayout);
d3d11DevCon->IASetInputLayout(vertLayout);

d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
//Create and set viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = SCREENWIDTH;
viewport.Height = SCREENHEIGHT;
viewport.MinDepth = 0.0;
viewport.MaxDepth = 1.0;

d3d11DevCon->RSSetViewports(1, &viewport);


D3D11_BUFFER_DESC constantBufferDesc;
ZeroMemory(&constantBufferDesc, sizeof(D3D11_BUFFER_DESC));

constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
constantBufferDesc.ByteWidth = sizeof(cbPerObject);
constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constantBufferDesc.CPUAccessFlags = 0;
constantBufferDesc.MiscFlags = 0;

hr = d3d11Device->CreateBuffer(&constantBufferDesc, NULL, &cbPerObjectBuffer);

ZeroMemory(&constantBufferDesc, sizeof(D3D11_BUFFER_DESC));

constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
constantBufferDesc.ByteWidth = sizeof(cbPerFrame);
constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constantBufferDesc.CPUAccessFlags = 0;
constantBufferDesc.MiscFlags = 0;

hr = d3d11Device->CreateBuffer(&constantBufferDesc, NULL, &cbPerFrameBuffer);

camPosition = XMVectorSet(0.0f, 5.0f, -10.0f, 0.0f);
camTarget = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
camUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);

camProjection = XMMatrixPerspectiveFovLH(0.4f*3.14f, (float)SCREENWIDTH / SCREENHEIGHT, 1.0f, 1000.0f);

//Describe and create rasterizer state
D3D11_RASTERIZER_DESC wfdesc;
ZeroMemory(&wfdesc, sizeof(D3D11_RASTERIZER_DESC));
wfdesc.FillMode = D3D11_FILL_SOLID;
wfdesc.CullMode = D3D11_CULL_NONE;
hr = d3d11Device->CreateRasterizerState(&wfdesc, &FULL);

//hr = CreateWICTextureFromFile(d3d11Device, L"gray.jpg", NULL, &CubeTexture, 0);
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;

hr = d3d11Device->CreateSamplerState(&sampDesc, &CubesTexSamplerState);

//describe and create blend state
/*D3D11_BLEND_DESC blendDesc;
ZeroMemory(&blendDesc, sizeof(blendDesc));

D3D11_RENDER_TARGET_BLEND_DESC rtbd;
ZeroMemory(&rtbd, sizeof(rtbd));

rtbd.BlendEnable = true;
rtbd.SrcBlend = D3D11_BLEND_SRC_COLOR;
rtbd.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
rtbd.BlendOp = D3D11_BLEND_OP_ADD;
rtbd.SrcBlendAlpha = D3D11_BLEND_ONE;
rtbd.DestBlendAlpha = D3D11_BLEND_ZERO;
rtbd.BlendOpAlpha = D3D11_BLEND_OP_ADD;
rtbd.RenderTargetWriteMask = D3D10_COLOR_WRITE_ENABLE_ALL;

blendDesc.AlphaToCoverageEnable = false;
blendDesc.RenderTarget[0] = rtbd;

//d3d11Device->CreateBlendState(&blendDesc, &Transparency);*/

//define rasterizer states for blending
D3D11_RASTERIZER_DESC cmdesc;
ZeroMemory(&cmdesc, sizeof(D3D11_RASTERIZER_DESC));

cmdesc.CullMode = D3D11_CULL_BACK;
cmdesc.FillMode = D3D11_FILL_SOLID;

cmdesc.FrontCounterClockwise = true;
hr = d3d11Device->CreateRasterizerState(&cmdesc, &CCWcullMode);

cmdesc.FrontCounterClockwise = false;
hr = d3d11Device->CreateRasterizerState(&cmdesc, &CWcullMode);

cmdesc.CullMode = D3D11_CULL_NONE;

hr = d3d11Device->CreateRasterizerState(&cmdesc, &NOcullMode);

//light setting
//light.dir = XMFLOAT3(1.0f, 0.0f, 0.0f);
light.ambient = XMFLOAT4(0.3f, 0.3f, 0.3f, 1.0f);
light.diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
light.pos = XMFLOAT3(2.0f, 4.0f, 0.0f);
light.range = 250.0f;
light.att = XMFLOAT3(0.0f, 0.2f, 0.0f);

return true;
}

效果.fx

cbuffer cbPerObject {
float4x4 WVP;
float4x4 World;
};
//note: keep structure of structs in fx files same as those in c++ code.
struct Light {
float3 dir;
float3 att;
float3 pos;
float range;
float4 ambient;
float4 diffuse;
};

cbuffer cbPerFrame {
Light light;
};

struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float2 texCoord : TEXCOORD;
float3 normal: NORMAL;
float4 worldPos: POSITION;
};
Texture2D ObjTexture;
SamplerState ObjSamplerState;

VS_OUTPUT VS(float4 inPos: POSITION, float2 texCoord : TEXCOORD, float3 normal : NORMAL) {
VS_OUTPUT output;
output.texCoord = texCoord;
output.Pos = mul(inPos, WVP);
output.worldPos = mul(inPos, World);
output.normal = mul(normal, World);
return output;
}


float4 PS(VS_OUTPUT input) : SV_TARGET{
input.normal = normalize(input.normal);

float4 diffuse = ObjTexture.Sample(ObjSamplerState, input.texCoord);
float3 finalColor = float3(0.0, 0.0, 0.0);
float3 lightToPixelVec = light.pos - input.worldPos;
float d = length(lightToPixelVec);


float3 finalAmbient = diffuse*light.ambient;
if (d > light.range) {
return float4(finalAmbient, diffuse.a);
}
lightToPixelVec /= d;
float Intensity = dot(lightToPixelVec, input.normal)*20;
if (Intensity > 0.0f) {
finalColor += Intensity*diffuse*light.diffuse;
finalColor /= light.att[0] + (light.att[1] * d) + (light.att[2] * (d*d));
}
finalColor = saturate(finalColor + finalAmbient);

return float4(1.0, 1.0, 1.0, 1.0);//float4(finalColor,diffuse.a);

}

float4 D2D_PS(VS_OUTPUT input) : SV_TARGET
{ input.normal = normalize(input.normal);
float4 diffuse = ObjTexture.Sample(ObjSamplerState, input.texCoord);

return diffuse;
}

如果需要任何更多信息,我随时准备提供帮助。

最佳答案

检查您的代码并将其与我过去为 DirectX 11 渲染编写的代码进行比较,您至少遗漏了一件事 - 深度/模板状态。我不确定如果你决定在没有它的情况下使用深度/模板缓冲区,GPU 会做什么,但它看起来是你的系统调用(不起作用)和我的系统调用之间的唯一区别(确实如此)。

您可以使用 ID3D11Device::CreateDepthStencilState 创建深度/模板状态,并使用 ID3D11DeviceContext::OMSetDepthStencilState 将其设置到管道。此函数的工作原理与 ID3D11DeviceContext::OMSetBlendState 相同,传递 nullptrNULL0 作为状态对象将绑定(bind)默认状态。

关于c++ - Directx11 项目不会显示图形输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49817035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com