Search code examples
c#openglglslopentk

OpenGL3 Rotate object around itself center


Ill try to rotate object in openGL (using OpenTK framework), but he rotate around zero point. Its logicly that object rotating around center, but i dont known how shoud hes rotating aroud itself center (or other point)

    public static void Texture(Region region, float x, float y, float z, float rotateX, float rotateY, float rotateZ)
        => Texture(
            region,
            Matrix4.CreateTranslation(x, y, z),
            Matrix4.CreateRotationX(rotateX) * Matrix4.CreateRotationY(rotateY) * Matrix4.CreateRotationZ(rotateZ),
            TestGame.Camera.GetViewMatrix(),
            TestGame.Camera.GetProjectionMatrix()
            );
    public static void Texture(Region region, Matrix4 translate, Matrix4 model, Matrix4 view, Matrix4 projection)
    {
        Shaders.TextureShader.Texture(TextureUnit.Texture0); //Bind texture as 0 in shader
        region.Reference.Use(); //Bind texture as 0

        Shaders.TextureShader.Matrix4("translate", translate);
        Shaders.TextureShader.Matrix4("model", model);
        Shaders.TextureShader.Matrix4("view", view);
        Shaders.TextureShader.Matrix4("projection", projection);
        Shaders.TextureShader.Draw();
    }
#version 330 core

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

out vec2 texCoord;

uniform mat4 translate;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main(void)
{
    texCoord = aTexCoord;

    gl_Position = vec4(aPosition, 1) * translate * model * view * projection;
}

Solution

  • Matrix multiplications are not Commutative, the order mattes. Rotate the object before translating it:

    gl_Position = vec4(aPosition, 1) * translate * model * view * projection;

    gl_Position = vec4(aPosition, 1) * model * translate * view * projection;