Skip to content

Commit

Permalink
phong shading, bind shader access hack
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcypher89 committed Apr 27, 2024
1 parent 6e82fce commit f01d7e5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
48 changes: 33 additions & 15 deletions src/car_view/shaders/shader_car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,21 @@ 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;
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;
}
Expand All @@ -104,34 +107,36 @@ 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()
{
vec3 n = normalize( norm );
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"
Expand All @@ -146,6 +151,11 @@ shader_car::shader_car() :
{
}

void shader_car::bind_me()
{
this->bind();
}

void shader_car::render(const r4::matrix4<float>& m, const ruis::vertex_array& va, const ruis::texture_2d& tex) const
{
ASSERT(dynamic_cast<const ruis::render_opengles::texture_2d*>(&tex))
Expand All @@ -158,6 +168,8 @@ void shader_car::render(const r4::matrix4<float>& m, const ruis::vertex_array& v

void shader_car::set_uniform_matrix3f(GLint id, const r4::matrix3<float>& m) const
{
if(id < 0)
return;
auto mm = m.tposed();
glUniformMatrix3fv(
id,
Expand All @@ -173,6 +185,8 @@ void shader_car::set_uniform_matrix3f(GLint id, const r4::matrix3<float>& m) con

void shader_car::set_uniform_matrix4f(GLint id, const r4::matrix4<float>& m) const
{
if(id < 0)
return;
auto mm = m.tposed();
glUniformMatrix4fv(
id,
Expand All @@ -188,12 +202,16 @@ void shader_car::set_uniform_matrix4f(GLint id, const r4::matrix4<float>& 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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/car_view/shaders/shader_car.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 16 additions & 5 deletions src/car_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<float>(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);
Expand All @@ -274,14 +278,16 @@ void car_widget::render(const ruis::matrix4& matrix) const
// mat4_modelview, mat4_projection, mat3_normal, vec4_light_position, vec3_light_intensity;

//(static_cast<ruis::render_opengles::shader_base>(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);
shader->set_uniform_matrix4f(shader->mat4_projection, projection);
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());

Expand All @@ -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

0 comments on commit f01d7e5

Please sign in to comment.