Skip to content

Commit

Permalink
Refactor: shader initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 14, 2023
1 parent 9a5bfa0 commit 84f614a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
15 changes: 14 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Shader_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ bool Shader_program::init()
}
bool Shader_program::init(const char* vs, const char* gs, const char* fs)
{
return false;
if (init() == false)
return false;

add_shader_from_file(vs, GL_VERTEX_SHADER);
add_shader_from_file(gs, GL_GEOMETRY_SHADER);
add_shader_from_file(fs, GL_FRAGMENT_SHADER);

link();
validate();

return true;
}
void Shader_program::add_shader(const char* shader_code, GLenum shader_type)
{
Expand Down Expand Up @@ -57,6 +67,9 @@ void Shader_program::add_shader(const char* shader_code, GLenum shader_type)
void Shader_program::add_shader_from_file(const char* shader_file,
GLenum shader_type)
{
if (strlen(shader_file) == 0)
return;

auto src = read_file(shader_file);
add_shader(src.c_str(), shader_type);
}
Expand Down
47 changes: 16 additions & 31 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,39 +99,26 @@ void MainWidget::init_geometry()
m_sphere->set_color(c, c, c, 1);


m_world_coord_axes = std::make_unique<World_coord_axes>(2);
const float axes_length = 2;
m_world_coord_axes = std::make_unique<World_coord_axes>(axes_length);
}
void MainWidget::init_shader_programs()
{
// SMOOTH SHADER PROGRAM
{
m_sp_smooth.init();

const char* vs = "shaders/smooth_vs.glsl";
const char* gs = "shaders/geometry.glsl";
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_sp_smooth.add_shader_from_file(fs, GL_FRAGMENT_SHADER);

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


// COLOR ONLY SHADER PROGRAM
{
m_sp_color_only.init();

const char* vs = "shaders/color_only_vs.glsl";
const char* fs = "shaders/color_only_fs.glsl";
m_sp_color_only.add_shader_from_file(vs, GL_VERTEX_SHADER);
//m_shader_program.add_shader_from_file(gs, GL_GEOMETRY_SHADER);
m_sp_color_only.add_shader_from_file(fs, GL_FRAGMENT_SHADER);
init_sp_smooth();
init_sp_color_only();
}
void MainWidget::init_sp_smooth()
{
const char* vs = "shaders/smooth_vs.glsl";
const char* fs = "shaders/smooth_fs.glsl";
m_sp_smooth.init(vs, "", fs);

m_sp_color_only.link();
m_sp_color_only.validate();
}
}
void MainWidget::init_sp_color_only()
{
const char* vs = "shaders/color_only_vs.glsl";
const char* fs = "shaders/color_only_fs.glsl";
m_sp_color_only.init(vs, "", fs);
}


Expand All @@ -143,8 +130,6 @@ void MainWidget::resizeGL(int w, int h)
const qreal z_near = 1.0, z_far = 100.0, fov = 45.0;
m_camera.perspective(fov, aspect, z_near, z_far);
}


void MainWidget::paintGL()
{
QMatrix4x4 model;
Expand Down
8 changes: 3 additions & 5 deletions Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,17 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
void mouseReleaseEvent(QMouseEvent *e) override;
void timerEvent(QTimerEvent *e) override;


void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;


void add_shader(GLuint the_program,
const char* shader_code,
GLenum shader_type);

void init_camera();
void init_geometry();

void init_shader_programs();
void init_sp_smooth();
void init_sp_color_only();

private:
// Objects in the scene
Expand Down

0 comments on commit 84f614a

Please sign in to comment.