Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 16, 2023
1 parent e87f5a5 commit d68e430
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 48 deletions.
48 changes: 6 additions & 42 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <QMouseEvent>

#include "Tools.h"


Main_widget::~Main_widget()
{
Expand All @@ -15,19 +17,6 @@ Main_widget::~Main_widget()
doneCurrent();
}


std::ostream& operator << (std::ostream& os, const QVector2D& v)
{
os << v.x() << ", " << v.y();
return os;
}
std::ostream& operator << (std::ostream& os, const QVector3D& v)
{
os << v.x() << ", " << v.y() << ", " << v.z();
return os;
}


void Main_widget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
{
switch (e->button())
Expand Down Expand Up @@ -89,7 +78,6 @@ void Main_widget::mouseMoveEvent(QMouseEvent* e)

m_camera.rotate(rot_matrix);
}

}
else if(m_middle_mouse_button_down)
{
Expand Down Expand Up @@ -154,34 +142,10 @@ void Main_widget::init_geometry()
}
void Main_widget::init_shader_programs()
{
init_sp_smooth();
init_sp_per_vertex_color();
init_sp_arc();
}
void Main_widget::init_sp_smooth()
{
const char* vs = "shaders/smooth_vs.glsl";
const char* fs = "shaders/smooth_fs.glsl";
m_sp_smooth.init(vs, "", fs);
}
void Main_widget::init_sp_per_vertex_color()
{
const char* vs = "shaders/per_vertex_color_vs.glsl";
const char* fs = "shaders/per_vertex_color_fs.glsl";
m_sp_per_vertex_color.init(vs, "", fs);
}
void Main_widget::init_sp_arc()
{
const char* vs = "shaders/arc_vs.glsl";
const char* fs = "shaders/arc_fs.glsl";
m_sp_arc.init(vs, "", fs);
}


std::ostream& operator << (std::ostream& os, const QVector4D& v)
{
os << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w();
return os;
Shader_program::set_shader_path("shaders/");
m_sp_smooth.init_with_vs_fs("smooth");;
m_sp_per_vertex_color.init_with_vs_fs("per_vertex_color");
m_sp_arc.init_with_vs_fs("arc");
}

void Main_widget::resizeGL(int w, int h)
Expand Down
7 changes: 2 additions & 5 deletions Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase


void init_camera();
void init_geometry();

void init_geometry();
void init_shader_programs();
void init_sp_smooth();
void init_sp_per_vertex_color();
void init_sp_arc();


private:
// Objects in the scene
Expand Down
21 changes: 21 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
#include "Tools.h"


std::string Shader_program::s_shader_path;


void Shader_program::set_shader_path(const char* path)
{
s_shader_path = std::string(path);
}

bool Shader_program::init()
{
initializeOpenGLFunctions();
Expand Down Expand Up @@ -33,6 +41,19 @@ bool Shader_program::init(const char* vs, const char* gs, const char* fs)

return true;
}
bool Shader_program::init(const std::string& vs,
const std::string& gs,
const std::string& fs)
{
return init(vs.c_str(), gs.c_str(), fs.c_str());
}
bool Shader_program::init_with_vs_fs(const char* shader_file_prefix)
{
std::string prefix(shader_file_prefix);
std::string vs = s_shader_path + prefix + "_vs.glsl";
std::string fs = s_shader_path + prefix + "_fs.glsl";
return init(vs, "", fs);
}
void Shader_program::add_shader(const char* shader_code, GLenum shader_type)
{
GLuint the_shader = glCreateShader(shader_type);
Expand Down
12 changes: 11 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Shader_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef SHADER_PROGRAM_H
#define SHADER_PROGRAM_H

#include <string>
#include <qmatrix4x4.h>
#include "Common_defs.h"

Expand All @@ -10,8 +11,16 @@ class Shader_program : protected OpenGLFunctionsBase
{
public:

static void set_shader_path(const char* path);

bool init();
bool init(const char* vs, const char* gs, const char* fs);
bool init(const std::string& vs,
const std::string& gs,
const std::string& fs);

// initialize with just the vertex and fragment shaders
bool init_with_vs_fs(const char* shader_file_prefix);

void add_shader(const char* shader_code, GLenum shader_type);
void add_shader_from_file(const char* shader_file, GLenum shader_type);
Expand All @@ -30,8 +39,9 @@ class Shader_program : protected OpenGLFunctionsBase
void set_uniform(GLint uniform_loc, const QVector4D& v);
void set_uniform(const GLchar* name, const QVector4D& v);

//private:
private:
GLuint m_program;
static std::string s_shader_path;
};


Expand Down
16 changes: 16 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ std::string read_file(const std::string& file_name)

return std::string(&bytes[0], file_size);
}

std::ostream& operator << (std::ostream& os, const QVector2D& v)
{
os << v.x() << ", " << v.y();
return os;
}
std::ostream& operator << (std::ostream& os, const QVector3D& v)
{
os << v.x() << ", " << v.y() << ", " << v.z();
return os;
}
std::ostream& operator << (std::ostream& os, const QVector4D& v)
{
os << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w();
return os;
}
7 changes: 7 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

#include <string>

#include <QVector2D>
#include <QVector3D>


std::string read_file(const std::string& file_name);

std::ostream& operator << (std::ostream& os, const QVector2D& v);
std::ostream& operator << (std::ostream& os, const QVector3D& v);
std::ostream& operator << (std::ostream& os, const QVector4D& v);


#endif

0 comments on commit d68e430

Please sign in to comment.