From 2568c13b15ab8964c95018e8fe0e4446fc254c79 Mon Sep 17 00:00:00 2001 From: end Date: Thu, 29 Sep 2016 12:28:35 +0200 Subject: [PATCH] * shader are now dynamically loaded --- .../shader}/gameobjects.frag | 4 +-- .../shader}/gameobjects.vert | 4 +-- .../shader}/particle.frag | 4 +-- .../shader}/particle.vert | 4 +-- .../shader}/simple.frag | 4 +-- .../shader}/simple.vert | 4 +-- .../shader}/sphere.frag | 4 +-- .../shader}/sphere.vert | 4 +-- game/glclasses.cpp | 25 +++++++++++++-- game/glclasses.hpp | 1 + game/renderer_polygon_3d/particle_batch.cpp | 13 +++----- .../renderer_polygon_3d.cpp | 32 +++++-------------- 12 files changed, 44 insertions(+), 59 deletions(-) rename {game/renderer_polygon_3d => data/shader}/gameobjects.frag (97%) rename {game/renderer_polygon_3d => data/shader}/gameobjects.vert (94%) rename {game/renderer_polygon_3d => data/shader}/particle.frag (93%) rename {game/renderer_polygon_3d => data/shader}/particle.vert (92%) rename {game/renderer_polygon_3d => data/shader}/simple.frag (76%) rename {game/renderer_polygon_3d => data/shader}/simple.vert (78%) rename {game/renderer_polygon_3d => data/shader}/sphere.frag (87%) rename {game/renderer_polygon_3d => data/shader}/sphere.vert (87%) diff --git a/game/renderer_polygon_3d/gameobjects.frag b/data/shader/gameobjects.frag similarity index 97% rename from game/renderer_polygon_3d/gameobjects.frag rename to data/shader/gameobjects.frag index 8955dae..78d2e63 100644 --- a/game/renderer_polygon_3d/gameobjects.frag +++ b/data/shader/gameobjects.frag @@ -1,4 +1,3 @@ -R"raw_string( #version 120 varying vec3 vertex; @@ -41,5 +40,4 @@ void main() //gl_FragColor = vec4(ISpecular, 1.0); gl_FragColor = vec4((IAmbient + IDiffuse) + ISpecular, 1.0); //gl_FragColor = vec4(0.5+0.5*normal, 1.0); -} -)raw_string" +} \ No newline at end of file diff --git a/game/renderer_polygon_3d/gameobjects.vert b/data/shader/gameobjects.vert similarity index 94% rename from game/renderer_polygon_3d/gameobjects.vert rename to data/shader/gameobjects.vert index 1d560eb..7d9c827 100644 --- a/game/renderer_polygon_3d/gameobjects.vert +++ b/data/shader/gameobjects.vert @@ -1,4 +1,3 @@ -R"raw_string( #version 120 attribute vec3 in_vertex; @@ -21,5 +20,4 @@ void main() normal = normalize((model*vec4(in_normal.xyz, 0.0)).xyz); gl_Position = vec4(p, 1.0); -} -)raw_string" +} \ No newline at end of file diff --git a/game/renderer_polygon_3d/particle.frag b/data/shader/particle.frag similarity index 93% rename from game/renderer_polygon_3d/particle.frag rename to data/shader/particle.frag index 558dc62..c79fd63 100644 --- a/game/renderer_polygon_3d/particle.frag +++ b/data/shader/particle.frag @@ -1,4 +1,3 @@ -R"raw_string( #version 120 varying vec2 vertex; varying vec2 velocity; @@ -14,5 +13,4 @@ void main() decay = 5.0*decay*decay; // (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" \ No newline at end of file +} \ No newline at end of file diff --git a/game/renderer_polygon_3d/particle.vert b/data/shader/particle.vert similarity index 92% rename from game/renderer_polygon_3d/particle.vert rename to data/shader/particle.vert index 9894d7b..4815a04 100644 --- a/game/renderer_polygon_3d/particle.vert +++ b/data/shader/particle.vert @@ -1,4 +1,3 @@ -R"raw_string( #version 120 attribute vec2 in_vertex; attribute vec2 in_position; @@ -16,5 +15,4 @@ void main() vertex = in_vertex; velocity = in_velocity; -} -)raw_string" \ No newline at end of file +} \ No newline at end of file diff --git a/game/renderer_polygon_3d/simple.frag b/data/shader/simple.frag similarity index 76% rename from game/renderer_polygon_3d/simple.frag rename to data/shader/simple.frag index 7bf2e0d..15dc3ce 100644 --- a/game/renderer_polygon_3d/simple.frag +++ b/data/shader/simple.frag @@ -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" +} \ No newline at end of file diff --git a/game/renderer_polygon_3d/simple.vert b/data/shader/simple.vert similarity index 78% rename from game/renderer_polygon_3d/simple.vert rename to data/shader/simple.vert index b05d873..0f78a65 100644 --- a/game/renderer_polygon_3d/simple.vert +++ b/data/shader/simple.vert @@ -1,9 +1,7 @@ -R"raw_string( #version 120 varying vec3 vertex; void main() { gl_Position = gl_Vertex; vertex = gl_Position.xyz; -} -)raw_string" +} \ No newline at end of file diff --git a/game/renderer_polygon_3d/sphere.frag b/data/shader/sphere.frag similarity index 87% rename from game/renderer_polygon_3d/sphere.frag rename to data/shader/sphere.frag index 90e0164..77594cf 100644 --- a/game/renderer_polygon_3d/sphere.frag +++ b/data/shader/sphere.frag @@ -1,4 +1,3 @@ -R"raw_string( #version 120 varying vec3 vertex; uniform vec3 color; @@ -7,5 +6,4 @@ void main() //gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); //gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0); gl_FragColor = vec4(color, 1.0); -} -)raw_string" \ No newline at end of file +} \ No newline at end of file diff --git a/game/renderer_polygon_3d/sphere.vert b/data/shader/sphere.vert similarity index 87% rename from game/renderer_polygon_3d/sphere.vert rename to data/shader/sphere.vert index ccf4687..9ca5c0c 100644 --- a/game/renderer_polygon_3d/sphere.vert +++ b/data/shader/sphere.vert @@ -1,4 +1,3 @@ -R"raw_string( #version 120 varying vec3 vertex; uniform mat4 model; @@ -10,5 +9,4 @@ void main() vec3 p = (model*gl_Vertex).xyz; gl_Position = vec4(p, 1.0); vertex = p.xyz; -} -)raw_string" \ No newline at end of file +} \ No newline at end of file diff --git a/game/glclasses.cpp b/game/glclasses.cpp index 0787a2e..51cdd2e 100644 --- a/game/glclasses.cpp +++ b/game/glclasses.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include 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()); } diff --git a/game/glclasses.hpp b/game/glclasses.hpp index cc75484..7247ae7 100644 --- a/game/glclasses.hpp +++ b/game/glclasses.hpp @@ -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(); }; diff --git a/game/renderer_polygon_3d/particle_batch.cpp b/game/renderer_polygon_3d/particle_batch.cpp index 3a5b2d6..1e0f4a5 100644 --- a/game/renderer_polygon_3d/particle_batch.cpp +++ b/game/renderer_polygon_3d/particle_batch.cpp @@ -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() diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index 0b3b2a5..9762f12 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -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 }