Skip to content

Commit

Permalink
Merge pull request #66 from tahtituho/texture_repeat_mode_and_post_te…
Browse files Browse the repository at this point in the history
…xtures

support for different texture wraps and post processing textures
  • Loading branch information
helgrima authored Jun 21, 2023
2 parents 57f4bfb + cbecdd4 commit 519938a
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
# well on Windows or Mac. You can convert this to a matrix build if you need
# cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.23.1)
project(ShaderSystem VERSION 1.5.0)
project(ShaderSystem VERSION 1.6.0)
configure_file(ShaderSystemConfig.h.in ShaderSystemConfig.h)
# Most probably we need to copy exe and dlls files to Debug folder
# Also assets folder and configuration.json should be copied to Debug folder
Expand Down
18 changes: 18 additions & 0 deletions assets/shaders/fragment_post.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@
out vec4 FragColor;

uniform sampler2D mainImage;
uniform sampler2D logo;
uniform float effect1;
in vec2 texPosition;

#define PI 3.14159265359

vec2 rotateUV(vec2 uv, float rotation)
{
float mid = 0.5;
return vec2(
cos(rotation) * (uv.x - mid) + sin(rotation) * (uv.y - mid) + mid,
cos(rotation) * (uv.y - mid) - sin(rotation) * (uv.x - mid) + mid
);
}

void main(void) {
vec4 color = mod(gl_FragCoord.y, 2.0) * texture(mainImage, texPosition);

vec2 logoUv = texPosition;
logoUv.y = (logoUv.y - 1.0) * -1.0;
vec4 logoColor = texture(logo, rotateUV(logoUv * vec2(4.0, 4.0) - vec2(0.1, 0.1), PI * 1.9));

color = mix(color, logoColor, logoColor.a);
color = pow(color, vec4(1.0 / 1.2));
gl_FragColor = color;
}
Binary file added assets/textures/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 13 additions & 2 deletions configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@
{
"type": "texture",
"name": "texture01",
"file": "assets\\textures\\texture01.png"
"file": "assets\\textures\\texture01.png",
"wrapS": "repeat",
"wrapT": "repeat"
},
{
"type": "texture",
"name": "texture02",
"file": "assets\\textures\\texture02.png"
"file": "assets\\textures\\texture02.png",
"wrapS": "repeat",
"wrapT": "repeat"
},
{
"type": "texture",
"name": "logo",
"file": "assets\\textures\\logo.png",
"wrapS": "clampToBorder",
"wrapT": "clampToBorder"
}
],
"variables": [
Expand Down
12 changes: 4 additions & 8 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,12 @@ namespace DemoSystem
bool persistent;
};

struct Asset
struct Texture
{
enum AssetType
{
TEXTURE
};

AssetType type;
std::string file;
std::string name;
std::string wrapS;
std::string wrapT;
};
static Configuration *getInstance();
~Configuration();
Expand All @@ -105,7 +101,7 @@ namespace DemoSystem
Sync sync;
Shaders shaders;
std::list<Configuration::Variable> trackVariables;
std::list<Configuration::Asset> assets;
std::list<Configuration::Texture> textures;

private:
static Configuration *instance;
Expand Down
3 changes: 3 additions & 0 deletions include/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
#define HELPERS_H
#include <string>
#include <fstream>
#include <map>
#include <GL/glew.h>

namespace DemoSystem
{
class Helpers
{
public:
static std::string readFile(std::string file);
static int mapWrapToOpenGL(std::string configuration);
};
} // namespace DemoSystem
#endif
6 changes: 5 additions & 1 deletion include/Textures.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <list>
#include <vector>
#include "Configuration.h"
#include "Helpers.h"

namespace DemoSystem
{
class Textures
Expand All @@ -18,12 +20,14 @@ namespace DemoSystem
unsigned int height;
unsigned int handle;
unsigned int uniform;
int wrapS;
int wrapT;
};

Textures();
~Textures();
std::string add(std::string file, std::string name);
void setTextures(std::list<Configuration::Asset> textures);
void setTextures(std::list<Configuration::Texture> textures);
std::list<Textures::Texture> textures;
};

Expand Down
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(int argc, char *args[])
graphics.initialize(configurations->shaders, configurations->screen, configurations->demo);
logger.initialize(25, 0.0, configurations->screen.height, !configurations->demo.release);

logger.write(DemoSystem::Logger::INFO, "shader system version " + VERSION + " by tahtituho 2022");
logger.write(DemoSystem::Logger::INFO, "shader system version " + VERSION + " by tahtituho 2023");
logger.write(DemoSystem::Logger::INFO, "opengl vendor: " + std::string((const char *)glGetString(GL_VENDOR)));
logger.write(DemoSystem::Logger::INFO, "opengl renderer: " + std::string((const char *)glGetString(GL_RENDERER)));
logger.write(DemoSystem::Logger::INFO, "opengl version: " + std::string((const char *)glGetString(GL_VERSION)));
Expand Down Expand Up @@ -80,7 +80,7 @@ int main(int argc, char *args[])
synchronizer.initializeTrackVariables(configurations->trackVariables);
synchronizer.initializeBasicVariables();

textures.setTextures(configurations->assets);
textures.setTextures(configurations->textures);

// Main Graphics registrations
graphics.registerLogger(&logger);
Expand Down
12 changes: 6 additions & 6 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ bool DemoSystem::Configuration::read(std::string file)
{
for (int i = 0; i < c["assets"].size(); i++)
{
Asset a;
a.name = c["assets"][i]["name"].asString();
a.file = c["assets"][i]["file"].asString();
std::string type = c["assets"][i]["type"].asString();
if (type == "texture")
{
a.type = Configuration::Asset::AssetType::TEXTURE;
Texture a;
a.name = c["assets"][i]["name"].asString();
a.file = c["assets"][i]["file"].asString();
a.wrapS = c["assets"][i]["wrapS"].asString();
a.wrapT = c["assets"][i]["wrapT"].asString();
this->textures.push_back(a);
}

this->assets.push_back(a);
}
}
return true;
Expand Down
28 changes: 25 additions & 3 deletions src/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ void DemoSystem::Framebuffer::drawFBO() {
}
}
}


unsigned int textureIndex = 1;

for (auto it = this->textures->begin(); it != this->textures->end(); it++)
{
glActiveTexture(GL_TEXTURE0 + textureIndex);
glBindTexture(GL_TEXTURE_2D, it->handle);
glUniform1i(it->uniform, textureIndex);
textureIndex++;
}

glBindBuffer(GL_ARRAY_BUFFER, vbo_fbo_vertices);
glVertexAttribPointer(
attribute_position_postproc, // attribute
Expand Down Expand Up @@ -116,12 +126,12 @@ void DemoSystem::Framebuffer::addUniformsPost()
uniform_name = "position";
this->attribute_position_postproc = glGetAttribLocation(this->program, uniform_name);
if (this->attribute_position_postproc == -1) {
this->logger->write(DemoSystem::Logger::ERR, "Could not bind uniform");
this->logger->write(DemoSystem::Logger::ERR, "Could not bind position uniform");
}
uniform_name = "mainImage";
this->uniform_mainImage = glGetUniformLocation(this->program, uniform_name);
if (this->uniform_mainImage == -1) {
this->logger->write(DemoSystem::Logger::ERR, "Could not bind uniform");
this->logger->write(DemoSystem::Logger::ERR, "Could not bind main image uniform");
}
// Tracked uniforms
for (auto tv = this->synchronizer->trackVariables.begin(); tv != this->synchronizer->trackVariables.end(); tv++)
Expand All @@ -131,6 +141,18 @@ void DemoSystem::Framebuffer::addUniformsPost()
tv->uniform = glGetUniformLocation(this->program, tv->name.c_str());
}
}

for (auto it = this->textures->begin(); it != this->textures->end(); it++)
{
glGenTextures(1, &it->handle);
glBindTexture(GL_TEXTURE_2D, it->handle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, it->wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, it->wrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, it->width, it->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &it->image[0]);
it->uniform = glGetUniformLocation(this->program, it->name.c_str());
}
}

void DemoSystem::Framebuffer::resizeFBO(unsigned int width, unsigned int height) {
Expand Down
1 change: 1 addition & 0 deletions src/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void DemoSystem::Graphics::registerCamera(DemoSystem::Camera *camera)
void DemoSystem::Graphics::registerTextures(std::list<Textures::Texture> *textures)
{
this->mainShader.registerTextures(textures);
this->postprocessingShader.registerTextures(textures);
}

void DemoSystem::Graphics::registerKeyboardCallback(GLFWkeyfun callback)
Expand Down
25 changes: 25 additions & 0 deletions src/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,28 @@ std::string DemoSystem::Helpers::readFile(std::string file)
std::string content = std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
return content;
}

int DemoSystem::Helpers::mapWrapToOpenGL(std::string configuration)
{
if (configuration == "repeat")
{
return GL_REPEAT;
}
else if(configuration == "mirroredRepeat")
{
return GL_MIRRORED_REPEAT;
}
else if(configuration == "clampToEdge")
{
return GL_CLAMP_TO_EDGE;
}
else if (configuration == "clampToBorder")
{
return GL_CLAMP_TO_BORDER;
}
else if(configuration == "mirrorClampToEdge")
{
return GL_MIRROR_CLAMP_TO_EDGE;
}
return GL_REPEAT;
}
6 changes: 3 additions & 3 deletions src/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ void DemoSystem::Shader::addUniforms()
{
glGenTextures(1, &it->handle);
glBindTexture(GL_TEXTURE_2D, it->handle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, it->wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, it->wrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, it->width, it->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &it->image[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, it->width, it->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &it->image[0]);
it->uniform = glGetUniformLocation(this->program, it->name.c_str());
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/Textures.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#include "Textures.h"
#include "Helpers.h"

DemoSystem::Textures::Textures()
{
}

void DemoSystem::Textures::setTextures(std::list<Configuration::Asset> assets)
void DemoSystem::Textures::setTextures(std::list<Configuration::Texture> assets)
{
std::list<Configuration::Asset> assetTextures(assets);
assetTextures.remove_if([](Configuration::Asset a) {
return a.type != Configuration::Asset::AssetType::TEXTURE;
});

for (Configuration::Asset asset : assetTextures)
for (Configuration::Texture asset : assets)
{
DemoSystem::Textures::Texture t;
t.name = asset.name;
std::vector<unsigned char> buffer;
lodepng::load_file(buffer, asset.file);
unsigned error = lodepng::decode(t.image, t.width, t.height, buffer);
if (error == 0)
if (error != 0)
{
this->textures.push_back(t);
continue;
}
t.wrapS = Helpers::mapWrapToOpenGL(asset.wrapS);
t.wrapT = Helpers::mapWrapToOpenGL(asset.wrapT);
this->textures.push_back(t);
}
}

Expand Down

0 comments on commit 519938a

Please sign in to comment.