post processing light
This commit is contained in:
parent
f15213db1b
commit
70b0fee526
7 changed files with 169 additions and 58 deletions
20
data/shader/multitexture.frag
Normal file
20
data/shader/multitexture.frag
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#version 120
|
||||||
|
|
||||||
|
uniform sampler2D tex0;
|
||||||
|
uniform sampler2D tex1;
|
||||||
|
|
||||||
|
varying vec3 vertex;
|
||||||
|
varying vec2 uv;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec3 color = texture2D(tex0, uv).rgb;
|
||||||
|
vec3 current = texture2D(tex1, uv).rgb;
|
||||||
|
float a = length(color) - length(current);
|
||||||
|
if (a > 0.3) {
|
||||||
|
color = mix(vec3(a/3.0), current, color);
|
||||||
|
} else {
|
||||||
|
color = current;
|
||||||
|
}
|
||||||
|
gl_FragColor = vec4(texture2D(tex0, uv).xyz + texture2D(tex1, uv).xyz, 1.0);
|
||||||
|
}
|
13
data/shader/multitexture.vert
Normal file
13
data/shader/multitexture.vert
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#version 120
|
||||||
|
|
||||||
|
uniform vec2 uvScale;
|
||||||
|
|
||||||
|
varying vec3 vertex;
|
||||||
|
varying vec2 uv;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = gl_Vertex;
|
||||||
|
vertex = gl_Position.xyz;
|
||||||
|
uv = uvScale * (0.5 + vertex.xy / 2.0);
|
||||||
|
}
|
|
@ -42,7 +42,7 @@ void main()
|
||||||
//v /= 1.0 + max(0.0, min(35.0*(decay-0.2), 1.0)) + 10.0 * explCenterDist;
|
//v /= 1.0 + max(0.0, min(35.0*(decay-0.2), 1.0)) + 10.0 * explCenterDist;
|
||||||
v /= 1.0 + 10.0 * explCenterDist;
|
v /= 1.0 + 10.0 * explCenterDist;
|
||||||
|
|
||||||
gl_FragColor = vec4(hsv2rgb(vec3(h, s, v)), 1.0);
|
gl_FragColor = vec4(hsv2rgb(vec3(h, s, v)), v*0.2);
|
||||||
|
|
||||||
//gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
|
//gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
|
||||||
//gl_FragColor.rgb = vec3(0.5+0.5*(a/pi));
|
//gl_FragColor.rgb = vec3(0.5+0.5*(a/pi));
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#version 120
|
#version 120
|
||||||
|
|
||||||
uniform sampler2D tex0;
|
uniform sampler2D tex;
|
||||||
uniform sampler2D tex1;
|
|
||||||
|
|
||||||
varying vec3 vertex;
|
varying vec3 vertex;
|
||||||
varying vec2 uv;
|
varying vec2 uv;
|
||||||
|
@ -18,7 +17,7 @@ void main()
|
||||||
vec2 texCoord = uv.xy - float(int(gaussRadius/2)) * scale;
|
vec2 texCoord = uv.xy - float(int(gaussRadius/2)) * scale;
|
||||||
vec3 color = vec3(0.0, 0.0, 0.0);
|
vec3 color = vec3(0.0, 0.0, 0.0);
|
||||||
for (int i=0; i<gaussRadius; ++i) {
|
for (int i=0; i<gaussRadius; ++i) {
|
||||||
color += gaussFilter[i] * texture2D(tex0, texCoord).xyz;
|
color += gaussFilter[i] * texture2D(tex, texCoord).xyz;
|
||||||
texCoord += scale;
|
texCoord += scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,11 @@ class GameWindow : public endofthejedi::GLWindow {
|
||||||
glClearColor(0.5f, 0.6f, 0.7f, 1.0f);
|
glClearColor(0.5f, 0.6f, 0.7f, 1.0f);
|
||||||
resize();
|
resize();
|
||||||
|
|
||||||
|
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glEnable( GL_BLEND );
|
||||||
|
glEnable(GL_ALPHA_TEST);
|
||||||
|
|
||||||
m_renderer.setup();
|
m_renderer.setup();
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,28 +13,25 @@ namespace endofthejedi {
|
||||||
{
|
{
|
||||||
m_lastTime = -1.0;
|
m_lastTime = -1.0;
|
||||||
|
|
||||||
std::cout<<"setup polygon 3d" << std::endl;
|
//std::cout << "setup polygon 3d" << std::endl;
|
||||||
|
|
||||||
std::string vss_game_objects = "../data/shader/gameobjects.vert";
|
|
||||||
std::string fss_game_objects = "../data/shader/gameobjects.frag";
|
|
||||||
|
|
||||||
m_shader_game_objects.init();
|
m_shader_game_objects.init();
|
||||||
m_shader_game_objects.loadFile(vss_game_objects, GL_VERTEX_SHADER);
|
m_shader_game_objects.loadFile("../data/shader/gameobjects.vert",
|
||||||
m_shader_game_objects.loadFile(fss_game_objects, GL_FRAGMENT_SHADER);
|
GL_VERTEX_SHADER);
|
||||||
|
m_shader_game_objects.loadFile("../data/shader/gameobjects.frag",
|
||||||
std::string vss_particles = "../data/shader/particle.vert";
|
GL_FRAGMENT_SHADER);
|
||||||
std::string fss_particles = "../data/shader/particle.frag";
|
|
||||||
|
|
||||||
m_shader_particles.init();
|
m_shader_particles.init();
|
||||||
m_shader_particles.loadFile(vss_particles, GL_VERTEX_SHADER);
|
m_shader_particles.loadFile("../data/shader/particle.vert",
|
||||||
m_shader_particles.loadFile(fss_particles, GL_FRAGMENT_SHADER);
|
GL_VERTEX_SHADER);
|
||||||
|
m_shader_particles.loadFile("../data/shader/particle.frag",
|
||||||
std::string vss_background = "../data/shader/background.vert";
|
GL_FRAGMENT_SHADER);
|
||||||
std::string fss_background = "../data/shader/background.frag";
|
|
||||||
|
|
||||||
m_shader_background.init();
|
m_shader_background.init();
|
||||||
m_shader_background.loadFile(vss_background, GL_VERTEX_SHADER);
|
m_shader_background.loadFile("../data/shader/background.vert",
|
||||||
m_shader_background.loadFile(fss_background, GL_FRAGMENT_SHADER);
|
GL_VERTEX_SHADER);
|
||||||
|
m_shader_background.loadFile("../data/shader/background.frag",
|
||||||
|
GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
//addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
|
//addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
|
||||||
addModel("../data/mesh/rocket.stl", &m_missileModel);
|
addModel("../data/mesh/rocket.stl", &m_missileModel);
|
||||||
|
@ -51,6 +48,11 @@ namespace endofthejedi {
|
||||||
m_postprocess_shader.loadFile("../data/shader/postprocess.frag",
|
m_postprocess_shader.loadFile("../data/shader/postprocess.frag",
|
||||||
GL_FRAGMENT_SHADER);
|
GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
|
m_multitexture_shader.init();
|
||||||
|
m_multitexture_shader.loadFile("../data/shader/multitexture.vert",
|
||||||
|
GL_VERTEX_SHADER);
|
||||||
|
m_multitexture_shader.loadFile("../data/shader/multitexture.frag",
|
||||||
|
GL_FRAGMENT_SHADER);
|
||||||
m_postprocess_fbo.init();
|
m_postprocess_fbo.init();
|
||||||
m_postprocess_tex0.init();
|
m_postprocess_tex0.init();
|
||||||
m_postprocess_tex1.init();
|
m_postprocess_tex1.init();
|
||||||
|
@ -136,6 +138,7 @@ namespace endofthejedi {
|
||||||
// TODO: add little rocks flying around
|
// TODO: add little rocks flying around
|
||||||
|
|
||||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||||
|
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
m_postprocess_fbo.bind();
|
m_postprocess_fbo.bind();
|
||||||
|
|
||||||
|
@ -164,7 +167,6 @@ namespace endofthejedi {
|
||||||
configureLightningInShader(&m_shader_game_objects);
|
configureLightningInShader(&m_shader_game_objects);
|
||||||
|
|
||||||
//std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl;
|
//std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl;
|
||||||
glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio);
|
|
||||||
|
|
||||||
renderPlanets();
|
renderPlanets();
|
||||||
renderShips();
|
renderShips();
|
||||||
|
@ -174,22 +176,68 @@ namespace endofthejedi {
|
||||||
|
|
||||||
renderTraces();
|
renderTraces();
|
||||||
|
|
||||||
m_postprocess_fbo.unbind();
|
glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio);
|
||||||
|
m_postprocess_tex1.bind();
|
||||||
|
m_postprocess_fbo.attachTexture(GL_COLOR_ATTACHMENT0,
|
||||||
|
m_postprocess_tex1.getName());
|
||||||
|
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
m_shader_game_objects.bind();
|
||||||
|
configureLightningInShader(&m_shader_game_objects);
|
||||||
|
glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio);
|
||||||
|
renderPlanets();
|
||||||
|
renderShips();
|
||||||
|
renderMissiles();
|
||||||
|
|
||||||
|
renderParticles();
|
||||||
|
|
||||||
|
renderTraces();
|
||||||
|
|
||||||
|
// postprocessing
|
||||||
|
m_postprocess_shader.bind();
|
||||||
//glColor3f(1.0, 0.0, 0.0);
|
//glColor3f(1.0, 0.0, 0.0);
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
m_postprocess_tex1.bind();
|
||||||
|
|
||||||
|
glUniform1i(m_postprocess_shader.location("tex"), 0);
|
||||||
|
glUniform2f(m_postprocess_shader.location("uvScale"), 1, 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
glUniform2f(m_postprocess_shader.location("scale"), 2.0f / m_w,
|
||||||
|
0.0f);
|
||||||
|
//glVertex2f(1.0f, -1.0f);
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
glVertex2f(-1.0f, -1.0f);
|
||||||
|
glVertex2f(1.0f, -1.0f);
|
||||||
|
glVertex2f(1.0f, 1.0f);
|
||||||
|
glVertex2f(-1.0f, 1.0f);
|
||||||
|
glEnd();
|
||||||
|
//glVertex2f(1.0f, 1.0f);
|
||||||
|
glUniform2f(m_postprocess_shader.location("scale"), 0.0f,
|
||||||
|
2.0f / m_h);
|
||||||
|
//glVertex2f(-1.0f, 1.0f);
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
glVertex2f(-1.0f, -1.0f);
|
||||||
|
glVertex2f(1.0f, -1.0f);
|
||||||
|
glVertex2f(1.0f, 1.0f);
|
||||||
|
glVertex2f(-1.0f, 1.0f);
|
||||||
|
glEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
m_postprocess_fbo.unbind();
|
||||||
|
|
||||||
|
// multitexturing (final step)
|
||||||
|
m_multitexture_shader.bind();
|
||||||
|
|
||||||
|
glUniform1i(m_multitexture_shader.location("tex0"), 0);
|
||||||
|
glUniform1i(m_multitexture_shader.location("tex1"), 1);
|
||||||
|
glUniform2f(m_multitexture_shader.location("uvScale"), 1, 1);
|
||||||
|
glUniform2f(m_multitexture_shader.location("scale"), 0.2f / 1000.0f, 0.0f);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
m_postprocess_tex0.bind();
|
m_postprocess_tex0.bind();
|
||||||
glActiveTexture(GL_TEXTURE1);
|
glActiveTexture(GL_TEXTURE1);
|
||||||
m_postprocess_tex1.bind();
|
m_postprocess_tex1.bind();
|
||||||
//glBegin(GL_QUADS);
|
|
||||||
m_postprocess_shader.bind();
|
|
||||||
glUniform1i(m_postprocess_shader.location("tex0"), 0);
|
|
||||||
glUniform1i(m_postprocess_shader.location("tex1"), 1);
|
|
||||||
glUniform2f(m_postprocess_shader.location("uvScale"), 1, 1);
|
|
||||||
glUniform2f(m_postprocess_shader.location("scale"), 1.0f / 1000.0f, 0.0f);
|
|
||||||
//glVertex2f(-1.0f, -1.0f);
|
|
||||||
//glVertex2f(1.0f, -1.0f);
|
|
||||||
//glVertex2f(1.0f, 1.0f);
|
|
||||||
//glVertex2f(-1.0f, 1.0f);
|
|
||||||
//glEnd();
|
//glEnd();
|
||||||
glColor3f(1.0, 0.0, 0.0);
|
glColor3f(1.0, 0.0, 0.0);
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
|
@ -304,7 +352,6 @@ namespace endofthejedi {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// simple reflection, ignoring the missile velocity: this looks good enough
|
// simple reflection, ignoring the missile velocity: this looks good enough
|
||||||
(void) missileVelocity;
|
|
||||||
glm::vec3 planetNormal = glm::normalize(pos - glm::vec3(nearestPlanet->position, 0.0f));
|
glm::vec3 planetNormal = glm::normalize(pos - glm::vec3(nearestPlanet->position, 0.0f));
|
||||||
v = glm::length(v) * planetNormal;
|
v = glm::length(v) * planetNormal;
|
||||||
|
|
||||||
|
@ -383,7 +430,7 @@ namespace endofthejedi {
|
||||||
if (!gotIt) {
|
if (!gotIt) {
|
||||||
addExplosionEffect(
|
addExplosionEffect(
|
||||||
expl->id, expl->position,
|
expl->id, expl->position,
|
||||||
expl->missileVelocity,
|
glm::vec3(expl->missileVelocity,1.0),
|
||||||
(expl->hit == game::Hit::Planet),
|
(expl->hit == game::Hit::Planet),
|
||||||
1000, 1.0);
|
1000, 1.0);
|
||||||
}
|
}
|
||||||
|
@ -401,7 +448,7 @@ namespace endofthejedi {
|
||||||
|
|
||||||
addExplosionEffect(
|
addExplosionEffect(
|
||||||
expl->id, expl->position,
|
expl->id, expl->position,
|
||||||
expl->missileVelocity,
|
glm::vec3(expl->missileVelocity,1.0),
|
||||||
(expl->hit == game::Hit::Planet),
|
(expl->hit == game::Hit::Planet),
|
||||||
1000, 1.0);
|
1000, 1.0);
|
||||||
}
|
}
|
||||||
|
@ -453,27 +500,55 @@ namespace endofthejedi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererPolygon3d::renderPlanets()
|
void RendererPolygon3d::renderPlanets() {
|
||||||
{
|
|
||||||
m_planetModel->bind();
|
m_planetModel->bind();
|
||||||
|
|
||||||
// TODO: put material into attributes and render witd glDrawInstanced
|
// TODO: put material into attributes and render witd glDrawInstanced
|
||||||
// too (same for missiles)
|
// too (same for missiles)
|
||||||
for (const game::Planet *planet : m_state->planets) {
|
for (const game::Planet *planet : m_state->planets) {
|
||||||
glm::mat4 model = computeModelMatrix(planet);
|
glm::mat4 model = computeModelMatrix(planet);
|
||||||
glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE,
|
||||||
|
glm::value_ptr(model));
|
||||||
|
|
||||||
glm::vec3 c = planet->getColor();
|
glm::vec3 c = planet->getColor();
|
||||||
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, c.z);
|
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y,
|
||||||
glUniform1i(m_shader_game_objects.location("materialSeed"), planet->seed);
|
c.z);
|
||||||
glUniform1i(m_shader_game_objects.location("materialKind"), (int) planet->material);
|
glUniform1i(m_shader_game_objects.location("materialSeed"),
|
||||||
|
planet->seed);
|
||||||
|
glUniform1i(m_shader_game_objects.location("materialKind"),
|
||||||
|
(int)planet->material);
|
||||||
|
|
||||||
m_planetModel->render();
|
m_planetModel->render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererPolygon3d::renderMissiles()
|
void RendererPolygon3d::renderSun() {
|
||||||
{
|
m_planetModel->bind();
|
||||||
|
|
||||||
|
// TODO: put material into attributes and render witd glDrawInstanced
|
||||||
|
// too (same for missiles)
|
||||||
|
for (const game::Planet *planet : m_state->planets) {
|
||||||
|
|
||||||
|
if (planet->material != game::Planet::Material::Sun)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
glm::mat4 model = computeModelMatrix(planet);
|
||||||
|
glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE,
|
||||||
|
glm::value_ptr(model));
|
||||||
|
|
||||||
|
glm::vec3 c = planet->getColor();
|
||||||
|
glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y,
|
||||||
|
c.z);
|
||||||
|
glUniform1i(m_shader_game_objects.location("materialSeed"),
|
||||||
|
planet->seed);
|
||||||
|
glUniform1i(m_shader_game_objects.location("materialKind"),
|
||||||
|
(int)planet->material);
|
||||||
|
|
||||||
|
m_planetModel->render();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RendererPolygon3d::renderMissiles() {
|
||||||
// TODO: add fire trail for missiles near the sun
|
// TODO: add fire trail for missiles near the sun
|
||||||
|
|
||||||
m_missileModel->bind();
|
m_missileModel->bind();
|
||||||
|
|
|
@ -2,29 +2,27 @@
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtx/euler_angles.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/random.hpp>
|
#include <glm/gtc/random.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
#include <glm/gtx/euler_angles.hpp>
|
||||||
#include <glm/vec3.hpp>
|
#include <glm/vec3.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
|
|
||||||
#include "glclasses.hpp"
|
#include "glclasses.hpp"
|
||||||
|
|
||||||
#include "game.hpp"
|
#include "game.hpp"
|
||||||
|
|
||||||
#include "state/trace.hpp"
|
|
||||||
#include "state/object.hpp"
|
|
||||||
#include "state/missile.hpp"
|
|
||||||
#include "state/player.hpp"
|
|
||||||
#include "state/planet.hpp"
|
|
||||||
#include "state/ship.hpp"
|
|
||||||
#include "state/explosion.hpp"
|
#include "state/explosion.hpp"
|
||||||
|
#include "state/missile.hpp"
|
||||||
|
#include "state/object.hpp"
|
||||||
|
#include "state/planet.hpp"
|
||||||
|
#include "state/player.hpp"
|
||||||
|
#include "state/ship.hpp"
|
||||||
|
#include "state/trace.hpp"
|
||||||
|
|
||||||
|
#include "image_texture.hpp"
|
||||||
#include "particle_batch.hpp"
|
#include "particle_batch.hpp"
|
||||||
#include "polygon_model.hpp"
|
#include "polygon_model.hpp"
|
||||||
#include "image_texture.hpp"
|
|
||||||
|
|
||||||
namespace endofthejedi {
|
namespace endofthejedi {
|
||||||
|
|
||||||
class RendererPolygon3d : public Renderer {
|
class RendererPolygon3d : public Renderer {
|
||||||
|
@ -37,6 +35,7 @@ namespace endofthejedi {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void renderPlanets();
|
void renderPlanets();
|
||||||
|
void renderSun();
|
||||||
void renderMissiles();
|
void renderMissiles();
|
||||||
void renderShips();
|
void renderShips();
|
||||||
void renderParticles();
|
void renderParticles();
|
||||||
|
@ -97,6 +96,7 @@ namespace endofthejedi {
|
||||||
std::string m_backgroundTexturePath;
|
std::string m_backgroundTexturePath;
|
||||||
ImageTexture *m_texture;
|
ImageTexture *m_texture;
|
||||||
Shader m_postprocess_shader;
|
Shader m_postprocess_shader;
|
||||||
|
Shader m_multitexture_shader;
|
||||||
Texture m_postprocess_tex0;
|
Texture m_postprocess_tex0;
|
||||||
Texture m_postprocess_tex1;
|
Texture m_postprocess_tex1;
|
||||||
Framebuffer m_postprocess_fbo;
|
Framebuffer m_postprocess_fbo;
|
||||||
|
|
Loading…
Reference in a new issue