DirectX10 - Simple texture initialization problem

Started by
3 comments, last by Klashnikov Kid 13 years, 6 months ago
Hey guys, I've been slowly but surely working my way through learning how to use DirectX10 and I've recently ran into a dead end.

I've been trying to get a simple texture mapped onto a cube and although based on my reading material it seems like I'm doing everything right, I'm obviously missing something.

First I'm creating a Shader resource view to the image file. Then I'm getting a handle to the texture variable in the shader. Followed by setting that shader value to the resource view.
HRESULT _hr = D3DX10CreateShaderResourceViewFromFile(device, TEXT("CubeFace.png"), NULL, NULL, &fxDiffuseMapRV, NULL);if (FAILED(_hr))		FatalError(_hr, TEXT("Failed to create diffuse map view for ground tile."));fxDiffuseMapV = fx->GetVariableByName("diffuseMap")->AsShaderResource();fxDiffuseMapV->SetResource(fxDiffuseMapRV);


Here is my simple shader. All the relevant buffer data is set, including an identity matrix for the texture matrix.
Texture2D diffuseMap;cbuffer cbPerObject{	float4x4 wvpM;	float4x4 worldM;	float4x4 texM;};struct VS_IN{	float3 posL    : POSITION;	float2 texC    : TEXCOORD;};struct VS_OUT{	float4 posH    : SV_POSITION;        float2 texC    : TEXCOORD;};SamplerState Sampler{	Filter = MIN_MAG_MIP_LINEAR;};VS_OUT VS(VS_IN vIn){	VS_OUT vOut;	vOut.posH = mul(float4(vIn.posL, 1.0f), wvpM);		vOut.texC  = mul(float4(vIn.texC, 0.0f, 1.0f), texM);		return vOut;}float4 PS(VS_OUT pIn) : SV_Target{   float4 diffuse = diffuseMap.Sample(Sampler, pIn.texC);      return diffuse;}technique10 Render{    pass P0    {        SetVertexShader( CompileShader( vs_4_0, VS() ) );        SetGeometryShader( NULL );        SetPixelShader( CompileShader( ps_4_0, PS() ) );    }}


And just in case it is causing the problem (however, I'm fairly sure it isn't) my vertex data.
        vertices[0].position = D3DXVECTOR3(-2.0f, -4.0, -2.0f);	vertices[1].position = D3DXVECTOR3( 2.0f, -4.0, -2.0f);	vertices[2].position = D3DXVECTOR3(-2.0f, -6.0, -2.0f);	vertices[3].position = D3DXVECTOR3( 2.0f, -6.0, -2.0f);	vertices[4].position = D3DXVECTOR3(-2.0f, -4.0,  2.0f);	vertices[5].position = D3DXVECTOR3( 2.0f, -4.0,  2.0f);	vertices[6].position = D3DXVECTOR3(-2.0f, -6.0,  2.0f);	vertices[7].position = D3DXVECTOR3( 2.0f, -6.0,  2.0f);	vertices[0].textureCoordinate = D3DXVECTOR2(0.0f, 1.0f);	vertices[1].textureCoordinate = D3DXVECTOR2(1.0f, 1.0f);	vertices[2].textureCoordinate = D3DXVECTOR2(1.0f, 1.0f);	vertices[3].textureCoordinate = D3DXVECTOR2(0.0f, 1.0f);	vertices[4].textureCoordinate = D3DXVECTOR2(0.0f, 0.0f);	vertices[5].textureCoordinate = D3DXVECTOR2(1.0f, 0.0f);	vertices[6].textureCoordinate = D3DXVECTOR2(1.0f, 0.0f);	vertices[7].textureCoordinate = D3DXVECTOR2(0.0f, 0.0f);

(I'm using an index buffer and I'm aware that only the top and bottom might not look screwy. It's going to be a floor tile.)

I've have no problem drawing a white cube when I override the returned color in the shader but when I don't, it's solid black.

My debug output reads "The Pixel Shader unit expects a Shader Resource View at Slot 0, but none is bound." so is my shader resource view just not getting set for somereason?

Thanks!
Advertisement
After you set the resource you must call
mTech->GetPassByIndex(0)->Apply(0); 


replace mTech with your ID3D10EffectTechnique* var.

Also, you dont have to multiply your texC by an identity matrix...
So replace:
vOut.texC  = mul(float4(vIn.texC, 0.0f, 1.0f), texM);


By:
vOut.texC  = float4(vIn.texC, 0.0f, 1.0f);


[Edited by - Aqua Costa on October 24, 2010 11:13:09 AM]
If calling the Apply method doesnt correct your issue can you please post you draw method?
Here is my draw technique. I believe I'm applying the shader as far as I know.

device->IASetInputLayout(vertexLayout);	device->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);	device->IASetVertexBuffers(0, 1, &vertexBuffer, &vertexStride, &vertexOffset);	device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);	for (UINT p = 0; p < fxTechniqueDescription.Passes; ++p)			fxTechnique->GetPassByIndex(p)->Apply(NULL);	device->DrawIndexed(indicesSize, 0, 0);


Also you mentioned not transforming the texture coordinates, but I think ya accidentally duplicated the code. :P

I believe the book I'm learning from included the texture matrix for animated texture effects. Which you are right since I'm definitely not doing anything of the sort yet.
Solved.

Geez... my cube's constructor didn't have the call to my texture initialization... Excuse me while I go kick myself.

One last question though, is not using an index buffer the only way to map a cube with conflicting UV coordinates?

This topic is closed to new replies.

Advertisement