Запутанная проблема со списками и рендерингом с помощью Silk Dot Net и OpenGL.C#

Место общения программистов C#
Anonymous
Запутанная проблема со списками и рендерингом с помощью Silk Dot Net и OpenGL.

Сообщение Anonymous »

Я пытаюсь изучить/реализовать OpenGL с помощью библиотеки Silk.net, и у меня работает фреймворк рендеринга. Я могу добавить фигуру в список рендеринга из моего метода обновления слоя, а также фигуру из коллекции из этого метода обновления, но если я попытаюсь добавить фигуру из поля класса, она не будет визуализирована.

Код: Выделить всё

Section of Layer.class

private static List _componentsField = [];

public void Update()
{
var taskbarPanel = new Panel("TaskbarPanel", WindowLeft, WindowTop - 30f, 0f, WindowRight, 30f, Color.Black);
var fileButton = new Panel("FileButton", WindowLeft + 5f, WindowTop - 25f, 0f, 50f, 20f, Color.Red);
var editButton = new Panel("EditButton", WindowLeft + 55f, WindowTop - 25f, 0f, 50f, 20f, Color.Red);
var viewButton = new Panel("ViewButton", WindowLeft + 105f, WindowTop - 25f, 0f, 50f, 20f, Color.Green);

// This works
_renderer.AddShape(taskbarPanel.Name, taskbarPanel.Quad);

// This works
List components = [];
componentsLocal.Add(fileButton);
_renderer.AddShape(componentsLocal[0].Name, componentsLocal[0].Quad);

// This doesn't work
_componentsField.Add(editButton);
_renderer.AddShape(_componentsField[0].Name, _componentsField[0].Quad);

// this doesn't work
Component[] copy = new Component[_componentsField.Count];
Array.Copy(_componentsField.ToArray(), copy, _componentsField.Count);
_renderer.AddShape(copy[0].Name, copy[0].Quad);
}

Код: Выделить всё

Section of Renderer.class

public void AddShape(string name, RenderShape shape)
{
// Offset indices
shape.OffsetIndices(VertexCount);

// Increment the vertex count;
VertexCount += shape.VertexCount;

// Add to the dictionary if it isn't already in there.
Shapes.TryAdd(name, shape);
}

public void Update()
{
List vertices = [];
List indices = [];

foreach (RenderShape shape in Shapes.Values)
{
vertices.AddRange(shape.Vertices);
indices.AddRange(shape.Indices);
}

// Add subdata to the buffer
VertexBuffer.BufferSubData(vertices.ToArray());
IndexBuffer.BufferSubData(indices.ToArray());
}

public unsafe void Render()
{
Bind();

// Clear the frame
OpenGL.Clear(ClearBufferMask.ColorBufferBit);

// Get the indices count
uint indicesCount = 0;
foreach (var shape in Shapes.Values)
indicesCount += (uint)shape.Indices.Count;

// Draw the vertex array
OpenGL.DrawElements(PrimitiveType.Triangles, indicesCount, DrawElementsType.UnsignedInt, null);

Unbind();
}

Код: Выделить всё

Section of Application.class

public Application()
{
// Create the window options with default size and title
WindowOptions options = WindowOptions.Default with
{
Size = new(800, 600),
Title = "Notator"
};

// Create the window
_window = Window.Create(options);

// Attach methods to the window events
_window.Load += OnLoad;
_window.Update += OnUpdate;
_window.Render += OnRender;
_window.Closing += OnClosing;

// Tell the window to run
_window.Run();
}

private unsafe void OnLoad()
{
// Create the renderer
_renderer = new Renderer(_window, "Basic.shader");

// Bind the textures
_renderer.BindTexture("silk.png",  0);
_renderer.BindTexture("silk2.png", 1);

// Add layers
_layers.Add("Taskbar", new Layer(_renderer));
}

private void OnUpdate(double deltaTime)
{
foreach (Layer layer in _layers.Values)
{
layer.Update();
}

// update the renderer
_renderer.Update();
}

private unsafe void OnRender(double deltaTime)
{
// Tell the renderer to render
_renderer.Render();
}

private void OnClosing()
{
// Remove the renderer from OpenGL
_renderer.Delete();
}
Я могу только догадываться, что это как-то связано с адресами данных или с чем-то, что не отображается в отладчике Visual Studio, но мне явно не хватает правильного понимания OpenGL, потому что я действительно понятия не имею, как устранить и исправить эту неполадку.
Отредактировано, чтобы прояснить разницу между полем компонента и переменной локального компонента.

Подробнее здесь: https://stackoverflow.com/questions/787 ... and-opengl

Вернуться в «C#»