LastHorizon

08-18-2016, 05:40 AM

Hi there,

I am having a bit of a perplexing problem with regards to translating an object so that I may rotate it about its geometric centre. I understand all of the theory (at least I believe I do) and have implemented this algorithm not using OpenGL, however when I come to write it in GLSL I am experiencing some odd behaviour.

#version 450 core

layout(location = 0) in vec3 in_position;

layout(location = 1) in vec4 in_color;

uniform mat4 projection_matrix, view_matrix;

uniform vec3 rotation;

out vec4 color;

void main()

{

color = in_color;

mat4 rotate_x, rotate_y, rotate_z;

mat4 translate1, translate2;

mat4 scale_double, scale_half;

translate1 = mat4(1.0, 0.0, 0.0, -0.5,

0.0, 1.0, 0.0, -0.25,

0.0, 0.0, 1.0, 0.0,

0.0, 0.0, 0.0, 1.0);

translate2 = mat4(1.0, 0.0, 0.0, 0.5,

0.0, 1.0, 0.0, 0.25,

0.0, 0.0, 1.0, 0.0,

0.0, 0.0, 0.0, 1.0);

scale_double = mat4(2.0, 0.0, 0.0, 0.0,

0.0, 2.0, 0.0, 0.0,

0.0, 0.0, 2.0, 0.0,

0.0, 0.0, 0.0, 1.0);

scale_half = mat4(0.5, 0.0, 0.0, 0.0,

0.0, 0.5, 0.0, 0.0,

0.0, 0.0, 0.5, 0.0,

0.0, 0.0, 0.0, 1.0);

// rotate_x = mat4(1.0, 0.0, 0.0, 0.0,

// 0.0, cos(rotation.x), sin(rotation.x), 0.0,

// 0.0, -sin(rotation.x), cos(rotation.x), 0.0,

// 0.0, 0.0, 0.0, 1.0);

// rotate_y = mat4(cos(rotation.y), 0.0, -sin(rotation.y), 0.0,

// 0.0, 1.0, 0.0, 0.0,

// sin(rotation.y), 0.0, cos(rotation.y), 0.0,

// 0.0, 0.0, 0.0, 1.0);

rotate_z = mat4(cos(rotation.z), -sin(rotation.z), 0.0, 0.0,

sin(rotation.z), cos(rotation.z), 0.0, 0.0,

0.0, 0.0, 1.0, 0.0,

0.0, 0.0, 0.0, 1.0);

gl_Position = translate2 * rotate_z * translate1 * vec4(in_position, 1);

}

Above is my shader code. The object to be rotated is a simple triangle made from the vertices (0, 0, 0) (0.5, 0, 0) and (0.5, 0.5, 0) which makes the centroid (0.5, 0.25, 0). As can be seen I attempt to translate the triangle before rotating it and translating it back (since the resultant of my matrix multiplication describes this action).

However when I run the code I get the point in my triangle at the origin resolutely stuck at the origin and the triangle rotates around that instead.

If anyone has any hints & tips or ideas it would be most appreciated.

Cheers

I am having a bit of a perplexing problem with regards to translating an object so that I may rotate it about its geometric centre. I understand all of the theory (at least I believe I do) and have implemented this algorithm not using OpenGL, however when I come to write it in GLSL I am experiencing some odd behaviour.

#version 450 core

layout(location = 0) in vec3 in_position;

layout(location = 1) in vec4 in_color;

uniform mat4 projection_matrix, view_matrix;

uniform vec3 rotation;

out vec4 color;

void main()

{

color = in_color;

mat4 rotate_x, rotate_y, rotate_z;

mat4 translate1, translate2;

mat4 scale_double, scale_half;

translate1 = mat4(1.0, 0.0, 0.0, -0.5,

0.0, 1.0, 0.0, -0.25,

0.0, 0.0, 1.0, 0.0,

0.0, 0.0, 0.0, 1.0);

translate2 = mat4(1.0, 0.0, 0.0, 0.5,

0.0, 1.0, 0.0, 0.25,

0.0, 0.0, 1.0, 0.0,

0.0, 0.0, 0.0, 1.0);

scale_double = mat4(2.0, 0.0, 0.0, 0.0,

0.0, 2.0, 0.0, 0.0,

0.0, 0.0, 2.0, 0.0,

0.0, 0.0, 0.0, 1.0);

scale_half = mat4(0.5, 0.0, 0.0, 0.0,

0.0, 0.5, 0.0, 0.0,

0.0, 0.0, 0.5, 0.0,

0.0, 0.0, 0.0, 1.0);

// rotate_x = mat4(1.0, 0.0, 0.0, 0.0,

// 0.0, cos(rotation.x), sin(rotation.x), 0.0,

// 0.0, -sin(rotation.x), cos(rotation.x), 0.0,

// 0.0, 0.0, 0.0, 1.0);

// rotate_y = mat4(cos(rotation.y), 0.0, -sin(rotation.y), 0.0,

// 0.0, 1.0, 0.0, 0.0,

// sin(rotation.y), 0.0, cos(rotation.y), 0.0,

// 0.0, 0.0, 0.0, 1.0);

rotate_z = mat4(cos(rotation.z), -sin(rotation.z), 0.0, 0.0,

sin(rotation.z), cos(rotation.z), 0.0, 0.0,

0.0, 0.0, 1.0, 0.0,

0.0, 0.0, 0.0, 1.0);

gl_Position = translate2 * rotate_z * translate1 * vec4(in_position, 1);

}

Above is my shader code. The object to be rotated is a simple triangle made from the vertices (0, 0, 0) (0.5, 0, 0) and (0.5, 0.5, 0) which makes the centroid (0.5, 0.25, 0). As can be seen I attempt to translate the triangle before rotating it and translating it back (since the resultant of my matrix multiplication describes this action).

However when I run the code I get the point in my triangle at the origin resolutely stuck at the origin and the triangle rotates around that instead.

If anyone has any hints & tips or ideas it would be most appreciated.

Cheers