Skip to content

Commit

Permalink
Lighting corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Aug 16, 2023
1 parent bf4edb9 commit ef68eb7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ void Main_widget::paintGL()

const auto view = m_camera.get_view_matrix();
const auto projection = m_camera.get_projection_matrix();
const auto mvp = projection * view * m_model;
const auto model_view = view * m_model;
const auto mvp = projection * model_view;
const auto normal_matrix = model_view.normalMatrix();

// compute the cutting plane
// remember that we are passing the local vertex positions of the sphere
Expand All @@ -544,6 +546,7 @@ void Main_widget::paintGL()
auto& sp = m_sp_smooth;
sp.use();
sp.set_uniform("u_mvp", mvp);
sp.set_uniform("u_normal_matrix", normal_matrix);
auto sphere_color = QVector4D(167, 205, 242, 255) / 255;
sp.set_uniform("u_color", sphere_color);
sp.set_uniform("u_plane", QVector4D(0,0,0,0));
Expand Down
10 changes: 10 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ void Shader_program::set_uniform(const GLchar* name, const QMatrix4x4& m)
set_uniform(uniform_loc, m);
}

void Shader_program::set_uniform(GLint uniform_loc, const QMatrix3x3& m)
{
glUniformMatrix3fv(uniform_loc, 1, GL_FALSE, m.data());
}
void Shader_program::set_uniform(const GLchar* name, const QMatrix3x3& m)
{
const auto uniform_loc = get_uniform_location(name);
set_uniform(uniform_loc, m);
}

void Shader_program::set_uniform(GLint uniform_loc, const QVector4D& v)
{
glUniform4f(uniform_loc, v.x(), v.y(), v.z(), v.w());
Expand Down
3 changes: 3 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class Shader_program : protected OpenGLFunctionsBase
void set_uniform(GLint uniform_loc, const QMatrix4x4& m);
void set_uniform(const GLchar* name, const QMatrix4x4& m);

void set_uniform(GLint uniform_loc, const QMatrix3x3& m);
void set_uniform(const GLchar* name, const QMatrix3x3& m);

void set_uniform(GLint uniform_loc, const QVector4D& v);
void set_uniform(const GLchar* name, const QVector4D& v);

Expand Down
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/shaders/smooth_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main()
if( dot(u_plane, vec4(v_pos, 1)) < 0 )
discard;

const vec3 lightDir = normalize(vec3(1,.5,.5));
const vec3 lightDir = normalize(vec3(0,0,-1));

//float c = clamp(dot(lightDir,triNormal), 0, 1);
vec3 n = normalize(v_normal);
Expand Down
5 changes: 3 additions & 2 deletions Arrangement_on_surface_2/demo/earth/shaders/smooth_vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ layout (location = 1) in vec3 a_normal;
out vec3 v_pos;
out vec3 v_normal;

uniform mat4 u_mvp;
uniform mat4 u_mvp;
uniform mat3 u_normal_matrix;

void main()
{
v_pos = a_pos;
v_normal = a_normal;
v_normal = u_normal_matrix * a_normal;
gl_Position = u_mvp * vec4(a_pos.xyz, 1);
//gl_Position = vec4(pos.xyz, 1);
}

0 comments on commit ef68eb7

Please sign in to comment.