Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

custom vertexemlements

$
0
0

@Mattlekim wrote:

Hi everyone the problem is my modals are rendering black. And iv repeatedly check my render code and not been able to find any problems. Iv therefore uploaded my vertextpostioncolornormal structure and normal calculate. I am not to sure about this so someone to check the code would be greatly appreciated.

namespace BlockEmpire.Engine.Graphics
{
    public struct VertexPositionColourNormal
    {
        public Vector3 Position;
        public Color Color;
        public Vector3 Normal;

        public VertexPositionColourNormal(Vector3 pos, Color col)
        {
            Position = pos;
            Color = col;
            Normal = Vector3.Zero;
        }

        public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
        (
            new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
            new VertexElement(sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0),
            new VertexElement(sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
        );
    }

public class GraphicsHelper
    {


        public static VertexPositionColourNormal[] CalculateNormal(VertexPositionColourNormal[] verts)
        {
            for (int i = 0; i < verts.Length; i++)
                verts[i].Normal = Vector3.Zero; //set all normals to zero

            for (int i=0;i< verts.Length / 3; i++)
            {

                    int index1 = i * 3;
                    int index2 = i * 3 + 1;
                    int index3 = i * 3 + 2;

                    Vector3 side1 = verts[index1].Position - verts[index3].Position;
                    Vector3 side2 = verts[index1].Position - verts[index2].Position;
                    Vector3 normal = Vector3.Cross(side1, side2);

                    verts[index1].Normal += normal;
                    verts[index2].Normal += normal;
                    verts[index3].Normal += normal;
            }

            return verts;
        }
    }
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles