поэтому после реализации камеры / вида на моей сцене объекты, которые я рисовал, исчезли.перед добавлением камеры моя сцена нарисовала бы 2 квадрата.после некоторого тестирования я считаю, что причиной проблемы является uView
в gl_Position = vec4(vPosition, 1) * uModel * uView;
в файле шейдера.это то, что вызывает проблему, или это что-то еще?
основной файл:
using OpenTK;
using System;
using OpenTK.Graphics;
using Labs.Utility;
using OpenTK.Graphics.OpenGL;
namespace Labs.Lab2
{
public class Lab2_2Window : GameWindow
{
public Lab2_2Window()
: base(
800, // Width
600, // Height
GraphicsMode.Default,
"Lab 2_2 Understanding the Camera",
GameWindowFlags.Default,
DisplayDevice.Default,
3, // major
3, // minor
GraphicsContextFlags.ForwardCompatible
)
{
}
private int[] mVBO_IDs = new int[2];
private int mVAO_ID;
private ShaderUtility mShader;
private ModelUtility mModel;
private Matrix4 mView;
protected override void OnLoad(EventArgs e)
{
// Set some GL state
GL.ClearColor(Color4.DodgerBlue);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.CullFace);
mModel = ModelUtility.LoadModel(@"Utility/Models/lab22model.sjg");
mShader = new ShaderUtility(@"Lab2/Shaders/vLab22.vert", @"Lab2/Shaders/fSimple.frag");
GL.UseProgram(mShader.ShaderProgramID);
int vPositionLocation = GL.GetAttribLocation(mShader.ShaderProgramID, "vPosition");
int vColourLocation = GL.GetAttribLocation(mShader.ShaderProgramID, "vColour");
mVAO_ID = GL.GenVertexArray();
GL.GenBuffers(mVBO_IDs.Length, mVBO_IDs);
GL.BindVertexArray(mVAO_ID);
GL.BindBuffer(BufferTarget.ArrayBuffer, mVBO_IDs[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(mModel.Vertices.Length * sizeof(float)), mModel.Vertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, mVBO_IDs[1]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(mModel.Indices.Length * sizeof(float)), mModel.Indices, BufferUsageHint.StaticDraw);
int size;
GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
if (mModel.Vertices.Length * sizeof(float) != size)
{
throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
}
GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
if (mModel.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(vColourLocation);
GL.VertexAttribPointer(vColourLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 3 * sizeof(float));
mView = Matrix4.Identity;
Vector3 eye = new Vector3(1.2f, 2.0f, -5.0f);
Vector3 lookAt = new Vector3(0, 0, 0);
Vector3 up = new Vector3(0, 1, 0);
mView = Matrix4.LookAt(eye, lookAt, up);
int uView = GL.GetUniformLocation(mShader.ShaderProgramID, "uView");
GL.UniformMatrix4(uView, true, ref mView);
GL.BindVertexArray(0);
base.OnLoad(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
int uModelLocation = GL.GetUniformLocation(mShader.ShaderProgramID, "uModel"); //moved square to right of screen
Matrix4 m1 = Matrix4.CreateTranslation(0.5f, 0, 0); //moved square to right of screen
Matrix4 r1 = Matrix4.CreateRotationZ(0);
Matrix4 final1 = m1 * r1; //combines translation and rotation - order matters
GL.UniformMatrix4(uModelLocation, true, ref final1);
GL.BindVertexArray(mVAO_ID);
GL.DrawElements(BeginMode.Triangles, mModel.Indices.Length, DrawElementsType.UnsignedInt, 0);
Matrix4 r2 = Matrix4.CreateRotationZ(0);
Matrix4 m2 = Matrix4.CreateTranslation(-0.5f, 0, 0); //draws second square on the left
Matrix4 final2 = r2 * m2; //combines translation and rotation - order matters
GL.UniformMatrix4(uModelLocation, true, ref final2);
GL.BindVertexArray(mVAO_ID);
GL.DrawElements(BeginMode.Triangles, mModel.Indices.Length, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
this.SwapBuffers();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (e.KeyChar == 'a')
{
mView = mView * Matrix4.CreateTranslation(1, 0, 0);
int uView = GL.GetUniformLocation(mShader.ShaderProgramID, "uView");
GL.UniformMatrix4(uView, true, ref mView);
}
if (e.KeyChar == 'd')
{
mView = mView * Matrix4.CreateTranslation(-1, 0, 0);
int uView = GL.GetUniformLocation(mShader.ShaderProgramID, "uView");
GL.UniformMatrix4(uView, true, ref mView);
}
}
protected override void OnUnload(EventArgs e)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
GL.BindVertexArray(0);
GL.DeleteBuffers(mVBO_IDs.Length, mVBO_IDs);
GL.DeleteVertexArray(mVAO_ID);
mShader.Delete();
base.OnUnload(e);
}
}
}
файл шейдера:
#version 330
uniform mat4 uModel;
uniform mat4 uView;
in vec3 vPosition;
in vec3 vColour;
out vec4 oColour;
void main()
{
gl_Position = vec4(vPosition, 1) * uModel * uView;
oColour = vec4(vColour, 1);
}