Below is a program to animate a linkage with 3 joints and 9 degree of freedoms.
// Bone_Animation.h
#pragma once
#include <vector>
#include <glm/glm.hpp>
#include <GLFW/glfw3.h>
class Bone_Animation
{
public:
Bone_Animation();
~Bone_Animation();
void init();
void update(float delta_time);
void reset();
void render();
void handleInput(GLFWwindow* window);
public:
// Here the head of each vector is the root bone
std::vector<glm::vec3> scale_vector;
std::vector<glm::vec3> rotation_degree_vector;
std::vector<glm::vec4> colors;
glm::vec3 root_position;
};
// Bone_Animation.cpp
#include "Bone_Animation.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp>
Bone_Animation::Bone_Animation()
{
}
Bone_Animation::~Bone_Animation()
{
}
void Bone_Animation::init()
{
root_position = { 2.0f, 1.0f, 2.0f };
scale_vector =
{
{1.0f, 1.0f, 1.0f},
{0.5f, 4.0f, 0.5f},
{0.5f, 3.0f, 0.5f},
{0.5f, 2.0f, 0.5f}
};
rotation_degree_vector =
{
{0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f}
};
colors =
{
{0.7f, 0.0f, 0.0f, 1.0f},
{0.7f, 0.7f, 0.0f, 1.0f},
{0.7f, 0.0f, 0.7f, 1.0f},
{0.0f, 0.7f, 0.7f, 1.0f}
};
}
void Bone_Animation::update(float delta_time)
{
// Update the bone animation logic here
}
void Bone_Animation::reset()
{
// Reset bone positions and rotations to their initial state
}
void Bone_Animation::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up the OpenGL rendering for each bone
for (size_t i = 0; i < scale_vector.size(); ++i)
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, root_position);
// Apply rotations in the order y, z, x
model = glm::rotate(model, glm::radians(rotation_degree_vector[i].y), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, glm::radians(rotation_degree_vector[i].z), glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::rotate(model, glm::radians(rotation_degree_vector[i].x), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::scale(model, scale_vector[i]);
// Render the cube with the specified color
// You can use any rendering library you prefer for this part
// For simplicity, let's assume you have a function renderCube(model, color) for rendering a cube
renderCube(model, colors[i]);
}
}
void Bone_Animation::handleInput(GLFWwindow* window)
{
// Handle user input for interactive control
// Update rotation_degree_vector based on user input
// For example, you can use GLFW's input functions to get key or mouse input
}
// Add the rendering function (e.g., renderCube) and GLFW setup in your main file.
So, to use the code and get the animation and interactive control, one can use the OpenGL graphics library with GLFW for window management