Advertisement

Rotating using GLM

Started by November 05, 2020 04:30 PM
3 comments, last by taby 4 years, 2 months ago

I'm trying to rotate a vec4, but nothing's working out. It doesn't rotate the object properly. I wonder, is there a function in GLM that converts a unit vector to yaw and pitch?

The full code is at: https://github.com/sjhalayka/sphere

Can you spot a problem with my code? Thanks for your time.

vec3 dir(0, -1, 0);

float yaw = atan2f(dir.x, dir.z);
float pitch = -atan2f(dir.y, sqrt(dir.x * dir.x + dir.z * dir.z));

game_piece_mesh.rotate_and_translate_mesh(yaw, pitch, dir);
...
...
void mesh::rotate_and_translate_mesh(float yaw, float pitch, vec3 translate_vec)
{
	for (size_t i = 0; i < triangles.size(); i++)
	{
		vec4 v0;
		v0.x = triangles[i].vertex[0].x;
		v0.y = triangles[i].vertex[0].y;
		v0.z = triangles[i].vertex[0].z;
		v0.w = 1;

		vec4 v1;
		v1.x = triangles[i].vertex[1].x;
		v1.y = triangles[i].vertex[1].y;
		v1.z = triangles[i].vertex[1].z;
		v1.w = 1;

		vec4 v2;
		v2.x = triangles[i].vertex[2].x;
		v2.y = triangles[i].vertex[2].y;
		v2.z = triangles[i].vertex[2].z;
		v2.w = 1;

		static const mat4 identity_mat = glm::mat4(1.0f);
		static const float pi = 4.0f * atanf(1.0f);

		cout << yaw << ' ' << pitch << endl;

		mat4 rot0_mat = rotate(identity_mat, pitch, glm::vec3(1.0, 0.0, 0.0));
		mat4 rot1_mat = rotate(identity_mat, yaw, glm::vec3(0.0, 1.0, 0.0));
		mat4 translate_mat = translate(identity_mat, translate_vec);

		mat4 transform = translate_mat * rot1_mat * rot0_mat;

		v0 = transform * v0;
		v1 = transform * v1;
		v2 = transform * v2;

		triangles[i].vertex[0].x = v0.x;
		triangles[i].vertex[0].y = v0.y;
		triangles[i].vertex[0].z = v0.z;

		triangles[i].vertex[1].x = v1.x;
		triangles[i].vertex[1].y = v1.y;
		triangles[i].vertex[1].z = v1.z;

		triangles[i].vertex[2].x = v2.x;
		triangles[i].vertex[2].y = v2.y;
		triangles[i].vertex[2].z = v2.z;

	}

	get_vertices_and_normals_from_triangles();

	init_opengl_data();
}
is this what u want ?

float yaw = atan2f(dir.x, dir.z); // this is 0.0

also pi is not used, just in case u haven't noticed

btw, u can use the notation glm::pi<double>( ) , for example like this:

...
double my_roll = glm::pi<double>() * 0.005; 
...
 
Advertisement

I'm not sure why it's not working. :(

It works fine in https://github.com/sjhalayka/bezier_fractal​​ – with the exception that it's OpenGL 1.x code.

 

Aha. I apologize for wasting your time! I figured it out… my model wasn't aligned along the z axis in the modeling program. When I aligned the cone to the z axis, it all starts to work properly. I figured this out because of the web page for gluCylinder, which is a cylinder aligned to the z axis.

This topic is closed to new replies.

Advertisement