mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-18 03:09:18 +02:00
work
This commit is contained in:
37
gl/cubemap.cpp
Normal file
37
gl/cubemap.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "cubemap.h"
|
||||
|
||||
gl::cubemap::cubemap()
|
||||
{
|
||||
glGenTextures(1, *this);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
}
|
||||
|
||||
gl::cubemap::~cubemap()
|
||||
{
|
||||
glDeleteTextures(1, *this);
|
||||
}
|
||||
|
||||
void gl::cubemap::alloc(GLint format, int width, int height, GLenum components, GLenum type)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, *this);
|
||||
for (GLuint tgt = GL_TEXTURE_CUBE_MAP_POSITIVE_X; tgt <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; tgt++)
|
||||
glTexImage2D(tgt, 0, format, width, height, 0, components, type, nullptr);
|
||||
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
}
|
||||
|
||||
void gl::cubemap::bind(int unit)
|
||||
{
|
||||
glActiveTexture(unit);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, *this);
|
||||
}
|
||||
|
||||
void gl::cubemap::generate_mipmaps()
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, *this);
|
||||
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
}
|
||||
19
gl/cubemap.h
Normal file
19
gl/cubemap.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "object.h"
|
||||
|
||||
namespace gl
|
||||
{
|
||||
// cubemap texture rendertarget
|
||||
// todo: integrate with texture system
|
||||
class cubemap : public object
|
||||
{
|
||||
public:
|
||||
cubemap();
|
||||
~cubemap();
|
||||
|
||||
void alloc(GLint format, int width, int height, GLenum components, GLenum type);
|
||||
void bind(int unit);
|
||||
void generate_mipmaps();
|
||||
};
|
||||
}
|
||||
@@ -22,12 +22,24 @@ void gl::framebuffer::attach(const opengl_texture &tex, GLenum location)
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, location, tex.target, tex.id, 0);
|
||||
}
|
||||
|
||||
void gl::framebuffer::attach(const cubemap &tex, int face, GLenum location)
|
||||
{
|
||||
bind();
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, location, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, *tex, 0);
|
||||
}
|
||||
|
||||
void gl::framebuffer::attach(const renderbuffer &rb, GLenum location)
|
||||
{
|
||||
bind();
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, location, GL_RENDERBUFFER, *rb);
|
||||
}
|
||||
|
||||
void gl::framebuffer::detach(GLenum location)
|
||||
{
|
||||
bind();
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, location, GL_RENDERBUFFER, 0);
|
||||
}
|
||||
|
||||
bool gl::framebuffer::is_complete()
|
||||
{
|
||||
bind();
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "bindable.h"
|
||||
#include "renderbuffer.h"
|
||||
#include "Texture.h"
|
||||
#include "cubemap.h"
|
||||
|
||||
namespace gl
|
||||
{
|
||||
@@ -14,7 +15,9 @@ namespace gl
|
||||
~framebuffer();
|
||||
|
||||
void attach(const opengl_texture &tex, GLenum location);
|
||||
void attach(const cubemap &tex, int face, GLenum location);
|
||||
void attach(const renderbuffer &rb, GLenum location);
|
||||
void detach(GLenum location);
|
||||
void clear(GLbitfield mask);
|
||||
|
||||
bool is_complete();
|
||||
|
||||
@@ -50,10 +50,62 @@ void gl::shader::expand_includes(std::string &str)
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, gl::shader::components_e> gl::shader::components_mapping =
|
||||
{
|
||||
{ "R", components_e::R },
|
||||
{ "RG", components_e::RG },
|
||||
{ "RGB", components_e::RGB },
|
||||
{ "RGBA", components_e::RGBA },
|
||||
{ "sRGB", components_e::sRGB },
|
||||
{ "sRGB_A", components_e::sRGB_A }
|
||||
};
|
||||
|
||||
void gl::shader::parse_config(std::string &str)
|
||||
{
|
||||
size_t start_pos = 0;
|
||||
|
||||
std::string magic = "#texture";
|
||||
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::istringstream ss(str.substr(fp + 1, fe - fp - 1));
|
||||
std::string token;
|
||||
|
||||
texture_entry conf;
|
||||
|
||||
size_t arg = 0;
|
||||
while (std::getline(ss, token, ','))
|
||||
{
|
||||
std::istringstream token_ss(token);
|
||||
if (arg == 0)
|
||||
token_ss >> conf.name;
|
||||
else if (arg == 1)
|
||||
token_ss >> conf.id;
|
||||
else if (arg == 2)
|
||||
{
|
||||
std::string comp;
|
||||
token_ss >> comp;
|
||||
conf.components = components_mapping[comp];
|
||||
}
|
||||
arg++;
|
||||
}
|
||||
|
||||
if (arg == 3)
|
||||
texture_conf.push_back(conf);
|
||||
|
||||
str.erase(start_pos, fe - start_pos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
gl::shader::shader(const std::string &filename)
|
||||
{
|
||||
std::string str = read_file(filename);
|
||||
expand_includes(str);
|
||||
parse_config(str);
|
||||
|
||||
const GLchar *cstr = str.c_str();
|
||||
|
||||
@@ -92,21 +144,12 @@ void gl::program::init()
|
||||
{
|
||||
bind();
|
||||
|
||||
int i = 0;
|
||||
GLuint loc;
|
||||
while (true)
|
||||
for (shader::texture_entry &e : texture_conf)
|
||||
{
|
||||
std::string name = "tex" + std::to_string(i + 1);
|
||||
loc = glGetUniformLocation(*this, name.c_str());
|
||||
if (loc != -1)
|
||||
glUniform1i(loc, i);
|
||||
else
|
||||
break;
|
||||
i++;
|
||||
GLuint loc = glGetUniformLocation(*this, e.name.c_str());
|
||||
glUniform1i(loc, e.id);
|
||||
}
|
||||
|
||||
//tbd: do something better
|
||||
|
||||
glUniform1i(glGetUniformLocation(*this, "shadowmap"), MAX_TEXTURES + 0);
|
||||
glUniform1i(glGetUniformLocation(*this, "envmap"), MAX_TEXTURES + 1);
|
||||
|
||||
@@ -136,6 +179,7 @@ gl::program::program(std::vector<std::reference_wrapper<const gl::shader>> shade
|
||||
|
||||
void gl::program::attach(const gl::shader &s)
|
||||
{
|
||||
std::copy(s.texture_conf.begin(), s.texture_conf.end(), std::back_inserter(texture_conf));
|
||||
glAttachShader(*this, *s);
|
||||
}
|
||||
|
||||
|
||||
24
gl/shader.h
24
gl/shader.h
@@ -15,9 +15,31 @@ namespace gl
|
||||
shader(const std::string &filename);
|
||||
~shader();
|
||||
|
||||
enum class components_e
|
||||
{
|
||||
R,
|
||||
RG,
|
||||
RGB,
|
||||
RGBA,
|
||||
sRGB,
|
||||
sRGB_A
|
||||
};
|
||||
|
||||
struct texture_entry
|
||||
{
|
||||
std::string name;
|
||||
size_t id;
|
||||
components_e components;
|
||||
};
|
||||
|
||||
std::vector<texture_entry> texture_conf;
|
||||
|
||||
private:
|
||||
void expand_includes(std::string &str);
|
||||
void parse_config(std::string &str);
|
||||
std::string read_file(const std::string &filename);
|
||||
|
||||
static std::unordered_map<std::string, components_e> components_mapping;
|
||||
};
|
||||
|
||||
class program : public object, public bindable<program>
|
||||
@@ -33,6 +55,8 @@ namespace gl
|
||||
void attach(const shader &);
|
||||
void link();
|
||||
|
||||
std::vector<shader::texture_entry> texture_conf;
|
||||
|
||||
private:
|
||||
void init();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user