* shader are now dynamically loaded

This commit is contained in:
end 2016-09-29 12:28:35 +02:00
parent aff49c268b
commit 2568c13b15
12 changed files with 44 additions and 59 deletions

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120
varying vec3 vertex;
@ -42,4 +41,3 @@ void main()
gl_FragColor = vec4((IAmbient + IDiffuse) + ISpecular, 1.0);
//gl_FragColor = vec4(0.5+0.5*normal, 1.0);
}
)raw_string"

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120
attribute vec3 in_vertex;
@ -22,4 +21,3 @@ void main()
gl_Position = vec4(p, 1.0);
}
)raw_string"

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120
varying vec2 vertex;
varying vec2 velocity;
@ -15,4 +14,3 @@ void main()
// (length(10.0*velocity));
gl_FragColor = vec4(1.0/max(1.0, decay), 1.0/max(1.0, 6.0*decay), 0.0, 1.0);
}
)raw_string"

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120
attribute vec2 in_vertex;
attribute vec2 in_position;
@ -17,4 +16,3 @@ void main()
vertex = in_vertex;
velocity = in_velocity;
}
)raw_string"

View file

@ -1,8 +1,6 @@
R"raw_string(
#version 120
varying vec3 vertex;
void main()
{
gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0);
}
)raw_string"

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120
varying vec3 vertex;
void main()
@ -6,4 +5,3 @@ void main()
gl_Position = gl_Vertex;
vertex = gl_Position.xyz;
}
)raw_string"

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120
varying vec3 vertex;
uniform vec3 color;
@ -8,4 +7,3 @@ void main()
//gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0);
gl_FragColor = vec4(color, 1.0);
}
)raw_string"

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120
varying vec3 vertex;
uniform mat4 model;
@ -11,4 +10,3 @@ void main()
gl_Position = vec4(p, 1.0);
vertex = p.xyz;
}
)raw_string"

View file

@ -2,6 +2,8 @@
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
namespace endofthejedi {
VAO::VAO() { glGenVertexArrays(1, &m_name); }
@ -80,7 +82,7 @@ namespace endofthejedi {
void Shader::unbind() { glUseProgram(0); }
void Shader::load(const std::string &path, GLenum shadertype) {
void Shader::load(const std::string &data, GLenum shadertype) {
if (m_program == 0) {
std::cout<<"[shader] error: shader program is invalid (0)!" << std::endl;
exit(-1);
@ -89,7 +91,7 @@ namespace endofthejedi {
GLuint shader = glCreateShader(shadertype);
const char *shaderdata = path.c_str();
const char *shaderdata = data.c_str();
glShaderSource(shader, 1, &shaderdata, NULL);
glCompileShader(shader);
checkShader(shader);
@ -101,6 +103,25 @@ namespace endofthejedi {
glDeleteShader(shader);
}
void Shader::loadFile(const std::string &path, GLenum shadertype) {
std::string content;
std::ifstream fileStream(path, std::ios::in);
if(!fileStream.is_open()) {
std::cerr << "Could not read file " << path << ". File does not exist." << std::endl;
return;
}
std::string line = "";
while(!fileStream.eof()) {
std::getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
load(content, shadertype);
}
GLuint Shader::location(const std::string &name) {
return glGetUniformLocation(m_program, name.c_str());
}

View file

@ -57,6 +57,7 @@ class Shader {
void bind();
void unbind();
void load(const std::string &data, GLenum shadertype);
void loadFile(const std::string& data, GLenum shadertype);
GLuint location(const std::string &name);
GLuint program();
};

View file

@ -30,17 +30,12 @@ namespace endofthejedi {
m_data_kind.resize(m_numParticles);
m_data_max_age.resize(m_numParticles);
std::string vss_particles =
#include "particle.vert"
;
std::string fss_particles =
#include "particle.frag"
;
std::string vss_particles = "../data/shader/particle.vert";
std::string fss_particles = "../data/shader/particle.frag";
m_shader.init();
m_shader.load(vss_particles.c_str(), GL_VERTEX_SHADER);
m_shader.load(fss_particles.c_str(), GL_FRAGMENT_SHADER);
m_shader.loadFile(vss_particles, GL_VERTEX_SHADER);
m_shader.loadFile(fss_particles, GL_FRAGMENT_SHADER);
}
ParticleBatch::~ParticleBatch()

View file

@ -19,34 +19,18 @@ namespace endofthejedi {
addModel("../data/mesh/planet_64.stl", &m_planetModel);
addModel("../data/mesh/ship_ufo.stl", &m_shipModel);
std::string vss_simple =
#include "simple.vert"
;
std::string fss_simple =
#include "simple.frag"
;
std::string vss_game_objects =
#include "gameobjects.vert"
;
std::string fss_game_objects =
#include "gameobjects.frag"
;
m_shader.init();
#if 0
(void) vss_simple;
(void) fss_simple;
m_shader.load(vss_simple.c_str(), GL_VERTEX_SHADER);
m_shader.load(fss_simple.c_str(), GL_FRAGMENT_SHADER);
std::string vss_simple = "../data/shader/simple.vert";
std::string fss_simple = "../data/shader/simple.frag";
m_shader.loadFile(vss_simple, GL_VERTEX_SHADER);
m_shader.loadFile(fss_simple, GL_FRAGMENT_SHADER);
#else
(void) vss_game_objects;
(void) fss_game_objects;
m_shader.load(vss_game_objects.c_str(), GL_VERTEX_SHADER);
m_shader.load(fss_game_objects.c_str(), GL_FRAGMENT_SHADER);
std::string vss_game_objects = "../data/shader/gameobjects.vert";
std::string fss_game_objects = "../data/shader/gameobjects.frag";
m_shader.loadFile(vss_game_objects, GL_VERTEX_SHADER);
m_shader.loadFile(fss_game_objects, GL_FRAGMENT_SHADER);
#endif
}