Texture not showing up

Started by
11 comments, last by BrakePad 15 years, 2 months ago
[Reposting here as this seems to be the better sub section for my questions] I have problems texturing my quad in DirectX. I am pretty sure that I have loaded the textures correctly. Can anyone please see if I have missed out something? At the moment it just show the quad in a grey-ish (default) colour.

#define D3DFVF_CUSTOMVERTEX  D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1

void Setup()
{

	//
	// Create vertex and index buffers.
	//

	Device->CreateVertexBuffer(
		24 * sizeof(Vertex), 
		0,
		Vertex::FVF,
		D3DPOOL_MANAGED,
		&VB,
		0);


	Vertex* v = 0;
	VB->Lock(0, 0, (void**)&v, 0);

	// floor
	v[0] = Vertex(-7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
	v[1] = Vertex(-7.5f, 0.0f,   0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
	v[2] = Vertex( 7.5f, 0.0f,   0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
	
	v[3] = Vertex(-7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
	v[4] = Vertex( 7.5f, 0.0f,   0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
	v[5] = Vertex( 7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);

	VB->;Unlock();

	D3DXCreateTextureFromFile(Device, "floor.jpg", &FloorTex);
	
	Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
}


bool Display(float timeDelta)
{
	if( Device )
	{
		// snipped out some initialisation etc

		D3DXMatrixInverse(&mWorldViewIT, NULL, &mWorldView);
		D3DXMatrixTranspose(&mWorldViewIT, &mWorldViewIT);
		mFX->SetMatrix(mhWorldViewIT,&(mWorldViewIT));
		mFX->SetMatrix(mhWVP,&(mWorld*mView*mproj));

		//Begin passes
		mFX->Begin(&numPasses,0);
		//draw textured quad
		for(int i=0; i < numPasses; ++i)
		{
			mFX->BeginPass(i);			
			mFX->SetTexture(mhgTex,FloorTex);
			mFX->CommitChanges();
			Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
			Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
			mFX->EndPass();		

		}
		mFX->End();		
		
		Device->EndScene();
		Device->Present(0,0,0,0); 
	}
	return true;
}






Advertisement
1. You're not checking any return values. If D3DXCreateTextureFromFile() fails, you don't notice, so FloorTex is a null pointer. Test the return value with the SUCCEEDED or FAILED macros.

2. What do the Debug Runtimes say?
Is your shader passing the texture uv coords correctly from your vertex - pixel shader?

Also, are you correctly sampling the texture in your pixel shader?
Quote:1. You're not checking any return values. If D3DXCreateTextureFromFile() fails, you don't notice, so FloorTex is a null pointer. Test the return value with the SUCCEEDED or FAILED macros.

2. What do the Debug Runtimes say?


Right, I tested the loading of texture and it does load correctly. Haven't got time at the moment to report Debug Runtimes yet , will do so this evening.

I think it has to do with my pixel/vertex shader. It works for loading textures for my .X mesh but it seems that it doesn't like in this case.

Can someone have a look at my shader file to see if I did something wrong?

//-------------------------------------------------------------------------------//Vertex and pixel shader demonstrating ambient, diffuse and specular lighting// Ambient intensity values are pre baked//------------------------------------------------------------------------------------------------------//------------------------------------------------------------------------------------------------------// Global params provided by the app//------------------------------------------------------------------------------------------------------uniform extern float4x4 gWVP;uniform extern float4x4 gWorldViewIT;uniform extern float4x4 gWorldView;uniform extern float4x4 gViewIT;uniform extern float4 gDiffuseMtrl;uniform extern float4 gAmbMtrl;uniform extern float4 gSpecMtrl;uniform extern float4 gLightCol;uniform extern float4 gLightDir;// light intensityfloat4 I_a = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffusefloat4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specularfloat  n   : MATERIALPOWER = 64.0f;                          // power//---------------------------------------------------------------------------------------------------------------------------// Input channel (vertex shader)//---------------------------------------------------------------------------------------------------------------------------struct InputVS{	float3 posH : POSITION0;	float3 Norm : NORMAL;};//---------------------------------------------------------------------------------------------------------------------------// Output channel (vertex shader)//---------------------------------------------------------------------------------------------------------------------------struct OutputVS{	float4 posH : POSITION0;	float3 col: COLOR0;	float3 col_amb: TEXCOORD0;	float3 col_diff: TEXCOORD1;	float3 col_spec: TEXCOORD2;	float3 EV: TEXCOORD3;		float3 N: TEXCOORD4;	float3 E: TEXCOORD5;};//---------------------------------------------------------------------------------------------------------------------------// Vertex shader//---------------------------------------------------------------------------------------------------------------------------OutputVS PhongShaderVS(InputVS input){	//Zero out our output	OutputVS outVS = (OutputVS)0;		//Transform to homogeneous clipspace	outVS.posH = mul(float4(input.posH, 1.0f), gWVP);	//eye position 	outVS.EV = mul(input.posH,gWorldView).xyz;	//normals	outVS.N =mul(input.Norm,gWorldViewIT).xyz;	//eye vector	outVS.E=normalize(outVS.EV);			outVS.col_amb =  I_a*gAmbMtrl*gLightCol;	outVS.col_diff = I_d*gDiffuseMtrl*gLightCol;	outVS.col_spec = I_s*gSpecMtrl*gLightCol;		//return output	return outVS;}//---------------------------------------------------------------------------------------------------------------------------// Input channel pixel shader//---------------------------------------------------------------------------------------------------------------------------struct InputPS{	float4 col: COLOR0;	float4 col_amb: TEXCOORD0;	float4 col_diff: TEXCOORD1;	float4 col_spec: TEXCOORD2;	float3 EV: TEXCOORD3;	float3 N: TEXCOORD4;	float3 E: TEXCOORD5;};//---------------------------------------------------------------------------------------------------------------------------// Pixel shader (input channel):output channel//---------------------------------------------------------------------------------------------------------------------------float4 PhongShaderPS(InputPS input): COLOR{	//transform light vector into viewspace and normalize	float4 finalColour=0;		float3 L=-normalize(mul(gLightDir,gWorldView));			//half angle vector used in approximation to specular component	float3 H=normalize(input.E+L);		//ambient term		float4 ambient={0.4, 0.4, 0.4, 1.00f};	//Calculate diff, specular and ambient components of lighht	float diff = max(0, dot(input.N,L));    	//specular component using Blinns half angle	//	float spec = pow(max(0, dot(input.N, H)), n);			finalColour =input.col_amb*ambient + input.col_diff*diff +input.col_spec*spec;	finalColour.a=1.0;			//eye position 	//float3 EV = mul(input.posH,gWorldView).xyz;	//normals	//float3 N =mul(input.Norm,gWorldViewIT).xyz;	//eye vector	//float3 E=normalize(EV);		//half angle vector used in approximation to specular component		//float3 H=normalize(E+L);				return finalColour;	}technique PhongShaderTech{	pass P0	{ 	    Lighting       = TRUE;        SpecularEnable = TRUE;		vertexShader = compile vs_3_0 PhongShaderVS();		pixelShader = compile ps_3_0 PhongShaderPS();		//specify render device states associated with the pass		FillMode = Solid;// WireFrame;	}}

What result did you expect since you haven't got a Sampler in your shader?
Quote:Original post by ming zhu

What result did you expect since you haven't got a Sampler in your shader?


Sorry, I am very new do DirectX as you can see from my codes. I am familiar with sampling done within the main file eg:
device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );


However I am not familiar with the HLSL version. Can you explain briefly what I need to do? Do I add sampling in the shader technique?
You set a sampler in your shader as follows:

texture myTexture;sampler2D TextureSampler= sampler_state{    Texture = <myTexture>;    MIPFILTER = LINEAR;    MAGFILTER = LINEAR;    MINFILTER = LINEAR;};


the texture name can be anything you want... Now you need to tell the shader what texture you're using before calling DrawPrimitve like so:

m_effect->SetTexture("myTexture", m_texture);m_effect->CommitChanges();


Then in your pixel shader you need to sample the texture, and return the texel colour for the given uv coordinates like so:

output.Color = tex2D(TextureSampler, input.TexCoord);


where input.TexCoord is the uv coordinates (float2) of your texture, but you need to pass these texture coordinates through your vertex shader also, otherwise your pixel shader has no idea what UV coords to use...

Hope this helps a bit more!

bp

You need to add a tex-coord param to your vertex shader's input for passing in the UV sets.

And instead of using gDiffuseMtrl/gAmbMtrl/gSpecMtrl, you may want to use a sampler to get the pixel color from your texture.

You also need to pass the uv numbers down to the pixel shader, as an output of the vertex shader.

A very simple approach would be like this,

texture gTexture0;

sampler SimpleSampler = sampler_state
{
texture = <gTexture0>;
MipFilter = LINEAR;
MinFilter = LINEAR;
};

struct InputVS
{
float3 posH : POSITION0;
float3 Norm : NORMAL;
float2 uv : TEXCOORD0;
};

struct OutputVS
{
float4 posH : POSITION0;
float2 uv: TEXCOORD0;
};

OutputVS SimpleVS(InputVS input)
{
OutputVS outVS = (OutputVS)0;

...

outVS.uv = input.uv;

return outVS;
}

float4 SimplePS(OutputVS input): COLOR
{
float4 finalColour=0;

...

finalColour = tex2D( SimpleSampler, input.uv );

return finalColour;
}

You can then do shadings or whatever you like :)
Thanks ming zhu and brakepad. I am also looking at Frank Luna's book but all it still seems a little bit confusing. It's easy when you work through the examples as they are focussed on getting one thing done. But when you try to build your own scene, things get a bit icky.

Anyway, I modified my shader file shown below.Please kindly see if I made any mistakes.

//-------------------------------------------------------------------------------//Vertex and pixel shader demonstrating ambient, diffuse and specular lighting// Ambient intensity values are pre baked//------------------------------------------------------------------------------------------------------//------------------------------------------------------------------------------------------------------// Global params provided by the app//------------------------------------------------------------------------------------------------------uniform extern float4x4 gWVP;uniform extern float4x4 gWorldViewIT;uniform extern float4x4 gWorldView;uniform extern float4x4 gViewIT;uniform extern float4 gDiffuseMtrl;uniform extern float4 gAmbMtrl;uniform extern float4 gSpecMtrl;uniform extern float4 gLightCol;uniform extern float4 gLightDir;// light intensityfloat4 I_a = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffusefloat4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // speculartexture gTexture0;sampler SimpleSampler = sampler_state{	texture = <gTexture0>;	MipFilter = LINEAR;	MinFilter = LINEAR;};float  n   : MATERIALPOWER = 64.0f;                          // power//---------------------------------------------------------------------------------------------------------------------------// Input channel (vertex shader)//---------------------------------------------------------------------------------------------------------------------------struct InputVS{	float3 posH : POSITION0;	float3 Norm : NORMAL;	float2 uv : TEXCOORD0;	};//---------------------------------------------------------------------------------------------------------------------------// Output channel (vertex shader)//---------------------------------------------------------------------------------------------------------------------------struct OutputVS{	float4 posH : POSITION0;	float3 col: COLOR0;	float3 col_amb: TEXCOORD0;	float3 col_diff: TEXCOORD1;	float3 col_spec: TEXCOORD2;	float3 EV: TEXCOORD3;		float3 N: TEXCOORD4;	float3 E: TEXCOORD5;		float2 uv: TEXCOORD0;};//---------------------------------------------------------------------------------------------------------------------------// Vertex shader//---------------------------------------------------------------------------------------------------------------------------OutputVS PhongShaderVS(InputVS input){	//Zero out our output	OutputVS outVS = (OutputVS)0;		//Transform to homogeneous clipspace	outVS.posH = mul(float4(input.posH, 1.0f), gWVP);	//eye position 	outVS.EV = mul(input.posH,gWorldView).xyz;	//normals	outVS.N =mul(input.Norm,gWorldViewIT).xyz;	//eye vector	outVS.E=normalize(outVS.EV);			outVS.col_amb =  I_a*gAmbMtrl*gLightCol;	outVS.col_diff = I_d*gDiffuseMtrl*gLightCol;	outVS.col_spec = I_s*gSpecMtrl*gLightCol;		outVS.uv = input.uv; 		//return output	return outVS;}//---------------------------------------------------------------------------------------------------------------------------// Input channel pixel shader//---------------------------------------------------------------------------------------------------------------------------struct InputPS{	float4 col: COLOR0;	float4 col_amb: TEXCOORD0;	float4 col_diff: TEXCOORD1;	float4 col_spec: TEXCOORD2;	float3 EV: TEXCOORD3;	float3 N: TEXCOORD4;	float3 E: TEXCOORD5;};//---------------------------------------------------------------------------------------------------------------------------// Pixel shader (input channel):output channel//---------------------------------------------------------------------------------------------------------------------------float4 PhongShaderPS(InputPS input): COLOR{	//transform light vector into viewspace and normalize	float4 finalColour=0;		float3 L=-normalize(mul(gLightDir,gWorldView));			//half angle vector used in approximation to specular component	float3 H=normalize(input.E+L);		//ambient term		float4 ambient={0.4, 0.4, 0.4, 1.00f};	//Calculate diff, specular and ambient components of lighht	float diff = max(0, dot(input.N,L));    	//specular component using Blinns half angle	//	float spec = pow(max(0, dot(input.N, H)), n);			finalColour =input.col_amb*ambient + input.col_diff*diff +input.col_spec*spec;	finalColour.a=1.0;			//eye position 	//float3 EV = mul(input.posH,gWorldView).xyz;	//normals	//float3 N =mul(input.Norm,gWorldViewIT).xyz;	//eye vector	//float3 E=normalize(EV);		//half angle vector used in approximation to specular component		//float3 H=normalize(E+L);			finalColour = tex2D( SimpleSampler, input.uv );	return finalColour;	}technique PhongShaderTech{	pass P0	{ 	    Lighting       = TRUE;        SpecularEnable = TRUE;		vertexShader = compile vs_3_0 PhongShaderVS();		pixelShader = compile ps_3_0 PhongShaderPS();		//specify render device states associated with the pass		FillMode = Solid;// WireFrame;	}}


With this I guess all I have to do is Begin pass, setTexture, CommitChanges, and the DrawPrimitive?
At the moment your INPUTPS structure does not specify a uv parameter, so i'm guessing your shader code doesn't even compile?

Also, your vertex output structure specifies 2 variables as : TEXCOORD0 semantic - that may cause a few problems!

I'd suggest you break the shader down to a simple texturing shader, then add your lighting in afterwards. For a simple shader you should only need to pass one Texture coord (TEXTURE0 or whichever) to the vertex shader - you correctly assign the vertex output uv to it's input uv, but then fail to provide the pixel shader with this information as there is no uv declared in your INPUTPS struct.

try breaking it down a bit and getting the texture working, then move onto the lighting, hopefully then you won't get any of your semantics confused!

HTH,

bp

This topic is closed to new replies.

Advertisement