Dies ist das erste Mal, dass ich mit OpenGL/OpenTK gearbeitet habe und beim Versuch, zwei Objekte zu rendern, wird einer von ihnen nicht richtig dargestellt. Ich habe mit dem Code experimentiert und kann einfach nicht herausfinden, was vor sich geht. Ich stehe auf der Idee, dass ich die mVAO/mVBO-Arrays falsch verwende.OpenGL/OpenTK - Mehrere Objekte nicht wie erwartet gerendert
The object on top of the cylinder is the one that isn't rendering properly
and this is how that object should look
das zweite Objekt ist nur ein Zylinder für sie zu stehen, die nicht wie die erste verzerrt zu werden scheint.
Dies ist mein Code:
private int[] mVBO_IDs = new int[4];
private int[] mVAO_IDs = new int[3];
private ShaderUtility mShader;
private ModelUtility mSphereModelUtility;
private ModelUtility mCylinderModelUtility;
private Matrix4 mView, mSphereModel, mGroundModel, mCylinderModel;
protected override void OnLoad(EventArgs e)
{
// Set some GL state
GL.ClearColor(Color4.LightBlue);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.CullFace);
mShader = new ShaderUtility(@"Lab3/Shaders/vPassThrough.vert", @"Lab3/Shaders/fLighting.frag");
GL.UseProgram(mShader.ShaderProgramID);
int vPositionLocation = GL.GetAttribLocation(mShader.ShaderProgramID, "vPosition");
int vNormalLocation = GL.GetAttribLocation(mShader.ShaderProgramID, "vNormal");
GL.GenVertexArrays(mVAO_IDs.Length, mVAO_IDs);
GL.GenBuffers(mVBO_IDs.Length, mVBO_IDs);
float[] squareVertices = new float[] {-10, 0, -10,0,1,0,
-10, 0, 10,0,1,0,
10, 0, 10,0,1,0,
10, 0, -10,0,1,0,};
/// Floor
GL.BindVertexArray(mVAO_IDs[0]);
GL.BindBuffer(BufferTarget.ArrayBuffer, mVBO_IDs[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(squareVertices.Length * sizeof(float)), squareVertices, BufferUsageHint.StaticDraw);
int size;
GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
if (squareVertices.Length * sizeof(float) != size)
{
throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
}
GL.EnableVertexAttribArray(vPositionLocation);
GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);
GL.EnableVertexAttribArray(vNormalLocation);
GL.VertexAttribPointer(vNormalLocation, 3, VertexAttribPointerType.Float, true, 6 * sizeof(float), 3);
/// Model 1
mSphereModelUtility = ModelUtility.LoadModel(@"Utility/Models/model.bin");
GL.BindVertexArray(mVAO_IDs[1]);
GL.BindBuffer(BufferTarget.ArrayBuffer, mVBO_IDs[1]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(mSphereModelUtility.squareVertices.Length * sizeof(float)), mSphereModelUtility.squareVertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, mVBO_IDs[2]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(mSphereModelUtility.Indices.Length * sizeof(float)), mSphereModelUtility.Indices, BufferUsageHint.StaticDraw);
GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
if (mSphereModelUtility.squareVertices.Length * sizeof(float) != size)
{
throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
}
GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
if (mSphereModelUtility.Indices.Length * sizeof(float) != size)
{
throw new ApplicationException("Index data not loaded onto graphics card correctly");
}
GL.EnableVertexAttribArray(vPositionLocation);
GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);
GL.EnableVertexAttribArray(vNormalLocation);
GL.VertexAttribPointer(vNormalLocation, 3, VertexAttribPointerType.Float, true, 6 * sizeof(float), 0);
GL.BindVertexArray(0);
/// Model 2
mCylinderModelUtility = ModelUtility.LoadModel(@"Utility/Models/cylinder.bin");
GL.BindVertexArray(mVAO_IDs[2]);
GL.BindBuffer(BufferTarget.ArrayBuffer, mVBO_IDs[2]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(mCylinderModelUtility.squareVertices.Length * sizeof(float)), mCylinderModelUtility.squareVertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, mVBO_IDs[3]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(mCylinderModelUtility.Indices.Length * sizeof(float)), mCylinderModelUtility.Indices, BufferUsageHint.StaticDraw);
GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
if (mCylinderModelUtility.squareVertices.Length * sizeof(float) != size)
{
throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
}
GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
if (mCylinderModelUtility.Indices.Length * sizeof(float) != size)
{
throw new ApplicationException("Index data not loaded onto graphics card correctly");
}
GL.EnableVertexAttribArray(vPositionLocation);
GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);
GL.EnableVertexAttribArray(vNormalLocation);
GL.VertexAttribPointer(vNormalLocation, 3, VertexAttribPointerType.Float, true, 6 * sizeof(float), 0);
GL.BindVertexArray(0);
/// Positions
mView = Matrix4.CreateTranslation(0, -1.5f, 0);
int uView = GL.GetUniformLocation(mShader.ShaderProgramID, "uView");
GL.UniformMatrix4(uView, true, ref mView);
mGroundModel = Matrix4.CreateTranslation(0, 0, -5f);
mSphereModel = Matrix4.CreateTranslation(0, 3f, -5f);
mCylinderModel = Matrix4.CreateTranslation(0, 1f, -5f);
Vector3 t = mSphereModel.ExtractTranslation();
Matrix4 translation = Matrix4.CreateTranslation(t);
Matrix4 inverseTranslation = Matrix4.CreateTranslation(-t);
mSphereModel = mSphereModel * inverseTranslation * Matrix4.CreateRotationY((float)(Math.PI * -90/180.0)) *
translation;
t = mCylinderModel.ExtractTranslation();
translation = Matrix4.CreateTranslation(t);
inverseTranslation = Matrix4.CreateTranslation(-t);
mCylinderModel = mCylinderModel * inverseTranslation * Matrix4.CreateRotationY((float)(Math.PI * -90/180.0)) *
translation;
/// Lighting
int uLightPositionLocation = GL.GetUniformLocation(mShader.ShaderProgramID, "uLightPosition");
Vector4 lightPosition = Vector4.Transform(new Vector4(3, 5, -5f, 1), mView);
GL.Uniform4(uLightPositionLocation, lightPosition);
base.OnLoad(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
int uModel = GL.GetUniformLocation(mShader.ShaderProgramID, "uModel");
GL.UniformMatrix4(uModel, true, ref mGroundModel);
GL.BindVertexArray(mVAO_IDs[0]);
GL.DrawArrays(PrimitiveType.TriangleFan, 0, 4);
Matrix4 m = mSphereModel * mGroundModel;
uModel = GL.GetUniformLocation(mShader.ShaderProgramID, "uModel");
GL.UniformMatrix4(uModel, true, ref m);
GL.BindVertexArray(mVAO_IDs[1]);
GL.DrawElements(PrimitiveType.Triangles, mSphereModelUtility.Indices.Length, DrawElementsType.UnsignedInt, 0);
m = mCylinderModel * mGroundModel;
uModel = GL.GetUniformLocation(mShader.ShaderProgramID, "uModel");
GL.UniformMatrix4(uModel, true, ref m);
GL.BindVertexArray(mVAO_IDs[2]);
GL.DrawElements(PrimitiveType.Triangles, mCylinderModelUtility.Indices.Length, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
this.SwapBuffers();
}