Jump to content
  • Advertisement
Sign in to follow this  
Skillet

problem texturing a model using a custom shader

This topic is 2860 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, I've recently been having a series of difficulties with instancing and applying textures. I decided to strip everything back to try and figure out where I was going wrong. I'm getting to grips with shaders, so thought the answer may lie there and how I was passing the information to the shader. Long story short, I took out the "instancing" side of my code and attempted to get the code as close as I could to examples and tutorials I've already seen. Here's the stream elements and draw call I've set up:

private void GenerateStreamElements()
{
streamElements = new VertexElement[1];

int position = 0;
streamElements[position] = new VertexElement(0, 0, VertexElementFormat.Vector3,
VertexElementMethod.Default,VertexElementUsage.Position, 0);

//////////////////////////////////
vertexDeclaration = new VertexDeclaration(GraphicsDevice, streamElements);
}

and here's the draw call:

private void DrawModels(ModelInfo part)
{
GraphicsDevice.VertexDeclaration = vertexDeclaration;
effect.CurrentTechnique = effect.Techniques["Instancing"];

effect.Parameters["World"].SetValue(world);
effect.Parameters["WVP"].SetValue(view * projection);
GraphicsDevice.Indices = part._indexBuffer;

effect.Parameters["ModelTexture"].SetValue(texture);

effect.Begin();
foreach (EffectPass p in effect.CurrentTechnique.Passes)
{
part._part.Effect = effect;

p.Begin();
GraphicsDevice.Vertices[0].SetSource(part._vertexBuffer, 0, part._vertexStride);

GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
0, 0, part._vertexCount, 0, part._primitiveCount);

p.End();
}
effect.End();
}

When I use this draw call exactly the same problem happens as the "instanced example" I've used in previous threads. This led me to the conclusion that instancing wasn't the problem. The only two things left were the custom shader I was using, or the draw call/stream elements themselves. When I looked at the online examples again I realised that the one I was using set an effects file but didn't actually call drawindexprimatives to render the model info, instead it used Mesh.Draw(). By this point I thought "sod it" and rewrote a slip of code to incorporate the mesh draw method (while still using the customer shader I've written) here's the result:

public void OldDraw()
{
foreach (ModelMesh mesh in TestModel.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
part.Effect = effect;

effect.CurrentTechnique = effect.Techniques["Instancing"];

effect.Parameters["World"].SetValue(world);
effect.Parameters["WVP"].SetValue(view * projection);
effect.Parameters["ModelTexture"].SetValue(texture);
}
mesh.Draw();
}
}

To my great and total delight, the code generated the model with a texture! I feel like I'm so close to understanding why the first lot of code doesn't work but I just can't put the last bits together. Why does the latter work but the former fail? Oh and bytheby thanks to Shawn Hargreaves and Funkbrotha10 who were a massive help with my last post. =)

Share this post


Link to post
Share on other sites
Advertisement
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!