C# OpenGL模型覆盖了以前的模型
我一直在试图找出我的代码中的这个错误已经有一段时间了,现在我正在寻求stackoverflow的帮助!无论如何,我已经创建了一个类,用于在openGL中创建模型。它处理给定模型的VBO、VAO和EBO,但当我创建两个并显示它们时,第二个总是覆盖前一个,我不知道为什么,我把它展示给我的一个朋友,我们一起什么也没发现。这是模型类:C# OpenGL模型覆盖了以前的模型,c#,opengl,opentk,C#,Opengl,Opentk,我一直在试图找出我的代码中的这个错误已经有一段时间了,现在我正在寻求stackoverflow的帮助!无论如何,我已经创建了一个类,用于在openGL中创建模型。它处理给定模型的VBO、VAO和EBO,但当我创建两个并显示它们时,第二个总是覆盖前一个,我不知道为什么,我把它展示给我的一个朋友,我们一起什么也没发现。这是模型类: using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using System; na
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using System;
namespace YISOPENGLEVIL.Components.Objects
{
struct Vertex
{
Vector3 Position;
Vector3 Color;
public Vertex(Vector3 position, Vector3 color)
{
Position = position;
Color = color;
}
}
class Model
{
public int VBO, VAO, EBO;
public Vertex[] Data;
public int[] Indices;
public Model() { }
public Model(Vertex[] data, int[] indices)
{
Data = data;
Indices = indices;
VAO = GL.GenVertexArray();
VBO = GL.GenBuffer();
EBO = GL.GenBuffer();
GL.BindVertexArray(VAO);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
GL.BufferData(BufferTarget.ArrayBuffer, Data.Length * 24, Data, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 24, 0);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 24, 12);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, EBO);
GL.BufferData(BufferTarget.ElementArrayBuffer, Indices.Length * 4, Indices, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(0);
GL.EnableVertexAttribArray(1);
}
protected static Vector3 toVec3(Color4 c)
{
return new Vector3(c.R, c.G, c.B);
}
public void Render()
{
//GL.DrawArrays(PrimitiveType.Triangles, 0, Data.Length);
GL.DrawElements(BeginMode.Triangles, Indices.Length, DrawElementsType.UnsignedInt, 0);
}
}
}
我的元素类,通过一个模型,然后用来显示它
using OpenTK;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
namespace YISOPENGLEVIL.Components.Objects
{
class Element
{
private int transform;
private int s;
public Vector3 Position;
public Vector3 Rotation;
private Matrix4 View;
private Model Model;
public Element(Model m, int Shader)
{
s = Shader;
Model = m;
Position = new Vector3(0, 0, 0);
Rotation = new Vector3(0, 0, 0);
}
public void Render()
{
var t = Matrix4.CreateTranslation(Position.X, Position.Y, Position.Z);
var rx = Matrix4.CreateRotationX(Rotation.X);
var ry = Matrix4.CreateRotationY(Rotation.Y);
var rz = Matrix4.CreateRotationZ(Rotation.Z);
View = t * rx * ry * rz;
transform = GL.GetUniformLocation(s, "transform");
GL.UniformMatrix4(transform, false, ref View);
Model.Render();
}
public static void RenderAll()
{
}
}
}
这就是实现:
m1 = new Cube(1.5f, Color4.Aqua);
m2 = new Cube(1f, Color4.Red);
e1 = new Element(m1, s.Program);
e2 = new Element(m2, s.Program);
e1.Position = new Vector3(0, 0, 0);
e2.Position = new Vector3(0, 0, 1);
以及:
cube类扩展了模型,并通过创建顶点数组的私有静态方法将数据传递到模型构造函数中。真正奇怪的是,如果我放入print语句,它表明两个元素都定义得很好,并且都有正确的数据,最后一个总是覆盖前一个(有两个对象在正确的位置,但都显示红色的小立方体)。我不明白。。。有什么想法吗
程序的屏幕截图顺便说一句,OpenGL并不是邪恶的。尝试在Direct3DS中打开一个窗口我知道,但这是我遇到的第一百万个问题,所以我喜欢这个问题name@QuantumAbyss我现在遇到同样的问题,使用C OpenGL API,你解决了吗?
e1.Render();
e2.Render();