Add gl wrapper classes

This commit is contained in:
hodasemi 2018-05-17 18:59:31 +02:00
parent e10f534816
commit 880c7b0aac
8 changed files with 272 additions and 76 deletions

View file

@ -2,5 +2,11 @@
"C_Cpp.default.includePath": [ "C_Cpp.default.includePath": [
"CGII/src/", "CGII/src/",
"framework/include/" "framework/include/"
] ],
"files.associations": {
"array": "cpp",
"initializer_list": "cpp",
"string_view": "cpp",
"valarray": "cpp"
}
} }

View file

@ -1,4 +1,4 @@
#version 330 compatibility #version 150
layout(triangles) in; layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out; layout(triangle_strip, max_vertices = 3) out;

View file

@ -11,7 +11,8 @@
Bone::Bone() Bone::Bone()
: parent(nullptr), length(0.0f), direction_in_world_space(0.0, 0.0, 0.0), translationTransforms(0) : parent(nullptr), length(0.0f), direction_in_world_space(0.0, 0.0, 0.0), translationTransforms(0)
{} {
}
Bone::~Bone() Bone::~Bone()
{ {
@ -26,10 +27,23 @@ Bone::~Bone()
orientation.clear(); orientation.clear();
} }
void print_mat4(Mat4 &m)
{
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
std::cout << m[x * 4 + y] << " ";
}
std::cout << std::endl;
}
}
void Bone::calculate_matrices() void Bone::calculate_matrices()
{ {
orientationTransformGlobalToLocal.identity(); orientationTransformGlobalToLocal.identity();
std::for_each(orientation.begin(), orientation.end(), [&](AtomicTransform* t) { std::for_each(orientation.begin(), orientation.end(), [&](AtomicTransform *t) {
orientationTransformGlobalToLocal = orientationTransformGlobalToLocal * t->calculate_matrix(); orientationTransformGlobalToLocal = orientationTransformGlobalToLocal * t->calculate_matrix();
}); });
orientationTransformLocalToGlobal = cgv::math::inv(orientationTransformGlobalToLocal); orientationTransformLocalToGlobal = cgv::math::inv(orientationTransformGlobalToLocal);
@ -55,10 +69,10 @@ Mat4 Bone::calculate_transform_prev_to_current_without_dofs()
return t; return t;
} }
void Bone::add_dof(AtomicTransform* dof) void Bone::add_dof(AtomicTransform *dof)
{ {
dof->set_index_in_amc(dofs.size()); dof->set_index_in_amc(dofs.size());
if (dynamic_cast<AtomicTranslationTransform*>(dof)) if (dynamic_cast<AtomicTranslationTransform *>(dof))
{ {
dofs.push_front(std::shared_ptr<AtomicTransform>(dof)); dofs.push_front(std::shared_ptr<AtomicTransform>(dof));
++translationTransforms; ++translationTransforms;
@ -67,40 +81,40 @@ void Bone::add_dof(AtomicTransform* dof)
dofs.insert(dofs.begin() + translationTransforms, std::shared_ptr<AtomicTransform>(dof)); dofs.insert(dofs.begin() + translationTransforms, std::shared_ptr<AtomicTransform>(dof));
} }
void Bone::set_name(const std::string& name) { this->name = name; } void Bone::set_name(const std::string &name) { this->name = name; }
const std::string& Bone::get_name() const { return name; } const std::string &Bone::get_name() const { return name; }
void Bone::set_direction_in_world_space(const Vec3& direction) { this->direction_in_world_space = direction; } void Bone::set_direction_in_world_space(const Vec3 &direction) { this->direction_in_world_space = direction; }
const Vec3& Bone::get_direction_in_world_space() const { return direction_in_world_space; } const Vec3 &Bone::get_direction_in_world_space() const { return direction_in_world_space; }
void Bone::set_length(float l) { this->length = l; } void Bone::set_length(float l) { this->length = l; }
float Bone::get_length() const { return length; } float Bone::get_length() const { return length; }
void Bone::add_axis_rotation(AtomicTransform* transform) { orientation.push_front(transform); } void Bone::add_axis_rotation(AtomicTransform *transform) { orientation.push_front(transform); }
void Bone::add_child(Bone* child) void Bone::add_child(Bone *child)
{ {
child->set_parent(this); child->set_parent(this);
children.push_back(child); children.push_back(child);
} }
Bone* Bone::child_at(int i) const { return children[i]; } Bone *Bone::child_at(int i) const { return children[i]; }
int Bone::childCount() const { return children.size(); } int Bone::childCount() const { return children.size(); }
void Bone::set_parent(Bone* parent) void Bone::set_parent(Bone *parent)
{ {
this->parent = parent; this->parent = parent;
} }
Bone* Bone::get_parent() const { return parent; } Bone *Bone::get_parent() const { return parent; }
int Bone::dof_count() const { return dofs.size(); } int Bone::dof_count() const { return dofs.size(); }
std::shared_ptr<AtomicTransform> Bone::get_dof(int dofIndex) const { return dofs[dofIndex]; } std::shared_ptr<AtomicTransform> Bone::get_dof(int dofIndex) const { return dofs[dofIndex]; }
const Mat4& Bone::get_binding_pose_matrix() const const Mat4 &Bone::get_binding_pose_matrix() const
{ {
return transformLocalToGlobal; return transformLocalToGlobal;
} }
const Mat4& Bone::get_translation_transform_current_joint_to_next() const { return translationTransformCurrentJointToNext; } const Mat4 &Bone::get_translation_transform_current_joint_to_next() const { return translationTransformCurrentJointToNext; }
const Mat4& Bone::get_orientation_transform_prev_joint_to_current() const { return orientationTransformPrevJointToCurrent; } const Mat4 &Bone::get_orientation_transform_prev_joint_to_current() const { return orientationTransformPrevJointToCurrent; }
Vec4 Bone::get_bone_local_root_position() const { return Vec4(0, 0, 0, 1); } Vec4 Bone::get_bone_local_root_position() const { return Vec4(0, 0, 0, 1); }
Vec4 Bone::get_bone_local_tip_position() const { return translationTransformCurrentJointToNext * Vec4(0, 0, 0, 1); } Vec4 Bone::get_bone_local_tip_position() const { return translationTransformCurrentJointToNext * Vec4(0, 0, 0, 1); }

View file

@ -0,0 +1,32 @@
#ifndef BUFFER
#define BUFFER
#include <vector>
#include <GL/gl.h>
template <class T>
unsigned int sizevec(const std::vector<T> &p_vec) { return sizeof(T) * p_vec.size(); }
class Buffer
{
public:
Buffer()
{
glGenBuffers(1, &m_vbo);
}
~Buffer()
{
glDeleteBuffers(1, &m_vbo);
}
GLuint getBuffer()
{
return m_vbo;
}
private:
GLuint m_vbo;
};
#endif // BUFFER

View file

@ -0,0 +1,133 @@
#ifndef PROGRAM
#define PROGRAM
#include <fstream>
#include <iostream>
#include <string>
#include <GL/gl.h>
class Program
{
public:
Program()
{
}
~Program()
{
glDeleteProgram(m_prog);
}
void createProgram(const char *vs, const char *fs)
{
GLuint vert = createShader(vs, GL_VERTEX_SHADER);
GLuint frag = createShader(fs, GL_FRAGMENT_SHADER);
if ((vert == 0) || (frag == 0))
{
return;
}
m_prog = glCreateProgram();
glAttachShader(m_prog, vert);
glAttachShader(m_prog, frag);
glLinkProgram(m_prog);
glDeleteShader(vert);
glDeleteShader(frag);
}
void createProgram(const char *vs, const char *gs, const char *fs)
{
GLuint vert = createShader(vs, GL_VERTEX_SHADER);
GLuint geom = createShader(gs, GL_GEOMETRY_SHADER);
GLuint frag = createShader(fs, GL_FRAGMENT_SHADER);
if ((vert == 0) || (frag == 0) || (geom == 0))
{
return;
}
m_prog = glCreateProgram();
glAttachShader(m_prog, vert);
glAttachShader(m_prog, geom);
glAttachShader(m_prog, frag);
glLinkProgram(m_prog);
glDeleteShader(vert);
glDeleteShader(geom);
glDeleteShader(frag);
}
void createProgram(const std::string vs, const std::string fs)
{
createProgram(vs.c_str(), fs.c_str());
}
GLuint getProgram()
{
return m_prog;
}
private:
inline std::string loadShaderText(const char *path)
{
std::ifstream is(path);
std::string s_save;
if (is.is_open())
{
s_save.assign(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>());
is.close();
}
else
{
std::cout << "could not open " << path << std::endl;
}
return s_save;
}
inline GLuint createShader(const char *path, GLenum shadertype)
{
std::string s_source = loadShaderText(path);
if (s_source.empty())
{
return 0;
}
const char *c_source = s_source.c_str();
GLuint shader = glCreateShader(shadertype);
glShaderSource(shader, 1, (const char **)&c_source, NULL);
glCompileShader(shader);
GLint shaderCompiled = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &shaderCompiled);
if (shaderCompiled != GL_TRUE)
{
GLint log_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
char *log = new char[log_length];
glGetShaderInfoLog(shader, (GLsizei)log_length, NULL, log);
std::cout << "error compiling shader( " << path << " ): " << std::endl
<< log << std::endl;
delete[] log;
glDeleteShader(shader);
shader = 0;
}
return shader;
}
GLuint m_prog;
};
#endif // PROGRAM

View file

@ -18,19 +18,27 @@ using namespace cgv::utils;
cgv::render::shader_program Mesh::prog; cgv::render::shader_program Mesh::prog;
// The constructor of this class // The constructor of this class
SkeletonViewer::SkeletonViewer(DataStore* data) SkeletonViewer::SkeletonViewer(DataStore *data)
: node("Skeleton Viewer"), data(data) : node("Skeleton Viewer"), data(data)
{ {
program.createProgram("CGII/glsl/skinning.glvs", "CGII/glsl/skinning.glgs", "CGII/glsl/skinning.glfs");
connect(data->skeleton_changed, this, &SkeletonViewer::skeleton_changed); connect(data->skeleton_changed, this, &SkeletonViewer::skeleton_changed);
connect(get_animation_trigger().shoot, this, &SkeletonViewer::timer_event); connect(get_animation_trigger().shoot, this, &SkeletonViewer::timer_event);
} }
//draws a part of a skeleton, represented by the given root node //draws a part of a skeleton, represented by the given root node
void SkeletonViewer::draw_skeleton_subtree(Bone* node, const Mat4& global_to_parent_local) void SkeletonViewer::draw_skeleton_subtree(Bone *node, const Mat4 &global_to_parent_local)
{ {
//Task: Draw skeleton //Task: Draw skeleton
Mat4 transform;
for (int i = 0; i < node->childCount(); i++)
{
draw_skeleton_subtree(node->child_at(i), transform);
}
} }
void SkeletonViewer::timer_event(double, double dt) void SkeletonViewer::timer_event(double, double dt)
@ -57,9 +65,10 @@ void SkeletonViewer::skeleton_changed(std::shared_ptr<Skeleton> s)
generate_tree_view_nodes(); generate_tree_view_nodes();
//Fit view to skeleton //Fit view to skeleton
std::vector<cgv::render::view*> view_ptrs; std::vector<cgv::render::view *> view_ptrs;
cgv::base::find_interface<cgv::render::view>(get_node(), view_ptrs); cgv::base::find_interface<cgv::render::view>(get_node(), view_ptrs);
if (view_ptrs.empty()) { if (view_ptrs.empty())
{
// If there is no view, we cannot update it // If there is no view, we cannot update it
cgv::gui::message("could not find a view to adjust!!"); cgv::gui::message("could not find a view to adjust!!");
} }
@ -77,7 +86,7 @@ void SkeletonViewer::skeleton_changed(std::shared_ptr<Skeleton> s)
post_redraw(); post_redraw();
} }
void SkeletonViewer::recursive_connect_signals(Bone* b) void SkeletonViewer::recursive_connect_signals(Bone *b)
{ {
for (int i = 0; i < b->dof_count(); ++i) for (int i = 0; i < b->dof_count(); ++i)
connect(b->get_dof(i)->changed_signal, this, &SkeletonViewer::dof_changed); connect(b->get_dof(i)->changed_signal, this, &SkeletonViewer::dof_changed);
@ -103,7 +112,7 @@ void SkeletonViewer::generate_tree_view_nodes()
generate_tree_view_nodes(tree_view, data->get_skeleton()->get_root()); generate_tree_view_nodes(tree_view, data->get_skeleton()->get_root());
} }
void SkeletonViewer::generate_tree_view_nodes(gui_group_ptr parent, Bone* bone) void SkeletonViewer::generate_tree_view_nodes(gui_group_ptr parent, Bone *bone)
{ {
if (bone->childCount() == 0) if (bone->childCount() == 0)
{ {
@ -123,7 +132,7 @@ void SkeletonViewer::generate_tree_view_nodes(gui_group_ptr parent, Bone* bone)
void SkeletonViewer::start_choose_base() void SkeletonViewer::start_choose_base()
{ {
Bone* b = data->get_endeffector(); Bone *b = data->get_endeffector();
data->set_endeffector(nullptr); data->set_endeffector(nullptr);
data->set_base(b); data->set_base(b);
} }
@ -134,7 +143,7 @@ void SkeletonViewer::tree_selection_changed(base_ptr p, bool select)
if (select) if (select)
{ {
Bone* bone = gui_to_bone.at(p); Bone *bone = gui_to_bone.at(p);
generate_bone_gui(bone); generate_bone_gui(bone);
data->set_endeffector(bone); data->set_endeffector(bone);
} }
@ -254,7 +263,7 @@ void SkeletonViewer::create_gui()
rebind(this, &SkeletonViewer::load_pinocchio)); rebind(this, &SkeletonViewer::load_pinocchio));
} }
void SkeletonViewer::generate_bone_gui(Bone* bone) void SkeletonViewer::generate_bone_gui(Bone *bone)
{ {
// Add bone-specific gui elements to bone_group. // Add bone-specific gui elements to bone_group.
// Use the layout "\n" to specify vertical alignment // Use the layout "\n" to specify vertical alignment
@ -270,7 +279,7 @@ void SkeletonViewer::generate_bone_gui(Bone* bone)
} }
} }
void SkeletonViewer::draw(context& c) void SkeletonViewer::draw(context &c)
{ {
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);

View file

@ -7,6 +7,9 @@
#include "common.h" #include "common.h"
#include "DataStore.h" #include "DataStore.h"
#include "GLClasses/buffer.h"
#include "GLClasses/program.h"
#include <cgv/gui/trigger.h> #include <cgv/gui/trigger.h>
#include <cgv/gui/provider.h> #include <cgv/gui/provider.h>
#include <cgv/gui/event_handler.h> #include <cgv/gui/event_handler.h>
@ -23,23 +26,23 @@ using namespace cgv::utils;
class SkeletonViewer : public node, public drawable, public provider class SkeletonViewer : public node, public drawable, public provider
{ {
private: private:
DataStore* data; DataStore *data;
gui_group_ptr tree_view; gui_group_ptr tree_view;
gui_group_ptr bone_group; gui_group_ptr bone_group;
// Maps gui elements in the tree view to a specific bone // Maps gui elements in the tree view to a specific bone
std::map<base_ptr, Bone*> gui_to_bone; std::map<base_ptr, Bone *> gui_to_bone;
// slot for the signal // slot for the signal
void timer_event(double, double dt); void timer_event(double, double dt);
void skeleton_changed(std::shared_ptr<Skeleton>); void skeleton_changed(std::shared_ptr<Skeleton>);
void generate_tree_view_nodes(); void generate_tree_view_nodes();
void generate_tree_view_nodes(gui_group_ptr parent, Bone* bone); void generate_tree_view_nodes(gui_group_ptr parent, Bone *bone);
void tree_selection_changed(base_ptr p, bool select); void tree_selection_changed(base_ptr p, bool select);
void generate_bone_gui(Bone* bone); void generate_bone_gui(Bone *bone);
void load_skeleton(); void load_skeleton();
void write_pinocchio(); void write_pinocchio();
@ -47,25 +50,24 @@ private:
void load_animation(); void load_animation();
void start_choose_base(); void start_choose_base();
void draw_skeleton_subtree(Bone* node, const Mat4& global_to_parent_local); void draw_skeleton_subtree(Bone *node, const Mat4 &global_to_parent_local);
void dof_changed(double new_value); void dof_changed(double new_value);
void recursive_connect_signals(Bone* b); void recursive_connect_signals(Bone *b);
void start_animation(); void start_animation();
void stop_animation(); void stop_animation();
public: public:
// The constructor of this class // The constructor of this class
SkeletonViewer(DataStore*); SkeletonViewer(DataStore *);
// Create the gui elements // Create the gui elements
void create_gui(); void create_gui();
// Draw the scene // Draw the scene
void draw(context& c); void draw(context &c);
std::string get_parent_type() const; std::string get_parent_type() const;
};
Program program;
};

View file

@ -16,15 +16,15 @@ using namespace cgv::base;
struct Initializer struct Initializer
{ {
DataStore* data; DataStore *data;
Initializer() Initializer()
{ {
register_object(base_ptr(new CGVDemo()), ""); //register_object(base_ptr(new CGVDemo()), "");
data = new DataStore(); data = new DataStore();
/*register_object(base_ptr(new SkeletonViewer(data)), "");*/ register_object(base_ptr(new SkeletonViewer(data)), "");
/*register_object(base_ptr(new IKViewer(data)), "");*/ /*register_object(base_ptr(new IKViewer(data)), "");*/