16 default:
throw std::runtime_error(
"Not supported VBO type!");
94 const float3* tangents,
int vert_count,
const int3* indx,
int indx_count)
99 _vertex_count(vert_count)
108 if (uvs)
_uvs.
upload(1, (
float*)uvs, 2, vert_count);
114 return std::unique_ptr<vao>(
new vao(mesh.
positions.data(),
205 "attribute vec3 position;\n" 206 "attribute vec2 textureCoords;\n" 207 "varying vec2 textCoords;\n" 208 "uniform vec2 elementPosition;\n" 209 "uniform vec2 elementScale;\n" 212 " gl_Position = vec4(position * vec3(elementScale, 1.0) + vec3(elementPosition, 0.0), 1.0);\n" 213 " textCoords = textureCoords;\n" 218 "varying vec2 textCoords;\n" 219 "uniform sampler2D textureSampler;\n" 220 "uniform float opacity;\n" 221 "uniform vec2 rayOrigin;\n" 222 "uniform float power;\n" 223 "void main(void) {\n" 224 " vec4 FragColor = texture2D(textureSampler, textCoords);\n" 225 " int samples = 120;\n" 226 " vec2 delta = vec2(textCoords - rayOrigin);\n" 227 " delta *= 1.0 / float(samples) * 0.99;" 228 " vec2 coord = textCoords;\n" 229 " float illuminationDecay = power;\n" 230 " for(int i=0; i < samples ; i++)\n" 233 " vec4 texel = texture2D(textureSampler, coord);\n" 234 " texel *= illuminationDecay * 0.4;\n" 235 " texel.x *= 80.0 / 255.0;\n" 236 " texel.y *= 99.0 / 255.0;\n" 237 " texel.z *= 115.0 / 255.0;\n" 238 " FragColor += texel;\n" 239 " illuminationDecay *= power;\n" 241 " FragColor = clamp(FragColor, 0.0, 1.0);\n" 242 " gl_FragColor = vec4(FragColor.xyz, opacity);\n" 247 "varying vec2 textCoords;\n" 248 "uniform sampler2D textureSampler;\n" 249 "uniform float opacity;\n" 250 "void main(void) {\n" 251 " vec2 tex = vec2(textCoords.x, 1.0 - textCoords.y);\n" 252 " vec4 color = texture2D(textureSampler, tex);\n" 253 " gl_FragColor = vec4(color.xyz, opacity);\n" 282 tex_2d_shader->begin();
283 tex_2d_shader->set_opacity(opacity);
284 tex_2d_shader->end();
285 draw_texture({ 0.f, 0.f }, { 1.f, 1.f }, tex);
294 tex_2d_shader->begin();
295 tex_2d_shader->set_opacity(opacity);
296 tex_2d_shader->end();
297 draw_texture({ 0.f, 0.f }, { 1.0f, 1.0f }, tex1, tex2);
311 _shader->load_uniform(_rays_location, center);
316 _shader->load_uniform(_power_location, power);
325 auto texture0_sampler_location =
_shader->get_uniform_location(
"textureSampler");
328 _shader->load_uniform(texture0_sampler_location, 0);
391 res.
uvs.emplace_back(
float2{ 0.f, 1.f });
392 res.
uvs.emplace_back(
float2{ 1.f, 1.f });
393 res.
uvs.emplace_back(
float2{ 1.f, 0.f });
394 res.
uvs.emplace_back(
float2{ 0.f, 0.f });
426 load_uniform(location, value ? 1.
f : 0.
f);
456 auto lambda = [&]() {
462 throw std::runtime_error(
"Unknown shader type!");
465 const auto gl_type = lambda();
469 char const * source_ptr = shader_code.c_str();
470 int length = int(shader_code.size());
480 if ((result ==
GL_FALSE) && (log_length > 0)) {
481 std::vector<char> error_message(log_length + 1);
486 throw std::runtime_error(error);
513 _shaders.push_back(&shader);
517 for (
auto ps : _shaders)
529 if ((result ==
GL_FALSE) && (log_length > 0)) {
530 std::vector<char> error_message(log_length + 1);
534 for (
auto ps : _shaders)
538 throw std::runtime_error(error);
545 if ((result ==
GL_FALSE) && (log_length > 0)) {
546 std::vector<char> error_message(log_length + 1);
550 for (
auto ps : _shaders)
554 throw std::runtime_error(error);
557 for (
auto ps : _shaders)
588 res->attach(fragment);
679 else if (
s == 0x8CD9) res =
"GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS";
693 std::stringstream ss;
695 bool has_errors =
false;
707 default: error=
"Unknown";
break;
710 ss <<
"GL_" << error.c_str() <<
" - " << file <<
":" << line <<
"\n";
717 auto error = ss.str();
718 throw std::runtime_error(
error);
void set_ray_center(const float2 ¢er)
#define GL_FRAMEBUFFER_COMPLETE
std::vector< float3 > positions
#define glGetShaderInfoLog
#define GL_TEXTURE_MAG_FILTER
void bind_attribute(int attr, const std::string &name)
GLuint const GLchar * name
static const char * vertex_shader_text
static std::unique_ptr< vao > create(const obj_mesh &m)
#define glFramebufferTexture2D
#define glBindRenderbuffer
#define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
#define GL_DEPTH_COMPONENT32
GLenum GLenum GLenum GLenum GLenum scale
std::unique_ptr< shader_program > _shader
GLuint64 GLenum void * handle
#define glDeleteRenderbuffers
void createDepthBufferAttachment()
#define glCheckFramebufferStatus
std::vector< float3 > tangents
#define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
#define glUniformMatrix4fv
GLdouble GLdouble GLdouble w
GLsizei const GLchar *const * string
#define GL_DEPTH_ATTACHMENT
#define glGenRenderbuffers
#define GL_VALIDATE_STATUS
GLfloat GLfloat GLfloat GLfloat h
static const char * default_vertex_shader()
void attach(const shader &shader)
#define glBindFragDataLocation
void set_position_and_scale(const float2 &position, const float2 &scale)
#define glDeleteVertexArrays
#define GL_INFO_LOG_LENGTH
#define glGenVertexArrays
#define glVertexAttribPointer
std::vector< float3 > normals
shader(const std::string &code, shader_type type)
#define glEnableVertexAttribArray
#define glDeleteFramebuffers
#define glBindVertexArray
#define glGenFramebuffers
uint32_t _opacity_location
#define glBindFramebuffer
std::vector< float2 > uvs
void update_positions(const float3 *vert)
#define GL_FRAMEBUFFER_UNSUPPORTED
void createDepthTextureAttachment(uint32_t texture)
#define glDisableVertexAttribArray
#define GL_TEXTURE_MIN_FILTER
void load_uniform(int location, float value)
#define glGetUniformLocation
#define GL_FRAGMENT_SHADER
void draw_indexed_triangles()
void draw(texture_2d_shader &shader, uint32_t tex)
static const char * fragment_shader_text
#define GL_ONE_MINUS_SRC_ALPHA
#define glRenderbufferStorage
int get_uniform_location(const std::string &name)
#define glValidateProgram
#define glBindAttribLocation
static obj_mesh create_mesh()
#define GL_INVALID_FRAMEBUFFER_OPERATION
std::vector< int3 > indexes
static std::unique_ptr< shader_program > load(const std::string &vertex_shader, const std::string &fragment_shader, const char *input0=nullptr, const char *input1=nullptr, const char *output0=nullptr, const char *output1=nullptr)
void upload(int attribute, const float *xyz, int size, int count, bool dynamic=false)
void createTextureAttachment(uint32_t texture)
#define GL_COMPILE_STATUS
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
#define glGetProgramInfoLog
uint32_t _position_location
vbo(vbo_type type=vbo_type::array_buffer)
#define GL_ELEMENT_ARRAY_BUFFER
#define GL_COLOR_ATTACHMENT0
static const char * splash_shader_text
GLenum GLuint GLenum GLsizei length
void _check_gl_error(const char *file, int line)
static int convert_type(vbo_type type)
#define GL_INVALID_OPERATION
#define GL_DEPTH_COMPONENT
void set_opacity(float opacity)
void draw_texture(uint32_t tex, float opacity=1.f)
#define glFramebufferRenderbuffer
vao(const float3 *vert, const float2 *uvs, const float3 *normals, const float3 *tangents, int vert_count, const int3 *indx, int indx_count)
void set_power(float power)