XNA: The current vertex declaration does not include all the elements required by the current vertex shader. Normal0 is missing

时光总嘲笑我的痴心妄想 提交于 2019-12-22 09:13:48

问题


Hey I'm having a little trouble. I've been working with xna for a while, but I'm completely new to 3D. I'm following the code verbatim from the winformsgraphicsdevice sample on the msdn website. It has a control that draws a primitive triangle to the screen. Simple as that, but I get an exception on this line:

GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1);

Which says:

"The current vertex declaration does not include all the elements required by the current vertex shader. Normal0 is missing."

I'm assuming it has something to do with my VertexPositionColor variable, vertices. That code is here:

vertices = new VertexPositionColor[3];
vertices[0] = new VertexPositionColor(new Vector3(-1, -1, 0), Color.Black);
vertices[1] = new VertexPositionColor(new Vector3( 1, -1, 0), Color.Black);
vertices[2] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Black);

回答1:


Your vertex shader is demanding a normal value (used for lighting calculations) but VertexPositionColor struct doesn't have it.

You'll have to create a struct for storing vertex position, color AND normal data as it's not a prebuilt type on XNA.

You can learn how to create it here: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Terrain_lighting.php

There he creates a struct called VertexPositionColorNormal, use it instead of VertexPositionColor that you're currently using.

If you don't want any lighting and you're not using BasicEffect, just remove your lighting variables/calculations from the technique you're using. If you are using BasicEffect try setting LightingEnabled and TextureEnabled properties to false.




回答2:


How are you initializing your shader? If you are using BasicEffect, are you setting the VertexColorEnabled property to true?

The error is saying that the shader/effect that you are using does not match the primitive that you are trying to draw. It is also telling you that the shader is trying to find a Normal (used for lighting) and it cannot find it in the provided structure (since you are providing a color instead).

See this BasicEffect sample to see how you can use it if you want to pass lighting information instead http://msdn.microsoft.com/en-us/library/bb203926.aspx



来源:https://stackoverflow.com/questions/9268743/xna-the-current-vertex-declaration-does-not-include-all-the-elements-required-b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!