So I'm trying to figure out the model, view and projection matrices.
I can, with some effort, find my drawings (3x3x3 structure of cubes) in 3D space and it looks like:
The problem is, the cubes seem to be offset from the center (where the lines meet at 0, 0, 0
), which is where around they're initially positioned, and don't really seem to be sticking to it when moving camera. All around while moving camera the cube movement is off, seems off and inverted.
The green area is the far end of the view frustum, and the near end is where the black lines meet.
So far, from what I've gathered, I'm passing to the shaders:
// The model matrix
model.position = { x * scale, y * scale, z * scale }; // 3 x 3 x 3, scale = 50.0
drx::util::MAT4F mModel;
drx::util::MAT4F mSize;
mModel.LoadIdentity();
mModel.Translate(model.position.x, model.position.y, model.position.z);
mSize.Scale(scale, scale, scale);
mModel = mModel.Add(mSize);
// from the MAT4F structure
void Translate(float x, float y, float z) {
this->matrix[3][0] = x;
this->matrix[3][1] = y;
this->matrix[3][2] = z;
}
void Scale(float x, float y, float z) {
this->matrix[0][0] = x;
this->matrix[1][1] = y;
this->matrix[2][2] = z;
}
// The view matrix
m.LoadIdentity();
m.matrix[0][0] = this->right.x;
m.matrix[1][0] = this->right.y;
m.matrix[2][0] = this->right.z;
m.matrix[0][1] = this->up.x;
m.matrix[1][1] = this->up.y;
m.matrix[2][1] = this->up.z;
m.matrix[0][2] = this->front.x; //
m.matrix[1][2] = this->front.y; // Direction vector
m.matrix[2][2] = this->front.z; //
m.matrix[3][0] = this->position.x; //
m.matrix[3][1] = this->position.y; // Eye or camera position vector
m.matrix[3][2] = this->position.z; //
// The projection matrix
float sf = tanf(fov / 2.0f);
this->matrix[0][0] = 1.0f / (ar * sf);
this->matrix[1][1] = 1.0f / sf;
this->matrix[2][2] = -((f + n) / (f - n));
this->matrix[2][3] = -1.0f;
this->matrix[3][2] = -((2.0f * f * n) / (f - n));
this->matrix[3][3] = 1.0f;
And on vertex shader, I have:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec2 texCoord;
out vec4 ourColor;
out vec2 texCoords;
uniform vec4 myColor;
uniform float scale;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 model;
uniform vec3 myOtherColor;
void main()
{
vec4 myPos = vec4(aPos, 1.0);
gl_Position = projection * view * model * myPos;
ourColor = vec4(inColor, 1.0);
texCoords = texCoord;
}