Как правильно создать вершины для такого типа проволочной сферы? - PullRequest
1 голос
/ 03 августа 2020

Я сейчас пытаюсь создать проволочную сферу, которая выглядит так: enter image description here

I came up with code that looks like this:

using SharpDX;
using SharpDX.Direct3D11;
using System;
using System.Collections.Generic;

namespace VoidwalkerEngine.Framework.DirectX.Rendering
{
    public static class Wireframe
    {
        public static ModelMesh GenerateWireframeSphere(Device device, Vector3 location, float radius)
        {
            float pi = (float)Math.PI;
            List vertices = new List();
            float twoPi = 2 * pi;
            float angleStep = twoPi / 16f;
            for (float angle = 0f; angle 

Which results in a sphere that looks like this (Using LineStrip): enter image description here

As you can see, the circles are not complete, and not only that, but there is an extra line connecting the top ring to the middle ring. I feel as though this is actually a side effect of using LineStrip. If I use LineList instead, my code results in this:

введите описание изображения здесь

Намного лучше, но мне не хватает сегментов. LineList явно путь к go, но я не знаю, как правильно обновить свой код, чтобы добавить эти недостающие сегменты линии. Кто-нибудь знает, как это сделать?

1 Ответ

0 голосов
/ 04 августа 2020

Я смог понять это. И ответ в том, что я был довольно тупым. Я забыл, что мне нужно связать вершины вместе, а также l oop обратно и «кусать хвост» каждого кольца. Вот решение:

using SharpDX;
using SharpDX.Direct3D11;
using System;

namespace VoidwalkerEngine.Framework.DirectX.Rendering
{
    public static class Wireframe
    {
        public static ModelMesh GenerateWireframeSphere(Device device, Vector3 location, float radius)
        {
            float pi = (float)Math.PI;
            Vertex[] vertices = new Vertex[96];
            float twoPi = 2 * pi;
            float angleStep = twoPi / 16f;
            int iterator = 0;
            /**
             * 1st Ring
             */
            int firstIndex = 0;
            for (float angle = 0f; angle <= twoPi; angle += angleStep)
            {
                if (iterator > firstIndex + 1)
                {
                    vertices[iterator] = vertices[iterator++ - 1];
                }
                float x = radius * (float)Math.Sin(angle);
                float y = radius * (float)Math.Cos(angle);
                float z = 0;
                vertices[iterator++] = new Vertex(location.X + x, location.Y + y, location.Z + z);
            }
            vertices[iterator] = vertices[iterator++ - 1];
            vertices[iterator++] = vertices[firstIndex];
            /**
             * 2nd Ring
             */
            firstIndex = iterator;
            for (float angle = 0f; angle <= twoPi; angle += angleStep)
            {
                if (iterator > firstIndex + 1)
                {
                    vertices[iterator] = vertices[iterator++ - 1];
                }
                float x = radius * (float)Math.Sin(angle);
                float y = 0;
                float z = radius * (float)Math.Cos(angle);
                vertices[iterator++] = new Vertex(location.X + x, location.Y + y, location.Z + z);
            }
            vertices[iterator] = vertices[iterator++ - 1];
            vertices[iterator++] = vertices[firstIndex];
            /**
             * 3rd Ring
             */
            firstIndex = iterator;
            for (float angle = 0f; angle <= twoPi; angle += angleStep)
            {
                if (iterator > firstIndex + 1)
                {
                    vertices[iterator] = vertices[iterator++ - 1];
                }
                float x = 0;
                float y = radius * (float)Math.Sin(angle);
                float z = radius * (float)Math.Cos(angle);
                vertices[iterator++] = new Vertex(location.X + x, location.Y + y, location.Z + z);
            }
            vertices[iterator] = vertices[iterator++ - 1];
            vertices[iterator++] = vertices[firstIndex];
            return new ModelMesh(device, vertices);
        }
    }
}

И изображение, которое показывает результат: введите описание изображения здесь

Следует отметить, что разрешение этой сферы является статическим c, потому что в приведенном выше примере я не рассчитывал динамическое c разрешение на основе размера. Для этого также потребуется изменить размер массива. Поэтому, если вы в конечном итоге используете это, помните об ограничении, переключитесь на список вместо массива или добейтесь небольшого объема работы, которую вам придется сделать, чтобы добавить динамическое c разрешение.

...