Thursday, June 14, 2018

xna - Drawing simple geometric figures with DrawUserPrimitives?


I'm trying to draw a simple triangle based on an array of vertex. I've been searching for a tutorial and I found a simple example on riemers but I couldn't get it to work. I think it was made for XNA 3 and it seems there were some changes to XNA 4?


Using this example:


http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/The_first_triangle.php


I get this error:



Additional information: The current vertex declaration does not include all the elements required by the current vertex shader. TextureCoordinate0 is missing.



I'm not english so I'm having some trouble to understand everything.



For what I understand error is confusing because I'm trying to draw a triangle color based and not texture based and it shouldn't need a texture.


Also I saw some articles about dynamic shadows and lights and I would like to know if this is the kind of code used to do it with some tweaks like culling because I'm wondering if its heavy code for performance in real time.


    public void LoadContent()
{
effect = Game.Content.Load("Effect1");
playerVision = new VertexPositionColor[3];
playerVision[0].Position = new Vector3(-0.5f, -0.5f, 0f);
playerVision[0].Color = Color.Red;
playerVision[1].Position = new Vector3(0, 0.5f, 0f);
playerVision[1].Color = Color.Green;

playerVision[2].Position = new Vector3(0.5f, -0.5f, 0f);
playerVision[2].Color = Color.Yellow;
}

// comes from draw method
private void drawPlayerVision(GameTime gameTime, SpriteBatch spriteBatch)
{
effect.CurrentTechnique = effect.Techniques["Pretransformed"];
Game.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, playerVision, 0, 1, VertexPositionColor.VertexDeclaration);
}


Answer



Your problem is that effect is not sent into graphics card, you need to do this:


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

foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();


Game.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, playerVision, 0, 1, VertexPositionColor.VertexDeclaration);

}

On first line, you must select effect technique (effect file from Riemers have more of them), then send effect to graphics card and finally draw your primitive.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...