From f01d7e5507cc155f3a0506b5b180aa8bf3a01c01 Mon Sep 17 00:00:00 2001 From: timofei iniushin Date: Fri, 26 Apr 2024 23:04:01 -0400 Subject: [PATCH] phong shading, bind shader access hack --- src/car_view/shaders/shader_car.cpp | 48 ++++++++++++++++++++--------- src/car_view/shaders/shader_car.hpp | 2 ++ src/car_widget.cpp | 21 ++++++++++--- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/car_view/shaders/shader_car.cpp b/src/car_view/shaders/shader_car.cpp index 1c56467..db823ce 100644 --- a/src/car_view/shaders/shader_car.cpp +++ b/src/car_view/shaders/shader_car.cpp @@ -73,9 +73,12 @@ shader_car::shader_car() : uniform highp mat4 matrix; // mvp matrix uniform highp mat4 mat4_mv; // modelview matrix - uniform highp mat4 mat4_p; // projection matrix + //uniform highp mat4 mat4_p; // projection matrix uniform highp mat3 mat3_n; // normal matrix (mat3) + uniform vec4 LightPosition; + uniform vec3 LightIntensity; + varying highp vec3 pos; varying highp vec2 tc0; varying highp vec3 norm; @@ -83,8 +86,8 @@ shader_car::shader_car() : void main(void) { //mat3 normalMatrix = mat3( transpose(inverse( mat4_mv )) ); //TODO: remove from shader - //norm = normalize( normalMatrix * a2 ); - norm = normalize( matrix * vec4(a2, 0) ).xyz; + norm = normalize( mat3_n * a2 ); + //norm = normalize( matrix * vec4(a2, 0) ).xyz; pos = vec3( mat4_mv * a0 ); gl_Position = matrix * a0; } @@ -104,17 +107,18 @@ shader_car::shader_car() : varying highp vec2 tc0; varying highp vec3 norm; + uniform highp mat4 matrix; // mvp matrix + uniform highp mat4 mat4_mv; // modelview matrix + //uniform highp mat4 mat4_p; // projection matrix + uniform highp mat3 mat3_n; // normal matrix (mat3) + uniform vec4 LightPosition; uniform vec3 LightIntensity; - // uniform vec3 Kd; // Diffuse reflectivity - // uniform vec3 Ka; // Ambient reflectivity - // uniform vec3 Ks; // Specular reflectivity - // uniform float Shininess; // Specular shininess factor const vec3 Kd = vec3(0.5, 0.5, 0.5); // Diffuse reflectivity - const vec3 Ka = vec3(0.5, 0.5, 0.5); // Ambient reflectivity - const vec3 Ks = vec3(0.5, 0.5, 0.5); // Specular reflectivity - const float Shininess = 8.0; // Specular shininess factor + const vec3 Ka = vec3(0.08, 0.08, 0.08); // Ambient reflectivity + const vec3 Ks = vec3(0.7, 0.7, 0.7); // Specular reflectivity + const float Shininess = 20.0; // Specular shininess factor vec3 ads() { @@ -122,16 +126,17 @@ shader_car::shader_car() : vec3 s = normalize( vec3(LightPosition) - pos ); vec3 v = normalize( vec3(-pos) ); vec3 r = reflect( -s, n ); - return LightIntensity * ( Ka + Kd * max( dot(s, n), 0.0 ) + Ks * pow( max( dot(r,v), 0.0 ), Shininess ) ) * 4.0; + return LightIntensity * ( Ka + Kd * max( dot(s, n), 0.0 ) + Ks * pow( max( dot(r,v), 0.0 ), Shininess ) ) * 1.0; } void main() { - float f = max( dot(norm, vec3(1.0, 0, 0)), 0.0); - gl_FragColor = vec4(f+0.05, f+0.05, f+0.05, 1.0); - //gl_FragColor = vec4(ads(), 1.0); + //float f = max( dot(norm, vec3(1.0, 0, 0)), 0.0); + //vec3 light = LightIntensity * (f + 0.03); + //gl_FragColor = vec4(light, 1.0); + gl_FragColor = vec4(ads(), 1.0); //gl_FragColor = vec4(LightIntensity, 1.0); - //gl_FragColor = vec4(1, 1, 1, 1); + //gl_FragColor = vec4(LightIntensity, 1); } )qwertyuiop" @@ -146,6 +151,11 @@ shader_car::shader_car() : { } +void shader_car::bind_me() +{ + this->bind(); +} + void shader_car::render(const r4::matrix4& m, const ruis::vertex_array& va, const ruis::texture_2d& tex) const { ASSERT(dynamic_cast(&tex)) @@ -158,6 +168,8 @@ void shader_car::render(const r4::matrix4& m, const ruis::vertex_array& v void shader_car::set_uniform_matrix3f(GLint id, const r4::matrix3& m) const { + if(id < 0) + return; auto mm = m.tposed(); glUniformMatrix3fv( id, @@ -173,6 +185,8 @@ void shader_car::set_uniform_matrix3f(GLint id, const r4::matrix3& m) con void shader_car::set_uniform_matrix4f(GLint id, const r4::matrix4& m) const { + if(id < 0) + return; auto mm = m.tposed(); glUniformMatrix4fv( id, @@ -188,12 +202,16 @@ void shader_car::set_uniform_matrix4f(GLint id, const r4::matrix4& m) con void shader_car::set_uniform3f(GLint id, float x, float y, float z) const { + if(id < 0) + return; glUniform3f(id, x, y, z); //ruis::render_opengles::assert_opengl_no_error(); } void shader_car::set_uniform4f(GLint id, float x, float y, float z, float w) const { + if(id < 0) + return; glUniform4f(id, x, y, z, w); //ruis::render_opengles::assert_opengl_no_error(); } diff --git a/src/car_view/shaders/shader_car.hpp b/src/car_view/shaders/shader_car.hpp index b15d138..382bd0e 100644 --- a/src/car_view/shaders/shader_car.hpp +++ b/src/car_view/shaders/shader_car.hpp @@ -40,6 +40,8 @@ class shader_car : public ruis::render_opengles::shader_base virtual void set_uniform3f(GLint id, float x, float y, float z) const; virtual void set_uniform4f(GLint id, float x, float y, float z, float w) const; virtual GLint get_uniform(const char* name); + + void bind_me(); // protected -> public }; } // namespace ruis::render_opengles \ No newline at end of file diff --git a/src/car_widget.cpp b/src/car_widget.cpp index 3ebf3aa..e3ccf53 100644 --- a/src/car_widget.cpp +++ b/src/car_widget.cpp @@ -246,9 +246,8 @@ void car_widget::render(const ruis::matrix4& matrix) const //model.translate(0, -0.7, -3); model.rotate(this->rot); - modelview = view * model; + modelview = view * model; // v * m mvp = projection * view * model; // p * v * m - //mvp = projection;// * mvp; // The normal matrix is typically the inverse transpose of the // upper-left 3 x 3 portion of the model-view matrix. We use the @@ -258,8 +257,13 @@ void car_widget::render(const ruis::matrix4& matrix) const normal.invert(); normal.transpose(); - ruis::vec4 light_pos{3.0f, 4.0f, 5.0f, 1.0f}; - ruis::vec3 light_int{0.5f, 0.7f, 0.9f}; + float fms = static_cast(this->time_sec) / std::milli::den; + + float xx = 4 * cosf(fms / 3); + float yy = 4 * sinf(fms / 3); + + ruis::vec4 light_pos{xx, yy, 0.0f, 1.0f}; + ruis::vec3 light_int{0.95f, 0.98f, 1.0f}; glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); @@ -274,6 +278,8 @@ void car_widget::render(const ruis::matrix4& matrix) const // mat4_modelview, mat4_projection, mat3_normal, vec4_light_position, vec3_light_intensity; //(static_cast(shader)) + shader->bind_me(); + shader->set_uniform4f(shader->vec4_light_position, light_pos[0], light_pos[1], light_pos[2], light_pos[3]); shader->set_uniform3f(shader->vec3_light_intensity, light_int[0], light_int[1], light_int[2]); shader->set_uniform_matrix4f(shader->mat4_modelview, modelview); @@ -281,7 +287,7 @@ void car_widget::render(const ruis::matrix4& matrix) const shader->set_uniform_matrix3f(shader->mat3_normal, normal); GLenum err; - while((err = glGetError()) != GL_NO_ERROR) {} // skip all uniform-related errors + while((err = glGetError()) != GL_NO_ERROR) {} // skip all uniform-related errors (TODO: remove asap) shader->render(mvp, *this->car_vao, this->tex_car_diffuse->tex()); @@ -293,3 +299,8 @@ void car_widget::render(const ruis::matrix4& matrix) const glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); } + +// shader bind +// uniform errors not so severe? +// setup modifying of libs on local machine +// cubemaps textures, rendering shadow passes to depth_attachment fbo's