@@ -18,84 +18,103 @@ namespace BlueBear {

GLint Shader::CURRENT_PROGRAM = -1;

Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath) {
// 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensures ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::badbit);
try
{
// Open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// Convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure e) {
Log::getInstance().error( "Shader::Shader", "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" );
}
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar * fShaderCode = fragmentCode.c_str();
// 2. Compile shaders
GLuint vertex, fragment;
GLint success;
GLchar infoLog[512];
// Vertex Shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
// Print compile errors if any
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::stringstream stream;
stream << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
Log::getInstance().error( "Shader::Shader", stream.str() );
}
// Fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
// Print compile errors if any
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::stringstream stream;
stream << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog;
Log::getInstance().error( "Shader::Shader", stream.str() );
}
// Shader Program
this->Program = glCreateProgram();
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
glLinkProgram(this->Program);
// Print linking errors if any
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
std::stringstream stream;
stream << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog;
Log::getInstance().error( "Shader::Shader", stream.str() );
}
// Delete the shaders as they're linked into our program now and no longer necessery
glDeleteShader(vertex);
glDeleteShader(fragment);
void Shader::sendDeferred() {
if( vPath.empty() && fPath.empty() ) {
return;
}

const char* vertexPath = vPath.c_str();
const char* fragmentPath = fPath.c_str();

// 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensures ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::badbit);
try
{
// Open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// Convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure e) {
Log::getInstance().error( "Shader::Shader", "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" );
}
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar * fShaderCode = fragmentCode.c_str();
// 2. Compile shaders
GLuint vertex, fragment;
GLint success;
GLchar infoLog[512];
// Vertex Shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
// Print compile errors if any
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::stringstream stream;
stream << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
Log::getInstance().error( "Shader::Shader", stream.str() );
}
// Fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
// Print compile errors if any
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::stringstream stream;
stream << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog;
Log::getInstance().error( "Shader::Shader", stream.str() );
}
// Shader Program
this->Program = glCreateProgram();
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
glLinkProgram(this->Program);
// Print linking errors if any
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
std::stringstream stream;
stream << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog;
Log::getInstance().error( "Shader::Shader", stream.str() );
}
// Delete the shaders as they're linked into our program now and no longer necessery
glDeleteShader(vertex);
glDeleteShader(fragment);

vPath.clear();
fPath.clear();
}

Shader::Shader( const std::string& vertexPath, const std::string& fragmentPath, bool defer ) : vPath( vertexPath ), fPath( fragmentPath ) {
if( !defer ) {
sendDeferred();
}
}

Shader::Shader( const GLchar* vertexPath, const GLchar* fragmentPath ) : vPath( vertexPath ), fPath( fragmentPath ) {
sendDeferred();
}

void Shader::use() {
@@ -0,0 +1,13 @@
#version 330 core
in vec2 fragTexture;
in vec3 fragNormal;
in vec3 fragPos;

out vec4 color;

uniform vec3 cameraPos;
uniform sampler2D diffuse0;

void main() {
color = texture( diffuse0, fragTexture );
}
@@ -0,0 +1,32 @@
#version 330 core
layout (location = 0) in vec3 position; // The position variable has attribute position 0
layout (location = 1) in vec3 normal; // This is currently unused
layout (location = 2) in vec2 texture;
layout (location = 3) in ivec4 boneIDs;
layout (location = 4) in vec4 boneWeights;

out vec2 fragTexture;
out vec3 fragNormal;
out vec3 fragPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform mat4 bones[ 16 ];

void main()
{
mat4 boneTransform =
( bones[ boneIDs[ 0 ] ] * boneWeights[ 0 ] ) +
( bones[ boneIDs[ 1 ] ] * boneWeights[ 1 ] ) +
( bones[ boneIDs[ 2 ] ] * boneWeights[ 2 ] ) +
( bones[ boneIDs[ 3 ] ] * boneWeights[ 3 ] );

mat4 mvp = projection * view * model;

gl_Position = mvp * boneTransform * vec4( position, 1.0f );
fragTexture = texture;
fragNormal = normal;
fragPos = vec3( model * vec4( position, 1.0 ) );
}
@@ -0,0 +1,13 @@
#version 330 core
in vec2 fragTexture;
in vec3 fragNormal;
in vec3 fragPos;

out vec4 color;

uniform vec3 cameraPos;
uniform sampler2D diffuse0;

void main() {
color = texture( diffuse0, fragTexture );
}
@@ -0,0 +1,32 @@
#version 330 core
layout (location = 0) in vec3 position; // The position variable has attribute position 0
layout (location = 1) in vec3 normal; // This is currently unused
layout (location = 2) in vec2 texture;
layout (location = 3) in ivec4 boneIDs;
layout (location = 4) in vec4 boneWeights;

out vec2 fragTexture;
out vec3 fragNormal;
out vec3 fragPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform mat4 bones[ 16 ];

void main()
{
mat4 boneTransform =
( bones[ boneIDs[ 0 ] ] * boneWeights[ 0 ] ) +
( bones[ boneIDs[ 1 ] ] * boneWeights[ 1 ] ) +
( bones[ boneIDs[ 2 ] ] * boneWeights[ 2 ] ) +
( bones[ boneIDs[ 3 ] ] * boneWeights[ 3 ] );

mat4 mvp = projection * view * model;

gl_Position = mvp * boneTransform * vec4( position, 1.0f );
fragTexture = texture;
fragNormal = normal;
fragPos = vec3( model * vec4( position, 1.0 ) );
}
@@ -0,0 +1,13 @@
#version 330 core
in vec2 fragTexture;
in vec3 fragNormal;
in vec3 fragPos;

out vec4 color;

uniform vec3 cameraPos;
uniform sampler2D diffuse0;

void main() {
color = texture( diffuse0, fragTexture );
}
@@ -0,0 +1,32 @@
#version 330 core
layout (location = 0) in vec3 position; // The position variable has attribute position 0
layout (location = 1) in vec3 normal; // This is currently unused
layout (location = 2) in vec2 texture;
layout (location = 3) in ivec4 boneIDs;
layout (location = 4) in vec4 boneWeights;

out vec2 fragTexture;
out vec3 fragNormal;
out vec3 fragPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform mat4 bones[ 16 ];

void main()
{
mat4 boneTransform =
( bones[ boneIDs[ 0 ] ] * boneWeights[ 0 ] ) +
( bones[ boneIDs[ 1 ] ] * boneWeights[ 1 ] ) +
( bones[ boneIDs[ 2 ] ] * boneWeights[ 2 ] ) +
( bones[ boneIDs[ 3 ] ] * boneWeights[ 3 ] );

mat4 mvp = projection * view * model;

gl_Position = mvp * boneTransform * vec4( position, 1.0f );
fragTexture = texture;
fragNormal = normal;
fragPos = vec3( model * vec4( position, 1.0 ) );
}
@@ -0,0 +1,13 @@
#version 330 core
in vec2 fragTexture;
in vec3 fragNormal;
in vec3 fragPos;

out vec4 color;

uniform vec3 cameraPos;
uniform sampler2D diffuse0;

void main() {
color = texture( diffuse0, fragTexture );
}
@@ -0,0 +1,32 @@
#version 330 core
layout (location = 0) in vec3 position; // The position variable has attribute position 0
layout (location = 1) in vec3 normal; // This is currently unused
layout (location = 2) in vec2 texture;
layout (location = 3) in ivec4 boneIDs;
layout (location = 4) in vec4 boneWeights;

out vec2 fragTexture;
out vec3 fragNormal;
out vec3 fragPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform mat4 bones[ 16 ];

void main()
{
mat4 boneTransform =
( bones[ boneIDs[ 0 ] ] * boneWeights[ 0 ] ) +
( bones[ boneIDs[ 1 ] ] * boneWeights[ 1 ] ) +
( bones[ boneIDs[ 2 ] ] * boneWeights[ 2 ] ) +
( bones[ boneIDs[ 3 ] ] * boneWeights[ 3 ] );

mat4 mvp = projection * view * model;

gl_Position = mvp * boneTransform * vec4( position, 1.0f );
fragTexture = texture;
fragNormal = normal;
fragPos = vec3( model * vec4( position, 1.0 ) );
}