的OpenGL / OpenTK绘图与指数:尝试读取或写入受保护的内存问题

尤安Hollidge:

我试图显示四(或两个三角形)与C#和OpenGL。下面是我为我的网类代码。在它,你必须兼具创造驻维也纳各组织和维罗的,以及我用来呈现网格的功能。

using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Graphics.OpenGL;
using BuildMe.Core;
using OpenTK;

namespace BuildMe.Render
{
    class Mesh
    {

        private Vector3[] verts;
        private uint[] indices;
        private int instances;
        private int VAO;

        public uint[] Indices { get => indices; set => indices = value; }
        public int Instances { get => instances; set => instances = value; }
        public Vector3[] Verts { get => verts; set => verts = value; }

        public Mesh(Vector3[] verts, uint[] indices, int instances)
        {
            this.verts = verts;
            this.indices = indices;
            this.instances = instances;
            this.VAO = CreateVAO();
            StoreVAOData();
        }

        private int CreateVAO()
        {
            int VAO = GL.GenVertexArray();
            RenderLoop.VAOs.Add(VAO);
            return (VAO);
        }

        private void StoreVAOData()
        {
            GL.BindVertexArray(VAO);

            LoadVerts();
            LoadIndices();

            GL.BindVertexArray(0);
        }

        private void LoadVerts()
        {
            int vbo = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ArrayBuffer, Vector3.SizeInBytes * verts.Length, verts, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            RenderLoop.VBOs.Add(vbo);
        }

        private void LoadIndices()
        {
            int vbo = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(int) * indices.Length, indices, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(0, 1, VertexAttribPointerType.UnsignedInt, false, sizeof(int), 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            RenderLoop.VBOs.Add(vbo);
        }

        public int GetVAO()
        {
            return (VAO);
        }

        public void Render()
        {
            GL.BindVertexArray(this.GetVAO());
            GL.EnableVertexAttribArray(0);
            GL.EnableVertexAttribArray(1);
            GL.DrawElements(PrimitiveType.Triangles, this.Indices.Length, DrawElementsType.UnsignedInt, 0);
            GL.DisableVertexAttribArray(0);
            GL.DisableVertexAttribArray(1);
            GL.BindVertexArray(0);
        }

    }
}

这里就是我所说的函数模型渲染。

        private void Render(object sender, FrameEventArgs e)
        {
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.IndexArray);
            GL.Color3(Color.Green);
            foreach (Mesh mesh in SceneMeshes)
                mesh.Render();
        }

我得到的错误是,如果有帮助。但我认为这只是意味着我要么宣布维也纳各组织的错误或我使用了错误的功能来呈现它。

An unhandled exception of type 'System.AccessViolationException' occurred in OpenTK.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

提前致谢。

Rabbid76:

EnableClientStateVertexAttribPointer没有互动起来。如果你要使用的客户端功能,那么你必须使用VertexPointer(见glVertexPointer):

GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);

GL.VertexPointer(3, VertexAttribPointerType.Float, Vector3.SizeInBytes, 0);

GL.EnableClientState(ArrayCap.IndexArray);不会做你期望的。ArrayCap.IndexArray是为颜色索引属性。
索引缓冲器结合到靶BufferTarget.ElementArrayBuffer它不是一个属性,也没有启用。
进一步更索引缓冲器是由顶点数组对象说明。该指令GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);将打破绑定:

private void LoadIndices()
{
    int ibo = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);
    GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(int) * indices.Length, indices, BufferUsageHint.StaticDraw);
    // GL.VertexAttribPointer(...) <--- REMOVE
    // GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); // <--- REMOVE
    RenderLoop.VBOs.Add(ibo);
}

该客户端能力的陈述顶点数组对象因此,它可以在规定LoadVerts()

private void LoadVerts()
{
    int vbo = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
    GL.BufferData(BufferTarget.ArrayBuffer, Vector3.SizeInBytes * verts.Length, verts, BufferUsageHint.StaticDraw);
    // GL.VertexAttribPointer(...); <---- REMOVE
    GL.VertexPointer(3, VertexAttribPointerType.Float, Vector3.SizeInBytes, 0); // <--- ADD
    GL.EnableClientState(ArrayCap.VertexArray); // <--- ADD
    GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
    RenderLoop.VBOs.Add(vbo);
}

所有必要的规范的顶点数组对象陈述,所以它足以结合的VAO,绘图调用之前:

public void Render()
{
    GL.BindVertexArray(this.GetVAO());
    GL.DrawElements(PrimitiveType.Triangles, this.Indices.Length, DrawElementsType.UnsignedInt, 0);
    GL.BindVertexArray(0);
}
private void Render(object sender, FrameEventArgs e)
{
    GL.Color3(Color.Green);
    foreach (Mesh mesh in SceneMeshes)
        mesh.Render();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试读取或写入受保护的内存

OpenTK:尝试读取或写入受保护的内存。这通常表明其他内存已损坏

P调用“尝试读取或写入受保护的内存”

SQL-尝试读取或写入受保护的内存

尝试读取或写入受保护的内存。这通常表明其他内存已损坏

尝试读取或写入受保护的内存:C ++的内存修改值

尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

并行读取AutoCad数据库时出现“尝试读取或写入受保护的内存”错误

用于写入和读取图像OpenGL的内存屏障问题

尝试读取或写入受保护的内存的平台调用错误

使用线程池限制最大线程数-尝试读取或写入受保护的内存错误

通过DllImport在C#中调用C方法-尝试读取或写入受保护的内存

尝试读取或写入受保护的内存.....在Silverlight Windows Phone 8.1项目中

urlmon.dll引起的“尝试读取或写入受保护的内存”

打开SQL Server连接的原因:System.AccessViolationException尝试读取或写入受保护的内存

VB.NET访问-尝试读取或写入受保护的内存

添加单例Kafka生产者:尝试读取或写入受保护的内存

未处理的异常:System.AccessViolationException:尝试读取或写入受保护的内存

使用反射调用dll函数时尝试读取或写入受保护的内存

c# emgu/opencv 用法抛出异常 - 尝试读取或写入受保护的内存

附加信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏

PInvoke ReadFile kernel32:尝试读取或写入受保护的内存。这通常表明其他内存已损坏

启动MvvmCross Uwp应用程序时发生异常:“ System.AccessViolationException:尝试读取或写入受保护的内存”

尝试读取或写入受保护的内存。这通常表明其他内存已损坏,同时使用 C# 读取 CSV 文件

OpenGL中的缓冲绘图

多对象绘图(OpenGL)

使用glutDisplayFunc(opengl)绘图

尝试使用GetClassName读取或写入受保护的内存

OpenGL Cubemap:写入mipmap