mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-18 00:49:19 +02:00
material changes, shader params, texture binding changes, more changes, ...
This commit is contained in:
6
Logs.h
6
Logs.h
@@ -9,15 +9,15 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "uilayer.h"
|
||||
|
||||
enum class logtype : unsigned int {
|
||||
|
||||
generic = ( 1 << 0 ),
|
||||
file = ( 1 << 1 ),
|
||||
model = ( 1 << 2 ),
|
||||
texture = ( 1 << 3 ),
|
||||
lua = ( 1 << 4 )
|
||||
lua = ( 1 << 4 ),
|
||||
material = ( 1 << 5 ),
|
||||
shader = (1 << 6 )
|
||||
};
|
||||
|
||||
void WriteLog( const char *str, logtype const Type = logtype::generic );
|
||||
|
||||
21
Model3d.cpp
21
Model3d.cpp
@@ -338,15 +338,8 @@ int TSubModel::Load( cParser &parser, TModel3d *Model, /*int Pos,*/ bool dynamic
|
||||
if (m_material != null_handle)
|
||||
{
|
||||
opengl_material &mat = GfxRenderer.Material(m_material);
|
||||
// if material don't have opacity set, set legacy submodel opacity
|
||||
if (std::isnan(mat.opacity))
|
||||
{
|
||||
if (iFlags & 0x20) // translucent
|
||||
mat.opacity = 0.0f;
|
||||
else if (iFlags & 0x10) // opaque
|
||||
mat.opacity = 0.5f;
|
||||
}
|
||||
else // but if it does, replace submodel opacity with material one
|
||||
// if material have opacity set, replace submodel opacity with it
|
||||
if (!std::isnan(mat.opacity))
|
||||
{
|
||||
iFlags &= ~0x30;
|
||||
if (mat.opacity == 0.0f)
|
||||
@@ -1685,12 +1678,14 @@ void TSubModel::BinInit(TSubModel *s, float4x4 *m, std::vector<std::string> *t,
|
||||
if (!(iFlags & 0x30) && m_material != null_handle)
|
||||
{
|
||||
opengl_material &mat = GfxRenderer.Material(m_material);
|
||||
if (std::isnan(mat.opacity))
|
||||
// if material don't have opacity set, try to guess it
|
||||
mat.opacity = mat.get_or_guess_opacity();
|
||||
float opacity = mat.opacity;
|
||||
|
||||
// if material don't have opacity set, try to guess it
|
||||
if (std::isnan(opacity))
|
||||
opacity = mat.get_or_guess_opacity();
|
||||
|
||||
// set phase flag based on material opacity
|
||||
if (mat.opacity == 0.0f)
|
||||
if (opacity == 0.0f)
|
||||
iFlags |= 0x20; // translucent
|
||||
else
|
||||
iFlags |= 0x10; // opaque
|
||||
|
||||
103
Texture.cpp
103
Texture.cpp
@@ -571,16 +571,64 @@ opengl_texture::load_TGA() {
|
||||
}
|
||||
|
||||
bool
|
||||
opengl_texture::bind() {
|
||||
opengl_texture::bind(size_t unit) {
|
||||
|
||||
if( ( false == is_ready )
|
||||
&& ( false == create() ) ) {
|
||||
return false;
|
||||
}
|
||||
::glBindTexture( target, id );
|
||||
|
||||
if (m_units[unit] == id)
|
||||
return true;
|
||||
|
||||
if (GLEW_ARB_direct_state_access)
|
||||
{
|
||||
glBindTextureUnit(unit, id);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (unit != m_activeunit)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
m_activeunit = unit;
|
||||
}
|
||||
glBindTexture(target, id);
|
||||
}
|
||||
|
||||
m_units[unit] = id;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void opengl_texture::unbind(size_t unit)
|
||||
{
|
||||
if (GLEW_ARB_direct_state_access)
|
||||
{
|
||||
glBindTextureUnit(unit, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (unit != m_activeunit)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
m_activeunit = unit;
|
||||
}
|
||||
//todo: for other targets
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void opengl_texture::reset_unit_cache()
|
||||
{
|
||||
for( auto &unit : m_units ) {
|
||||
unit = 0;
|
||||
}
|
||||
m_activeunit = -1;
|
||||
}
|
||||
|
||||
std::array<GLuint, gl::MAX_TEXTURES + 2> opengl_texture::m_units = { 0 };
|
||||
GLint opengl_texture::m_activeunit = -1;
|
||||
|
||||
std::unordered_map<GLint, int> opengl_texture::precompressed_formats =
|
||||
{
|
||||
{ GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 8 },
|
||||
@@ -891,15 +939,6 @@ opengl_texture::flip_vertical() {
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
texture_manager::unit( GLint const Textureunit ) {
|
||||
|
||||
if( m_activeunit == Textureunit ) { return; }
|
||||
|
||||
m_activeunit = Textureunit;
|
||||
::glActiveTexture( GL_TEXTURE0 + Textureunit );
|
||||
}
|
||||
|
||||
// ustalenie numeru tekstury, wczytanie jeśli jeszcze takiej nie było
|
||||
texture_handle
|
||||
texture_manager::create(std::string Filename, bool const Loadnow , GLint fh) {
|
||||
@@ -969,35 +1008,10 @@ texture_manager::bind( std::size_t const Unit, texture_handle const Texture ) {
|
||||
|
||||
m_textures[ Texture ].second = m_garbagecollector.timestamp();
|
||||
|
||||
if( Texture == m_units[ Unit ] ) {
|
||||
// don't bind again what's already active
|
||||
return;
|
||||
}
|
||||
// TBD, TODO: do binding in texture object, add support for other types than 2d
|
||||
|
||||
unit(Unit);
|
||||
if( Texture != null_handle ) {
|
||||
#ifndef EU07_DEFERRED_TEXTURE_UPLOAD
|
||||
// NOTE: we could bind dedicated 'error' texture here if the id isn't valid
|
||||
::glBindTexture( texture(Texture).target, texture(Texture).id );
|
||||
m_units[ Unit ] = Texture;
|
||||
#else
|
||||
if( true == texture( Texture ).bind() ) {
|
||||
m_units[ Unit ] = Texture;
|
||||
}
|
||||
else {
|
||||
// TODO: bind a special 'error' texture on failure
|
||||
::glBindTexture( texture(Texture).target, 0 );
|
||||
m_units[ Unit ] = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
::glBindTexture( GL_TEXTURE_2D, 0 );
|
||||
m_units[ Unit ] = 0;
|
||||
}
|
||||
// all done
|
||||
return;
|
||||
if (Texture != null_handle)
|
||||
texture(Texture).bind(Unit);
|
||||
else
|
||||
opengl_texture::unbind(Unit);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -1017,14 +1031,7 @@ void
|
||||
texture_manager::update() {
|
||||
|
||||
if( m_garbagecollector.sweep() > 0 ) {
|
||||
reset_unit_cache();
|
||||
}
|
||||
}
|
||||
|
||||
void texture_manager::reset_unit_cache()
|
||||
{
|
||||
for( auto &unit : m_units ) {
|
||||
unit = -1;
|
||||
opengl_texture::reset_unit_cache();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
14
Texture.h
14
Texture.h
@@ -13,6 +13,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "winheaders.h"
|
||||
#include <string>
|
||||
#include "ResourceManager.h"
|
||||
#include "gl/ubo.h"
|
||||
|
||||
struct opengl_texture {
|
||||
static DDSURFACEDESC2 deserialize_ddsd(std::istream&);
|
||||
@@ -26,7 +27,8 @@ struct opengl_texture {
|
||||
void
|
||||
load();
|
||||
bool
|
||||
bind();
|
||||
bind(size_t unit);
|
||||
static void unbind(size_t unit);
|
||||
bool
|
||||
create();
|
||||
// releases resources allocated on the opengl end, storing local copy if requested
|
||||
@@ -43,6 +45,7 @@ struct opengl_texture {
|
||||
|
||||
void alloc_rendertarget(GLint format, GLint components, GLint type, int width, int height, int samples = 1);
|
||||
void set_components_hint(GLint hint);
|
||||
static void reset_unit_cache();
|
||||
|
||||
// members
|
||||
GLuint id{ (GLuint)-1 }; // associated GL resource
|
||||
@@ -87,6 +90,9 @@ private:
|
||||
static std::unordered_map<GLint, int> precompressed_formats;
|
||||
static std::unordered_map<GLint, GLint> drivercompressed_formats;
|
||||
static std::unordered_map<GLint, std::unordered_map<GLint, GLint>> mapping;
|
||||
|
||||
static std::array<GLuint, gl::MAX_TEXTURES + 2> m_units;
|
||||
static GLint m_activeunit;
|
||||
};
|
||||
|
||||
typedef int texture_handle;
|
||||
@@ -97,9 +103,6 @@ public:
|
||||
texture_manager();
|
||||
~texture_manager() { delete_textures(); }
|
||||
|
||||
// activates specified texture unit
|
||||
void
|
||||
unit( GLint const Textureunit );
|
||||
// creates texture object out of data stored in specified file
|
||||
texture_handle
|
||||
create( std::string Filename, bool const Loadnow = true, GLint format_hint = GL_SRGB_ALPHA );
|
||||
@@ -112,7 +115,6 @@ public:
|
||||
// performs a resource sweep
|
||||
void
|
||||
update();
|
||||
void reset_unit_cache();
|
||||
// debug performance string
|
||||
std::string
|
||||
info() const;
|
||||
@@ -142,8 +144,6 @@ private:
|
||||
texturetimepointpair_sequence m_textures;
|
||||
index_map m_texturemappings;
|
||||
garbage_collector<texturetimepointpair_sequence> m_garbagecollector { m_textures, 600, 60, "texture" };
|
||||
std::array<texture_handle, 4> m_units;
|
||||
GLint m_activeunit { 0 };
|
||||
};
|
||||
|
||||
// reduces provided data image to half of original size, using basic 2x2 average
|
||||
|
||||
@@ -33,8 +33,7 @@ void gl::postfx::apply(opengl_texture &src, framebuffer *dst)
|
||||
|
||||
program.bind();
|
||||
vao->bind();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
src.bind();
|
||||
src.bind(0);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDepthMask(GL_FALSE);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
118
gl/shader.cpp
118
gl/shader.cpp
@@ -4,6 +4,7 @@
|
||||
#include <sstream>
|
||||
#include "shader.h"
|
||||
#include "glsl_common.h"
|
||||
#include "Logs.h"
|
||||
|
||||
inline bool strcend(std::string const &value, std::string const &ending)
|
||||
{
|
||||
@@ -60,7 +61,25 @@ std::unordered_map<std::string, gl::shader::components_e> gl::shader::components
|
||||
{ "sRGB_A", components_e::sRGB_A }
|
||||
};
|
||||
|
||||
void gl::shader::parse_config(std::string &str)
|
||||
std::unordered_map<std::string, gl::shader::defaultparam_e> gl::shader::defaultparams_mapping =
|
||||
{
|
||||
{ "required", defaultparam_e::required },
|
||||
{ "nan", defaultparam_e::nan },
|
||||
{ "none", defaultparam_e::zero },
|
||||
{ "one", defaultparam_e::one },
|
||||
{ "ambient", defaultparam_e::ambient },
|
||||
{ "diffuse", defaultparam_e::diffuse },
|
||||
{ "specular", defaultparam_e::specular }
|
||||
};
|
||||
|
||||
void gl::shader::process_source(std::string &str)
|
||||
{
|
||||
expand_includes(str);
|
||||
parse_texture_entries(str);
|
||||
parse_param_entries(str);
|
||||
}
|
||||
|
||||
void gl::shader::parse_texture_entries(std::string &str)
|
||||
{
|
||||
size_t start_pos = 0;
|
||||
|
||||
@@ -75,6 +94,7 @@ void gl::shader::parse_config(std::string &str)
|
||||
std::istringstream ss(str.substr(fp + 1, fe - fp - 1));
|
||||
std::string token;
|
||||
|
||||
std::string name;
|
||||
texture_entry conf;
|
||||
|
||||
size_t arg = 0;
|
||||
@@ -82,35 +102,103 @@ void gl::shader::parse_config(std::string &str)
|
||||
{
|
||||
std::istringstream token_ss(token);
|
||||
if (arg == 0)
|
||||
token_ss >> conf.name;
|
||||
token_ss >> name;
|
||||
else if (arg == 1)
|
||||
{
|
||||
token_ss >> conf.id;
|
||||
if (conf.id >= gl::MAX_TEXTURES)
|
||||
log_error("invalid texture binding: " + std::to_string(conf.id));
|
||||
}
|
||||
else if (arg == 2)
|
||||
{
|
||||
std::string comp;
|
||||
token_ss >> comp;
|
||||
conf.components = components_mapping[comp];
|
||||
if (components_mapping.find(comp) == components_mapping.end())
|
||||
log_error("unknown components: " + comp);
|
||||
else
|
||||
conf.components = components_mapping[comp];
|
||||
}
|
||||
arg++;
|
||||
}
|
||||
|
||||
if (arg == 3)
|
||||
texture_conf.push_back(conf);
|
||||
texture_conf.emplace(std::make_pair(name, conf));
|
||||
else
|
||||
log_error("invalid argument count to #texture");
|
||||
|
||||
str.erase(start_pos, fe - start_pos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void gl::shader::parse_param_entries(std::string &str)
|
||||
{
|
||||
size_t start_pos = 0;
|
||||
|
||||
std::string magic = "#param";
|
||||
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;
|
||||
|
||||
std::string name;
|
||||
param_entry conf;
|
||||
|
||||
size_t arg = 0;
|
||||
while (std::getline(ss, token, ','))
|
||||
{
|
||||
std::istringstream token_ss(token);
|
||||
if (arg == 0)
|
||||
token_ss >> name;
|
||||
else if (arg == 1)
|
||||
{
|
||||
token_ss >> conf.location;
|
||||
if (conf.location >= gl::MAX_PARAMS)
|
||||
log_error("invalid param binding: " + std::to_string(conf.location));
|
||||
}
|
||||
else if (arg == 2)
|
||||
token_ss >> conf.offset;
|
||||
else if (arg == 3)
|
||||
token_ss >> conf.size;
|
||||
else if (arg == 4)
|
||||
{
|
||||
std::string tok;
|
||||
token_ss >> tok;
|
||||
if (defaultparams_mapping.find(tok) == defaultparams_mapping.end())
|
||||
log_error("unknown param default: " + tok);
|
||||
conf.defaultparam = defaultparams_mapping[tok];
|
||||
}
|
||||
arg++;
|
||||
}
|
||||
|
||||
if (arg == 5)
|
||||
param_conf.emplace(std::make_pair(name, conf));
|
||||
else
|
||||
log_error("invalid argument count to #param");
|
||||
|
||||
str.erase(start_pos, fe - start_pos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void gl::shader::log_error(const std::string &str)
|
||||
{
|
||||
ErrorLog("bad shader: " + name + ": " + str, logtype::shader);
|
||||
}
|
||||
|
||||
gl::shader::shader(const std::string &filename)
|
||||
{
|
||||
name = filename;
|
||||
std::string str = read_file(filename);
|
||||
expand_includes(str);
|
||||
parse_config(str);
|
||||
process_source(str);
|
||||
|
||||
const GLchar *cstr = str.c_str();
|
||||
|
||||
if (!cstr[0])
|
||||
throw std::runtime_error("cannot read shader " + filename);
|
||||
throw shader_exception("cannot read shader: " + filename);
|
||||
|
||||
GLuint type;
|
||||
if (strcend(filename, ".vert"))
|
||||
@@ -118,7 +206,7 @@ gl::shader::shader(const std::string &filename)
|
||||
else if (strcend(filename, ".frag"))
|
||||
type = GL_FRAGMENT_SHADER;
|
||||
else
|
||||
throw std::runtime_error("unknown shader " + filename);
|
||||
throw shader_exception("unknown shader " + filename);
|
||||
|
||||
**this = glCreateShader(type);
|
||||
glShaderSource(*this, 1, &cstr, 0);
|
||||
@@ -130,7 +218,7 @@ gl::shader::shader(const std::string &filename)
|
||||
{
|
||||
GLchar info[512];
|
||||
glGetShaderInfoLog(*this, 512, 0, info);
|
||||
throw std::runtime_error("failed to compile " + filename + ": " + std::string(info));
|
||||
throw shader_exception("failed to compile " + filename + ": " + std::string(info));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -144,9 +232,10 @@ void gl::program::init()
|
||||
{
|
||||
bind();
|
||||
|
||||
for (shader::texture_entry &e : texture_conf)
|
||||
for (auto it : texture_conf)
|
||||
{
|
||||
GLuint loc = glGetUniformLocation(*this, e.name.c_str());
|
||||
shader::texture_entry &e = it.second;
|
||||
GLuint loc = glGetUniformLocation(*this, it.first.c_str());
|
||||
glUniform1i(loc, e.id);
|
||||
}
|
||||
|
||||
@@ -179,7 +268,10 @@ 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));
|
||||
for (auto it : s.texture_conf)
|
||||
texture_conf.emplace(std::make_pair(it.first, std::move(it.second)));
|
||||
for (auto it : s.param_conf)
|
||||
param_conf.emplace(std::make_pair(it.first, std::move(it.second)));
|
||||
glAttachShader(*this, *s);
|
||||
}
|
||||
|
||||
@@ -193,7 +285,7 @@ void gl::program::link()
|
||||
{
|
||||
GLchar info[512];
|
||||
glGetProgramInfoLog(*this, 512, 0, info);
|
||||
throw std::runtime_error("failed to link program: " + std::string(info));
|
||||
throw shader_exception("failed to link program: " + std::string(info));
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
53
gl/shader.h
53
gl/shader.h
@@ -9,6 +9,11 @@
|
||||
|
||||
namespace gl
|
||||
{
|
||||
class shader_exception : public std::runtime_error
|
||||
{
|
||||
using runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
class shader : public object
|
||||
{
|
||||
public:
|
||||
@@ -17,29 +22,56 @@ namespace gl
|
||||
|
||||
enum class components_e
|
||||
{
|
||||
R,
|
||||
RG,
|
||||
RGB,
|
||||
RGBA,
|
||||
sRGB,
|
||||
sRGB_A
|
||||
R = GL_R,
|
||||
RG = GL_RG,
|
||||
RGB = GL_RGB,
|
||||
RGBA = GL_RGBA,
|
||||
sRGB = GL_SRGB,
|
||||
sRGB_A = GL_SRGB_ALPHA
|
||||
};
|
||||
|
||||
struct texture_entry
|
||||
{
|
||||
std::string name;
|
||||
size_t id;
|
||||
components_e components;
|
||||
};
|
||||
|
||||
std::vector<texture_entry> texture_conf;
|
||||
enum class defaultparam_e
|
||||
{
|
||||
required,
|
||||
nan,
|
||||
zero,
|
||||
one,
|
||||
ambient,
|
||||
diffuse,
|
||||
specular
|
||||
};
|
||||
|
||||
struct param_entry
|
||||
{
|
||||
size_t location;
|
||||
size_t offset;
|
||||
size_t size;
|
||||
defaultparam_e defaultparam;
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, texture_entry> texture_conf;
|
||||
std::unordered_map<std::string, param_entry> param_conf;
|
||||
std::string name;
|
||||
|
||||
private:
|
||||
void process_source(std::string &str);
|
||||
|
||||
void expand_includes(std::string &str);
|
||||
void parse_config(std::string &str);
|
||||
void parse_texture_entries(std::string &str);
|
||||
void parse_param_entries(std::string &str);
|
||||
|
||||
std::string read_file(const std::string &filename);
|
||||
|
||||
static std::unordered_map<std::string, components_e> components_mapping;
|
||||
static std::unordered_map<std::string, defaultparam_e> defaultparams_mapping;
|
||||
|
||||
void log_error(const std::string &str);
|
||||
};
|
||||
|
||||
class program : public object, public bindable<program>
|
||||
@@ -55,7 +87,8 @@ namespace gl
|
||||
void attach(const shader &);
|
||||
void link();
|
||||
|
||||
std::vector<shader::texture_entry> texture_conf;
|
||||
std::unordered_map<std::string, shader::texture_entry> texture_conf;
|
||||
std::unordered_map<std::string, shader::param_entry> param_conf;
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
177
material.cpp
177
material.cpp
@@ -13,9 +13,11 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "renderer.h"
|
||||
#include "Globals.h"
|
||||
#include "utilities.h"
|
||||
#include "Logs.h"
|
||||
|
||||
bool
|
||||
opengl_material::deserialize( cParser &Input, bool const Loadnow ) {
|
||||
parse_info = std::make_unique<parse_info_s>();
|
||||
|
||||
bool result { false };
|
||||
while( true == deserialize_mapping( Input, 0, Loadnow ) ) {
|
||||
@@ -25,6 +27,112 @@ opengl_material::deserialize( cParser &Input, bool const Loadnow ) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void opengl_material::log_error(const std::string &str)
|
||||
{
|
||||
ErrorLog("bad material: " + name + ": " + str, logtype::material);
|
||||
}
|
||||
|
||||
void opengl_material::finalize(bool Loadnow)
|
||||
{
|
||||
if (!shader)
|
||||
{
|
||||
log_error("shader not specified, assuming \"default\"");
|
||||
shader = GfxRenderer.Fetch_Shader("default");
|
||||
}
|
||||
|
||||
if (parse_info)
|
||||
{
|
||||
for (auto it : parse_info->tex_mapping)
|
||||
{
|
||||
std::string key = it.first;
|
||||
std::string value = it.second.name;
|
||||
|
||||
if (key.size() > 0 && key[0] != '_')
|
||||
{
|
||||
size_t num = std::stoi(key) - 1;
|
||||
if (num < gl::MAX_TEXTURES)
|
||||
textures[num] = GfxRenderer.Fetch_Texture(value, Loadnow);
|
||||
else
|
||||
log_error("invalid texture binding: " + std::to_string(num));
|
||||
}
|
||||
else if (key.size() > 2)
|
||||
{
|
||||
key.erase(0, 1);
|
||||
key.pop_back();
|
||||
if (shader->texture_conf.find(key) != shader->texture_conf.end())
|
||||
textures[shader->texture_conf[key].id] = GfxRenderer.Fetch_Texture(value, Loadnow);
|
||||
else
|
||||
log_error("unknown texture binding: " + key);
|
||||
}
|
||||
else
|
||||
log_error("unrecognized texture binding: " + key);
|
||||
}
|
||||
|
||||
for (auto it : parse_info->param_mapping)
|
||||
{
|
||||
std::string key = it.first;
|
||||
glm::vec4 value = it.second.data;
|
||||
|
||||
if (key.size() > 1 && key[0] != '_')
|
||||
{
|
||||
size_t num = std::stoi(key) - 1;
|
||||
if (num < gl::MAX_PARAMS)
|
||||
params[num] = value;
|
||||
else
|
||||
log_error("invalid param binding: " + std::to_string(num));
|
||||
}
|
||||
else if (key.size() > 2)
|
||||
{
|
||||
key.erase(0, 1);
|
||||
key.pop_back();
|
||||
if (shader->param_conf.find(key) != shader->param_conf.end())
|
||||
{
|
||||
gl::shader::param_entry entry = shader->param_conf[key];
|
||||
for (size_t i = 0; i < entry.size; i++)
|
||||
params[entry.location][entry.offset + i] = value[i];
|
||||
}
|
||||
else
|
||||
log_error("unknown param binding: " + key);
|
||||
}
|
||||
else
|
||||
log_error("unrecognized param binding: " + key);
|
||||
}
|
||||
|
||||
parse_info.reset();
|
||||
}
|
||||
|
||||
for (auto it : shader->param_conf)
|
||||
{
|
||||
gl::shader::param_entry entry = it.second;
|
||||
if (std::isnan(params[entry.location][entry.offset]))
|
||||
{
|
||||
float value = std::numeric_limits<float>::quiet_NaN();
|
||||
if (entry.defaultparam == gl::shader::defaultparam_e::one)
|
||||
value = 1.0f;
|
||||
else if (entry.defaultparam == gl::shader::defaultparam_e::zero)
|
||||
value = 0.0f;
|
||||
else if (entry.defaultparam == gl::shader::defaultparam_e::required)
|
||||
log_error("unspecified required param: " + it.first);
|
||||
else if (entry.defaultparam != gl::shader::defaultparam_e::nan)
|
||||
{
|
||||
params_state.push_back(entry);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < entry.size; i++)
|
||||
params[entry.location][entry.offset + i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto it : shader->texture_conf)
|
||||
{
|
||||
gl::shader::texture_entry &entry = it.second;
|
||||
texture_handle handle = textures[entry.id];
|
||||
if (handle)
|
||||
GfxRenderer.Texture(handle).set_components_hint((GLint)entry.components);
|
||||
}
|
||||
}
|
||||
|
||||
// imports member data pair from the config file
|
||||
bool
|
||||
opengl_material::deserialize_mapping( cParser &Input, int const Priority, bool const Loadnow ) {
|
||||
@@ -52,6 +160,17 @@ opengl_material::deserialize_mapping( cParser &Input, int const Priority, bool c
|
||||
}
|
||||
else if (key.compare(0, 7, "texture") == 0) {
|
||||
key.erase(0, 7);
|
||||
|
||||
std::replace(value.begin(), value.end(), '\\', '/');
|
||||
auto it = parse_info->tex_mapping.find(key);
|
||||
if (it == parse_info->tex_mapping.end())
|
||||
parse_info->tex_mapping.emplace(std::make_pair(key, parse_info_s::tex_def({ value, Priority })));
|
||||
else if (Priority > it->second.priority)
|
||||
{
|
||||
parse_info->tex_mapping.erase(it);
|
||||
parse_info->tex_mapping.emplace(std::make_pair(key, parse_info_s::tex_def({ value, Priority })));
|
||||
}
|
||||
/*
|
||||
size_t num = std::stoi(key) - 1;
|
||||
if (num < textures.size() &&
|
||||
(textures[num] == null_handle || Priority > m_texture_priority[num]))
|
||||
@@ -60,9 +179,28 @@ opengl_material::deserialize_mapping( cParser &Input, int const Priority, bool c
|
||||
textures[num] = GfxRenderer.Fetch_Texture( value, Loadnow );
|
||||
m_texture_priority[num] = Priority;
|
||||
}
|
||||
*/
|
||||
}
|
||||
else if (key.compare(0, 7, "param") == 0) {
|
||||
else if (key.compare(0, 5, "param") == 0) {
|
||||
key.erase(0, 5);
|
||||
|
||||
std::istringstream stream(value);
|
||||
glm::vec4 data;
|
||||
stream >> data.r;
|
||||
stream >> data.g;
|
||||
stream >> data.b;
|
||||
stream >> data.a;
|
||||
|
||||
auto it = parse_info->param_mapping.find(key);
|
||||
if (it == parse_info->param_mapping.end())
|
||||
parse_info->param_mapping.emplace(std::make_pair(key, parse_info_s::param_def({ data, Priority })));
|
||||
else if (Priority > it->second.priority)
|
||||
{
|
||||
parse_info->param_mapping.erase(it);
|
||||
parse_info->param_mapping.emplace(std::make_pair(key, parse_info_s::param_def({ data, Priority })));
|
||||
}
|
||||
|
||||
/*
|
||||
size_t num = std::stoi(key) - 1;
|
||||
if (num < params.size() &&
|
||||
(Priority > m_param_priority[num]))
|
||||
@@ -75,13 +213,20 @@ opengl_material::deserialize_mapping( cParser &Input, int const Priority, bool c
|
||||
stream >> params[num].a;
|
||||
|
||||
m_param_priority[num] = Priority;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
else if (key == "shader:" &&
|
||||
(!shader || Priority > m_shader_priority))
|
||||
{
|
||||
shader = GfxRenderer.Fetch_Shader(value);
|
||||
m_shader_priority = Priority;
|
||||
try
|
||||
{
|
||||
shader = GfxRenderer.Fetch_Shader(value);
|
||||
m_shader_priority = Priority;
|
||||
}
|
||||
catch (gl::shader_exception const &e)
|
||||
{
|
||||
log_error("invalid shader: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
else if (key == "opacity:" &&
|
||||
Priority > m_opacity_priority)
|
||||
@@ -134,16 +279,6 @@ float opengl_material::get_or_guess_opacity()
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
std::unordered_map<gl::shader::components_e, GLint> material_manager::components_mapping =
|
||||
{
|
||||
{ gl::shader::components_e::R, GL_RED },
|
||||
{ gl::shader::components_e::RG, GL_RG },
|
||||
{ gl::shader::components_e::RGB, GL_RGB },
|
||||
{ gl::shader::components_e::RGBA, GL_RGBA },
|
||||
{ gl::shader::components_e::sRGB, GL_SRGB },
|
||||
{ gl::shader::components_e::sRGB_A, GL_SRGB_ALPHA }
|
||||
};
|
||||
|
||||
// create material object from data stored in specified file.
|
||||
// NOTE: the deferred load parameter is passed to textures defined by material, the material itself is always loaded immediately
|
||||
material_handle
|
||||
@@ -190,19 +325,11 @@ material_manager::create( std::string const &Filename, bool const Loadnow ) {
|
||||
material.name = filename;
|
||||
}
|
||||
|
||||
if (!material.shader)
|
||||
material.shader = GfxRenderer.Fetch_Shader("default");
|
||||
|
||||
for (gl::shader::texture_entry &entry : material.shader->texture_conf)
|
||||
{
|
||||
material_handle handle = material.textures[entry.id];
|
||||
if (handle)
|
||||
GfxRenderer.Texture(handle).set_components_hint(components_mapping[entry.components]);
|
||||
}
|
||||
material.finalize(Loadnow);
|
||||
|
||||
material_handle handle = m_materials.size();
|
||||
m_materials.emplace_back( material );
|
||||
m_materialmappings.emplace( material.name, handle );
|
||||
m_materialmappings.emplace(material.name, handle);
|
||||
m_materials.emplace_back( std::move(material) );
|
||||
return handle;
|
||||
};
|
||||
|
||||
|
||||
30
material.h
30
material.h
@@ -19,10 +19,9 @@ typedef int material_handle;
|
||||
// a collection of parameters for the rendering setup.
|
||||
// for modern opengl this translates to set of attributes for shaders
|
||||
struct opengl_material {
|
||||
// primary texture, typically diffuse+apha
|
||||
// secondary texture, typically normal+reflection
|
||||
std::array<texture_handle, gl::MAX_TEXTURES> textures = { null_handle };
|
||||
std::array<glm::vec4, gl::MAX_PARAMS> params = { glm::vec4() };
|
||||
std::array<glm::vec4, gl::MAX_PARAMS> params = { glm::vec4(std::numeric_limits<float>::quiet_NaN()) };
|
||||
std::vector<gl::shader::param_entry> params_state;
|
||||
|
||||
std::shared_ptr<gl::program> shader;
|
||||
float opacity = std::numeric_limits<float>::quiet_NaN();
|
||||
@@ -36,21 +35,38 @@ struct opengl_material {
|
||||
// methods
|
||||
bool
|
||||
deserialize( cParser &Input, bool const Loadnow );
|
||||
void finalize(bool Loadnow);
|
||||
float get_or_guess_opacity();
|
||||
|
||||
private:
|
||||
// methods
|
||||
// imports member data pair from the config file, overriding existing parameter values of lower priority
|
||||
// imports member data pair from the config file
|
||||
bool
|
||||
deserialize_mapping( cParser &Input, int const Priority, bool const Loadnow );
|
||||
void log_error(const std::string &str);
|
||||
|
||||
// members
|
||||
// priorities for textures, shader, opacity
|
||||
int m_shader_priority = -1;
|
||||
int m_opacity_priority = -1;
|
||||
int m_selfillum_priority = -1;
|
||||
std::array<int, gl::MAX_TEXTURES> m_texture_priority = { -1 };
|
||||
std::array<int, gl::MAX_PARAMS> m_param_priority = { -1 };
|
||||
|
||||
struct parse_info_s
|
||||
{
|
||||
struct tex_def
|
||||
{
|
||||
std::string name;
|
||||
int priority;
|
||||
};
|
||||
struct param_def
|
||||
{
|
||||
glm::vec4 data;
|
||||
int priority;
|
||||
};
|
||||
std::unordered_map<std::string, tex_def> tex_mapping;
|
||||
std::unordered_map<std::string, param_def> param_mapping;
|
||||
};
|
||||
std::unique_ptr<parse_info_s> parse_info;
|
||||
};
|
||||
|
||||
class material_manager {
|
||||
@@ -79,8 +95,6 @@ private:
|
||||
// members:
|
||||
material_sequence m_materials;
|
||||
index_map m_materialmappings;
|
||||
|
||||
static std::unordered_map<gl::shader::components_e, GLint> components_mapping;
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
120
renderer.cpp
120
renderer.cpp
@@ -136,7 +136,23 @@ bool opengl_renderer::Init(GLFWwindow *Window)
|
||||
{{-size, size, 0.f}, glm::vec3(), {1.f, 1.f}}, {{size, size, 0.f}, glm::vec3(), {0.f, 1.f}}, {{-size, -size, 0.f}, glm::vec3(), {1.f, 0.f}}, {{size, -size, 0.f}, glm::vec3(), {0.f, 0.f}}},
|
||||
geometrybank, GL_TRIANGLE_STRIP);
|
||||
|
||||
m_vertex_shader = std::make_unique<gl::shader>("vertex.vert");
|
||||
try
|
||||
{
|
||||
m_vertex_shader = std::make_unique<gl::shader>("vertex.vert");
|
||||
m_line_shader = make_shader("traction.vert", "traction.frag");
|
||||
m_freespot_shader = make_shader("freespot.vert", "freespot.frag");
|
||||
m_shadow_shader = make_shader("simpleuv.vert", "shadowmap.frag");
|
||||
m_alpha_shadow_shader = make_shader("simpleuv.vert", "alphashadowmap.frag");
|
||||
m_pick_shader = make_shader("vertexonly.vert", "pick.frag");
|
||||
m_billboard_shader = make_shader("simpleuv.vert", "billboard.frag");
|
||||
m_invalid_material = Fetch_Material("invalid");
|
||||
}
|
||||
catch (gl::shader_exception const &e)
|
||||
{
|
||||
ErrorLog("invalid shader: " + std::string(e.what()));
|
||||
return false;
|
||||
}
|
||||
|
||||
scene_ubo = std::make_unique<gl::ubo>(sizeof(gl::scene_ubs), 0);
|
||||
model_ubo = std::make_unique<gl::ubo>(sizeof(gl::model_ubs), 1);
|
||||
light_ubo = std::make_unique<gl::ubo>(sizeof(gl::light_ubs), 2);
|
||||
@@ -151,15 +167,6 @@ bool opengl_renderer::Init(GLFWwindow *Window)
|
||||
model_ubo->update(model_ubs);
|
||||
scene_ubo->update(scene_ubs);
|
||||
|
||||
m_line_shader = make_shader("traction.vert", "traction.frag");
|
||||
m_freespot_shader = make_shader("freespot.vert", "freespot.frag");
|
||||
m_shadow_shader = make_shader("simpleuv.vert", "shadowmap.frag");
|
||||
m_alpha_shadow_shader = make_shader("simpleuv.vert", "alphashadowmap.frag");
|
||||
m_pick_shader = make_shader("vertexonly.vert", "pick.frag");
|
||||
m_billboard_shader = make_shader("simpleuv.vert", "billboard.frag");
|
||||
|
||||
m_invalid_material = Fetch_Material("invalid");
|
||||
|
||||
int samples = 1 << Global.iMultisampling;
|
||||
if (samples > 1)
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
@@ -428,7 +435,7 @@ void opengl_renderer::Render_pass(rendermode const Mode)
|
||||
glEnable(GL_FRAMEBUFFER_SRGB);
|
||||
glViewport(0, 0, Global.iWindowWidth, Global.iWindowHeight);
|
||||
m_pfx->apply(*m_main_tex, nullptr);
|
||||
m_textures.reset_unit_cache();
|
||||
opengl_texture::reset_unit_cache();
|
||||
glDisable(GL_FRAMEBUFFER_SRGB);
|
||||
|
||||
glDebug("uilayer render");
|
||||
@@ -877,13 +884,10 @@ void opengl_renderer::setup_drawing(bool const Alpha)
|
||||
// configures shadow texture unit for specified shadow map and conersion matrix
|
||||
void opengl_renderer::setup_shadow_map(opengl_texture *tex, renderpass_config conf)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + gl::MAX_TEXTURES + 0);
|
||||
if (tex)
|
||||
tex->bind();
|
||||
else
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_textures.reset_unit_cache();
|
||||
if (tex)
|
||||
tex->bind(gl::MAX_TEXTURES + 0);
|
||||
else
|
||||
opengl_texture::unbind(gl::MAX_TEXTURES + 0);
|
||||
|
||||
if (tex)
|
||||
{
|
||||
@@ -900,14 +904,17 @@ void opengl_renderer::setup_shadow_map(opengl_texture *tex, renderpass_config co
|
||||
void opengl_renderer::setup_env_map(gl::cubemap *tex)
|
||||
{
|
||||
if (tex)
|
||||
{
|
||||
tex->bind(GL_TEXTURE0 + gl::MAX_TEXTURES + 1);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
else
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + gl::MAX_TEXTURES + 1);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_textures.reset_unit_cache();
|
||||
opengl_texture::reset_unit_cache();
|
||||
}
|
||||
|
||||
void opengl_renderer::setup_environment_light(TEnvironmentType const Environment)
|
||||
@@ -975,7 +982,8 @@ bool opengl_renderer::Render(world_environment *Environment)
|
||||
// celestial bodies
|
||||
float const duskfactor = 1.0f - clamp(std::abs(Environment->m_sun.getAngle()), 0.0f, 12.0f) / 12.0f;
|
||||
glm::vec3 suncolor = interpolate(glm::vec3(255.0f / 255.0f, 242.0f / 255.0f, 231.0f / 255.0f), glm::vec3(235.0f / 255.0f, 140.0f / 255.0f, 36.0f / 255.0f), duskfactor);
|
||||
m_textures.reset_unit_cache();
|
||||
|
||||
opengl_texture::reset_unit_cache();
|
||||
|
||||
// m7t: restore celestial bodies
|
||||
|
||||
@@ -1081,15 +1089,33 @@ std::shared_ptr<gl::program> opengl_renderer::Fetch_Shader(const std::string &na
|
||||
return m_shaders[name];
|
||||
}
|
||||
|
||||
void opengl_renderer::Bind_Material(material_handle const Material)
|
||||
void opengl_renderer::Bind_Material(material_handle const Material, TSubModel *sm)
|
||||
{
|
||||
if (Material != null_handle)
|
||||
{
|
||||
auto &material = m_materials.material(Material);
|
||||
for (size_t i = 0; i < gl::MAX_PARAMS; i++)
|
||||
model_ubs.param[i] = material.params[i];
|
||||
|
||||
// if for some reason material don't have opacity set, guess it based on render phase
|
||||
memcpy(&model_ubs.param[0], &material.params[0], sizeof(model_ubs.param));
|
||||
|
||||
for (size_t i = 0; i < material.params_state.size(); i++)
|
||||
{
|
||||
gl::shader::param_entry entry = material.params_state[i];
|
||||
|
||||
glm::vec4 src;
|
||||
if (entry.defaultparam == gl::shader::defaultparam_e::ambient)
|
||||
src = sm->f4Ambient;
|
||||
else if (entry.defaultparam == gl::shader::defaultparam_e::diffuse)
|
||||
src = sm->f4Diffuse;
|
||||
else if (entry.defaultparam == gl::shader::defaultparam_e::specular)
|
||||
src = sm->f4Specular;
|
||||
else
|
||||
continue;
|
||||
|
||||
for (size_t j = 0; j < entry.size; j++)
|
||||
model_ubs.param[entry.location][entry.offset + j] = src[j];
|
||||
}
|
||||
|
||||
// if material don't have opacity set, guess it based on render phase
|
||||
if (std::isnan(material.opacity))
|
||||
model_ubs.opacity = m_blendingenabled ? 0.0f : 0.5f;
|
||||
else
|
||||
@@ -1097,14 +1123,32 @@ void opengl_renderer::Bind_Material(material_handle const Material)
|
||||
|
||||
material.shader->bind();
|
||||
|
||||
size_t unit = 0;
|
||||
for (auto &tex : material.textures)
|
||||
{
|
||||
if (tex == null_handle)
|
||||
break;
|
||||
m_textures.bind(unit, tex);
|
||||
unit++;
|
||||
}
|
||||
if (GLEW_ARB_multi_bind)
|
||||
{
|
||||
GLuint textures[gl::MAX_TEXTURES] = { 0 };
|
||||
size_t i;
|
||||
for (i = 0; i < gl::MAX_TEXTURES; i++)
|
||||
if (material.textures[i] != null_handle)
|
||||
{
|
||||
opengl_texture &tex = m_textures.texture(material.textures[i]);
|
||||
tex.create();
|
||||
textures[i] = tex.id;
|
||||
}
|
||||
else
|
||||
break;
|
||||
glBindTextures(0, i, textures);
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t unit = 0;
|
||||
for (auto &tex : material.textures)
|
||||
{
|
||||
if (tex == null_handle)
|
||||
break;
|
||||
m_textures.bind(unit, tex);
|
||||
unit++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (Material != m_invalid_material)
|
||||
Bind_Material(m_invalid_material);
|
||||
@@ -2838,7 +2882,6 @@ void opengl_renderer::Render_Alpha(TSubModel *Submodel)
|
||||
|
||||
m_billboard_shader->bind();
|
||||
Bind_Texture(0, m_glaretexture);
|
||||
m_textures.reset_unit_cache();
|
||||
model_ubs.param[0] = glm::vec4(glm::vec3(Submodel->f4Diffuse), glarelevel);
|
||||
|
||||
// main draw call
|
||||
@@ -3200,6 +3243,13 @@ bool opengl_renderer::Init_caps()
|
||||
const char *ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
|
||||
WriteLog(ext);
|
||||
}
|
||||
WriteLog("--------");
|
||||
|
||||
if (GLEW_ARB_multi_bind)
|
||||
WriteLog("ARB_multi_bind supported!");
|
||||
|
||||
if (GLEW_ARB_direct_state_access)
|
||||
WriteLog("ARB_direct_state_access supported!");
|
||||
|
||||
// ograniczenie maksymalnego rozmiaru tekstur - parametr dla skalowania tekstur
|
||||
{
|
||||
|
||||
@@ -150,7 +150,7 @@ class opengl_renderer
|
||||
gfx::vertex_array const &Vertices(gfx::geometry_handle const &Geometry) const;
|
||||
// material methods
|
||||
material_handle Fetch_Material(std::string const &Filename, bool const Loadnow = true);
|
||||
void Bind_Material(material_handle const Material);
|
||||
void Bind_Material(material_handle const Material, TSubModel *sm = nullptr);
|
||||
void Bind_Material_Shadow(material_handle const Material);
|
||||
|
||||
// shader methods
|
||||
|
||||
@@ -12,6 +12,10 @@ uniform sampler2D tex1;
|
||||
#texture (tex2, 1, RGB)
|
||||
uniform sampler2D tex2;
|
||||
|
||||
#param(color, 0, 0, 1, one)
|
||||
#param(color2, 0, 1, 1, zero)
|
||||
#param(color3, 0, 2, 1, one)
|
||||
|
||||
#include <common>
|
||||
|
||||
vec3 apply_fog(vec3 color)
|
||||
|
||||
Reference in New Issue
Block a user