Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Hierarchical transformations with GLM

This topic is 2094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, coming from the world of C# - OpenTK, I have started C++ stuff and I find it very interesting.


Now I try to understand more about skeletal animation, having browsed lots of code here and there, I am still unable to understand exactly the concept of Skeletal Animation. I take the long road trying to implement one tutorial of my own from scratch.


Sidenote: I have looked at lots of code but it's very complex to understand. Instead of having a complex solution (say MD5Loader) I want to get some simple stuff going on in 2D. I use exclusively GLFW, GLEW, GLM, and OpenGL3, while others don't so that add more trouble.



This moment what I want to do, is to how get some bones moving around the screen? Specifically, I have looked at the concept of Hierarchical Transformations, but I can't figure out how to do them in GLM.

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>

using namespace std;

int main()
	glm::vec3 positions[3];
	positions[0] = glm::vec3(0, -2, 0);
	positions[1] = glm::vec3(0, 1, 0); // Normally I would set this to 0
	positions[2] = glm::vec3(0, 2, 0);

	float rotations[3];

	glm::mat4 bones[3];

	// Setup window
	int width = 800;
	int height = 600;
	GLFWwindow* window;
	window = glfwCreateWindow(width, height, "GLFW", NULL, NULL);

	// Setup camera stuff
	glm::mat4 matrixProj = glm::perspective(
		45.0f, (float)width/height, 0.1f, 100.0f);

	glm::mat4 matrixView = glm::lookAt(
		glm::vec3(0, 0, 10), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));

	while (!glfwWindowShouldClose(window))

		glLoadMatrixf(glm::value_ptr(matrixProj * matrixView));

		float sinValue = glm::sin(glfwGetTime());
		float cosValue = glm::cos(glfwGetTime());

		// Normal bone positions
//		bones[0] = glm::translate(positions[0]);
//		bones[1] = glm::translate(positions[1]);
//		bones[2] = glm::translate(positions[2]);

		// Hierarchical bone transformations
		// If bones[1] set to zero then it fails to translate, how to avoid this issue?
		bones[0] = glm::translate(positions[0]);
		bones[0] *= glm::translate(glm::vec3(sinValue, 0.0f, 0.0f)); // Add some movement
		bones[1] = bones[0] * glm::translate(positions[1]);
		bones[2] = bones[1] + glm::translate(positions[2]);

		// Hierachical bone transformations with rotations
		// how to perform rotation as well as translation?
//		bones[0] = glm::translate(positions[0]);
//		bones[0] *= glm::translate(glm::vec3(sinValue, 0.0f, 0.0f));
//		bones[1] = glm::rotate(bones[0], (float)sinValue, glm::vec3(0,1,0))
//			* glm::translate(positions[1]);

		// Draw bones
		glColor3f(1.0f, 1.0f, 1.0f);
		for (int i = 0; i < 3; i++)
			glm::mat4 bone = bones[i];
			glVertex2f(bone[3][0], bone[3][1]);

		// Draw joints
		glColor3f(1.0f, 0.0f, 0.0f);
		for (int i = 0; i < 3; i++)
			glm::mat4 bone = bones[i];
			glVertex2f(bone[3][0], bone[3][1]);



	return 0;



Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!