Jump to content

  • Log In with Google      Sign In   
  • Create Account

Whoops! wrong Texture mapping, why does it happen?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 lucky6969b   Members   -  Reputation: 624

Like
0Likes
Like

Posted 01 November 2012 - 04:17 AM

In CreateMeshContainer
// if materials provided, copy them
if( NumMaterials > 0 )
{
		memcpy( pMeshContainer->pMaterials, pMaterials, sizeof( D3DXMATERIAL ) * NumMaterials );
		for( iMaterial = 0; iMaterial < NumMaterials; iMaterial++ )
  {
			if( pMeshContainer->pMaterials[iMaterial].pTextureFilename != NULL )
   {
	LPDIRECT3DTEXTURE9 texture = NULL;
				WCHAR strTexturePath[MAX_PATH];
				WCHAR wszBuf[MAX_PATH] = L"Data\\";
				MultiByteToWideChar( CP_ACP, 0, pMeshContainer->pMaterials[iMaterial].pTextureFilename, -1, strTexturePath, MAX_PATH );
				
  
				wcsncat(wszBuf, strTexturePath, wcslen(wszBuf)+wcslen(strTexturePath));
	
				if( FAILED( D3DXCreateTextureFromFileW( pd3dDevice, wszBuf,
													   &texture) ) )
	{
	 pMeshContainer->textures.clear();
	}
	else
	{
	 pMeshContainer->textures.push_back(texture);
	}
	  // don't remember a pointer into the dynamic memory, just forget the name after loading
	 pMeshContainer->pMaterials[iMaterial].pTextureFilename = NULL;
   }

		
		  
		}
	}
	else // if no materials provided, use a default one
  {
		pMeshContainer->pMaterials[0].pTextureFilename = NULL;
		memset( &pMeshContainer->pMaterials[0].MatD3D, 0, sizeof( D3DMATERIAL9 ) );
		pMeshContainer->pMaterials[0].MatD3D.Diffuse.r = 0.5f;
		pMeshContainer->pMaterials[0].MatD3D.Diffuse.g = 0.5f;
		pMeshContainer->pMaterials[0].MatD3D.Diffuse.b = 0.5f;
		pMeshContainer->pMaterials[0].MatD3D.Specular = pMeshContainer->pMaterials[0].MatD3D.Diffuse;
	}

In Rendering code,
for (iAttrib = 0; iAttrib < pMeshContainer->NumAttributeGroups; iAttrib++)
	{
	  D3DXMATRIX invWorld;
	  D3DXMatrixInverse(&invWorld, NULL, &pFrame->CombinedTransformationMatrix);
	  D3DXMatrixTranspose(&invWorld, &invWorld);
	  //Normal Static Mesh
	  g_pNormalEffect->SetMatrix("gWVP", &(pFrame->CombinedTransformationMatrix * (m_Cam.GetViewMatrix() * m_Cam.GetProjMatrix())));
	  g_pNormalEffect->SetMatrix("gWorld", &pFrame->CombinedTransformationMatrix);
	 g_pNormalEffect->SetMatrix("gWorldInvTrans", &invWorld);
	 D3DXHANDLE hTech = g_pNormalEffect->GetTechniqueByName("PhongDirLtTexTech");
	 g_pNormalEffect->SetTechnique(hTech);
	   // Sum of all ambient and emissive contribution
	 D3DXCOLOR color1( pMeshContainer->pMaterials[iAttrib].MatD3D.Ambient );
	 D3DXCOLOR color2( .25, .25, .25, 1.0 );
	 D3DXCOLOR ambEmm;
	 D3DXColorModulate( &ambEmm, &color1, &color2 );
	 ambEmm += D3DXCOLOR( pMeshContainer->pMaterials[iAttrib].MatD3D.Emissive );
	 //Render the mesh
	  // set material color properties
	 g_pNormalEffect->SetVector( "MaterialDiffuse",
										 ( D3DXVECTOR4* )&(
										 pMeshContainer->pMaterials[iAttrib].MatD3D.Diffuse ) );
	 g_pNormalEffect->SetVector( "MaterialAmbient", ( D3DXVECTOR4* )&ambEmm ) ;
	 if (pMeshContainer->pMaterials[iAttrib].MatD3D.Diffuse.a < 1.0f)
	 {
	   //g_pEffect1->SetValue("vMaterialColor", &boneMesh->materials[i].Diffuse, sizeof(D3DCOLORVALUE));
	   //m_pDevice->SetMaterial(&boneMesh->materials[i]);
	
	
	  if (!pMeshContainer->textures.empty())
		g_pNormalEffect->SetTexture("gTex", pMeshContainer->textures[iAttrib]);
	   g_pNormalEffect->Begin(NULL, NULL);
	   g_pNormalEffect->BeginPass(0);
	   pMeshContainer->MeshData.pMesh->DrawSubset(iAttrib);
	  
	   g_pNormalEffect->EndPass();
	   g_pNormalEffect->End();
	  }
	 }
   }

In shader,
float4 lhtDir = {0.0f, 0.0f, -1.0f, 1.0f};	//light Direction
float4 LightAmbient = {0.3f, 0.3f, 0.3f, 0.3f};
float4 LightDiffuse = {0.6f, 0.6f, 0.6f, 1.0f}; // Light Diffuse
float4 LightSpec = {0.3f, 0.3f, 0.3f, 0.3f};
float4 MaterialAmbient : MATERIALAMBIENT = {0.1f, 0.1f, 0.1f, 1.0f};
float4 MaterialDiffuse : MATERIALDIFFUSE = {0.8f, 0.8f, 0.8f, 1.0f};
float4 MaterialSpec : MATERIALSPECULAR = {0.1f, 0.1f, 0.1f, 0.1f};
extern float SpecPower = 0.5f;
uniform extern float4x4 gWorld;
uniform extern float4x4 gWorldInvTrans;
uniform extern float4x4 gWVP;  // World-View-Projection

uniform extern float3   gEyePosW;
uniform extern texture  gTex;
sampler TexS = sampler_state
{
Texture = <gTex>;
MinFilter = Anisotropic;
MagFilter = LINEAR;
MipFilter = LINEAR;
MaxAnisotropy = 8;
AddressU  = WRAP;
	AddressV  = WRAP;
};

struct OutputVS
{
	float4 posH	: POSITION0;
	float3 normalW : TEXCOORD0;
	float3 toEyeW  : TEXCOORD1;
	float2 tex0	: TEXCOORD2;
};
OutputVS PhongDirLtTexVS(float3 posL : POSITION0, float3 normalL : NORMAL0, float2 tex0: TEXCOORD0)
{
	// Zero out our output.
OutputVS outVS = (OutputVS)0;

// Transform normal to world space.
outVS.normalW = mul(float4(normalL, 0.0f), gWorldInvTrans).xyz;

// Transform vertex position to world space.
float3 posW  = mul(float4(posL, 1.0f), gWorld).xyz;

// Compute the unit vector from the vertex to the eye.
outVS.toEyeW = gEyePosW - posW;

// Transform to homogeneous clip space.
outVS.posH = mul(float4(posL, 1.0f), gWVP);

// Pass on texture coordinates to be interpolated in rasterization.
outVS.tex0 = tex0;
// Done--return the output.
	return outVS;
}
float4 PhongDirLtTexPS(float3 normalW : TEXCOORD0, float3 toEyeW  : TEXCOORD1, float2 tex0 : TEXCOORD2) : COLOR
{
// Interpolated normals can become unnormal--so normalize.
normalW = normalize(normalW);
toEyeW  = normalize(toEyeW);

// Light vector is opposite the direction of the light.
//float3 lightVecW = -gLight.dirW;
float3 lightVecW = lhtDir;

// Compute the reflection vector.
float3 r = reflect(-lightVecW, normalW);

// Determine how much (if any) specular light makes it into the eye.
float t  = pow(max(dot(r, toEyeW), 0.0f), SpecPower);

// Determine the diffuse light intensity that strikes the vertex.
float s = max(dot(lightVecW, normalW), 0.0f);

// Compute the ambient, diffuse and specular terms separatly.
float3 spec = t*(MaterialSpec*LightSpec).rgb;
float3 diffuse = s*(MaterialDiffuse*LightDiffuse).rgb;
float3 ambient = MaterialAmbient*LightAmbient;

// Get the texture color.
float4 texColor = tex2D(TexS, tex0);

// Combine the color from lighting with the texture color.
float3 color = (ambient + diffuse)*texColor.rgb + spec;
//float3 color = (ambient + diffuse).rgb + spec;

// Sum all the terms together and copy over the diffuse alpha.
	return float4(color, MaterialDiffuse.a*texColor.a);
//return float4(color, MaterialDiffuse.a);

}
technique PhongDirLtTexTech
{
	pass P0
	{
  Lighting = true;
  AlphaBlendEnable = TRUE;
		DestBlend = INVSRCALPHA;
		SrcBlend = SRCALPHA;
		// Specify the vertex and pixel shader associated with this pass.
		vertexShader = compile vs_2_0 PhongDirLtTexVS();
		pixelShader  = compile ps_2_0 PhongDirLtTexPS();
	}
}

The full alpha execution path is symmetrical.
Anyone shed some lights on this?
Thanks
Jack

Attached Thumbnails

  • texture.png

Edited by lucky6969b, 01 November 2012 - 04:19 AM.


Sponsor:

#2 Seabolt   Members   -  Reputation: 633

Like
0Likes
Like

Posted 01 November 2012 - 10:53 AM

Sorry what is it supposed to look like? If you have a texture applied that shouldn't have been, then I'd check this branch:

		  if (!pMeshContainer->textures.empty())
			    g_pNormalEffect->SetTexture("gTex", pMeshContainer->textures[iAttrib]);

Maybe you're expecting a texture there and are not getting it so the shader is reading the last set texture. In fact you're reading that texture no matter what it seems, so if that ever did happen you'd see some artifacts.
Perception is when one imagination clashes with another

#3 lucky6969b   Members   -  Reputation: 624

Like
0Likes
Like

Posted 01 November 2012 - 11:03 PM

Sorry what is it supposed to look like? If you have a texture applied that shouldn't have been, then I'd check this branch:

		  if (!pMeshContainer->textures.empty())
				g_pNormalEffect->SetTexture("gTex", pMeshContainer->textures[iAttrib]);

Maybe you're expecting a texture there and are not getting it so the shader is reading the last set texture. In fact you're reading that texture no matter what it seems, so if that ever did happen you'd see some artifacts.


Hello Seabolt,
My problem is that the texture should be applied to the skydome instead of the warehouse. The others are not artifacts, they are supposed to be buildings around the site.
What is general way to reset a texture after applying to a first object?

g_pNormalEffect->SetTexture("gTex", NULL);
Try this after render the subset, however, the entire scene is a patch of blackness now
Thanks for your consideration.
Jack

Edited by lucky6969b, 02 November 2012 - 12:15 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS