mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
work
This commit is contained in:
@@ -16,7 +16,7 @@ namespace gl
|
||||
active = this;
|
||||
T::bind(*static_cast<T*>(active));
|
||||
}
|
||||
void unbind()
|
||||
static void unbind()
|
||||
{
|
||||
active = nullptr;
|
||||
T::bind(0);
|
||||
|
||||
0
gl/framebuffer.cpp
Normal file
0
gl/framebuffer.cpp
Normal file
0
gl/framebuffer.h
Normal file
0
gl/framebuffer.h
Normal file
58
gl/glsl_common.h
Normal file
58
gl/glsl_common.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "ubo.h"
|
||||
|
||||
namespace gl
|
||||
{
|
||||
const std::string glsl_common =
|
||||
"const uint MAX_LIGHTS = " + std::to_string(MAX_LIGHTS) + "U;\n" +
|
||||
"const uint MAX_PARAMS = " + std::to_string(MAX_PARAMS) + "U;\n" +
|
||||
R"STRING(
|
||||
const uint LIGHT_SPOT = 0U;
|
||||
const uint LIGHT_POINT = 1U;
|
||||
const uint LIGHT_DIR = 2U;
|
||||
|
||||
struct light_s
|
||||
{
|
||||
vec3 pos;
|
||||
uint type;
|
||||
|
||||
vec3 dir;
|
||||
float in_cutoff;
|
||||
|
||||
vec3 color;
|
||||
float out_cutoff;
|
||||
|
||||
float linear;
|
||||
float quadratic;
|
||||
};
|
||||
|
||||
layout(std140) uniform light_ubo
|
||||
{
|
||||
vec3 ambient;
|
||||
float fog_density;
|
||||
|
||||
vec3 fog_color;
|
||||
uint lights_count;
|
||||
|
||||
light_s lights[MAX_LIGHTS];
|
||||
};
|
||||
|
||||
layout (std140) uniform model_ubo
|
||||
{
|
||||
mat4 modelview;
|
||||
mat3 modelviewnormal;
|
||||
vec4 param[MAX_PARAMS];
|
||||
|
||||
vec3 velocity;
|
||||
float opacity;
|
||||
float emission;
|
||||
};
|
||||
|
||||
layout (std140) uniform scene_ubo
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 lightview;
|
||||
float time;
|
||||
};
|
||||
|
||||
)STRING";
|
||||
}
|
||||
0
gl/renderbuffer.cpp
Normal file
0
gl/renderbuffer.cpp
Normal file
0
gl/renderbuffer.h
Normal file
0
gl/renderbuffer.h
Normal file
@@ -3,6 +3,7 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "shader.h"
|
||||
#include "glsl_common.h"
|
||||
|
||||
inline bool strcend(std::string const &value, std::string const &ending)
|
||||
{
|
||||
@@ -11,17 +12,51 @@ inline bool strcend(std::string const &value, std::string const &ending)
|
||||
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
|
||||
}
|
||||
|
||||
gl::shader::shader(const std::string &filename)
|
||||
std::string gl::shader::read_file(const std::string &filename)
|
||||
{
|
||||
std::stringstream stream;
|
||||
std::ifstream f;
|
||||
f.exceptions(std::ifstream::badbit);
|
||||
|
||||
f.open(filename);
|
||||
f.open("shaders/" + filename);
|
||||
stream << f.rdbuf();
|
||||
f.close();
|
||||
|
||||
std::string str = stream.str();
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
void gl::shader::expand_includes(std::string &str)
|
||||
{
|
||||
size_t start_pos = 0;
|
||||
|
||||
std::string magic = "#include";
|
||||
while ((start_pos = str.find(magic, start_pos)) != str.npos)
|
||||
{
|
||||
size_t fp = str.find('<', start_pos);
|
||||
size_t fe = str.find('>', start_pos);
|
||||
if (fp == str.npos || fe == str.npos)
|
||||
return;
|
||||
|
||||
std::string filename = str.substr(fp + 1, fe - fp - 1);
|
||||
std::string content;
|
||||
if (filename != "common")
|
||||
content = read_file(filename);
|
||||
else
|
||||
content = glsl_common;
|
||||
|
||||
str.replace(start_pos, fe - start_pos + 1, content);
|
||||
}
|
||||
}
|
||||
|
||||
gl::shader::shader(const std::string &filename)
|
||||
{
|
||||
std::string str = read_file(filename);
|
||||
expand_includes(str);
|
||||
|
||||
std::cout << (str) << std::endl;;
|
||||
|
||||
const GLchar *cstr = str.c_str();
|
||||
|
||||
if (!cstr[0])
|
||||
@@ -58,7 +93,19 @@ gl::shader::~shader()
|
||||
void gl::program::init()
|
||||
{
|
||||
bind();
|
||||
glUniform1i(glGetUniformLocation(*this, "tex"), 3);
|
||||
|
||||
int i = 0;
|
||||
GLuint loc;
|
||||
while (true)
|
||||
{
|
||||
std::string name = "tex" + std::to_string(i + 1);
|
||||
loc = glGetUniformLocation(*this, name.c_str());
|
||||
if (loc != -1)
|
||||
glUniform1i(loc, i);
|
||||
else
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
gl::program::program()
|
||||
|
||||
@@ -14,6 +14,10 @@ namespace gl
|
||||
public:
|
||||
shader(const std::string &filename);
|
||||
~shader();
|
||||
|
||||
private:
|
||||
void expand_includes(std::string &str);
|
||||
std::string read_file(const std::string &filename);
|
||||
};
|
||||
|
||||
class program : public object, public bindable<program>
|
||||
|
||||
@@ -3,6 +3,14 @@
|
||||
|
||||
void gl::program_mvp::init()
|
||||
{
|
||||
glUniformBlockBinding(*this, 0, glGetUniformBlockIndex(*this, "scene_ubo"));
|
||||
glUniformBlockBinding(*this, 1, glGetUniformBlockIndex(*this, "model_ubo"));
|
||||
GLuint index;
|
||||
|
||||
if ((index = glGetUniformBlockIndex(*this, "scene_ubo")) != GL_INVALID_INDEX)
|
||||
glUniformBlockBinding(*this, 0, index);
|
||||
|
||||
if ((index = glGetUniformBlockIndex(*this, "model_ubo")) != GL_INVALID_INDEX)
|
||||
glUniformBlockBinding(*this, 1, index);
|
||||
|
||||
if ((index = glGetUniformBlockIndex(*this, "light_ubo")) != GL_INVALID_INDEX)
|
||||
glUniformBlockBinding(*this, 2, index);
|
||||
}
|
||||
|
||||
72
gl/ubo.h
72
gl/ubo.h
@@ -2,7 +2,6 @@
|
||||
#include "bindable.h"
|
||||
|
||||
#define UBS_PAD(x) uint64_t : x * 4; uint64_t : x * 4;
|
||||
#define DEFINE_UBS(x, y) _Pragma("pack(push, 1)"); struct x y; _Pragma("pack(pop)")
|
||||
|
||||
namespace gl
|
||||
{
|
||||
@@ -20,29 +19,82 @@ namespace gl
|
||||
void update(void *data, int offset, int size);
|
||||
};
|
||||
|
||||
// layout std140
|
||||
// structs must match with GLSL
|
||||
// ordered to minimize padding
|
||||
|
||||
_Pragma("pack(push, 1)")
|
||||
|
||||
struct scene_ubs
|
||||
{
|
||||
glm::mat4 projection;
|
||||
|
||||
void set(const glm::mat4 &m)
|
||||
{
|
||||
projection = m;
|
||||
}
|
||||
glm::mat4 lightview;
|
||||
float time;
|
||||
};
|
||||
|
||||
static_assert(sizeof(scene_ubs) == 132, "bad size of ubs");
|
||||
|
||||
const size_t MAX_PARAMS = 3;
|
||||
|
||||
struct model_ubs
|
||||
{
|
||||
glm::mat4 modelview;
|
||||
glm::mat4 modelviewnormal;
|
||||
glm::mat3x4 modelviewnormal;
|
||||
glm::vec4 param[MAX_PARAMS];
|
||||
|
||||
void set(const glm::mat4 &m)
|
||||
glm::vec3 velocity;
|
||||
float opacity;
|
||||
float emission;
|
||||
|
||||
void set_modelview(const glm::mat4 &mv)
|
||||
{
|
||||
modelview = m;
|
||||
modelviewnormal = glm::mat3(glm::transpose(glm::inverse(m)));
|
||||
modelview = mv;
|
||||
modelviewnormal = glm::mat3(glm::transpose(glm::inverse(mv)));
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(model_ubs) == 156 + 8 * MAX_PARAMS, "bad size of ubs");
|
||||
|
||||
struct light_element_ubs
|
||||
{
|
||||
enum type_e
|
||||
{
|
||||
SPOT = 0,
|
||||
POINT,
|
||||
DIR
|
||||
};
|
||||
|
||||
glm::vec3 pos;
|
||||
type_e type;
|
||||
|
||||
glm::vec3 dir;
|
||||
float in_cutoff;
|
||||
|
||||
glm::vec3 color;
|
||||
float out_cutoff;
|
||||
|
||||
float linear;
|
||||
float quadratic;
|
||||
|
||||
UBS_PAD(8);
|
||||
};
|
||||
|
||||
static_assert(sizeof(light_element_ubs) == 64, "bad size of ubs");
|
||||
|
||||
const size_t MAX_LIGHTS = 16;
|
||||
|
||||
struct light_ubs
|
||||
{
|
||||
glm::vec3 ambient;
|
||||
float fog_density;
|
||||
|
||||
glm::vec3 fog_color;
|
||||
uint32_t lights_count;
|
||||
|
||||
light_element_ubs lights[MAX_LIGHTS];
|
||||
};
|
||||
|
||||
static_assert(sizeof(light_ubs) == 32 + sizeof(light_element_ubs) * MAX_LIGHTS, "bad size of ubs");
|
||||
|
||||
_Pragma("pack(pop)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user