Dav1dde / gl3n

OpenGL Maths for D (not glm for D).
http://dav1dde.github.com/gl3n/
Other
103 stars 49 forks source link

Matrix Translate resulting in different values from glm #81

Closed Payotz closed 7 years ago

Payotz commented 7 years ago

So basically, I'm trying to replicate what I've written in C++ into Dlang.

I have this as the Dlang code.

vec2 pos;
vec2 size;
float rotate;

pos.x = pos.y = 0;
size.x = size.y = 500;
rotate = 0;

mat4 model = mat4.identity;
writeln("Initial :");
writeln(model);
SDL_Delay(2000);

model = model.translate(vec3(pos,0.0f));
writeln("First :");
writeln(model);
SDL_Delay(2000);

model = model.rotation(rotate,vec3(pos,1.0f));
writeln("Second :");
writeln(model);
SDL_Delay(2000);

model = model.translate(vec3(-0.5f * size.x, -0.5f * size.y, 0.0f));
writeln("Third :");
writeln(model);
SDL_Delay(2000);

model = model.scale(size.x,size.y,1.0f);
writeln("Fourth :");
writeln(model);
SDL_Delay(2000);

And this as the C++ code:

    glm::vec2 pos;
    glm::vec2 size;
    GLfloat rotate;

    pos.x = pos.y = 0;
    size.x = size.y = 500;
    rotate = 0;

    glm::mat4 model;
    std::cout << "Initial : \n" << glm::to_string(model) << std::endl;
    SDL_Delay(2000);

    model = glm::translate(model,glm::vec3(pos, 0.0f));
    std::cout << "First : \n" << glm::to_string(model) << std::endl;
    SDL_Delay(2000);

    model = glm::rotate(model,rotate,glm::vec3(pos,1.0f));
    std::cout << "Second : \n" << glm::to_string(model) << std::endl;
    SDL_Delay(2000);

    model = glm::translate(model,glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f));
    std::cout << "Third :\n" << glm::to_string(model) << std::endl;
    SDL_Delay(2000);

    model = glm::scale(model,glm::vec3(size,1.0f));
    std::cout << "Fourth :\n" << glm::to_string(model) << std::endl;
    SDL_Delay(2000);

And the results are different:

Dlang code:

image

C++ code:

image

At first, the values are basically the same, then it starts to differ at the third output.

Dlang code: model = model.translate(vec3(-0.5f * size.x, -0.5f * size.y, 0.0f))

C++ code: model = glm::translate(model,glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f))

I'm not sure if this is a bug or not, I just don't know where to post it. I am probably doing something wrong here, but I'm still trying to wrap my head around matrices in general. If this is not a bug, then I'm very sorry for wasting your time.

Dav1dde commented 7 years ago

Hey, it has been a while and I've never really used glm. GLM prints out the values (and stores them) Column Major where as gl3n does it Row Major, that explains the differences in 3).

Regarding 4) Gl3n scales by multiplying the matrix (A) with a scaling matrix (B) on the left side B * A, where as it seems glm does a A * B, scaling matrix on the right side. gl3n does it that way, because it allows you to chain operations more naturally, since the operations happen in the order you chain them (first rotate, then apply the scale to this result).

Sorry that it took so long to answer, at first glance it looked really complicated so I postponed for a bit.

Payotz commented 7 years ago

Sorry for the late reply. I was out of town. Thank you. I understand what's the issue now.