58 lines
1.8 KiB
C++
58 lines
1.8 KiB
C++
#ifndef SURFACE_TEXTURE_CLASS
|
|
#define SURFACE_TEXTURE_CLASS
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include "shaderClass.h"
|
|
#include "texture.h"
|
|
|
|
// How one game element wears a texture. Texture is the shared image, this is per element,
|
|
// so the same image can be stretched on one thing and tiled on another.
|
|
class SurfaceTexture {
|
|
public:
|
|
enum class Mode {
|
|
Stretch, // the image fills each face once, whatever size it is
|
|
Tile // the image repeats every tileSize, so it keeps its size as the element grows
|
|
};
|
|
|
|
Texture* texture = nullptr;
|
|
Mode mode = Mode::Stretch;
|
|
float tileSize = 1.0f; // world units on meshes, pixels on gui
|
|
glm::vec2 offset = glm::vec2(0.0f); // slides the image, in repeats of it
|
|
|
|
SurfaceTexture() {}
|
|
|
|
static SurfaceTexture Stretched(Texture* texture) {
|
|
SurfaceTexture surface;
|
|
surface.texture = texture;
|
|
return surface;
|
|
}
|
|
|
|
static SurfaceTexture Tiled(Texture* texture, float tileSize) {
|
|
SurfaceTexture surface;
|
|
surface.texture = texture;
|
|
surface.mode = Mode::Tile;
|
|
surface.tileSize = tileSize;
|
|
return surface;
|
|
}
|
|
|
|
// Binds the image and sets the uv uniforms. size is the element's size, with the shader active.
|
|
void Apply(Shader& shader, GLuint unit, const glm::vec3& size) const {
|
|
glActiveTexture(GL_TEXTURE0 + unit);
|
|
shader.SetInt("tex", unit);
|
|
if (texture == nullptr) {
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
shader.SetInt("useTexture", 0);
|
|
return;
|
|
}
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->ID);
|
|
shader.SetInt("useTexture", 1);
|
|
shader.SetInt("uvMode", mode == Mode::Tile ? 1 : 0);
|
|
shader.Set3F("tileScale", size / glm::max(tileSize, 0.0001f));
|
|
shader.Set2F("uvOffset", offset);
|
|
}
|
|
};
|
|
|
|
#endif
|