This commit is contained in:
Andreas Ortmann 2016-09-29 17:54:01 +02:00
commit c06e138b3e
14 changed files with 48 additions and 60 deletions

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120 #version 120
varying vec3 vertex; varying vec3 vertex;
@ -42,4 +41,3 @@ void main()
gl_FragColor = vec4((IAmbient + IDiffuse) + ISpecular, 1.0); gl_FragColor = vec4((IAmbient + IDiffuse) + ISpecular, 1.0);
//gl_FragColor = vec4(0.5+0.5*normal, 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 #version 120
attribute vec3 in_vertex; attribute vec3 in_vertex;
@ -22,4 +21,3 @@ void main()
gl_Position = vec4(p, 1.0); gl_Position = vec4(p, 1.0);
} }
)raw_string"

View file

@ -1,4 +1,3 @@
R"raw_string(
#version 120 #version 120
varying vec2 vertex; varying vec2 vertex;
varying vec2 velocity; varying vec2 velocity;
@ -15,4 +14,3 @@ void main()
// (length(10.0*velocity)); // (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); 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 #version 120
attribute vec2 in_vertex; attribute vec2 in_vertex;
attribute vec2 in_position; attribute vec2 in_position;
@ -17,4 +16,3 @@ void main()
vertex = in_vertex; vertex = in_vertex;
velocity = in_velocity; velocity = in_velocity;
} }
)raw_string"

View file

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

View file

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

View file

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

View file

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

View file

@ -2,6 +2,8 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <fstream>
#include <sstream>
namespace endofthejedi { namespace endofthejedi {
VAO::VAO() { glGenVertexArrays(1, &m_name); } VAO::VAO() { glGenVertexArrays(1, &m_name); }
@ -80,7 +82,7 @@ namespace endofthejedi {
void Shader::unbind() { glUseProgram(0); } 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) { if (m_program == 0) {
std::cout<<"[shader] error: shader program is invalid (0)!" << std::endl; std::cout<<"[shader] error: shader program is invalid (0)!" << std::endl;
exit(-1); exit(-1);
@ -89,7 +91,7 @@ namespace endofthejedi {
GLuint shader = glCreateShader(shadertype); GLuint shader = glCreateShader(shadertype);
const char *shaderdata = path.c_str(); const char *shaderdata = data.c_str();
glShaderSource(shader, 1, &shaderdata, NULL); glShaderSource(shader, 1, &shaderdata, NULL);
glCompileShader(shader); glCompileShader(shader);
checkShader(shader); checkShader(shader);
@ -101,6 +103,25 @@ namespace endofthejedi {
glDeleteShader(shader); 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) { GLuint Shader::location(const std::string &name) {
return glGetUniformLocation(m_program, name.c_str()); return glGetUniformLocation(m_program, name.c_str());
} }

View file

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

View file

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

View file

@ -1,5 +1,7 @@
#include "polygon_model.hpp" #include "polygon_model.hpp"
#include <iostream>
void discardLastGlError() void discardLastGlError()
{ {
glGetError(); glGetError();
@ -22,6 +24,7 @@ void checkAndPrintGlError()
std::cout<<"glGetAttribLocation() returned error: " << errString << std::endl; std::cout<<"glGetAttribLocation() returned error: " << errString << std::endl;
} }
} }
namespace endofthejedi { namespace endofthejedi {
PolygonModel::PolygonModel(const std::string &filename) PolygonModel::PolygonModel(const std::string &filename)
: m_filename(filename) : m_filename(filename)

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <iostream> #include <vector>
#include <assimp/Importer.hpp> // C++ importer interface #include <assimp/Importer.hpp> // C++ importer interface
#include <assimp/scene.h> // Output data structure #include <assimp/scene.h> // Output data structure

View file

@ -13,34 +13,18 @@ namespace endofthejedi {
std::cout<<"setup polygon 3d" << std::endl; std::cout<<"setup polygon 3d" << std::endl;
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(); m_shader.init();
#if 0 #if 0
(void) vss_simple; std::string vss_simple = "../data/shader/simple.vert";
(void) fss_simple; std::string fss_simple = "../data/shader/simple.frag";
m_shader.load(vss_simple.c_str(), GL_VERTEX_SHADER); m_shader.loadFile(vss_simple, GL_VERTEX_SHADER);
m_shader.load(fss_simple.c_str(), GL_FRAGMENT_SHADER); m_shader.loadFile(fss_simple, GL_FRAGMENT_SHADER);
#else #else
(void) vss_game_objects; std::string vss_game_objects = "../data/shader/gameobjects.vert";
(void) fss_game_objects; std::string fss_game_objects = "../data/shader/gameobjects.frag";
m_shader.load(vss_game_objects.c_str(), GL_VERTEX_SHADER); m_shader.loadFile(vss_game_objects, GL_VERTEX_SHADER);
m_shader.load(fss_game_objects.c_str(), GL_FRAGMENT_SHADER); m_shader.loadFile(fss_game_objects, GL_FRAGMENT_SHADER);
#endif #endif
addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel); addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);