Sign in to follow this  
watermelonChris

Drawing a linelist... Error about vertex declaration/shader and normal0 missing.

Recommended Posts

Hey guys.

I'm stumped with this error 'The current vertex declaration does not include all the elements required by the current vertex shader. Normal0 is missing. It is occuring on this line: graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, gridVertices, 0, 6);


I've started a projet from scratch to try narrow down where I'm going wrong but no luck.

Basically what I'm trying to do is setup a 'grid'. (All points are calculated in the SetupGrid method). All the calculated vertices seem fine coordinate wise. Here's a copy of them either way:[list]
[*]+ [0] {Position:{X:-10 Y:0.5 Z:-10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [1] {Position:{X:-10 Y:0.5 Z:10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [2] {Position:{X:-5 Y:0.5 Z:-10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [3] {Position:{X:-5 Y:0.5 Z:10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [4] {Position:{X:0 Y:0.5 Z:-10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [5] {Position:{X:0 Y:0.5 Z:10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [6] {Position:{X:5 Y:0.5 Z:-10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [7] {Position:{X:5 Y:0.5 Z:10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [8] {Position:{X:10 Y:0.5 Z:-10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [9] {Position:{X:10 Y:0.5 Z:10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [10] {Position:{X:-10 Y:0.5 Z:-10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [11] {Position:{X:10 Y:0.5 Z:-10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [12] {Position:{X:-10 Y:0.5 Z:-5} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [13] {Position:{X:10 Y:0.5 Z:-5} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [14] {Position:{X:-10 Y:0.5 Z:0} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [15] {Position:{X:10 Y:0.5 Z:0} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [16] {Position:{X:-10 Y:0.5 Z:5} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [17] {Position:{X:10 Y:0.5 Z:5} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [18] {Position:{X:-10 Y:0.5 Z:10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
+ [19] {Position:{X:10 Y:0.5 Z:10} Color:{R:255 G:0 B:0 A:255}} Microsoft.Xna.Framework.Graphics.VertexPositionColor
[/list]
[code]using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DelXnaGrideLine
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
BasicEffect effect;
VertexPositionColor[] gridVertices;
int gridLineCount;
private void SetupGrid(int width, int height, int lineCount, float yHeight, Color color)
{
gridLineCount = lineCount * 2;
gridVertices = new VertexPositionColor[lineCount * 4];
int xDiff = width / (lineCount - 1);
int zDiff = height / (lineCount - 1);
int xStart = -width / 2;
int xEnd = width / 2;
int zStart = -height / 2;
int zEnd = height / 2;
for (int x = 0; x < lineCount; x++)
{
gridVertices[x * 2] = new VertexPositionColor(
new Vector3(xStart + (x * xDiff), yHeight, zStart),
color);
gridVertices[x * 2 + 1] = new VertexPositionColor(
new Vector3(xStart + (x * xDiff), yHeight, zEnd),
color);

}
int halfLine = lineCount * 2;
for (int z = 0; z < lineCount; z++)
{
gridVertices[halfLine + z * 2] = new VertexPositionColor(
new Vector3(xStart, yHeight, zStart + (z * xDiff)),
color);
gridVertices[halfLine + z * 2 + 1] = new VertexPositionColor(
new Vector3(xEnd, yHeight, zStart + (z * xDiff)),
color);
bool bob = true;
}
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
effect = new BasicEffect(GraphicsDevice);
effect.VertexColorEnabled = true;
effect.LightingEnabled = true;
SetupGrid(20, 20, 5, 0.5f, Color.Red);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
Vector3 cameraPosition = new Vector3(1, 12, 3);
Vector3 cameraView = new Vector3(0.5f, 0.5f, 0.5f);
effect.View = Matrix.CreateLookAt(cameraPosition, cameraView, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 1000.0f);
effect.World = Matrix.Identity;
effect.CurrentTechnique.Passes[0].Apply();
graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
PrimitiveType.LineList, gridVertices, 0, 6);
base.Draw(gameTime);
}
}
}
[/code]

Share this post


Link to post
Share on other sites
Hi,

perhaps it would help if you showed the vertex declaration for VertexPositionColor and vertex shader code too?

Best regards!

Share this post


Link to post
Share on other sites
[quote name='kauna' timestamp='1329736599' post='4914795']
Hi,

perhaps it would help if you showed the vertex declaration for VertexPositionColor and vertex shader code too?

Best regards!
[/quote]

The vertex declaration? I'm assuming it's this line (it's the second line in the SetupGrid() method):
gridVertices = new VertexPositionColor[lineCount * 4];

As for vertex shader code.. Uhm what where ?

Share this post


Link to post
Share on other sites
Hi!

If you turn on the lighting computations, the basic effect needs normals specified with each vertex. Since your vertices only consist of positions and colors, D3D will complain, since the data you have given the input assembler do not match the expectations of the vertex shader of the basic effect (it assumes that normals are coming in.)

So either, you turn off the lighting computations:
[CODE]effect.LightingEnabled = false;[/CODE]

Or you create a custom vertex struct containing positions, colors and normals, see [url="http://www.riemers.net/Forum/index.php?var=2061&var2=0"]here[/url]. (You may want to add a constructor to the struct…)
If the struct implements IVertexType it provides a VertexDeclaration, which the draw call will query.
Btw, a VertexDeclaration is a defintion telling the input assembler how to feed the vertex data you submitted into the vertex shader.

Hope that helps. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
Cheers!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this