Skip to content

Commit

Permalink
Added supporting of texture Atlases
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriiKoniushenko committed Oct 8, 2023
1 parent f911b46 commit f21eca0
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 232 deletions.
6 changes: 4 additions & 2 deletions game/assets/shaders/widget.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
out vec4 FragColor;

in vec2 ioCv;
in vec2 ioTextRectSize;

uniform sampler2D uTexture;
uniform float uGamma;
Expand Down Expand Up @@ -70,8 +71,9 @@ void main()
}
else
{
vec4 textureColor = texture(uTexture, ioCv);
vec3 diffuseColor = pow(texture(uTexture, ioCv).rgb, vec3(uGamma));
vec2 cv = ioCv + vec2(ioTextRectSize);
vec4 textureColor = texture(uTexture, cv);
vec3 diffuseColor = pow(texture(uTexture, cv).rgb, vec3(uGamma));
vec4 midColor = vec4(diffuseColor, textureColor.a);
vec4 outColor = brightnessMatrix(uBrightness) * contrastMatrix(uContrast) * saturationMatrix(uSaturation) * midColor;

Expand Down
9 changes: 7 additions & 2 deletions game/assets/shaders/widget.vert
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

layout (location = 0) in vec2 aVertex;
layout (location = 1) in vec2 aCv;
layout (location = 2) in vec2 aTextRectSize;

out vec2 ioCv;
out vec2 ioTextRectSize;

uniform vec2 uResolution;
uniform mat4 uTransform;
uniform vec2 uAtlasSize;
uniform vec2 uResolution;

void main()
{
ioCv = aCv;
ioCv = aCv / uAtlasSize;
ioTextRectSize = aTextRectSize / uAtlasSize;

vec4 offset = vec4(1.0f, -1.0f, 0.f, 0.f);
gl_Position = uTransform * vec4(aVertex / uResolution, 0.0, 1.0) - offset;
}
4 changes: 2 additions & 2 deletions game/source/VaKon2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ void VaKon2D::start() {

Widget widget;
widget.setTexture(texture);
widget.calculateFitTextureSize();
widget.setTextureRect(Utils::IRect{{320, 0},
widget.setSize({32, 32});
widget.setTextureRect(Utils::IRect{{352, 0},
{32, 32}});

while (!GetWindow().shouldClose()) {
Expand Down
130 changes: 83 additions & 47 deletions lib/core/core-wrappers/include/ShaderProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,54 +32,90 @@ struct GlColor;

class Shader;

class ShaderProgram : public Utils::NotCopyableButMovable
{
class ShaderProgram : public Utils::NotCopyableButMovable {
public:
explicit ShaderProgram(bool shouldCreate);
ShaderProgram(Shader& frag, Shader& vert);

ShaderProgram(ShaderProgram&& other) noexcept;
ShaderProgram& operator=(ShaderProgram&& other) noexcept;

void recreateAndLink(Shader& frag, Shader& vert);
void create();
void attachShader(Shader& shader);
void link();
void use() const;
_NODISCARD bool wasCreated() const;
_NODISCARD GLuint data();
_NODISCARD bool wasLinked() const;
void deleteProgram();
virtual void OnAfterLink();
_NODISCARD GLint getUniformLocation(const std::string& name);

_NODISCARD bool isValid() const;

void uniform(const std::string& name, const GlColor& color);
void uniform(const std::string& name, GLfloat v0);
void uniform(const std::string& name, GLfloat v0, GLfloat v1);
void uniform(const std::string& name, GLfloat v0, GLfloat v1, GLfloat v2);
void uniform(const std::string& name, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
void uniform(const std::string& name, GLint v0);
void uniform(const std::string& name, GLint v0, GLint v1);
void uniform(const std::string& name, GLint v0, GLint v1, GLint v2);
void uniform(const std::string& name, GLint v0, GLint v1, GLint v2, GLint v3);
void uniform(const std::string& name, GLuint v0);
void uniform(const std::string& name, GLuint v0, GLuint v1);
void uniform(const std::string& name, GLuint v0, GLuint v1, GLuint v2);
void uniform(const std::string& name, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
void uniform(const std::string& name, bool transpose, const glm::mat2& value);
void uniform(const std::string& name, bool transpose, const glm::mat3& value);
void uniform(const std::string& name, bool transpose, const glm::mat4& value);
void uniform(const std::string& name, bool transpose, const glm::mat2x3& value);
void uniform(const std::string& name, bool transpose, const glm::mat3x2& value);
void uniform(const std::string& name, bool transpose, const glm::mat2x4& value);
void uniform(const std::string& name, bool transpose, const glm::mat4x2& value);
void uniform(const std::string& name, bool transpose, const glm::mat3x4& value);
void uniform(const std::string& name, bool transpose, const glm::mat4x3& value);
explicit ShaderProgram(bool shouldCreate);

ShaderProgram(Shader &frag, Shader &vert);

ShaderProgram(ShaderProgram &&other) noexcept;

ShaderProgram &operator=(ShaderProgram &&other) noexcept;

void recreateAndLink(Shader &frag, Shader &vert);

void create();

void attachShader(Shader &shader);

void link();

void use() const;

_NODISCARD bool wasCreated() const;

_NODISCARD GLuint data();

_NODISCARD bool wasLinked() const;

void deleteProgram();

virtual void OnAfterLink();

_NODISCARD GLint getUniformLocation(const std::string &name);

_NODISCARD bool isValid() const;

void uniform(const std::string &name, const glm::ivec2 &color);

void uniform(const std::string &name, const glm::vec2 &color);

void uniform(const std::string &name, const GlColor &color);

void uniform(const std::string &name, GLfloat v0);

void uniform(const std::string &name, GLfloat v0, GLfloat v1);

void uniform(const std::string &name, GLfloat v0, GLfloat v1, GLfloat v2);

void uniform(const std::string &name, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);

void uniform(const std::string &name, GLint v0);

void uniform(const std::string &name, GLint v0, GLint v1);

void uniform(const std::string &name, GLint v0, GLint v1, GLint v2);

void uniform(const std::string &name, GLint v0, GLint v1, GLint v2, GLint v3);

void uniform(const std::string &name, GLuint v0);

void uniform(const std::string &name, GLuint v0, GLuint v1);

void uniform(const std::string &name, GLuint v0, GLuint v1, GLuint v2);

void uniform(const std::string &name, GLuint v0, GLuint v1, GLuint v2, GLuint v3);

void uniform(const std::string &name, bool transpose, const glm::mat2 &value);

void uniform(const std::string &name, bool transpose, const glm::mat3 &value);

void uniform(const std::string &name, bool transpose, const glm::mat4 &value);

void uniform(const std::string &name, bool transpose, const glm::mat2x3 &value);

void uniform(const std::string &name, bool transpose, const glm::mat3x2 &value);

void uniform(const std::string &name, bool transpose, const glm::mat2x4 &value);

void uniform(const std::string &name, bool transpose, const glm::mat4x2 &value);

void uniform(const std::string &name, bool transpose, const glm::mat3x4 &value);

void uniform(const std::string &name, bool transpose, const glm::mat4x3 &value);

private:
GLuint data_ = Gl::Program::invalidId;
bool wasLinked_ = false;
std::unordered_map<std::string, GLint> uniforms_;
GLuint data_ = Gl::Program::invalidId;
bool wasLinked_ = false;
std::unordered_map<std::string, GLint> uniforms_;
};
Loading

0 comments on commit f21eca0

Please sign in to comment.