Jump to content
  • Advertisement
Sign in to follow this  
_Camus_

HLSL and .X Mesh

This topic is 3672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, am learning my first steps on HLSL, i finally goy my phong specular light on a simple teapot. But i dont know how to apply a texture with HLSL, right now, the way i use to apply textures to my meshes is this: LPD3DXBUFFER bufMaterial; D3DXLoadMeshFromX(File, D3DXMESH_MANAGED, d3ddev, NULL, &bufMaterial, NULL, &Model->numMaterials, &Model->Mesh); D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer(); Model->Material = new D3DMATERIAL9[Model->numMaterials]; Model->Texture = new LPDIRECT3DTEXTURE9[Model->numMaterials]; for(DWORD index = 0; index < Model->numMaterials; index++) { Model->Material[index] = tempMaterials[index].MatD3D; Model->Material[index].Ambient = Model->Material[index].Diffuse; if(FAILED(D3DXCreateTextureFromFile(d3ddev,tempMaterials[index].pTextureFilename,&Model->Texture[index]))) { Model->Texture[index] = NULL;} } return; That for load, and for draw: for(DWORD index = 0; index < Model->numMaterials; index++) { d3ddev->SetMaterial(&Model->Material[index]); if(Model->Texture[index] != NULL) d3ddev->SetTexture(0, Model->Texture[index]); Model->Mesh->DrawSubset(index); } I dont know how to apply the: EffectFile->SetTexture("Map", texture); Can anyone explain to me this? or some info about, i cannot find this. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Hi ya,

You are using capital letters for your variables, naughty boy!

Your C++ code needs to look something like this:

Quote:

EffectFile->SetTexture( "xTexture", Model->Texture[index]);


and your HLSL code will need to have this at the top of it:
Quote:

Texture xTexture;

sampler ColoredTextureSampler = sampler_state { texture = <xTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};


and then stick this in your pixel shader...
Quote:

O.Color = tex2D(ColoredTextureSampler, PSIn.TexCoords);


make sure your vertex shader is outputting texture coordinates to the pixel shader.

If you are still having problems paste in your HLSL code and I'll give you a hand with that.

Andy

Share this post


Link to post
Share on other sites
Hi man thx por rply!

Well, i did what you said, and i think am doing something wrong,
coz my mesh appears, but black, i see the reflection of my specular
light, but the material its black, maybe i miss something, this is
my entire .fx:

float4x4 World;
float4x4 View;
float4x4 Projection;
float4 vecLightDir;
float4 vecEye;

Texture xTexture;

sampler ColoredTextureSampler = sampler_state {
texture = <xTexture>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter=LINEAR;
AddressU = mirror;
AddressV = mirror;};


struct VertexOut
{
float4 Pos : POSITION;
float3 Light : TEXCOORD0;
float3 Norm : TEXCOORD1;
float3 View : TEXCOORD2;
float2 Tex : TEXCOORD3;

};

VertexOut VertexShade(float4 Pos : POSITION, float3 Normal : NORMAL, float2 texCoords : TEXCOORD)
{
VertexOut Vert = (VertexOut)0;
float4x4 Transform;

Transform = mul(World, View);
Transform = mul(Transform, Projection);
Vert.Pos = mul(Pos, Transform);

Vert.Light = vecLightDir;
float3 PosWorld = normalize(mul(Pos,World));
Vert.View = vecEye - PosWorld;

Vert.Tex = texCoords.xy;

Vert.Norm = normalize(mul(Normal,World));


return Vert;
}

float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1, float3 View : TEXCOORD2, float2 Tex: TEXCOORD3) : COLOR
{
float4 diffuse = { 0.6f, 1.0f, 0.6f, 1.0f};
float4 ambient = {0.0, 0.1, 0.0, 1.0};

float4 color = tex2D(ColoredTextureSampler,Tex );


float3 Normal = normalize(Norm);
float3 LightDir = normalize(Light);
float3 ViewDir = normalize(View);
float4 diff = saturate(dot(Normal, LightDir));


float shadow = saturate(4*diff);

float3 Reflect = normalize(2 * diff * Normal - LightDir);
float4 specular = pow(saturate(dot(Reflect, ViewDir)), 2);

return color + shadow * (color * diff + specular);
}

technique FirstTechnique
{
pass FirstPass
{
Lighting = FALSE;
ZEnable = TRUE;

Sampler[0] = (ColoredTextureSampler);


VertexShader = compile vs_2_0 VertexShade();
PixelShader = compile ps_2_0 PS();

}
}

Thanks a lot, maybe i need to multiply the color by the normal or some?

Share this post


Link to post
Share on other sites
SOLVED.

This .fx its almost perfect, my mistake was in the .cpp,
sorry xD, the only change i made was:

return color + shadow * (color * diff + specular);

to this one:

return color * diff + specular;

and now my mesh looks awesome with phong light.

Thanks again :D

EDIT-

Now i am facing another thing, my model uses more than 1 bitmap, and
my mesh its textured with the last bitmap, i need to create one
sample per bitmap?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!