Skip to content

Commit

Permalink
cubemap
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcypher89 committed May 19, 2024
1 parent 404386e commit a578437
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 41 deletions.
Binary file added res/envs/castle/nx.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/castle/ny.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/castle/nz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/castle/px.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/castle/py.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/castle/pz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/hata/nx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/hata/ny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/hata/nz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/hata/px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/hata/py.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/envs/hata/pz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions res/main.res
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ tex_spray_arm{
file {spray/arm.jpg} mipmap{linear}
}

tex_cube_env_hata{
file_px{envs/hata/px.png}
file_nx{envs/hata/nx.png}
file_py{envs/hata/py.png}
file_ny{envs/hata/ny.png}
file_pz{envs/hata/pz.png}
file_nz{envs/hata/nz.png}

min_filter{linear}
mag_filter{linear}
mipmap{nearest}
}
Binary file modified res/spray/arm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/spray/diffuse.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/spray/normal.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 27 additions & 34 deletions src/carcockpit/car_view/shaders/shader_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

#include <ruis/render/opengles/index_buffer.hpp>
#include <ruis/render/opengles/texture_2d.hpp>
#include <ruis/render/opengles/texture_cube.hpp>
#include <ruis/render/opengles/util.hpp>
#include <ruis/render/opengles/vertex_array.hpp>
#include <ruis/render/opengles/vertex_buffer.hpp>
Expand Down Expand Up @@ -93,6 +94,7 @@ shader_adv::shader_adv() :
uniform sampler2D texture0; // color map tex
uniform sampler2D texture1; // normal map tex
uniform sampler2D texture2; // roughness map tex
uniform samplerCube texture3; // cube map
uniform vec4 light_position;
uniform vec3 light_intensity;
Expand All @@ -107,48 +109,28 @@ shader_adv::shader_adv() :
const vec3 Ka = vec3(0.1, 0.1, 0.1); // Ambient reflectivity
const vec3 Ks = vec3(0.7, 0.7, 0.7); // Specular reflectivity
const float Shininess = 40.0; // Specular shininess factor
// vec3 phongModelCB( vec3 norm, vec3 diffR, float glossFactor )
// {
// vec3 r = reflect( -light_dir, norm );
// vec3 ambient = light_intensity * Material.Ka;
// float sDotN = max( dot(light_dir, norm) , 0.0 );
// vec3 diffuse = light_intensity * diffR * sDotN;
// vec3 spec = vec3(0.0);
// if( sDotN > 0.0 )
// spec = light_intensity * Material.Ks * pow( max( dot(r, view_dir), 0.0 ), glossFactor );
// return ambient + diffuse + spec;
// }
vec3 phongModel( vec3 norm, vec3 diffR, float glossFactor )
vec3 phongModel( vec3 norm, vec3 diffR, float ambient_occ, float glossiness, float metalness ) // arm = ambient/roughness/metalness
{
vec3 r_env = reflect( view_dir, norm );
vec3 env_refl = textureCube( texture3, r_env).xyz;
vec3 r = reflect( -light_dir, norm );
float sDotN = max( dot(light_dir, norm) , 0.0 );
vec3 ambient = Ka * light_intensity;
vec3 diffuse = Kd * sDotN * light_intensity;
vec3 spec = Ks * pow( max( dot(r, view_dir), 0.0 ), glossFactor ) * light_intensity;
//vec3 spec = vec3(0.0);
//if( sDotN > 0.0 )
//spec = light_intensity * Ks * pow( max( dot(r, view_dir), 0.0 ), glossFactor );
return ( ambient + diffuse + spec ) * diffR;
}
vec3 diffuse = max( Kd - metalness, 0.0 ) * sDotN * light_intensity;
vec3 spec = Ks * pow( max( dot(r, view_dir), 0.0 ), glossiness ) * light_intensity;
// vec3 ads()
// {
// vec3 n = normalize( norm );
// vec3 s = normalize( vec3(light_position) - pos );
// vec3 v = normalize( vec3(-pos) );
// vec3 r = reflect( -s, n );
// return light_intensity * ( Ka + Kd * max( dot(s, n), 0.0 ) + Ks * pow( max( dot(r,v), 0.0 ), Shininess ) );
// }
return (( ambient + diffuse + spec ) * diffR) + (env_refl * metalness);
}
void main()
{
float rough = texture2D( texture2, tc ).y;
float gloss = 1.0 / rough; //((1.0 - pow(rough, 0.2) ) * 100.0 + 1.0 );
vec3 arm = texture2D( texture2, tc ).xyz;
//float gloss = 1.0 / arm.y;
float gloss = ((1.0 - pow(arm.y, 0.2) ) * 100.0 + 1.0 );
//vec3 normal = vec3(0, 0, 1.0);
vec4 normal4 = 2.0 * texture2D( texture1, tc ) - 1.0;
Expand All @@ -158,13 +140,19 @@ shader_adv::shader_adv() :
vec4 texColor = //vec4(0.5, 0.5, 0.5, 1.0);
texture2D( texture0, tc );
gl_FragColor = vec4( phongModel( normal, texColor.rgb, gloss), 1.0 );
gl_FragColor = vec4( phongModel( normal, texColor.rgb, arm.x, gloss, arm.z), 1.0 );
}
//vec3 spec = vec3(0.0);
//if( sDotN > 0.0 )
//spec = light_intensity * Ks * pow( max( dot(r, view_dir), 0.0 ), glossiness );
)qwertyuiop"
),
sampler_normal_map(this->get_uniform("texture1")),
sampler_roughness_map(this->get_uniform("texture2")),
sampler_cube(this->get_uniform("texture3")),
mat4_modelview(this->get_uniform("mat4_mv")),
//mat4_projection(this->get_uniform("mat4_p")),
mat3_normal(this->get_uniform("mat3_n")),
Expand Down Expand Up @@ -193,6 +181,7 @@ void shader_adv::render(
const ruis::render::texture_2d& tex_color,
const ruis::render::texture_2d& tex_normal,
const ruis::render::texture_2d& tex_roughness,
const ruis::render::texture_cube& tex_cube_env,
const ruis::vec4& light_pos = default_light_position,
const ruis::vec3& light_int = default_light_intensity
) const
Expand All @@ -201,6 +190,7 @@ void shader_adv::render(

this->set_uniform_sampler(sampler_normal_map, 1);
this->set_uniform_sampler(sampler_roughness_map, 2);
this->set_uniform_sampler(sampler_cube, 3);

ASSERT(dynamic_cast<const ruis::render::opengles::texture_2d*>(&tex_color));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
Expand All @@ -211,6 +201,9 @@ void shader_adv::render(
ASSERT(dynamic_cast<const ruis::render::opengles::texture_2d*>(&tex_roughness));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
static_cast<const ruis::render::opengles::texture_2d&>(tex_roughness).bind(2);
ASSERT(dynamic_cast<const ruis::render::opengles::texture_2d*>(&tex_roughness));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
static_cast<const ruis::render::opengles::texture_cube&>(tex_cube_env).bind(3);

ruis::mat3 normal = from_mat4(modelview);
normal.invert();
Expand Down
12 changes: 9 additions & 3 deletions src/carcockpit/car_view/shaders/shader_adv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
// #include <ruis/render/texturing_shader.hpp>
#include <ruis/render/opengles/shader_base.hpp>
#include <ruis/render/texture_2d.hpp>
#include <ruis/render/texture_cube.hpp>

namespace ruis {
using mat3 = r4::matrix3<real>;
Expand All @@ -36,15 +37,19 @@ namespace carcockpit {
class shader_adv : public ruis::render::opengles::shader_base
{
public:
GLint sampler_normal_map, sampler_roughness_map;
GLint sampler_normal_map;
GLint sampler_roughness_map;
GLint sampler_cube;

GLint mat4_modelview;
//GLint mat4_projection;
GLint mat3_normal;

GLint vec4_light_position, vec3_light_intensity, vec3_set_normal_mapping;
GLint vec4_light_position;
GLint vec3_light_intensity;
GLint vec3_set_normal_mapping;

ruis::vec3 set_normal_mapping_vector{1, 1, 1};
ruis::vec3 set_normal_mapping_vector{1, 1, 1}; // experimental

shader_adv();
void render(
Expand All @@ -55,6 +60,7 @@ class shader_adv : public ruis::render::opengles::shader_base
const ruis::render::texture_2d& tex_color,
const ruis::render::texture_2d& tex_normal,
const ruis::render::texture_2d& tex_roughness,
const ruis::render::texture_cube& tex_cube_env,
const ruis::vec4& light_pos,
const ruis::vec3& light_int
) const;
Expand Down
80 changes: 80 additions & 0 deletions src/carcockpit/car_view/shaders/shader_skybox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
carcockpit - Car cockpit example GUI project
Copyright (C) 2024 Gagistech Oy <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/* ================ LICENSE END ================ */

#include "shader_skybox.hpp"

#include <ruis/render/opengles/index_buffer.hpp>
#include <ruis/render/opengles/texture_cube.hpp>
#include <ruis/render/opengles/util.hpp>
#include <ruis/render/opengles/vertex_array.hpp>
#include <ruis/render/opengles/vertex_buffer.hpp>

using namespace carcockpit;

shader_skybox::shader_skybox() :
shader_base(
R"qwertyuiop(
attribute highp vec4 a0; // position
uniform highp mat4 matrix; // mvp matrix
uniform highp mat4 mat4_mv; // modelview matrix
varying highp vec3 pos;
void main(void)
{
pos = vec3( mat4_mv * a0 );
gl_Position = matrix * a0;
}
)qwertyuiop",
R"qwertyuiop(
precision highp float;
varying highp vec3 pos;
uniform samplerCube texture0;
void main()
{
gl_FragColor = vec4( textureCube(texture0, pos) );
}
)qwertyuiop"
),
mat4_modelview(this->get_uniform("mat4_mv"))
{}

void shader_skybox::render(
const ruis::render::vertex_array& va,
const r4::matrix4<float>& mvp,
const r4::matrix4<float>& modelview,
const ruis::render::texture_cube& tex_env_cube
) const
{
ASSERT(dynamic_cast<const ruis::render::opengles::texture_cube*>(&tex_env_cube))
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
static_cast<const ruis::render::opengles::texture_cube&>(tex_env_cube).bind(0);

this->bind(); // bind the program

this->set_uniform_matrix4f(this->mat4_modelview, modelview);

this->shader_base::render(mvp, va);
}
44 changes: 44 additions & 0 deletions src/carcockpit/car_view/shaders/shader_skybox.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
carcockpit - Car cockpit example GUI project
Copyright (C) 2024 Gagistech Oy <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/* ================ LICENSE END ================ */

#pragma once

// #include <ruis/render/texturing_shader.hpp>
#include <ruis/render/opengles/shader_base.hpp>
#include <ruis/render/texture_cube.hpp>

namespace carcockpit {

class shader_skybox : public ruis::render::opengles::shader_base
{
public:
GLint mat4_modelview, mat3_normal, vec4_light_position, vec3_light_intensity;

shader_skybox();
void render(
const ruis::render::vertex_array& va,
const r4::matrix4<float>& mvp,
const r4::matrix4<float>& modelview,
const ruis::render::texture_cube& tex_env_cube
) const;
};

} // namespace carcockpit
17 changes: 14 additions & 3 deletions src/carcockpit/car_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ car_widget::car_widget(utki::shared_ref<ruis::context> context, all_parameters p
this->tex_rust_normal = this->context.get().loader.load<ruis::res::texture_2d>("tex_spray_normal").to_shared_ptr();
this->tex_rust_roughness = this->context.get().loader.load<ruis::res::texture_2d>("tex_spray_arm").to_shared_ptr();

this->tex_cube_env_hata = this->context.get().loader.load<ruis::res::texture_cube>("tex_cube_env_hata").to_shared_ptr();

std::shared_ptr<ModelOBJ> light_model_obj = std::make_shared<ModelOBJ>();
std::shared_ptr<ModelOBJ> car_model_obj = std::make_shared<ModelOBJ>();
std::shared_ptr<ModelOBJ> lamba_left_model_obj = std::make_shared<ModelOBJ>();
Expand Down Expand Up @@ -240,6 +242,7 @@ car_widget::car_widget(utki::shared_ref<ruis::context> context, all_parameters p
o << "<< SHADER KOM PILE >>" << std::endl;
})

this->skybox_s = std::make_shared<shader_skybox>();
this->phong_s = std::make_shared<shader_phong>();
this->advanced_s = std::make_shared<shader_adv>();

Expand Down Expand Up @@ -301,6 +304,8 @@ void car_widget::set_normal_mapping(bool toggle)

bool car_widget::on_mouse_button(const ruis::mouse_button_event& e)
{
std::cout << "Is Down = " << e.is_down << std::endl;

if (e.button == ruis::mouse_button::wheel_up) {
camera_attractor /= 1.07;
} else if (e.button == ruis::mouse_button::wheel_down) {
Expand All @@ -312,7 +317,7 @@ bool car_widget::on_mouse_button(const ruis::mouse_button_event& e)
camera_changeview_start = camera_position;
}
}
return false;
return true;
}

bool car_widget::on_mouse_move(const ruis::mouse_move_event& e)
Expand All @@ -325,9 +330,9 @@ bool car_widget::on_mouse_move(const ruis::mouse_move_event& e)
// diff4 = inv_view * diff4;

ruis::quat q1, q2;
q1.set_rotation(0, 1, 0, -diff4.x() / 100.0f);
q1.set_rotation(0, 1, 0, -diff4.x() / 200.0f);
// camera_attractor.rotate(q);
q2.set_rotation(1, 0, 0, -diff4.y() / 100.0f);
q2.set_rotation(1, 0, 0, -diff4.y() / 200.0f);
// camera_attractor.rotate(q);

ruis::vec3 cam2go = camera_changeview_start;
Expand Down Expand Up @@ -431,6 +436,11 @@ void car_widget::render(const ruis::matrix4& matrix) const
// //phong_s->render(*this->vao_lamba_l, mvp, modelview,
// this->tex_car_diffuse->tex(), light_pos_view, light_int);
// phong_s->render(*this->vao_lamba_r, mvp, modelview, this->tex_car_diffuse->tex(), light_pos_view, light_int);
// skybox_s->render(*this->car_vao,
// mvp,
// modelview,
// this->tex_cube_env_hata->tex());

phong_s->render(*this->light_vao, mvp_monkey, modelview_monkey, this->tex_test->tex(), light_pos_view, light_int);

advanced_s->render(
Expand All @@ -441,6 +451,7 @@ void car_widget::render(const ruis::matrix4& matrix) const
this->tex_rust_diffuse->tex(),
this->tex_rust_normal->tex(),
this->tex_rust_roughness->tex(),
this->tex_cube_env_hata->tex(),
light_pos_view,
light_int
);
Expand Down
Loading

0 comments on commit a578437

Please sign in to comment.