I've recently tried to texture a cube in DirectX11, although I've encountered a problem and Im not sure how to fix it. The problem is I only want the texture to apply to one of my two cubes, and it applies to both and seems to be constantly overlapping the texture, and not keeping it as 1 solid texture.
here is what the application currently looks like when its ran: http://img525.imageshack.us/img525/8264/directx11bug.png
and here is what the texture should look like: http://www.directxtutorial.com/Images/Textures/Wood.png
This is my render function will draws the applies translation etc to the cubes
Object.cpp
void Object::Render(ID3D11DeviceContext * pImmediateContext)
{
/* VARIABLES */
//Load Constant Buffer Struct
CBUFFER cBuffer;
//Setup HLSL lighting
cBuffer.LightVector = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 0.0f);
cBuffer.LightColor = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
cBuffer.AmbientColor = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
//simple animation
static float Time = 0.0f;
Time += 0.0001f;
/* SETUP MATRIX */
//Create the view matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3(0.0f, 5.0f, 10.0f), //Camera position
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), //the look-at position
&D3DXVECTOR3(0.0f, 1.0f, 0.0f) //the up direction
);
//Create the projection matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
(FLOAT)D3DXToRadian(45), //Field of View
(FLOAT)1280 / (FLOAT) 720, //aspect ratio
1.0f, //near view-plane
100.0f); //far view-plane
// select which vertex buffer to display
UINT stride = sizeof(VERTEX);
UINT offset = 0;
pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);
pImmediateContext->IASetIndexBuffer(g_pIBuffer, DXGI_FORMAT_R32_UINT, 0);
// select which primtive type we are using
pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
/* CUBE 1 */
//Create the rotation matrix, spin on the Y axis at 0.001f per frame
D3DXMatrixRotationY(&matRotate, Time);
//Move the shape
D3DXMatrixTranslation(&matTranslate, 5.0f, 2.0f, 0.0f);
//Create the final transform
cBuffer.Final = matRotate * matTranslate * matView * matProjection;
cBuffer.Rotation = matRotate;
//Load in Shaders
VertexShader(pImmediateContext);
PixelShader(pImmediateContext);
//Draw
pImmediateContext->UpdateSubresource(g_pCBuffer, 0, 0, &cBuffer, 0, 0);
pImmediateContext->DrawIndexed(36, 0, 0);
/* CUBE 2 */
//Create the rotation matrix, spin on the Y axis at 0.001f per frame
D3DXMatrixRotationX(&matRotate, Time);
//Move the shape
D3DXMatrixTranslation(&matTranslate, -5.0f, 2.0f, 0.0f);
//Create the final transform
cBuffer.Final = matRotate * matTranslate * matView * matProjection;
cBuffer.Rotation = matRotate;
//Draw
pImmediateContext->UpdateSubresource(g_pCBuffer, 0, 0, &cBuffer, 0, 0);
pImmediateContext->PSSetShaderResources(0, 1, &woodTexture); //add texture
pImmediateContext->DrawIndexed(36, 0, 0);
}
main.cpp
obj.InitObject(gfx.getDevice(), gfx.getImmediateContext());
// Main message loop
MSG msg = {0};
obj.InitGraphics(gfx.getDevice(), gfx.getImmediateContext());
while( WM_QUIT != msg.message )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
gfx.clearBackBuffer(); //Clear Stage
obj.Render(gfx.getImmediateContext()); //Draw
gfx.present(); //Present
}
}
obj.CleanupObject(); //Cleanup Objects
gfx.CleanupDevice(); //Cleanup Graphics
return ( int )msg.wParam;
shader.fx
//Created by Tim Lawton 2012
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
//Constant Buffer
cbuffer ConstantBuffer
{
float4x4 final; //4x4 matrix which holds the final value
float4x4 rotation; //rotation matrix
float4 lightvec; //Light's Vector
float4 lightcol; //light's colour
float4 ambientcol; //Ambient light's colour
}
Texture2D Texture;
SamplerState ss;
//Struct to return two values
struct VOut
{
float4 color : COLOR;
float2 texcoord : TEXCOORD;
float4 position : SV_POSITION;
};
VOut VShader(float4 position : POSITION, float4 normal : NORMAL, float2 texcoord : TEXCOORD)
{
VOut output;
output.position = mul(final, position);
// set the ambient light
output.color = ambientcol;
// calculate the diffuse light and add it to the ambient light
float4 norm = normalize(mul(rotation, normal));
float diffusebrightness = saturate(dot(norm, lightvec));
output.color += lightcol * diffusebrightness;
//Set texture coordinates
output.texcoord = texcoord;
return output;
}
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PShader(float4 color : COLOR, float2 texcoord : TEXCOORD) : SV_TARGET
{
return color * Texture.Sample(ss, texcoord);
}
Now I don't think theres any problems coming from my .fx file, and yes I have inserts the U and V cords into the Vertex Struct to apply texture to the cube, and have the load up method and it is being released at the end.
Although I dont know why it's doing this, has anyone else encountered a problem like this before and how did they overcome it?
in OpenGL I was able to do Begin() and Flush() between each object I was drawing, is there a function similar to that in DirectX11?
Thanks!






