Skip to content

Commit

Permalink
Refactor: Shader_program added as an abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 12, 2023
1 parent eb03e6b commit 609a13e
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 105 deletions.
10 changes: 6 additions & 4 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ qt_standard_project_setup()

qt_add_executable(earth
main.cpp
mainwidget.cpp mainwidget.h
Camera.cpp Camera.h
Common_defs.h
Sphere.cpp Sphere.h
mainwidget.cpp mainwidget.h
Camera.cpp Camera.h
Common_defs.h

Shader_program.cpp Shader_program.h
Sphere.cpp Sphere.h
)


Expand Down
106 changes: 106 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

#include "Shader_program.h"

#include <iostream>


bool Shader_program::init()
{
initializeOpenGLFunctions();

m_program = glCreateProgram();
if (!m_program)
{
std::cout << "error creating shader program!\n";
return false;
}

return true;
}
void Shader_program::add_shader(const char* shader_code, GLenum shader_type)
{
GLuint the_shader = glCreateShader(shader_type);

const GLchar* the_code[] = { shader_code };
GLint code_length[] = { strlen(shader_code) };

glShaderSource(the_shader, 1, the_code, code_length);
glCompileShader(the_shader);


GLint result = 0;
GLchar elog[1024] = { 0 };
glGetShaderiv(the_shader, GL_COMPILE_STATUS, &result);
if (!result)
{
std::string shader_type_name;
switch (shader_type)
{
case GL_VERTEX_SHADER: shader_type_name = "VERTEX"; break;
case GL_GEOMETRY_SHADER: shader_type_name = "GEOMETRY"; break;
case GL_FRAGMENT_SHADER: shader_type_name = "FRAGMENT"; break;
}
glGetShaderInfoLog(the_shader, sizeof(elog), NULL, elog);
std::cout << "! error compiling the " << shader_type_name <<
" shader:\n" << elog << std::endl;
return;
}

glAttachShader(m_program, the_shader);
}

bool Shader_program::link()
{
GLint result = 0;
GLchar elog[1024] = { 0 };

glLinkProgram(m_program);
glGetProgramiv(m_program, GL_LINK_STATUS, &result);
if (!result)
{
glGetProgramInfoLog(m_program, sizeof(elog), NULL, elog);
std::cout << "! error linking program:\n" << elog << std::endl;
return false;
}
return true;
}
bool Shader_program::validate()
{
GLint result = 0;
GLchar elog[1024] = { 0 };

glValidateProgram(m_program);
glGetProgramiv(m_program, GL_VALIDATE_STATUS, &result);
if (!result)
{
glGetProgramInfoLog(m_program, sizeof(elog), NULL, elog);
std::cout << "! error validating program:\n" << elog << std::endl;
return false;
}
return true;
}

GLint Shader_program::get_uniform_location(const GLchar* name)
{
const auto uniform_loc = glGetUniformLocation(m_program, name);
return uniform_loc;
}

void Shader_program::use()
{
glUseProgram(m_program);
}
void Shader_program::unuse()
{
glUseProgram(0);
}

void Shader_program::set_uniform(GLint uniform_loc, const QMatrix4x4& m)
{
glUniformMatrix4fv(uniform_loc, 1, GL_FALSE, m.data());
}
void Shader_program::set_uniform(const GLchar* name, const QMatrix4x4& m)
{
const auto uniform_loc = get_uniform_location(name);
set_uniform(uniform_loc, m);
}
33 changes: 33 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#ifndef SHADER_PROGRAM_H
#define SHADER_PROGRAM_H

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


class Shader_program : protected OpenGLFunctionsBase
{
public:

bool init();

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

bool link();
bool validate();

GLint get_uniform_location(const GLchar* name);

void use();
void unuse();

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

//private:
GLuint m_program;
};


#endif
123 changes: 27 additions & 96 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ void MainWidget::mousePressEvent(QMouseEvent *e)
m_mouse_pressed = true;
m_last_mouse_pos = QVector2D(e->position());
}




void MainWidget::mouseMoveEvent(QMouseEvent* e)
{
auto current_mouse_pos = QVector2D(e->position());
Expand Down Expand Up @@ -135,103 +131,44 @@ void MainWidget::timerEvent(QTimerEvent *)

void MainWidget::initializeGL()
{
initializeOpenGLFunctions();

glClearColor(0, 0, 0, 1);
initializeOpenGLFunctions();

m_camera.set_pos(0,0,10);
init_geometry();
init_shader_program();
init_camera();
init_geometry();
init_shader_program();

// Enable depth buffer
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST); // Enable depth buffer
//glEnable(GL_CULL_FACE); // Enable back face culling

// Enable back face culling
//glEnable(GL_CULL_FACE);

// Use QBasicTimer because its faster than QTimer
m_timer.start(12, this);
// Use QBasicTimer because its faster than QTimer
m_timer.start(12, this);
}

void MainWidget::add_shader(GLuint the_program, const char* shader_code,
GLenum shader_type)
{
GLuint the_shader = glCreateShader(shader_type);

const GLchar* the_code[] = { shader_code };
GLint code_length[] = { strlen(shader_code) };

glShaderSource(the_shader, 1, the_code, code_length);
glCompileShader(the_shader);


GLint result = 0;
GLchar elog[1024] = { 0 };
glGetShaderiv(the_shader, GL_COMPILE_STATUS, &result);
if (!result)
{
std::string shader_type_name;
switch (shader_type)
{
case GL_VERTEX_SHADER: shader_type_name = "VERTEX"; break;
case GL_GEOMETRY_SHADER: shader_type_name = "GEOMETRY"; break;
case GL_FRAGMENT_SHADER: shader_type_name = "FRAGMENT"; break;
}
glGetShaderInfoLog(the_shader, sizeof(elog), NULL, elog);
std::cout << "! error compiling the " << shader_type_name <<
" shader:\n" << elog << std::endl;
return;
}

glAttachShader(the_program, the_shader);
}
void MainWidget::init_shader_program()
void MainWidget::init_camera()
{
m_shader = glCreateProgram();
if (!m_shader)
{
std::cout << "error creating shader program!\n";
return;
}

add_shader(m_shader, vertex_shader_code, GL_VERTEX_SHADER);
//add_shader(shader, geometry_shader_code, GL_GEOMETRY_SHADER);
add_shader(m_shader, fragment_shader_code, GL_FRAGMENT_SHADER);

GLint result = 0;
GLchar elog[1024] = { 0 };

glLinkProgram(m_shader);
glGetProgramiv(m_shader, GL_LINK_STATUS, &result);
if (!result)
{
glGetProgramInfoLog(m_shader, sizeof(elog), NULL, elog);
std::cout << "! error linking program:\n" << elog << std::endl;
return;
}

glValidateProgram(m_shader);
glGetProgramiv(m_shader, GL_VALIDATE_STATUS, &result);
if (!result)
{
glGetProgramInfoLog(m_shader, sizeof(elog), NULL, elog);
std::cout << "! error validating program:\n" << elog << std::endl;
return;
}

m_uniform_mvp = glGetUniformLocation(m_shader, "MVP");
std::cout << "uniform loc = " << m_uniform_mvp << std::endl;
m_camera.set_pos(0, 0, 10);
}



void MainWidget::init_geometry()
{
int num_slices, num_stacks;
num_slices = num_stacks = 64;
float r = 3;
m_sphere = std::make_unique<Sphere>(num_slices, num_stacks, r);
}
void MainWidget::init_shader_program()
{
m_shader_program.init();

m_shader_program.add_shader(vertex_shader_code, GL_VERTEX_SHADER);
//m_program.add_shader(geometry_shader_code, GL_GEOMETRY_SHADER);
m_shader_program.add_shader(fragment_shader_code, GL_FRAGMENT_SHADER);

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




Expand All @@ -254,21 +191,15 @@ void MainWidget::paintGL()
const auto view = m_camera.get_view_matrix();
const auto projection = m_camera.get_projection_matrix();
const auto mvp = projection * view * model;


// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(m_shader);
glUniformMatrix4fv(m_uniform_mvp, 1, GL_FALSE, mvp.data());
m_shader_program.use();
m_shader_program.set_uniform("MVP", mvp);

m_sphere->draw();

glUseProgram(0);
m_shader_program.unuse();
}
}


}
9 changes: 4 additions & 5 deletions Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "Camera.h"
#include "Common_defs.h"
#include "Shader_program.h"
#include "Sphere.h"


Expand All @@ -41,17 +42,15 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
void add_shader(GLuint the_program,
const char* shader_code,
GLenum shader_type);
void init_shader_program();

void init_camera();
void init_geometry();

void init_shader_program();

private:

std::unique_ptr<Sphere> m_sphere;

GLuint m_shader;
GLuint m_uniform_mvp; // uniform location for MVP-matrix in the shader
Shader_program m_shader_program;

// camera & controls
Camera m_camera;
Expand Down

0 comments on commit 609a13e

Please sign in to comment.