-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Shader_program added as an abstraction
- Loading branch information
1 parent
eb03e6b
commit 609a13e
Showing
5 changed files
with
176 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters