0
\$\begingroup\$

I am trying to make a 2D renderer using OpenTK and I am having trouble with the coordinate system. I have followed an opengl sprite tutorial and the Size vector is defined by pixels, however the Position is behaving really weird, like it is already off the screen when Y = 5f

Here is my vertex shader

#version 330 core

layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec2 aTexCoord;

out vec2 TexCoord;

uniform mat4 model;
uniform mat4 projection;

void main() {
    gl_Position = projection * model * vec4(aPosition, 0.0, 1.0);
    TexCoord = aTexCoord;
}

Here is my projection matrix (calculated in GameWindow.OnResize)

protected override void OnResize(ResizeEventArgs e) {
    base.OnResize(e);
        
    GL.Viewport(0, 0, e.Width, e.Height);
    ProjectionMatrix = Matrix4.CreateOrthographicOffCenter(0, e.Width, e.Height, 0, -1.0f, 1.0f);
}

And here is my model matrix calculation:

var model = Matrix4.CreateTranslation(Position.X, Position.Y, 0f) *
                Matrix4.CreateTranslation(0.5f * Size.X, 0.5f * Size.Y, 0f) *
                Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) *
                Matrix4.CreateTranslation(-0.5f * Size.X, -0.5f * Size.Y, 0f) *
                Matrix4.CreateScale(Size.X, Size.Y, 1f);

shader.SetUniform("model", model);
shader.SetUniform("projection", Program.Window.ProjectionMatrix);

And finally, the SetUniform method on the Shader class

public void SetUniform(string name, Matrix4 data) {
    Bind();
    GL.UniformMatrix4(GL.GetUniformLocation(ID, name), false, ref data);
    Unbind();
}
\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.