Skip to content

Commit

Permalink
Added sphere color
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 13, 2023
1 parent 1c98dba commit 42cc227
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 17 deletions.
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 @@ -112,3 +112,13 @@ void Shader_program::set_uniform(const GLchar* name, const QMatrix4x4& 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());
}
void Shader_program::set_uniform(const GLchar* name, const QVector4D& v)
{
const auto uniform_loc = get_uniform_location(name);
set_uniform(uniform_loc, v);
}
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 @@ -26,6 +26,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 QVector4D& v);
void set_uniform(const GLchar* name, const QVector4D& v);

//private:
GLuint m_program;
};
Expand Down
4 changes: 4 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ Sphere::Sphere(int num_slices, int num_stacks, float r)
// Note: calling this before glBindVertexArray(0) results in no output!
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void Sphere::set_color(float r, float g, float b, float a)
{
m_color = QVector4D(r, g, b, a);
}
void Sphere::draw()
{
// DRAW TRIANGLES
Expand Down
7 changes: 6 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
#define SPHERE_H

#include "Common_defs.h"
#include <qvector4d.h>


class Sphere : protected OpenGLFunctionsBase
{
public:
Sphere(int num_slices, int num_stacks, float r);

void set_color(float r, float g, float b, float a);
const QVector4D& get_color() { return m_color; }

void draw();

private:
GLuint m_vao, m_vbo, m_ibo, m_num_indices;
GLuint m_vao, m_vbo, m_ibo, m_num_indices;
QVector4D m_color;
};


Expand Down
23 changes: 13 additions & 10 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,22 @@ void MainWidget::init_geometry()
num_slices = num_stacks = 64;
float r = 3;
m_sphere = std::make_unique<Sphere>(num_slices, num_stacks, r);
const float c = 0.8;
m_sphere->set_color(c, c, c, 1);
}
void MainWidget::init_shader_program()
{
m_shader_program.init();
m_sp_smooth.init();

const char* vs = "shaders/vertex.glsl";
const char* vs = "shaders/smooth_vs.glsl";
const char* gs = "shaders/geometry.glsl";
const char* fs = "shaders/fragment.glsl";
m_shader_program.add_shader_from_file(vs, GL_VERTEX_SHADER);
const char* fs = "shaders/smooth_fs.glsl";
m_sp_smooth.add_shader_from_file(vs, GL_VERTEX_SHADER);
//m_shader_program.add_shader_from_file(gs, GL_GEOMETRY_SHADER);
m_shader_program.add_shader_from_file(fs, GL_FRAGMENT_SHADER);
m_sp_smooth.add_shader_from_file(fs, GL_FRAGMENT_SHADER);

m_shader_program.link();
m_shader_program.validate();
m_sp_smooth.link();
m_sp_smooth.validate();
}


Expand All @@ -129,11 +131,12 @@ void MainWidget::paintGL()
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
m_shader_program.use();
m_shader_program.set_uniform("MVP", mvp);
m_sp_smooth.use();
m_sp_smooth.set_uniform("MVP", mvp);
m_sp_smooth.set_uniform("uColor", m_sphere->get_color());

m_sphere->draw();

m_shader_program.unuse();
m_sp_smooth.unuse();
}
}
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
private:
std::unique_ptr<Sphere> m_sphere;

Shader_program m_shader_program;
Shader_program m_sp_smooth;

// camera & controls
Camera m_camera;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

#version 330

//in vec4 vCol;
flat in vec3 vNormal;
uniform vec4 uColor;
in vec3 vNormal;

out vec4 out_color;

out vec4 color;

void main()
{
Expand All @@ -13,7 +14,7 @@ void main()
//float c = clamp(dot(lightDir,triNormal), 0, 1);
vec3 n = normalize(vNormal);
float c = abs( dot(lightDir, n) );
color = vec4(.2, .2,0,1) + 0.8*vec4(c,c,0,1);
out_color = uColor * (vec4(.2, .2,.2,1) + 0.8*vec4(c,c,c,1));

//color = vec4(1,1,0,1);
//color = vCol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ layout (location = 1) in vec3 normal;

//out vec4 vCol;
//out vec3 vpos;
flat out vec3 vNormal;
out vec3 vNormal;

uniform mat4 MVP;

Expand Down

0 comments on commit 42cc227

Please sign in to comment.