Hello i am new to directX 11 and have been messing around making and creating polygons and have been recently adding textures. To do that you must first have a surface (meaning a polygon of some sort) and then you render the Texture onto the polygon. My issue was not rendering the Texture, but was having that same texture being displayed onto other polygons (If it wasn’t the texture it would be the color of the texture). I have been searching and i still haven’t found an answer or anyone with a similar problem as mine. Maybe there is something wrong with my code but I have seen many other people doing exactly what I was doing.
Here is my vertex texture code (XVertexShader.hlsl):
float2 inTextCord : TEXCOORD;
float4 outPos : SV_Position;
float2 outTextCord : TEXCOORD;
VS_output main(VS_input input)
output.outPos = float4(input.inPos, 1.0f);
output.outTextCord = input.inTextCord;
<code>struct VS_input
{
float3 inPos : POSITION;
float2 inTextCord : TEXCOORD;
};
struct VS_output
{
float4 outPos : SV_Position;
float2 outTextCord : TEXCOORD;
};
VS_output main(VS_input input)
{
VS_output output;
output.outPos = float4(input.inPos, 1.0f);
output.outTextCord = input.inTextCord;
return output;
}
</code>
struct VS_input
{
float3 inPos : POSITION;
float2 inTextCord : TEXCOORD;
};
struct VS_output
{
float4 outPos : SV_Position;
float2 outTextCord : TEXCOORD;
};
VS_output main(VS_input input)
{
VS_output output;
output.outPos = float4(input.inPos, 1.0f);
output.outTextCord = input.inTextCord;
return output;
}
Here is my pixel Shader code (XPixelShader.hlsl):
<code>Texture2D myTexture : register(t0);
SamplerState mySampler : register(s0);
float4 inPos : SV_Position;
float2 inTextCord : TEXCOORD;
float4 main(PS_input input) : SV_Target
return myTexture.Sample(mySampler, input.inTextCord);
<code>Texture2D myTexture : register(t0);
SamplerState mySampler : register(s0);
struct PS_input
{
float4 inPos : SV_Position;
float2 inTextCord : TEXCOORD;
};
float4 main(PS_input input) : SV_Target
{
return myTexture.Sample(mySampler, input.inTextCord);
}
</code>
Texture2D myTexture : register(t0);
SamplerState mySampler : register(s0);
struct PS_input
{
float4 inPos : SV_Position;
float2 inTextCord : TEXCOORD;
};
float4 main(PS_input input) : SV_Target
{
return myTexture.Sample(mySampler, input.inTextCord);
}
And here is my texture.cpp file
<code>#include "header.h"
void D3D::initXTexture() {
ID3D10Blob* pXVertexShader = nullptr;
hr = D3DX10CompileFromFile(L"XVertexShader.hlsl", 0, 0, "main", "vs_5_0", 0, 0, 0, &pXVertexShader, 0, 0);
MessageBox(NULL, L"Couldn't Create X vertex Shader", L"Error", MB_OK);
hr = device->CreateVertexShader(pXVertexShader->GetBufferPointer(), pXVertexShader->GetBufferSize(), nullptr, &XVertexShader);
MessageBox(NULL, L"Couldn't create Vertex Shader", L"Error", MB_OK);
devCon->VSSetShader(XVertexShader, 0, 0);
ID3D10Blob* pXPixelShader = nullptr;
hr = D3DX10CompileFromFile(L"XPixelShader.hlsl", 0, 0, "main", "ps_5_0", 0, 0, 0, &pXPixelShader, 0, 0);
MessageBox(NULL, L"Couldn't compile X pixel Shader", L"Erro", MB_OK);
hr = device->CreatePixelShader(pXPixelShader->GetBufferPointer(), pXPixelShader->GetBufferSize(), nullptr, &XPixelShader);
MessageBox(NULL, L"Couldn't create X Pixel Shader", L"Error", MB_OK);
devCon->PSSetShader(XPixelShader, 0, 0);
D3D11_INPUT_ELEMENT_DESC XLayoutDesc[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
UINT size = ARRAYSIZE(XLayoutDesc);
hr = device->CreateInputLayout(XLayoutDesc, size, pXVertexShader->GetBufferPointer(), pXVertexShader->GetBufferSize(), &XLayout);
MessageBox(NULL, L"Couldn't create input Layout", L"Error", MB_OK);
devCon->IASetInputLayout(XLayout);
void D3D::createXBuffer() {
{XMFLOAT3(-0.2f, 0.2f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
{XMFLOAT3(0.2f, 0.2f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
{XMFLOAT3(-0.2f, -0.2f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
{XMFLOAT3(0.2f, -0.2f, 0.0f), XMFLOAT2(1.0f, 1.0f)},
D3D11_BUFFER_DESC XVertexBufferDesc;
ZeroMemory(&XVertexBufferDesc, sizeof(D3D11_BUFFER_DESC));
XVertexBufferDesc.ByteWidth = 4 * sizeof(X);
XVertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
XVertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
XVertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA XVertexBufferSubData;
ZeroMemory(&XVertexBufferSubData, sizeof(D3D11_SUBRESOURCE_DATA));
XVertexBufferSubData.pSysMem = vertex;
hr = device->CreateBuffer(&XVertexBufferDesc, &XVertexBufferSubData, &XVertexBuffer);
MessageBox(NULL, L"Couldn't create X Vertex Buffer", L"Error", MB_OK);
D3D11_BUFFER_DESC XIndicesBufferDesc;
ZeroMemory(&XIndicesBufferDesc, sizeof(D3D11_BUFFER_DESC));
XIndicesBufferDesc.ByteWidth = 6 * sizeof(DWORD);
XIndicesBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
XIndicesBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
XIndicesBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA XIndicesBufferSubData;
ZeroMemory(&XIndicesBufferSubData, sizeof(D3D11_SUBRESOURCE_DATA));
XIndicesBufferSubData.pSysMem = indices;
hr = device->CreateBuffer(&XIndicesBufferDesc, &XIndicesBufferSubData, &XIndexBuffer);
MessageBox(NULL, L"Couldn't create X Indices Buffer", L"Error", MB_OK);
hr = D3DX11CreateShaderResourceViewFromFile(device, L"C:\Users\nunot\OneDrive\Desktop\texture-7515225_640.jpg", NULL, NULL, &shaderResourceView,
MessageBox(NULL, L"Coulnd't load shader from file", L"Error", MB_OK);
D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(samplerDesc));
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = device->CreateSamplerState(&samplerDesc, &samplerState);
MessageBox(NULL, L"Couldn't create sampler state", L"Error", MB_OK);
devCon->PSSetSamplers(0, 1, &samplerState);
void D3D::renderXTexture() {
devCon->IASetVertexBuffers(0, 1, &XVertexBuffer, &stride, &offset);
devCon->IASetIndexBuffer(XIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
devCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
devCon->PSSetShaderResources(0, 1, &shaderResourceView);
devCon->DrawIndexed(6, 0, 0);
<code>#include "header.h"
void D3D::initXTexture() {
HRESULT hr;
//X Vertex Shader
ID3D10Blob* pXVertexShader = nullptr;
hr = D3DX10CompileFromFile(L"XVertexShader.hlsl", 0, 0, "main", "vs_5_0", 0, 0, 0, &pXVertexShader, 0, 0);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't Create X vertex Shader", L"Error", MB_OK);
exit(-1);
}
hr = device->CreateVertexShader(pXVertexShader->GetBufferPointer(), pXVertexShader->GetBufferSize(), nullptr, &XVertexShader);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create Vertex Shader", L"Error", MB_OK);
exit(-1);
}
devCon->VSSetShader(XVertexShader, 0, 0);
//X Pixel Shader
ID3D10Blob* pXPixelShader = nullptr;
hr = D3DX10CompileFromFile(L"XPixelShader.hlsl", 0, 0, "main", "ps_5_0", 0, 0, 0, &pXPixelShader, 0, 0);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't compile X pixel Shader", L"Erro", MB_OK);
exit(-1);
}
hr = device->CreatePixelShader(pXPixelShader->GetBufferPointer(), pXPixelShader->GetBufferSize(), nullptr, &XPixelShader);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create X Pixel Shader", L"Error", MB_OK);
exit(-1);
}
devCon->PSSetShader(XPixelShader, 0, 0);
//X input Layout
D3D11_INPUT_ELEMENT_DESC XLayoutDesc[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
UINT size = ARRAYSIZE(XLayoutDesc);
hr = device->CreateInputLayout(XLayoutDesc, size, pXVertexShader->GetBufferPointer(), pXVertexShader->GetBufferSize(), &XLayout);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create input Layout", L"Error", MB_OK);
exit(-1);
}
devCon->IASetInputLayout(XLayout);
}
void D3D::createXBuffer() {
HRESULT hr;
//X vertex buffer
X vertex[] = {
{XMFLOAT3(-0.2f, 0.2f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
{XMFLOAT3(0.2f, 0.2f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
{XMFLOAT3(-0.2f, -0.2f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
{XMFLOAT3(0.2f, -0.2f, 0.0f), XMFLOAT2(1.0f, 1.0f)},
};
D3D11_BUFFER_DESC XVertexBufferDesc;
ZeroMemory(&XVertexBufferDesc, sizeof(D3D11_BUFFER_DESC));
XVertexBufferDesc.ByteWidth = 4 * sizeof(X);
XVertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
XVertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
XVertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA XVertexBufferSubData;
ZeroMemory(&XVertexBufferSubData, sizeof(D3D11_SUBRESOURCE_DATA));
XVertexBufferSubData.pSysMem = vertex;
hr = device->CreateBuffer(&XVertexBufferDesc, &XVertexBufferSubData, &XVertexBuffer);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create X Vertex Buffer", L"Error", MB_OK);
exit(-1);
}
//X indices buffer
DWORD indices[] = {
0 , 1, 2,
2, 1, 3,
};
D3D11_BUFFER_DESC XIndicesBufferDesc;
ZeroMemory(&XIndicesBufferDesc, sizeof(D3D11_BUFFER_DESC));
XIndicesBufferDesc.ByteWidth = 6 * sizeof(DWORD);
XIndicesBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
XIndicesBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
XIndicesBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA XIndicesBufferSubData;
ZeroMemory(&XIndicesBufferSubData, sizeof(D3D11_SUBRESOURCE_DATA));
XIndicesBufferSubData.pSysMem = indices;
hr = device->CreateBuffer(&XIndicesBufferDesc, &XIndicesBufferSubData, &XIndexBuffer);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create X Indices Buffer", L"Error", MB_OK);
exit(-1);
}
//Sampler for texture
hr = D3DX11CreateShaderResourceViewFromFile(device, L"C:\Users\nunot\OneDrive\Desktop\texture-7515225_640.jpg", NULL, NULL, &shaderResourceView,
NULL);
if (FAILED(hr)) {
MessageBox(NULL, L"Coulnd't load shader from file", L"Error", MB_OK);
exit(-1);
}
D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(samplerDesc));
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = device->CreateSamplerState(&samplerDesc, &samplerState);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create sampler state", L"Error", MB_OK);
exit(-1);
}
devCon->PSSetSamplers(0, 1, &samplerState);
}
void D3D::renderXTexture() {
UINT stride = sizeof(X);
UINT offset = 0;
devCon->IASetVertexBuffers(0, 1, &XVertexBuffer, &stride, &offset);
devCon->IASetIndexBuffer(XIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
devCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
devCon->PSSetShaderResources(0, 1, &shaderResourceView);
devCon->DrawIndexed(6, 0, 0);
}
</code>
#include "header.h"
void D3D::initXTexture() {
HRESULT hr;
//X Vertex Shader
ID3D10Blob* pXVertexShader = nullptr;
hr = D3DX10CompileFromFile(L"XVertexShader.hlsl", 0, 0, "main", "vs_5_0", 0, 0, 0, &pXVertexShader, 0, 0);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't Create X vertex Shader", L"Error", MB_OK);
exit(-1);
}
hr = device->CreateVertexShader(pXVertexShader->GetBufferPointer(), pXVertexShader->GetBufferSize(), nullptr, &XVertexShader);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create Vertex Shader", L"Error", MB_OK);
exit(-1);
}
devCon->VSSetShader(XVertexShader, 0, 0);
//X Pixel Shader
ID3D10Blob* pXPixelShader = nullptr;
hr = D3DX10CompileFromFile(L"XPixelShader.hlsl", 0, 0, "main", "ps_5_0", 0, 0, 0, &pXPixelShader, 0, 0);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't compile X pixel Shader", L"Erro", MB_OK);
exit(-1);
}
hr = device->CreatePixelShader(pXPixelShader->GetBufferPointer(), pXPixelShader->GetBufferSize(), nullptr, &XPixelShader);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create X Pixel Shader", L"Error", MB_OK);
exit(-1);
}
devCon->PSSetShader(XPixelShader, 0, 0);
//X input Layout
D3D11_INPUT_ELEMENT_DESC XLayoutDesc[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
UINT size = ARRAYSIZE(XLayoutDesc);
hr = device->CreateInputLayout(XLayoutDesc, size, pXVertexShader->GetBufferPointer(), pXVertexShader->GetBufferSize(), &XLayout);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create input Layout", L"Error", MB_OK);
exit(-1);
}
devCon->IASetInputLayout(XLayout);
}
void D3D::createXBuffer() {
HRESULT hr;
//X vertex buffer
X vertex[] = {
{XMFLOAT3(-0.2f, 0.2f, 0.0f), XMFLOAT2(0.0f, 0.0f)},
{XMFLOAT3(0.2f, 0.2f, 0.0f), XMFLOAT2(1.0f, 0.0f)},
{XMFLOAT3(-0.2f, -0.2f, 0.0f), XMFLOAT2(0.0f, 1.0f)},
{XMFLOAT3(0.2f, -0.2f, 0.0f), XMFLOAT2(1.0f, 1.0f)},
};
D3D11_BUFFER_DESC XVertexBufferDesc;
ZeroMemory(&XVertexBufferDesc, sizeof(D3D11_BUFFER_DESC));
XVertexBufferDesc.ByteWidth = 4 * sizeof(X);
XVertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
XVertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
XVertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA XVertexBufferSubData;
ZeroMemory(&XVertexBufferSubData, sizeof(D3D11_SUBRESOURCE_DATA));
XVertexBufferSubData.pSysMem = vertex;
hr = device->CreateBuffer(&XVertexBufferDesc, &XVertexBufferSubData, &XVertexBuffer);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create X Vertex Buffer", L"Error", MB_OK);
exit(-1);
}
//X indices buffer
DWORD indices[] = {
0 , 1, 2,
2, 1, 3,
};
D3D11_BUFFER_DESC XIndicesBufferDesc;
ZeroMemory(&XIndicesBufferDesc, sizeof(D3D11_BUFFER_DESC));
XIndicesBufferDesc.ByteWidth = 6 * sizeof(DWORD);
XIndicesBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
XIndicesBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
XIndicesBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA XIndicesBufferSubData;
ZeroMemory(&XIndicesBufferSubData, sizeof(D3D11_SUBRESOURCE_DATA));
XIndicesBufferSubData.pSysMem = indices;
hr = device->CreateBuffer(&XIndicesBufferDesc, &XIndicesBufferSubData, &XIndexBuffer);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create X Indices Buffer", L"Error", MB_OK);
exit(-1);
}
//Sampler for texture
hr = D3DX11CreateShaderResourceViewFromFile(device, L"C:\Users\nunot\OneDrive\Desktop\texture-7515225_640.jpg", NULL, NULL, &shaderResourceView,
NULL);
if (FAILED(hr)) {
MessageBox(NULL, L"Coulnd't load shader from file", L"Error", MB_OK);
exit(-1);
}
D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(samplerDesc));
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = device->CreateSamplerState(&samplerDesc, &samplerState);
if (FAILED(hr)) {
MessageBox(NULL, L"Couldn't create sampler state", L"Error", MB_OK);
exit(-1);
}
devCon->PSSetSamplers(0, 1, &samplerState);
}
void D3D::renderXTexture() {
UINT stride = sizeof(X);
UINT offset = 0;
devCon->IASetVertexBuffers(0, 1, &XVertexBuffer, &stride, &offset);
devCon->IASetIndexBuffer(XIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
devCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
devCon->PSSetShaderResources(0, 1, &shaderResourceView);
devCon->DrawIndexed(6, 0, 0);
}
In this image the rows and collumns i created where supposed to be fully white, yet it has the color of the texture
In this imagine I tried to simply change the color of one of my polygons and it simply made it worse