Commit e719db25 authored by Jerry's avatar Jerry
Browse files

Merge branch 'develop' into feature/cmake

parents 250e8fbd 50b5a4bb
...@@ -9,8 +9,8 @@ debug_render : false ...@@ -9,8 +9,8 @@ debug_render : false
shadow_resolution : 256 shadow_resolution : 256
shadows : true shadows : true
camX : 0 camX : 0
camY : 34 camY : 45
camZ : 55 camZ : 65
camAngle : -50 camAngle : -50
victim_damping : 1 victim_damping : 1
victim_mass : 1 victim_mass : 1
......
No preview for this file type
#version 330
in vec3 direction_;
in vec3 color0_;
in vec3 color1_;
out vec4 frag_color;
uniform vec3 light_direction;
uniform float g;
uniform float g_squared;
void main() {
float alpha = dot(light_direction, direction_) / length(direction_);
float mie_phase = 1.5f * ((1.f - g_squared) / (2.f + g_squared)) * (1.f + alpha * alpha) / pow(1.f + g_squared - 2.f * g * alpha, 1.5f);
frag_color = vec4(color0_ + mie_phase * color1_, 1.f);
frag_color.a = frag_color.b;
}
#version 330
in vec3 _position;
in vec2 _texcoord;
in vec3 _normal;
in vec3 _tangent;
out vec3 direction_;
out vec2 texcoord_;
out vec3 color0_;
out vec3 color1_;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform vec3 camera_position;
uniform vec3 light_direction;
uniform vec3 inverse_wave_length;
uniform float camera_height;
uniform float camera_height_squared;
uniform float outer_radius;
uniform float outer_radius_squared;
uniform float inner_radius;
uniform float inner_radius_squared;
uniform float rayleigh_times_energy;
uniform float mie_times_energy;
uniform float rayleigh_times_four_pi;
uniform float mie_times_four_pi;
uniform float scale;
uniform float scale_depth;
uniform float scale_over_scale_depth;
const int samples_integral = 2;
const float samples_float = 2.f;
float scale_angle(float alpha) {
float x = 1.f - alpha;
return scale_depth * exp(-0.00287f + x * (0.459f + x * (3.83f + x * (-6.80f + x * 5.25f))));
}
void main() {
vec3 ray = _position - camera_position;
float far = length(ray);
ray /= far;
float b = 2.f * dot(camera_position, ray);
float c = camera_height_squared - outer_radius_squared;
float det = max(0.f, b * b - 4.f * c);
float near = 0.5f * (-b - sqrt(det));
vec3 start = camera_position + ray * near;
far -= near;
float start_angle = dot(ray, start) / outer_radius;
float start_depth = exp(-1.f / scale_depth);
float start_offset = start_depth * start_angle;
float sample_length = far / samples_float;
float scaled_length = sample_length * scale;
vec3 sample_ray = ray * sample_length;
vec3 sample_point = start + sample_ray * 0.5f;
vec3 color0 = vec3(0.f);
for (int i = 0; i < samples_integral; ++i) {
float height = length(sample_point);
float depth = exp(scale_over_scale_depth * (inner_radius - height));
float camera_angle = dot(ray, sample_point) / height;
float light_angle = dot(light_direction, sample_point) / height;
float scatter = (start_offset + depth * (light_angle - camera_angle));
vec3 color1 = exp(-scatter * (inverse_wave_length * rayleigh_times_four_pi + mie_times_four_pi));
color0 += color1 * (depth * scaled_length);
sample_point += sample_ray;
}
gl_Position = projection * view * model * vec4(_position, 1.f);
texcoord_ = _texcoord;
color0_ = color0 * (inverse_wave_length * rayleigh_times_energy);
color1_ = color0 * mie_times_energy;
direction_ = camera_position - _position;
}
#version 330
in vec2 texcoord_;
in vec3 color0_;
in vec3 color1_;
out vec4 frag_color;
uniform sampler2D albedo_texture;
void main() {
frag_color = vec4(color0_ + texture(albedo_texture, texcoord_).rgb * color1_, 1.f);
}
#version 330
in vec3 _position;
in vec2 _texcoord;
in vec3 _normal;
in vec3 _tangent;
out vec2 texcoord_;
out vec3 color0_;
out vec3 color1_;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform vec3 camera_position;
uniform vec3 light_direction;
uniform vec3 inverse_wave_length;
uniform float camera_height;
uniform float camera_height_squared;
uniform float outer_radius;
uniform float outer_radius_squared;
uniform float inner_radius;
uniform float inner_radius_squared;
uniform float rayleigh_times_energy;
uniform float mie_times_energy;
uniform float rayleigh_times_four_pi;
uniform float mie_times_four_pi;
uniform float scale;
uniform float scale_depth;
uniform float scale_over_scale_depth;
const int samples_integral = 2;
const float samples_float = 2.f;
float scale_angle(float alpha) {
float x = 1.f - alpha;
return scale_depth * exp(-0.00287f + x * (0.459f + x * (3.83f + x * (-6.80f + x * 5.25f))));
}
void main() {
vec3 ray = _position - camera_position;
float far = length(ray);
ray /= far;
float b = 2.f * dot(camera_position, ray);
float c = camera_height_squared - outer_radius_squared;
float det = max(0.f, b * b - 4.f * c);
float near = 0.5f * (-b - sqrt(det));
vec3 start = camera_position + ray * near;
far -= near;
float depth = exp((inner_radius - outer_radius) / scale_depth);
float camera_angle = dot(-ray, _position) / length(_position);
float light_angle = dot(light_direction, _position) / length(_position);
float camera_scale = scale_angle(camera_angle);
float light_scale = scale_angle(light_angle);
float camera_offset = depth * camera_scale;
float temp = (light_scale + camera_scale);
float sample_length = far / samples_float;
float scaled_length = sample_length * scale;
vec3 sample_ray = ray * sample_length;
vec3 sample_point = start + sample_ray * 0.5f;
vec3 color0 = vec3(0.f);
vec3 color1 = vec3(0.f);
for (int i = 0; i < samples_integral; ++i) {
float height = length(sample_point);
float depth = exp(scale_over_scale_depth * (inner_radius - height));
float scatter = depth * temp - camera_offset;
color1 = exp(-scatter * (inverse_wave_length * rayleigh_times_four_pi + mie_times_four_pi));
color0 += color1 * (depth * scaled_length);
sample_point += sample_ray;
}
gl_Position = projection * view * model * vec4(_position, 1.f);
texcoord_ = _texcoord;
color0_ = color0 * (inverse_wave_length * rayleigh_times_energy + mie_times_energy);
color1_ = color1;
}
...@@ -25,20 +25,20 @@ vec3 blinn_phong(vec3 N, vec3 L, vec3 V, vec3 light_color, vec3 diff_color, floa ...@@ -25,20 +25,20 @@ vec3 blinn_phong(vec3 N, vec3 L, vec3 V, vec3 light_color, vec3 diff_color, floa
} }
void main() { void main() {
float depth = 2.0 * texture(depth_texture, gl_FragCoord.xy / resolution).x - 1.0; float depth = 2.0 * texture(depth_texture, gl_FragCoord.xy / resolution).x - 1.0;
vec3 clip_space; vec3 clip_space;
clip_space.xy = 2.0 * gl_FragCoord.xy / resolution - 1.0; clip_space.xy = 2.0 * gl_FragCoord.xy / resolution - 1.0;
clip_space.z = depth; clip_space.z = depth;
vec4 world_space = inv_view_projection * vec4(clip_space,1.0); vec4 world_space = inv_view_projection * vec4(clip_space,1.0);
vec3 p = world_space.xyz / world_space.w; vec3 p = world_space.xyz / world_space.w;
vec3 N = normalize(texture(normal_texture, gl_FragCoord.xy / resolution).xyz); vec3 N = normalize(texture(normal_texture, gl_FragCoord.xy / resolution).xyz);
vec3 V = normalize(view_vector - p); vec3 V = normalize(view_vector - p);
vec3 diffuse_color = texture(albedo_texture, gl_FragCoord.xy / resolution).rgb; vec3 diffuse_color = texture(albedo_texture, gl_FragCoord.xy / resolution).rgb;
float specular_exponent = texture(specular_texture, gl_FragCoord.xy / resolution).r * 128; float specular_exponent = texture(specular_texture, gl_FragCoord.xy / resolution).r * 128;
float emission = texture(specular_texture, gl_FragCoord.xy / resolution).g; float emission = texture(specular_texture, gl_FragCoord.xy / resolution).g;
vec3 L = point_light_position - p; vec3 L = point_light_position - p;
float d = length(L); float d = length(L);
float r = point_light_radius; float r = point_light_radius;
......
...@@ -8,9 +8,10 @@ out float _size_; ...@@ -8,9 +8,10 @@ out float _size_;
out vec3 _color_; out vec3 _color_;
uniform mat4 view; uniform mat4 view;
uniform mat4 model;
void main() { void main() {
vec4 position = view * vec4(_position, 1.f); vec4 position = view * model * vec4(_position, 1.f);
gl_Position = position; gl_Position = position;
_size_ = _size; _size_ = _size;
_color_ = _color; _color_ = _color;
......
...@@ -88,6 +88,12 @@ namespace gdw { ...@@ -88,6 +88,12 @@ namespace gdw {
unsigned int starfield_size_ = 0; unsigned int starfield_size_ = 0;
std::shared_ptr<const texture> star_texture_; std::shared_ptr<const texture> star_texture_;
std::shared_ptr<const mesh> planet_;
program planet_program_;
program athmosphere_program_;
glm::mat4 planet_model_;
glm::mat4 athmosphere_model_;
std::unordered_map<unsigned long long, camera_component*> camera_components_; std::unordered_map<unsigned long long, camera_component*> camera_components_;
std::vector<directional_light_component*> directional_light_components_; std::vector<directional_light_component*> directional_light_components_;
std::vector<light_component*> light_components_; std::vector<light_component*> light_components_;
...@@ -153,7 +159,8 @@ namespace gdw { ...@@ -153,7 +159,8 @@ namespace gdw {
void render_navMesh(camera_component &camera); void render_navMesh(camera_component &camera);
void render_final_image(); void render_final_image();
void generate_starfield(); void generate_starfield();
void render_starfield(camera_component& camera); void render_starfield(float delta_time, camera_component& camera);
void render_planet(camera_component& camera);
void register_component(camera_component* component); void register_component(camera_component* component);
......
deathstar_material.dds : 23 deathstar_material.dds : 23
coin.dds : 22 coin.dds : 22
next_id : 24 next_id : 25
planet_textur.dds : 19 planet_textur.dds : 19
victim2_diffuse.dds : 16 victim2_diffuse.dds : 16
black.dds.001.dds : 8 black.dds.001.dds : 8
...@@ -9,16 +9,17 @@ deathstar.dds : 20 ...@@ -9,16 +9,17 @@ deathstar.dds : 20
dome_material.dds : 18 dome_material.dds : 18
beam_stripes.dds : 13 beam_stripes.dds : 13
weighted_companion_cube_export_material.dds : 3 weighted_companion_cube_export_material.dds : 3
black.dds : 7
greyscale_normal.dds : 2
piratenschiff_diffuse.dds : 5 piratenschiff_diffuse.dds : 5
Meteor_normal_2.dds : 21
beam.dds : 12 beam.dds : 12
piratenschiff_normal.dds : 6 piratenschiff_normal.dds : 6
Meteor_normal_2.dds : 21
victim_diffuse.dds : 15
cannon.dds : 11 cannon.dds : 11
Meteor_LOW_Normals.dds : 10 victim_diffuse.dds : 15
Texture_Meteor.dds : 9 Texture_Meteor.dds : 9
weighted_companion_cube_export.dds : 1 weighted_companion_cube_export.dds : 1
Meteor_LOW_Normals.dds : 10
piratenschiff_material.dds : 4 piratenschiff_material.dds : 4
planet_normal.dds : 24
dome_diffuse.dds : 17 dome_diffuse.dds : 17
black.dds : 7
greyscale_normal.dds : 2
...@@ -82,10 +82,10 @@ namespace gdw { ...@@ -82,10 +82,10 @@ namespace gdw {
sun.emplace_back<directional_light_component>(glm::vec3(1.f), 0.5f); sun.emplace_back<directional_light_component>(glm::vec3(1.f), 0.5f);
sun.emplace_back<shadow_component>(50.f); sun.emplace_back<shadow_component>(50.f);
auto &planet = entity_manager.emplace_back(glm::vec3(80.f, -50.f, -100.f), /*auto &planet = entity_manager.emplace_back(glm::vec3(80.f, -50.f, -100.f),
glm::angleAxis(glm::radians(0.f), glm::vec3(0.f)), glm::angleAxis(glm::radians(0.f), glm::vec3(0.f)),
glm::vec3(100.f)); glm::vec3(100.f));
planet.emplace_back<staticmesh_component>("mesh/planet.msh"); planet.emplace_back<staticmesh_component>("mesh/planet.msh");*/
} }
if (deathstar_ == 0) { if (deathstar_ == 0) {
auto &deathstar = entity_manager.emplace_back(glm::vec3(-60.f, -20.f, -100.f), auto &deathstar = entity_manager.emplace_back(glm::vec3(-60.f, -20.f, -100.f),
......
...@@ -320,6 +320,42 @@ namespace gdw { ...@@ -320,6 +320,42 @@ namespace gdw {
} }
generate_starfield(); generate_starfield();
planet_ = mesh_manager_.load("mesh/planet.msh", staticmesh_layout_);
if (!planet_) {
throw std::runtime_error("could not load mesh/planet.msh");
}
planet_model_ = glm::translate(glm::mat4(1.f), glm::vec3(80.f, -50.f, -100.f));
planet_model_ *= glm::scale(glm::mat4(1.f), glm::vec3(100.f));
athmosphere_model_ = glm::translate(glm::mat4(1.f), glm::vec3(80.f, -50.f, -100.f));
athmosphere_model_ *= glm::scale(glm::mat4(1.f), glm::vec3(102.5f));
vertex_shader = shader_manager.load("shader/planet.vs", GL_VERTEX_SHADER);
if (!vertex_shader) {
throw std::runtime_error("could not load shader/planet.vs");
}
planet_program_.attach_shader(vertex_shader);
fragment_shader = shader_manager.load("shader/planet.fs", GL_FRAGMENT_SHADER);
if (!fragment_shader) {
throw std::runtime_error("could not load shader/planet.fs");
}
planet_program_.attach_shader(fragment_shader);
staticmesh_layout_.setup_program(planet_program_, "frag_color");
planet_program_.link();
vertex_shader = shader_manager.load("shader/athmosphere.vs", GL_VERTEX_SHADER);
if (!vertex_shader) {
throw std::runtime_error("could not load shader/athmosphere.vs");
}
athmosphere_program_.attach_shader(vertex_shader);
fragment_shader = shader_manager.load("shader/athmosphere.fs", GL_FRAGMENT_SHADER);
if (!fragment_shader) {
throw std::runtime_error("could not load shader/athmosphere.fs");
}
athmosphere_program_.attach_shader(fragment_shader);
staticmesh_layout_.setup_program(athmosphere_program_, "frag_color");
athmosphere_program_.link();
} }
void rendering_system::update(float delta_time) { void rendering_system::update(float delta_time) {
...@@ -330,7 +366,8 @@ namespace gdw { ...@@ -330,7 +366,8 @@ namespace gdw {
render_gbuffer(delta_time, *camera); render_gbuffer(delta_time, *camera);
render_lights(*camera); render_lights(*camera);
render_starfield(*camera); render_planet(*camera);
render_starfield(delta_time, *camera);
render_non_opauqe_geometry(delta_time, *camera); render_non_opauqe_geometry(delta_time, *camera);
render_final_image(); render_final_image();
if(engine_.getConfig().get("navDebug",true)) if(engine_.getConfig().get("navDebug",true))
...@@ -830,7 +867,18 @@ namespace gdw { ...@@ -830,7 +867,18 @@ namespace gdw {
starfield_size_ = stars.size(); starfield_size_ = stars.size();
} }
void rendering_system::render_starfield(camera_component& camera) { void rendering_system::render_starfield(float delta_time, camera_component& camera) {
static auto alpha = 0.f;
static const auto planet_acceleration_ = 0.005f;
static const auto planet_center = glm::vec3(80.f, -20.f, -100.f);
alpha += planet_acceleration_ * delta_time;
if (alpha >= 2.f * glm::pi<float>()) {
alpha = 0.f;
}
auto model = glm::translate(glm::mat4(1.f), planet_center);
model *= glm::toMat4(glm::angleAxis(alpha, glm::vec3(0.f, 1.f, 0.f)));
model *= glm::translate(glm::mat4(1.f), -planet_center);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE); glDepthMask(GL_FALSE);
glEnable(GL_BLEND); glEnable(GL_BLEND);
...@@ -839,6 +887,7 @@ namespace gdw { ...@@ -839,6 +887,7 @@ namespace gdw {
starfield_program_.use(); starfield_program_.use();
starfield_program_.uniform("projection", false, camera.projection()); starfield_program_.uniform("projection", false, camera.projection());
starfield_program_.uniform("view", false, camera.view()); starfield_program_.uniform("view", false, camera.view());
starfield_program_.uniform("model", false, model);
starfield_program_.uniform("albedo_texture", 0); starfield_program_.uniform("albedo_texture", 0);
star_texture_->bind(0); star_texture_->bind(0);
glDrawArraysInstanced(GL_POINTS, 0, 1, starfield_size_); glDrawArraysInstanced(GL_POINTS, 0, 1, starfield_size_);
...@@ -847,6 +896,115 @@ namespace gdw { ...@@ -847,6 +896,115 @@ namespace gdw {
glDisable(GL_BLEND); glDisable(GL_BLEND);
} }
void rendering_system::render_planet(camera_component& camera) {
if (directional_light_components_.empty()) {
return;
}
static const auto wavelength = glm::vec3(0.65f, 0.57f, 0.475f);
static const auto wavelength_high_four = glm::vec3(glm::pow(wavelength.r, 4.f), glm::pow(wavelength.g, 4.f), glm::pow(wavelength.b, 4.f));
static const auto inner_radius = 100.f;
static const auto outer_radius = 102.5f;
static const auto sun_energy = 20.f;
static const auto rayleigh_constant = 0.0025f;
static const auto rayleigh_times_energy = rayleigh_constant * sun_energy;
static const auto rayleigh_times_four_pi = rayleigh_constant * 4.f * glm::pi<float>();
static const auto mie_constant = 0.001f;
static const auto mie_times_energy = mie_constant * sun_energy;
static const auto mie_times_four_pi = mie_constant * 4.f * glm::pi<float>();
static const auto scale = 1.f / (outer_radius - inner_radius);
static const auto scale_depth = 0.25f;
static const auto g = -0.99f;
auto camera_position = camera.owner().position();
composition_buffer_.bind();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
planet_program_.use();
planet_program_.uniform("projection", false, camera.projection());
planet_program_.uniform("view", false, camera.view());
planet_program_.uniform("model", false, planet_model_);
planet_program_.uniform("camera_position", 405.f * glm::normalize(camera_position));
planet_program_.uniform("light_direction", directional_light_components_.front()->owner().position());
planet_program_.uniform("inverse_wave_length", 1.f / wavelength_high_four);
planet_program_.uniform("camera_height", glm::length(camera_position));
planet_program_.uniform("camera_height_squared", glm::length2(camera_position));
planet_program_.uniform("outer_radius", outer_radius);
planet_program_.uniform("outer_radius_squared", outer_radius * outer_radius);
planet_program_.uniform("inner_radius", inner_radius);
planet_program_.uniform("inner_radius_sqaured", inner_radius * inner_radius);
planet_program_.uniform("rayleigh_times_energy", rayleigh_times_energy);
planet_program_.uniform("mie_times_energy", mie_times_energy);
planet_program_.uniform("rayleigh_times_four_pi", rayleigh_times_four_pi);
planet_program_.uniform("mie_times_four_pi", mie_times_four_pi);
planet_program_.uniform("scale", scale);
planet_program_.uniform("scale_depth", scale_depth);
planet_program_.uniform("scale_over_scale_depth", scale / scale_depth);
planet_program_.uniform("albedo_texture", 0);
planet_->draw(planet_program_);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glCullFace(GL_FRONT);
athmosphere_program_.use();
athmosphere_program_.uniform("projection", false, camera.projection());
athmosphere_program_.uniform("view", false, camera.view());
athmosphere_program_.uniform("model", false, athmosphere_model_);
athmosphere_program_.uniform("camera_position", 405.f * glm::normalize(camera_position));
athmosphere_program_.uniform("light_direction", directional_light_components_.front()->owner().position());
athmosphere_program_.uniform("inverse_wave_length", 1.f / wavelength_high_four);
athmosphere_program_.uniform("camera_height", glm::length(camera_position));
athmosphere_program_.uniform("camera_height_squared", glm::length2(camera_position));
athmosphere_program_.uniform("outer_radius", outer_radius);
athmosphere_program_.uniform("outer_radius_squared", outer_radius * outer_radius);
athmosphere_program_.uniform("inner_radius", inner_radius);
athmosphere_program_.uniform("inner_radius_sqaured", inner_radius * inner_radius);
athmosphere_program_.uniform("rayleigh_times_energy", rayleigh_times_energy);
athmosphere_program_.uniform("mie_times_energy", mie_times_energy);
athmosphere_program_.uniform("rayleigh_times_four_pi", rayleigh_times_four_pi);
athmosphere_program_.uniform("mie_times_four_pi", mie_times_four_pi);
athmosphere_program_.uniform("scale", scale);
athmosphere_program_.uniform("scale_depth", scale_depth);
athmosphere_program_.uniform("scale_over_scale_depth", scale / scale_depth);
athmosphere_program_.uniform("g", g);
athmosphere_program_.uniform("g_squared", g * g);
planet_->draw(athmosphere_program_);
glCullFace(GL_BACK);
athmosphere_program_.use();
athmosphere_program_.uniform("projection", false, camera.projection());
athmosphere_program_.uniform("view", false, camera.view());
athmosphere_program_.uniform("model", false, athmosphere_model_);
athmosphere_program_.uniform("camera_position", 405.f * glm::normalize(camera_position));
athmosphere_program_.uniform("light_direction", directional_light_components_.front()->owner().position());
athmosphere_program_.uniform("inverse_wave_length", 1.f / wavelength_high_four);
athmosphere_program_.uniform("camera_height", glm::length(camera_position));
athmosphere_program_.uniform("camera_height_squared", glm::length2(camera_position));
athmosphere_program_.uniform("outer_radius", outer_radius);
athmosphere_program_.uniform("outer_radius_squared", outer_radius * outer_radius);
athmosphere_program_.uniform("inner_radius", inner_radius);
athmosphere_program_.uniform("inner_radius_sqaured", inner_radius * inner_radius);
athmosphere_program_.uniform("rayleigh_times_energy", rayleigh_times_energy);
athmosphere_program_.uniform("mie_times_energy", mie_times_energy);
athmosphere_program_.uniform("rayleigh_times_four_pi", rayleigh_times_four_pi);
athmosphere_program_.uniform("mie_times_four_pi", mie_times_four_pi);
athmosphere_program_.uniform("scale", scale);
athmosphere_program_.uniform("scale_depth", scale_depth);
athmosphere_program_.uniform("scale_over_scale_depth", scale / scale_depth);
athmosphere_program_.uniform("g", g);
athmosphere_program_.uniform("g_squared", g * g);
planet_->draw(athmosphere_program_);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
}
void rendering_system::register_component(camera_component* component) { void rendering_system::register_component(camera_component* component) {
camera_components_.emplace(std::make_pair(component->owner().id(), component)); camera_components_.emplace(std::make_pair(component->owner().id(), component));
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment