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": [
"CGII/src/",
"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(triangle_strip, max_vertices = 3) out;

View file

@ -11,7 +11,8 @@
Bone::Bone()
: parent(nullptr), length(0.0f), direction_in_world_space(0.0, 0.0, 0.0), translationTransforms(0)
{}
{
}
Bone::~Bone()
{
@ -26,6 +27,19 @@ Bone::~Bone()
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()
{
orientationTransformGlobalToLocal.identity();

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

@ -22,6 +22,8 @@ SkeletonViewer::SkeletonViewer(DataStore* 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(get_animation_trigger().shoot, this, &SkeletonViewer::timer_event);
@ -31,6 +33,12 @@ SkeletonViewer::SkeletonViewer(DataStore* data)
void SkeletonViewer::draw_skeleton_subtree(Bone *node, const Mat4 &global_to_parent_local)
{
//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)
@ -59,7 +67,8 @@ void SkeletonViewer::skeleton_changed(std::shared_ptr<Skeleton> s)
//Fit view to skeleton
std::vector<cgv::render::view *> 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
cgv::gui::message("could not find a view to adjust!!");
}

View file

@ -7,6 +7,9 @@
#include "common.h"
#include "DataStore.h"
#include "GLClasses/buffer.h"
#include "GLClasses/program.h"
#include <cgv/gui/trigger.h>
#include <cgv/gui/provider.h>
#include <cgv/gui/event_handler.h>
@ -52,8 +55,6 @@ private:
void dof_changed(double new_value);
void recursive_connect_signals(Bone *b);
void start_animation();
void stop_animation();
@ -67,5 +68,6 @@ public:
void draw(context &c);
std::string get_parent_type() const;
};
Program program;
};

View file

@ -20,11 +20,11 @@ struct Initializer
Initializer()
{
register_object(base_ptr(new CGVDemo()), "");
//register_object(base_ptr(new CGVDemo()), "");
data = new DataStore();
/*register_object(base_ptr(new SkeletonViewer(data)), "");*/
register_object(base_ptr(new SkeletonViewer(data)), "");
/*register_object(base_ptr(new IKViewer(data)), "");*/