Playing around with shaders for effects on the scene.

master
Zed A. Shaw 1 month ago
parent 3519c73079
commit 071808a0f8
  1. 3
      animator.cpp
  2. 11
      animator.hpp
  3. BIN
      assets/monster-1.ogg
  4. 9
      main.cpp
  5. 6
      meson.build
  6. 14
      raycaster.cpp
  7. 6
      raycaster.hpp
  8. 23
      shaders/modal.frag

@ -1,5 +1,7 @@
#include "animator.hpp"
#include "constants.hpp"
#include <SFML/Audio.hpp>
#include "dbc.hpp"
void Animator::step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int rect_h) {
if(playing) {
@ -15,4 +17,5 @@ void Animator::step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int
void Animator::play() {
playing = true;
sound.play();
}

@ -1,14 +1,25 @@
#pragma once
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Audio.hpp>
#include <string>
struct Animator {
int width = 0;
int height = 0;
int max_frames = 0;
sf::SoundBuffer buffer;
sf::Sound sound;
size_t count = 0;
int frame = 0;
bool playing = false;
Animator(int w, int h, int max, std::string sound_file) :
width(w), height(h), max_frames(max),
buffer(sound_file),
sound(buffer)
{
}
void step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int rect_h);
void play();

Binary file not shown.

@ -52,6 +52,7 @@ int main() {
Raycaster rayview(window, MAP, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
rayview.position_camera(player_x, player_y);
rayview.init_shaders();
double moveSpeed = 0.1;
double rotSpeed = 0.1;
@ -95,6 +96,14 @@ int main() {
stats.reset();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::P)) {
if(rayview.$active_shader == nullptr) {
rayview.$active_shader = &rayview.$paused;
} else {
rayview.$active_shader = nullptr;
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
rayview.$anim.play();
rotation = -30.0f;

@ -16,6 +16,11 @@ json = dependency('nlohmann_json')
opengl32 = cc.find_library('opengl32', required: true)
winmm = cc.find_library('winmm', required: true)
gdi32 = cc.find_library('gdi32', required: true)
flac = dependency('flac')
ogg = dependency('ogg')
vorbis = dependency('vorbis')
vorbisfile = dependency('vorbisfile')
vorbisenc = dependency('vorbisenc')
sfml_audio = dependency('sfml_audio')
sfml_graphics = dependency('sfml_graphics')
sfml_main = dependency('sfml_main')
@ -29,6 +34,7 @@ endif
dependencies = [
fmt, json, opengl32, freetype2,
flac, ogg, vorbis, vorbisfile, vorbisenc,
winmm, gdi32, sfml_audio, sfml_graphics,
sfml_main, sfml_network, sfml_system,
sfml_window, tracy

@ -36,7 +36,8 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh
$map(map),
spriteOrder(NUM_SPRITES),
spriteDistance(NUM_SPRITES),
ZBuffer(width)
ZBuffer(width),
$anim(256, 256, 10, "assets/monster-1.ogg")
{
$view_sprite.setPosition({0, 0});
$pixels = make_unique<RGBA[]>($width * $height);
@ -46,6 +47,13 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh
$view_texture.setSmooth(false);
}
void Raycaster::init_shaders() {
bool good = $paused.loadFromFile("shaders/modal.frag", sf::Shader::Type::Fragment);
dbc::check(good, "shader could not be loaded");
$paused.setUniform("offsetFactor", sf::Glsl::Vec2{0.01f, 0.01f});
$paused.setUniform("darkness", 0.01f);
}
void Raycaster::set_position(int x, int y) {
$view_sprite.setPosition({(float)x, (float)y});
}
@ -58,7 +66,7 @@ void Raycaster::position_camera(float player_x, float player_y) {
void Raycaster::draw_pixel_buffer() {
$view_texture.update((uint8_t *)$pixels.get(), {(unsigned int)$width, (unsigned int)$height}, {0, 0});
$window.draw($view_sprite);
$window.draw($view_sprite, $active_shader);
}
void Raycaster::clear() {
@ -152,7 +160,7 @@ void Raycaster::sprite_casting() {
$anim.step(*sf_sprite, texX, texY, texX_end - texX, textureHeight);
sf_sprite->setPosition({x, y});
$window.draw(*sf_sprite);
$window.draw(*sf_sprite, $active_shader);
}
}
}

@ -44,7 +44,9 @@ struct Raycaster {
std::vector<int> spriteOrder;
std::vector<double> spriteDistance;
std::vector<double> ZBuffer; // width
Animator $anim{256, 256, 10, 0};
Animator $anim;
sf::Shader $paused;
sf::Shader* $active_shader = nullptr;
Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height);
@ -63,6 +65,8 @@ struct Raycaster {
void position_camera(float player_x, float player_y);
void set_position(int x, int y);
void init_shaders();
inline size_t pixcoord(int x, int y) {
if(!(x >=0 && x < $width)) {
dbc::sentinel(fmt::format("pixcoord x={} but $width={}", x, $width));

@ -0,0 +1,23 @@
uniform sampler2D source;
uniform sampler2D bloom;
uniform vec2 offsetFactor;
uniform float darkness;
void main()
{
vec2 textureCoordinates = gl_TexCoord[0].xy;
vec4 color = vec4(0.0);
color += texture2D(source, textureCoordinates - 4.0 * offsetFactor) * 0.0162162162;
color += texture2D(source, textureCoordinates - 3.0 * offsetFactor) * 0.0540540541;
color += texture2D(source, textureCoordinates - 2.0 * offsetFactor) * 0.1216216216;
color += texture2D(source, textureCoordinates - offsetFactor) * 0.1945945946;
color += texture2D(source, textureCoordinates) * 0.2270270270;
color += texture2D(source, textureCoordinates + offsetFactor) * 0.1945945946;
color += texture2D(source, textureCoordinates + 2.0 * offsetFactor) * 0.1216216216;
color += texture2D(source, textureCoordinates + 3.0 * offsetFactor) * 0.0540540541;
color += texture2D(source, textureCoordinates + 4.0 * offsetFactor) * 0.0162162162;
vec4 sourceFragment = texture2D(source, gl_TexCoord[0].xy);
vec4 bloomFragment = texture2D(bloom, gl_TexCoord[0].xy);
gl_FragColor = color + sourceFragment - bloomFragment - darkness;
}
Loading…
Cancel
Save