implemented SDL3 as an option for window manager
This commit is contained in:
143
TSE_OpenGlImpl/src/shader/Shader.cpp
Normal file
143
TSE_OpenGlImpl/src/shader/Shader.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "GL/gl3w.h"
|
||||
#include "GL/gl.h"
|
||||
#include "Shader.hpp"
|
||||
#include "Debug.hpp"
|
||||
|
||||
TSE::uint TSE::OpenGL::Shader::activeProgramID = 0;
|
||||
|
||||
void TSE::OpenGL::Shader::Bind() const
|
||||
{
|
||||
Enable(true);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::Unbind() const
|
||||
{
|
||||
Disable(true);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::Enable(bool notify) const
|
||||
{
|
||||
activeProgramID = programID;
|
||||
glUseProgram(programID);
|
||||
if(notify) OnEnable();
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::Disable(bool notify) const
|
||||
{
|
||||
activeProgramID = 0;
|
||||
glUseProgram(0);
|
||||
if(notify) OnDisable();
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::Flush()
|
||||
{
|
||||
OnFlush();
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::DrawCall(int indexCount)
|
||||
{
|
||||
OnDrawCall(indexCount);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::PostDraw()
|
||||
{
|
||||
OnPostDraw();
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::Submit(const Transformable &t, float *&target, TransformationStack &stack, void (*restartDrawcall)(IRenderer &), IRenderer &rnd)
|
||||
{
|
||||
OnSubmit(t, target, stack, restartDrawcall, rnd);
|
||||
}
|
||||
|
||||
bool TSE::OpenGL::Shader::IsEnabled() const
|
||||
{
|
||||
return programID == activeProgramID;
|
||||
}
|
||||
|
||||
int TSE::OpenGL::Shader::packageSize()
|
||||
{
|
||||
return PackageSize;
|
||||
}
|
||||
|
||||
TSE::OpenGL::Shader::Shader(const std::vector<std::unique_ptr<ShaderPart>> &parts)
|
||||
{
|
||||
programID = glCreateProgram();
|
||||
|
||||
for (const auto& part : parts) {
|
||||
glAttachShader(programID, part->shaderPartID);
|
||||
}
|
||||
|
||||
glLinkProgram(programID);
|
||||
|
||||
int success;
|
||||
|
||||
glGetProgramiv(programID, GL_LINK_STATUS, &success);
|
||||
|
||||
if(!success)
|
||||
{
|
||||
char log[512];
|
||||
glGetProgramInfoLog(programID, 512, nullptr, log);
|
||||
TSE_ERROR(log);
|
||||
}
|
||||
|
||||
for (const auto& part : parts) {
|
||||
glDetachShader(programID, part->shaderPartID);
|
||||
}
|
||||
}
|
||||
|
||||
TSE::OpenGL::Shader::~Shader()
|
||||
{
|
||||
glDeleteProgram(programID);
|
||||
}
|
||||
|
||||
int TSE::OpenGL::Shader::GetUniformLocation(const char *name)
|
||||
{
|
||||
auto it = uniformLocations.find(name);
|
||||
if (it != uniformLocations.end()) return it->second;
|
||||
|
||||
int loc = glGetUniformLocation(programID, name);
|
||||
uniformLocations[name] = loc;
|
||||
return loc;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::SetUniform(const char *name, int value)
|
||||
{
|
||||
glUniform1i(GetUniformLocation(name), value);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::SetUniform(const char *name, const int *value, const int count)
|
||||
{
|
||||
glUniform1iv(GetUniformLocation(name), count, value);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::SetUniform(const char *name, const Matrix4x4 *value)
|
||||
{
|
||||
float colmbMajor[16];
|
||||
value->ToArrayColumnMajor(colmbMajor);
|
||||
glUniformMatrix4fv(GetUniformLocation(name),1, false, colmbMajor);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::SetUniform(const char *name, float value)
|
||||
{
|
||||
glUniform1f(GetUniformLocation(name), value);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::SetUniform(const char *name, const float *value, const int count)
|
||||
{
|
||||
glUniform1fv(GetUniformLocation(name), count, value);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::SetUniform(const char *name, const Vector2 *value)
|
||||
{
|
||||
glUniform2f(GetUniformLocation(name), value->x, value->y);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::SetUniform(const char *name, const Vector3 *value)
|
||||
{
|
||||
glUniform3f(GetUniformLocation(name), value->x, value->y, value->z);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::Shader::SetUniform(const char *name, const Vector4 *value)
|
||||
{
|
||||
glUniform4f(GetUniformLocation(name), value->x, value->y, value->z, value->w);
|
||||
}
|
||||
57
TSE_OpenGlImpl/src/shader/Shader.hpp
Normal file
57
TSE_OpenGlImpl/src/shader/Shader.hpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "ShaderPart.hpp"
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "Types.hpp"
|
||||
#include "interfaces/IShader.hpp"
|
||||
#include "elements/Transformable.hpp"
|
||||
#include "TransformationStack.hpp"
|
||||
#include "interfaces/IRenderer.hpp"
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
class Shader : public IShader
|
||||
{
|
||||
private:
|
||||
static uint activeProgramID;
|
||||
uint programID;
|
||||
mutable std::unordered_map<std::string, int> uniformLocations;
|
||||
|
||||
protected:
|
||||
int PackageSize;
|
||||
virtual void OnEnable() const {};
|
||||
virtual void OnDisable() const {};
|
||||
virtual void OnFlush() {};
|
||||
virtual void OnDrawCall(int indexCount) {};
|
||||
virtual void OnPostDraw() {};
|
||||
virtual void OnSubmit(const Transformable& t, float*& target, TransformationStack& stack, void (*restartDrawcall)(IRenderer&), IRenderer& rnd) {};
|
||||
|
||||
public:
|
||||
void Bind() const override;
|
||||
void Unbind() const override;
|
||||
void Enable(bool notify = false) const;
|
||||
void Disable(bool notify = false) const;
|
||||
void Flush();
|
||||
void DrawCall(int indexCount);
|
||||
void PostDraw();
|
||||
void Submit(const Transformable& t, float*& target, TransformationStack& stack, void (*restartDrawcall)(IRenderer&), IRenderer& rnd);
|
||||
bool IsEnabled() const;
|
||||
int packageSize();
|
||||
Shader(const std::vector<std::unique_ptr<ShaderPart>>& parts);
|
||||
virtual ~Shader();
|
||||
|
||||
protected:
|
||||
int GetUniformLocation(const char* name);
|
||||
|
||||
public:
|
||||
void SetUniform(const char* name, int value) override;
|
||||
void SetUniform(const char* name, const int* value, const int count) override;
|
||||
void SetUniform(const char* name, const Matrix4x4* value) override;
|
||||
void SetUniform(const char* name, float value) override;
|
||||
void SetUniform(const char* name, const float* value, const int count) override;
|
||||
void SetUniform(const char* name, const Vector2* value) override;
|
||||
void SetUniform(const char* name, const Vector3* value) override;
|
||||
void SetUniform(const char* name, const Vector4* value) override;
|
||||
};
|
||||
} // namespace TSE::OpenGL
|
||||
49
TSE_OpenGlImpl/src/shader/ShaderPart.cpp
Normal file
49
TSE_OpenGlImpl/src/shader/ShaderPart.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "GL/gl3w.h"
|
||||
#include "GL/gl.h"
|
||||
#include "ShaderPart.hpp"
|
||||
|
||||
#include "Debug.hpp"
|
||||
#include <fstream>
|
||||
#include "PathHelper.hpp"
|
||||
|
||||
void TSE::OpenGL::ShaderPart::Init(const string &str, int shaderType)
|
||||
{
|
||||
shaderPartID = glCreateShader(shaderType);
|
||||
const char * cstr = str.c_str();
|
||||
int length = str.length();
|
||||
glShaderSource(shaderPartID, 1, &cstr, &length);
|
||||
glCompileShader(shaderPartID);
|
||||
|
||||
int success;
|
||||
glGetShaderiv(shaderPartID, GL_COMPILE_STATUS, &success);
|
||||
|
||||
if(success == GL_FALSE)
|
||||
{
|
||||
int errorLength = 255;
|
||||
char* log = new char[errorLength];
|
||||
glGetShaderInfoLog(shaderPartID, errorLength, &errorLength, log);
|
||||
|
||||
TSE_ERROR(log);
|
||||
}
|
||||
}
|
||||
|
||||
TSE::OpenGL::ShaderPart::~ShaderPart()
|
||||
{
|
||||
glDeleteShader(shaderPartID);
|
||||
}
|
||||
|
||||
std::unique_ptr<TSE::OpenGL::ShaderPart> TSE::OpenGL::ShaderPart::LoadFromString(const std::string &str, int shaderType)
|
||||
{
|
||||
if (str.length() == 0) throw;
|
||||
std::unique_ptr<ShaderPart> shader = std::make_unique<ShaderPart>();
|
||||
shader->Init(str, shaderType);
|
||||
return shader;
|
||||
}
|
||||
|
||||
std::unique_ptr<TSE::OpenGL::ShaderPart> TSE::OpenGL::ShaderPart::LoadFromPath(const std::string &path, int shaderType)
|
||||
{
|
||||
std::ifstream stream;
|
||||
OpenFileReading(stream, path);
|
||||
std::string filecontent((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
|
||||
return LoadFromString(filecontent, shaderType);
|
||||
}
|
||||
23
TSE_OpenGlImpl/src/shader/ShaderPart.hpp
Normal file
23
TSE_OpenGlImpl/src/shader/ShaderPart.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "Types.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
class ShaderPart
|
||||
{
|
||||
public:
|
||||
uint shaderPartID = 0;
|
||||
|
||||
ShaderPart() = default;
|
||||
|
||||
private:
|
||||
void Init(const string& str, int shaderType);
|
||||
|
||||
public:
|
||||
~ShaderPart();
|
||||
static std::unique_ptr<ShaderPart> LoadFromString(const std::string& str, int shaderType);
|
||||
static std::unique_ptr<ShaderPart> LoadFromPath(const std::string& path, int shaderType);
|
||||
};
|
||||
} // namespace OpenGL
|
||||
238
TSE_OpenGlImpl/src/shader/basicOrderedSpriteSetShader.cpp
Normal file
238
TSE_OpenGlImpl/src/shader/basicOrderedSpriteSetShader.cpp
Normal file
@@ -0,0 +1,238 @@
|
||||
#include "basicOrderedSpriteSetShader.hpp"
|
||||
#include "BehaviourScripts/Renderable.hpp"
|
||||
#include "BehaviourScripts/OrdererSpriteSet.hpp"
|
||||
#include "Color.hpp"
|
||||
#include "basicOrderedSpriteSetShaderGLSL.hpp"
|
||||
|
||||
using namespace TSE;
|
||||
using namespace TSE::OpenGL;
|
||||
|
||||
#define SHADER_MESH_INDEX 0
|
||||
#define SHADER_POS_INDEX 1
|
||||
#define SHADER_LAYER_HEIGHT_INDEX 2
|
||||
#define SHADER_SPRITE_INDEX 3
|
||||
#define SHADER_NORMAL_INDEX 4
|
||||
#define SHADER_SCALE_INDEX 5
|
||||
|
||||
#define SHADER_PACKAGE_SIZE sizeof(float) * (3 + 1 + 1 + 1 + 2)
|
||||
|
||||
|
||||
TSE::OpenGL::BasicOrderedSpriteSetShader* BasicOrderedSpriteSetShader::instance = nullptr;
|
||||
|
||||
TSE::OpenGL::BasicOrderedSpriteSetShader *TSE::OpenGL::BasicOrderedSpriteSetShader::Instance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::Destroy()
|
||||
{
|
||||
if(instance != nullptr)
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::Init(float width, float height)
|
||||
{
|
||||
std::vector<std::unique_ptr<ShaderPart>> parts;
|
||||
parts.push_back(ShaderPart::LoadFromString(vertOrderedSet, GL_VERTEX_SHADER));
|
||||
parts.push_back(ShaderPart::LoadFromString(fragOrderedSet, GL_FRAGMENT_SHADER));
|
||||
instance = new BasicOrderedSpriteSetShader(std::move(parts));
|
||||
|
||||
instance->Enable();
|
||||
int texIDs[] = { 0 };
|
||||
instance->SetUniform("atlas", 0);
|
||||
instance->Disable();
|
||||
}
|
||||
|
||||
TSE::OpenGL::BasicOrderedSpriteSetShader::BasicOrderedSpriteSetShader(std::vector<std::unique_ptr<ShaderPart>> &&parts) : Shader(parts)
|
||||
{
|
||||
PackageSize = SHADER_PACKAGE_SIZE;
|
||||
}
|
||||
|
||||
TSE::OpenGL::BasicOrderedSpriteSetShader::~BasicOrderedSpriteSetShader()
|
||||
{
|
||||
if (meshVBO) glDeleteBuffers(1, &meshVBO);
|
||||
if (meshIBO) glDeleteBuffers(1, &meshIBO);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::SetMesh(const void *verts, int vertCount, int stride, int floatCountPerVertex, int posOffsetBytes, GLenum primitive, const void *indices, int indexCount, GLenum indexType)
|
||||
{
|
||||
GLint prevVAO = 0, prevArrayBuffer = 0, prevElementBuffer = 0;
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &prevVAO);
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prevArrayBuffer);
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &prevElementBuffer);
|
||||
|
||||
if (!meshVBO) glGenBuffers(1, &meshVBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, meshVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertCount * stride, verts, GL_STATIC_DRAW);
|
||||
|
||||
if (indices && indexCount > 0)
|
||||
{
|
||||
if (!meshIBO) glGenBuffers(1, &meshIBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIBO);
|
||||
GLsizeiptr idxSize =
|
||||
(indexType == GL_UNSIGNED_INT ? 4 :
|
||||
indexType == GL_UNSIGNED_SHORT? 2 : 1) * indexCount;
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, idxSize, indices, GL_STATIC_DRAW);
|
||||
meshIndexCount = indexCount;
|
||||
meshIndexType = indexType;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Kein Index-Buffer
|
||||
if (meshIBO) { glDeleteBuffers(1, &meshIBO); meshIBO = 0; }
|
||||
meshIndexCount = 0;
|
||||
}
|
||||
|
||||
meshVertexCount = vertCount;
|
||||
meshStride = stride;
|
||||
meshPosOffset = posOffsetBytes;
|
||||
meshPosSize = floatCountPerVertex;
|
||||
meshPrimitive = primitive;
|
||||
meshReady = true;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, prevArrayBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prevElementBuffer);
|
||||
glBindVertexArray(prevVAO);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::OnEnable() const
|
||||
{
|
||||
if (!meshReady)
|
||||
{
|
||||
// Fallback: unit-Quad als TRIANGLE_FAN (4 Vertices, 2D Positionen)
|
||||
const float quad[8] = { -0.5f,0, 0.5f,0, 0.5f,1, -0.5f,1 };
|
||||
const_cast<BasicOrderedSpriteSetShader*>(this)->SetMesh(
|
||||
quad, 4, sizeof(float)*2, 2, 0, GL_TRIANGLE_FAN
|
||||
);
|
||||
}
|
||||
|
||||
GLint prevArrayBuffer = 0;
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prevArrayBuffer);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, meshVBO);
|
||||
glEnableVertexAttribArray(SHADER_MESH_INDEX); // LOC_QUAD/pos
|
||||
glVertexAttribPointer(SHADER_MESH_INDEX, meshPosSize, GL_FLOAT, GL_FALSE, meshStride, (void*)meshPosOffset);
|
||||
glVertexAttribDivisor(SHADER_MESH_INDEX, 0); // per-vertex (Mesh)
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, prevArrayBuffer);
|
||||
|
||||
glEnableVertexAttribArray(SHADER_POS_INDEX);
|
||||
glVertexAttribPointer(SHADER_POS_INDEX, 3, GL_FLOAT, GL_FALSE, PackageSize, (void*)0);
|
||||
glVertexAttribDivisor(SHADER_POS_INDEX, 1);
|
||||
|
||||
glEnableVertexAttribArray(SHADER_LAYER_HEIGHT_INDEX);
|
||||
glVertexAttribPointer(SHADER_LAYER_HEIGHT_INDEX, 1, GL_FLOAT, GL_FALSE, PackageSize, (void*)(sizeof(float)*3));
|
||||
glVertexAttribDivisor(SHADER_LAYER_HEIGHT_INDEX, 1);
|
||||
|
||||
glEnableVertexAttribArray(SHADER_SPRITE_INDEX);
|
||||
glVertexAttribPointer(SHADER_SPRITE_INDEX, 1, GL_FLOAT, GL_FALSE, PackageSize, (void*)(sizeof(float)*4));
|
||||
glVertexAttribDivisor(SHADER_SPRITE_INDEX, 1);
|
||||
|
||||
glEnableVertexAttribArray(SHADER_NORMAL_INDEX);
|
||||
glVertexAttribPointer(SHADER_NORMAL_INDEX, 1, GL_FLOAT, GL_FALSE, PackageSize, (void*)(sizeof(float)*5));
|
||||
glVertexAttribDivisor(SHADER_NORMAL_INDEX, 1);
|
||||
|
||||
glEnableVertexAttribArray(SHADER_SCALE_INDEX);
|
||||
glVertexAttribPointer(SHADER_SCALE_INDEX, 2, GL_FLOAT, GL_FALSE, PackageSize, (void*)(sizeof(float)*6));
|
||||
glVertexAttribDivisor(SHADER_SCALE_INDEX, 1);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::OnDisable() const
|
||||
{
|
||||
glDisableVertexAttribArray(SHADER_MESH_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_POS_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_LAYER_HEIGHT_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_SPRITE_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_NORMAL_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_SCALE_INDEX);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::OnFlush()
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, TextureID);
|
||||
glDisable(GL_BLEND);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::OnDrawCall(int indexCount)
|
||||
{
|
||||
if (instanceCount <= 0) return;
|
||||
SetUniform("spriteCount", &SpriteCount);
|
||||
|
||||
GLint prevElementBuffer = 0;
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &prevElementBuffer);
|
||||
|
||||
if (meshIBO && meshIndexCount > 0)
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIBO);
|
||||
glDrawElementsInstanced(meshPrimitive, meshIndexCount, meshIndexType, (void*)0, instanceCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glDrawArraysInstanced(meshPrimitive, 0, meshVertexCount, instanceCount);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GLuint)prevElementBuffer);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::OnPostDraw()
|
||||
{
|
||||
glEnable(GL_BLEND);
|
||||
instanceCount = 0;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicOrderedSpriteSetShader::OnSubmit(const Transformable &t, float *&target, TransformationStack &stack, void (*restartDrawcall)(IRenderer &), IRenderer &rnd)
|
||||
{
|
||||
auto* r = dynamic_cast<Renderable*>(t.GetBehaviourScript(RENDERABLE));
|
||||
if (!r) return;
|
||||
|
||||
auto* tm = dynamic_cast<OrdererSpriteSet*>(t.GetBehaviourScript(ORDERERSPRITESET));
|
||||
if (!tm) return;
|
||||
|
||||
auto tileSet = tm->GetTileSet();
|
||||
TextureID = tileSet->GetTextueID();
|
||||
SpriteCount = tileSet->GetCount();
|
||||
|
||||
const std::vector<Vector2> orderedChunks = *tm->GetChunkPositionsInOrder();
|
||||
|
||||
Matrix4x4 matr = t.GetLocalMatrix();
|
||||
|
||||
stack.Push(matr);
|
||||
|
||||
for(auto chunkPos : orderedChunks)
|
||||
{
|
||||
auto chunk = tm->GetChunk(chunkPos);
|
||||
const int spriteCount = chunk->GetSpriteCount();
|
||||
const std::vector<Vector3> spritePositions = *chunk->GetOrderedPositions();
|
||||
const std::vector<Vector2i> spriteIds = *chunk->GetOrderedSpriteIds();
|
||||
const std::vector<Vector2> spriteScales = *chunk->GetOrderedScales();
|
||||
int chunkSize = chunk->GetChunksize();
|
||||
|
||||
for (int i = 0; i < spriteCount; i++)
|
||||
{
|
||||
Matrix4x4 mat = Matrix4x4::ToTranslationMatrix(chunkPos + spritePositions[i].ToVector2()) * Matrix4x4::ToRotationMatrix(Quaternion()) * Matrix4x4::ToScaleMatrix({1,1,1});
|
||||
stack.Push(mat);
|
||||
Vector3 pos = stack.Top() * Vector3(0,0,0);
|
||||
|
||||
*target++ = pos.x;
|
||||
*target++ = pos.y;
|
||||
*target++ = pos.z;
|
||||
*target++ = spritePositions[i].z;
|
||||
*target++ = spriteIds[i].x;
|
||||
*target++ = spriteIds[i].y;
|
||||
*target++ = spriteScales[i].x;
|
||||
*target++ = spriteScales[i].y;
|
||||
|
||||
++instanceCount;
|
||||
stack.Pop();
|
||||
|
||||
if(instanceCount >= 16000)
|
||||
restartDrawcall(rnd);
|
||||
}
|
||||
}
|
||||
|
||||
stack.Pop();
|
||||
restartDrawcall(rnd);
|
||||
}
|
||||
44
TSE_OpenGlImpl/src/shader/basicOrderedSpriteSetShader.hpp
Normal file
44
TSE_OpenGlImpl/src/shader/basicOrderedSpriteSetShader.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "GL/gl3w.h"
|
||||
#include "GL/gl.h"
|
||||
#include "Shader.hpp"
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
class BasicOrderedSpriteSetShader : public Shader
|
||||
{
|
||||
private:
|
||||
static BasicOrderedSpriteSetShader* instance;
|
||||
mutable bool meshReady = false;
|
||||
GLuint meshVBO = 0;
|
||||
GLuint meshIBO = 0;
|
||||
GLsizei meshVertexCount = 0; // für DrawArraysInstanced
|
||||
GLsizei meshIndexCount = 0; // für DrawElementsInstanced
|
||||
GLenum meshPrimitive = GL_TRIANGLES;
|
||||
GLenum meshIndexType = GL_UNSIGNED_SHORT;
|
||||
int instanceCount = 0; // eigener Instanzzähler
|
||||
GLint meshPosSize = 2; // 2D (Billboard-Formen), für 3D Meshes: 3
|
||||
GLsizei meshStride = sizeof(float) * 2;
|
||||
size_t meshPosOffset = 0;
|
||||
GLuint TextureID;
|
||||
Vector2 SpriteCount;
|
||||
|
||||
public:
|
||||
static BasicOrderedSpriteSetShader* Instance();
|
||||
static void Destroy();
|
||||
static void Init(float width, float height);
|
||||
BasicOrderedSpriteSetShader(std::vector<std::unique_ptr<ShaderPart>>&& parts);
|
||||
~BasicOrderedSpriteSetShader();
|
||||
void SetMesh(const void* verts, int vertCount, int stride, int floatCountPerVertex, int posOffsetBytes, GLenum primitive, const void* indices = nullptr, int indexCount = 0, GLenum indexType = GL_UNSIGNED_SHORT);
|
||||
|
||||
protected:
|
||||
void OnEnable() const override;
|
||||
void OnDisable() const override;
|
||||
void OnFlush() override;
|
||||
void OnDrawCall(int indexCount) override;
|
||||
void OnPostDraw() override;
|
||||
void OnSubmit(const Transformable& t, float*& target, TransformationStack& stack, void (*restartDrawcall)(IRenderer&), IRenderer& rnd) override;
|
||||
};
|
||||
} // namespace TSE::GLFW
|
||||
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
inline const char* vertOrderedSet = R"(
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec2 aPos;
|
||||
|
||||
layout(location = 1) in vec3 iTilePos;
|
||||
layout(location = 2) in float height;
|
||||
layout(location = 3) in float iSpriteId;
|
||||
layout(location = 4) in float iNormalId;
|
||||
layout(location = 5) in vec2 spriteScale;
|
||||
|
||||
uniform mat4 prMatrix;
|
||||
uniform mat4 camMatrix;
|
||||
|
||||
out vec2 vUV;
|
||||
flat out int vSpriteId;
|
||||
flat out int vNormalId;
|
||||
flat out float vTileNdcY;
|
||||
flat out float layerHeight;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 local = vec3(aPos.x, aPos.y, 0);
|
||||
vec2 baseUV = aPos + vec2(0.5, 0);
|
||||
vec3 tileSize = vec3(spriteScale.x, spriteScale.y, 1);
|
||||
|
||||
vec3 worldPos = (iTilePos * tileSize) + (local * tileSize);
|
||||
|
||||
vec4 clip = prMatrix * camMatrix * vec4(worldPos, 1.0);
|
||||
gl_Position = clip;
|
||||
|
||||
vUV = baseUV;
|
||||
vSpriteId = int(iSpriteId + 0.5);
|
||||
vNormalId = int(iNormalId + 0.5);
|
||||
layerHeight = height;
|
||||
|
||||
vec3 localbottom = vec3(0.5, 0, 0);
|
||||
vec3 worldPosBottom = (iTilePos * tileSize) + (localbottom * tileSize);
|
||||
vec4 clipbottom = prMatrix * camMatrix * vec4(worldPosBottom, 1.0);
|
||||
float ndcY = clipbottom.y / clipbottom.w;
|
||||
vTileNdcY = ndcY * 0.5 + 0.5;
|
||||
}
|
||||
)";
|
||||
|
||||
inline const char* fragOrderedSet = R"(
|
||||
#version 330 core
|
||||
|
||||
in vec2 vUV;
|
||||
flat in int vSpriteId;
|
||||
flat in int vNormalId;
|
||||
flat in float vTileNdcY;
|
||||
flat in float layerHeight;
|
||||
|
||||
uniform sampler2D atlas;
|
||||
uniform vec2 spriteCount;
|
||||
|
||||
layout(location = 0) out vec4 FragColor;
|
||||
layout(location = 1) out vec4 FragHeight;
|
||||
layout(location = 2) out vec4 FragDepth;
|
||||
|
||||
void main()
|
||||
{
|
||||
float t = (vTileNdcY + 1.0) * 0.5 *0.8;
|
||||
FragDepth = vec4(t, 0, 0, 1.0);
|
||||
|
||||
vec2 tileUVSize = 1.0 / spriteCount;
|
||||
|
||||
int cols = int(spriteCount.x);
|
||||
int sx = vSpriteId % cols;
|
||||
int sy = vSpriteId / cols;
|
||||
|
||||
vec2 atlasOffset = vec2(float(sx), float(sy)) * tileUVSize;
|
||||
vec2 atlasUV = atlasOffset + (vUV * tileUVSize);
|
||||
vec4 c = texture(atlas, atlasUV);
|
||||
if (c.a < 0.01) discard;
|
||||
float colorScaler = 1 - ((layerHeight - 1) * -1) * 0.3;
|
||||
c = vec4(c.x * colorScaler,c.y * colorScaler,c.z * colorScaler,c.w);
|
||||
|
||||
FragColor = c;
|
||||
|
||||
if(vNormalId != -1)
|
||||
{
|
||||
int sx2 = vNormalId % cols;
|
||||
int sy2 = vNormalId / cols;
|
||||
vec2 atlasOffsetNormal = vec2(float(sx2), float(sy2)) * tileUVSize;
|
||||
vec2 atlasUVNormal = atlasOffsetNormal + (vUV * tileUVSize);
|
||||
vec4 cNormal = texture(atlas, atlasUVNormal);
|
||||
cNormal.w = layerHeight;
|
||||
|
||||
FragHeight = cNormal;
|
||||
}
|
||||
}
|
||||
)";
|
||||
190
TSE_OpenGlImpl/src/shader/basicParticleShader.cpp
Normal file
190
TSE_OpenGlImpl/src/shader/basicParticleShader.cpp
Normal file
@@ -0,0 +1,190 @@
|
||||
#include "basicParticleShader.hpp"
|
||||
#include "basicParticleShaderGLSL.hpp"
|
||||
#include "BehaviourScripts/Renderable.hpp"
|
||||
#include "BehaviourScripts/ParticleSystem.hpp"
|
||||
#include "Color.hpp"
|
||||
|
||||
#define SHADER_MESH_INDEX 0
|
||||
#define SHADER_POS_INDEX 1
|
||||
#define SHADER_SIZE_INDEX 2
|
||||
#define SHADER_ROT_INDEX 3
|
||||
#define SHADER_COLOR_INDEX 4
|
||||
|
||||
#define SHADER_PACKAGE_SIZE sizeof(float) * (3 + 1 + 1 + 4)
|
||||
|
||||
TSE::OpenGL::BasicParticleShader* TSE::OpenGL::BasicParticleShader::instance = nullptr;
|
||||
|
||||
TSE::OpenGL::BasicParticleShader *TSE::OpenGL::BasicParticleShader::Instance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicParticleShader::Destroy()
|
||||
{
|
||||
if(instance != nullptr)
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicParticleShader::Init(float width, float height)
|
||||
{
|
||||
std::vector<std::unique_ptr<ShaderPart>> parts;
|
||||
parts.push_back(ShaderPart::LoadFromString(vertPart, GL_VERTEX_SHADER));
|
||||
parts.push_back(ShaderPart::LoadFromString(fragPart, GL_FRAGMENT_SHADER));
|
||||
instance = new BasicParticleShader(std::move(parts));
|
||||
}
|
||||
|
||||
TSE::OpenGL::BasicParticleShader::BasicParticleShader(std::vector<std::unique_ptr<ShaderPart>> &&parts) : Shader(parts)
|
||||
{
|
||||
PackageSize = SHADER_PACKAGE_SIZE;
|
||||
}
|
||||
|
||||
TSE::OpenGL::BasicParticleShader::~BasicParticleShader()
|
||||
{
|
||||
if (meshVBO) glDeleteBuffers(1, &meshVBO);
|
||||
if (meshIBO) glDeleteBuffers(1, &meshIBO);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicParticleShader::SetMesh(const void *verts, int vertCount, int stride, int floatCountPerVertex, int posOffsetBytes, GLenum primitive, const void *indices, int indexCount, GLenum indexType)
|
||||
{
|
||||
GLint prevVAO = 0, prevArrayBuffer = 0, prevElementBuffer = 0;
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &prevVAO);
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prevArrayBuffer);
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &prevElementBuffer);
|
||||
|
||||
if (!meshVBO) glGenBuffers(1, &meshVBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, meshVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertCount * stride, verts, GL_STATIC_DRAW);
|
||||
|
||||
if (indices && indexCount > 0)
|
||||
{
|
||||
if (!meshIBO) glGenBuffers(1, &meshIBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIBO);
|
||||
GLsizeiptr idxSize =
|
||||
(indexType == GL_UNSIGNED_INT ? 4 :
|
||||
indexType == GL_UNSIGNED_SHORT? 2 : 1) * indexCount;
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, idxSize, indices, GL_STATIC_DRAW);
|
||||
meshIndexCount = indexCount;
|
||||
meshIndexType = indexType;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Kein Index-Buffer
|
||||
if (meshIBO) { glDeleteBuffers(1, &meshIBO); meshIBO = 0; }
|
||||
meshIndexCount = 0;
|
||||
}
|
||||
|
||||
meshVertexCount = vertCount;
|
||||
meshStride = stride;
|
||||
meshPosOffset = posOffsetBytes;
|
||||
meshPosSize = floatCountPerVertex;
|
||||
meshPrimitive = primitive;
|
||||
meshReady = true;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, prevArrayBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prevElementBuffer);
|
||||
glBindVertexArray(prevVAO);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicParticleShader::OnEnable() const
|
||||
{
|
||||
if (!meshReady)
|
||||
{
|
||||
// Fallback: unit-Quad als TRIANGLE_FAN (4 Vertices, 2D Positionen)
|
||||
const float quad[8] = { -0.5f,-0.5f, 0.5f,-0.5f, 0.5f,0.5f, -0.5f,0.5f };
|
||||
const_cast<BasicParticleShader*>(this)->SetMesh(
|
||||
quad, 4, sizeof(float)*2, 2, 0, GL_TRIANGLE_FAN
|
||||
);
|
||||
}
|
||||
|
||||
GLint prevArrayBuffer = 0;
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prevArrayBuffer);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, meshVBO);
|
||||
glEnableVertexAttribArray(SHADER_MESH_INDEX); // LOC_QUAD/pos
|
||||
glVertexAttribPointer(SHADER_MESH_INDEX, meshPosSize, GL_FLOAT, GL_FALSE, meshStride, (void*)meshPosOffset);
|
||||
glVertexAttribDivisor(SHADER_MESH_INDEX, 0); // per-vertex (Mesh)
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, prevArrayBuffer);
|
||||
|
||||
// layout 1: position (vec3)
|
||||
glEnableVertexAttribArray(SHADER_POS_INDEX);
|
||||
glVertexAttribPointer(SHADER_POS_INDEX, 3, GL_FLOAT, GL_FALSE, PackageSize, (void*)0);
|
||||
glVertexAttribDivisor(SHADER_POS_INDEX, 1);
|
||||
|
||||
// layout 2: size (float)
|
||||
glEnableVertexAttribArray(SHADER_SIZE_INDEX);
|
||||
glVertexAttribPointer(SHADER_SIZE_INDEX, 1, GL_FLOAT, GL_FALSE, PackageSize, (void*)(sizeof(float)*3));
|
||||
glVertexAttribDivisor(SHADER_SIZE_INDEX, 1);
|
||||
|
||||
// layout 3: rotationRad (float)
|
||||
glEnableVertexAttribArray(SHADER_ROT_INDEX);
|
||||
glVertexAttribPointer(SHADER_ROT_INDEX, 1, GL_FLOAT, GL_FALSE, PackageSize, (void*)(sizeof(float)*4));
|
||||
glVertexAttribDivisor(SHADER_ROT_INDEX, 1);
|
||||
|
||||
// layout 4: color (vec4)
|
||||
glEnableVertexAttribArray(SHADER_COLOR_INDEX);
|
||||
glVertexAttribPointer(SHADER_COLOR_INDEX, 4, GL_FLOAT, GL_FALSE, PackageSize, (void*)(sizeof(float)*5));
|
||||
glVertexAttribDivisor(SHADER_COLOR_INDEX, 1);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicParticleShader::OnDisable() const
|
||||
{
|
||||
glDisableVertexAttribArray(SHADER_MESH_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_POS_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_SIZE_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_ROT_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_COLOR_INDEX);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicParticleShader::OnFlush()
|
||||
{
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicParticleShader::OnDrawCall(int indexCount)
|
||||
{
|
||||
if (instanceCount <= 0) return;
|
||||
|
||||
GLint prevElementBuffer = 0;
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &prevElementBuffer);
|
||||
|
||||
if (meshIBO && meshIndexCount > 0)
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIBO);
|
||||
glDrawElementsInstanced(meshPrimitive, meshIndexCount, meshIndexType, (void*)0, instanceCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glDrawArraysInstanced(meshPrimitive, 0, meshVertexCount, instanceCount);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GLuint)prevElementBuffer);
|
||||
instanceCount = 0;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicParticleShader::OnSubmit(const Transformable &t, float *&target, TransformationStack &stack, void (*restartDrawcall)(IRenderer &), IRenderer &rnd)
|
||||
{
|
||||
auto* r = dynamic_cast<Renderable*>(t.GetBehaviourScript(RENDERABLE));
|
||||
if (!r) return;
|
||||
|
||||
auto* ps = dynamic_cast<ParticleSystem*>(t.GetBehaviourScript(PARTICLESYSTEM));
|
||||
if (!ps) return;
|
||||
|
||||
const std::vector<TSE::Particle*>& particles = ps->GetParticles();
|
||||
|
||||
for(auto particle : particles)
|
||||
{
|
||||
*target++ = particle->position.x;
|
||||
*target++ = particle->position.y;
|
||||
*target++ = particle->position.z;
|
||||
*target++ = particle->size;
|
||||
*target++ = particle->rotationRad;
|
||||
*target++ = particle->color.r;
|
||||
*target++ = particle->color.g;
|
||||
*target++ = particle->color.b;
|
||||
*target++ = particle->color.a;
|
||||
|
||||
++instanceCount;
|
||||
}
|
||||
}
|
||||
41
TSE_OpenGlImpl/src/shader/basicParticleShader.hpp
Normal file
41
TSE_OpenGlImpl/src/shader/basicParticleShader.hpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "GL/gl3w.h"
|
||||
#include "GL/gl.h"
|
||||
#include "Shader.hpp"
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
class BasicParticleShader : public Shader
|
||||
{
|
||||
private:
|
||||
static BasicParticleShader* instance;
|
||||
mutable bool meshReady = false;
|
||||
GLuint meshVBO = 0;
|
||||
GLuint meshIBO = 0;
|
||||
GLsizei meshVertexCount = 0; // für DrawArraysInstanced
|
||||
GLsizei meshIndexCount = 0; // für DrawElementsInstanced
|
||||
GLenum meshPrimitive = GL_TRIANGLES;
|
||||
GLenum meshIndexType = GL_UNSIGNED_SHORT;
|
||||
int instanceCount = 0; // eigener Instanzzähler
|
||||
GLint meshPosSize = 2; // 2D (Billboard-Formen), für 3D Meshes: 3
|
||||
GLsizei meshStride = sizeof(float) * 2;
|
||||
size_t meshPosOffset = 0;
|
||||
|
||||
public:
|
||||
static BasicParticleShader* Instance();
|
||||
static void Destroy();
|
||||
static void Init(float width, float height);
|
||||
BasicParticleShader(std::vector<std::unique_ptr<ShaderPart>>&& parts);
|
||||
~BasicParticleShader();
|
||||
void SetMesh(const void* verts, int vertCount, int stride, int floatCountPerVertex, int posOffsetBytes, GLenum primitive, const void* indices = nullptr, int indexCount = 0, GLenum indexType = GL_UNSIGNED_SHORT);
|
||||
|
||||
protected:
|
||||
void OnEnable() const override;
|
||||
void OnDisable() const override;
|
||||
void OnFlush() override;
|
||||
void OnDrawCall(int indexCount) override;
|
||||
void OnSubmit(const Transformable& t, float*& target, TransformationStack& stack, void (*restartDrawcall)(IRenderer&), IRenderer& rnd) override;
|
||||
};
|
||||
} // namespace TSE::GLFW
|
||||
101
TSE_OpenGlImpl/src/shader/basicParticleShaderGLSL.hpp
Normal file
101
TSE_OpenGlImpl/src/shader/basicParticleShaderGLSL.hpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#pragma once
|
||||
|
||||
inline const char* vertPart = R"(
|
||||
#version 330 core
|
||||
|
||||
#ifndef USE_2D_BILLBOARD
|
||||
#define USE_2D_BILLBOARD 1 //2D mesh => 1 ; 3D mesh => 0
|
||||
#endif
|
||||
|
||||
// Mesh-Positions-Stream (per-vertex)
|
||||
#if USE_2D_BILLBOARD
|
||||
layout(location = 0) in vec2 meshPos; // z.B. Quad-Ecken (-0.5..0.5)
|
||||
#else
|
||||
layout(location = 0) in vec3 meshPos; // echte 3D-Mesh-Position
|
||||
#endif
|
||||
|
||||
// Instanz-Attribute
|
||||
layout(location = 1) in vec3 inPosition; // Partikelzentrum (Welt)
|
||||
layout(location = 2) in float inSize; // Uniform-Scale
|
||||
layout(location = 3) in float inRotation; // Rotation (nur im Billboard-Modus genutzt)
|
||||
layout(location = 4) in vec4 inColor; // RGBA
|
||||
|
||||
uniform mat4 prMatrix;
|
||||
uniform mat4 camMatrix;
|
||||
|
||||
out VS_OUT {
|
||||
vec4 color;
|
||||
#if USE_2D_BILLBOARD
|
||||
vec2 uv; // optional: für runde Maske im FS
|
||||
#endif
|
||||
} vs;
|
||||
|
||||
// Kameraachsen aus View-Matrix (Spalten 0/1)
|
||||
vec3 CameraRight(mat4 view) { return vec3(view[0][0], view[1][0], view[2][0]); }
|
||||
vec3 CameraUp (mat4 view) { return vec3(view[0][1], view[1][1], view[2][1]); }
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
vs.color = inColor;
|
||||
|
||||
#if USE_2D_BILLBOARD
|
||||
// 2D-Rotation im Billboard-Raum
|
||||
float c = cos(inRotation);
|
||||
float s = sin(inRotation);
|
||||
vec2 q = vec2(c * meshPos.x - s * meshPos.y,
|
||||
s * meshPos.x + c * meshPos.y);
|
||||
|
||||
vec3 right = CameraRight(camMatrix);
|
||||
vec3 up = CameraUp(camMatrix);
|
||||
|
||||
// Uniform-Scaling über inSize
|
||||
vec3 worldPos = inPosition + (q.x * inSize) * right
|
||||
+ (q.y * inSize) * up;
|
||||
|
||||
// optionales UV für Kreis-Maske
|
||||
vs.uv = meshPos * 0.5 + 0.5; // falls meshPos in -1..1: nimm (meshPos*0.5+0.5)
|
||||
#else
|
||||
// echtes 3D-Mesh: nur uniform Scale + Translation
|
||||
vec3 worldPos = inPosition + meshPos * inSize;
|
||||
#endif
|
||||
|
||||
gl_Position = prMatrix * camMatrix * vec4(worldPos, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
inline const char* fragPart = R"(
|
||||
#version 330 core
|
||||
|
||||
#ifndef USE_2D_BILLBOARD
|
||||
#define USE_2D_BILLBOARD 1
|
||||
#endif
|
||||
|
||||
// Optional runde Maske an/aus (nur sinnvoll im Billboard-Modus mit z.B. Quad)
|
||||
#ifndef USE_ROUND_MASK
|
||||
#define USE_ROUND_MASK 0
|
||||
#endif
|
||||
|
||||
in VS_OUT {
|
||||
vec4 color;
|
||||
#if USE_2D_BILLBOARD
|
||||
vec2 uv;
|
||||
#endif
|
||||
} fs;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
#if USE_2D_BILLBOARD && USE_ROUND_MASK
|
||||
// weiche Kreis-Maske (aus uv ~ 0..1)
|
||||
vec2 c = fs.uv * 2.0 - 1.0; // nach [-1..1]
|
||||
float r = length(c);
|
||||
if (r > 1.0) discard;
|
||||
float alpha = 1.0 - smoothstep(0.95, 1.0, r);
|
||||
outColor = vec4(fs.color.rgb, fs.color.a * alpha);
|
||||
#else
|
||||
outColor = fs.color;
|
||||
#endif
|
||||
}
|
||||
)";
|
||||
92
TSE_OpenGlImpl/src/shader/basicShader.cpp
Normal file
92
TSE_OpenGlImpl/src/shader/basicShader.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "GL/gl3w.h"
|
||||
#include "GL/gl.h"
|
||||
#include "basicShader.hpp"
|
||||
|
||||
#include "basicShaderGLSL.hpp"
|
||||
#include "BehaviourScripts/Renderable.hpp"
|
||||
#include "Color.hpp"
|
||||
|
||||
#define SHADER_VERTEX_INDEX 0
|
||||
#define SHADER_COLOR_INDEX 1
|
||||
|
||||
#define SHADER_PACKAGE_SIZE sizeof(float) * (3 + 4)
|
||||
|
||||
TSE::OpenGL::BasicShader* TSE::OpenGL::BasicShader::instance = nullptr;
|
||||
|
||||
TSE::OpenGL::BasicShader *TSE::OpenGL::BasicShader::Instance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicShader::Destroy()
|
||||
{
|
||||
if(instance != nullptr)
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicShader::Init(float width, float height)
|
||||
{
|
||||
std::vector<std::unique_ptr<ShaderPart>> parts;
|
||||
parts.push_back(ShaderPart::LoadFromString(vert, GL_VERTEX_SHADER));
|
||||
parts.push_back(ShaderPart::LoadFromString(frag, GL_FRAGMENT_SHADER));
|
||||
instance = new BasicShader(std::move(parts));
|
||||
}
|
||||
|
||||
TSE::OpenGL::BasicShader::BasicShader(std::vector<std::unique_ptr<ShaderPart>> &&parts) : Shader(parts)
|
||||
{
|
||||
PackageSize = SHADER_PACKAGE_SIZE;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicShader::OnEnable() const
|
||||
{
|
||||
glEnableVertexAttribArray(SHADER_VERTEX_INDEX);
|
||||
glVertexAttribPointer(SHADER_VERTEX_INDEX, 3, GL_FLOAT, false, SHADER_PACKAGE_SIZE, (void*)0);
|
||||
glEnableVertexAttribArray(SHADER_COLOR_INDEX);
|
||||
glVertexAttribPointer(SHADER_COLOR_INDEX, 4, GL_FLOAT, false, SHADER_PACKAGE_SIZE, (void*)(sizeof(float) * 3));
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicShader::OnDisable() const
|
||||
{
|
||||
glDisableVertexAttribArray(SHADER_VERTEX_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_COLOR_INDEX);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicShader::OnFlush()
|
||||
{
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicShader::OnDrawCall(int indexCount)
|
||||
{
|
||||
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, NULL);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicShader::OnSubmit(const Transformable &t, float *&target, TransformationStack &stack, void (*restartDrawcall)(IRenderer &), IRenderer &rnd)
|
||||
{
|
||||
auto* r = dynamic_cast<Renderable*>(t.GetBehaviourScript(RENDERABLE));
|
||||
if (!r) return;
|
||||
|
||||
|
||||
const Vector3* verts = r->GetVertices();
|
||||
ushort vCount = r->GetVertexCount();
|
||||
const Color color = r->GetMaterial()->GetValue<Color>("mainColor");
|
||||
Matrix4x4 matr = t.GetLocalMatrix();
|
||||
|
||||
stack.Push(matr);
|
||||
const Matrix4x4& top = stack.Top();
|
||||
|
||||
//Todo transformable.lastmatrix hier ergänzen
|
||||
|
||||
for (ushort i = 0; i < vCount; i++) {
|
||||
Vector3 p = top * verts[i];
|
||||
*target++ = p.x;
|
||||
*target++ = p.y;
|
||||
*target++ = p.z;
|
||||
*target++ = color.r;
|
||||
*target++ = color.g;
|
||||
*target++ = color.b;
|
||||
*target++ = color.a;
|
||||
}
|
||||
|
||||
stack.Pop();
|
||||
}
|
||||
25
TSE_OpenGlImpl/src/shader/basicShader.hpp
Normal file
25
TSE_OpenGlImpl/src/shader/basicShader.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "Shader.hpp"
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
class BasicShader : public Shader
|
||||
{
|
||||
private:
|
||||
static BasicShader* instance;
|
||||
|
||||
public:
|
||||
static BasicShader* Instance();
|
||||
static void Destroy();
|
||||
static void Init(float width, float height);
|
||||
BasicShader(std::vector<std::unique_ptr<ShaderPart>>&& parts);
|
||||
|
||||
protected:
|
||||
void OnEnable() const override;
|
||||
void OnDisable() const override;
|
||||
void OnFlush() override;
|
||||
void OnDrawCall(int indexCount) override;
|
||||
void OnSubmit(const Transformable& t, float*& target, TransformationStack& stack, void (*restartDrawcall)(IRenderer&), IRenderer& rnd) override;
|
||||
};
|
||||
} // namespace TSE::OpenGL
|
||||
42
TSE_OpenGlImpl/src/shader/basicShaderGLSL.hpp
Normal file
42
TSE_OpenGlImpl/src/shader/basicShaderGLSL.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
inline const char* vert = R"(
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 position;
|
||||
layout (location = 1) in vec4 color;
|
||||
|
||||
|
||||
uniform mat4 prMatrix;
|
||||
uniform mat4 camMatrix;
|
||||
|
||||
out DATA
|
||||
{
|
||||
vec4 color_out;
|
||||
} vs_out;
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = prMatrix * camMatrix * vec4(position.x, position.y, position.z, 1.0);
|
||||
vs_out.color_out = color;
|
||||
}
|
||||
)";
|
||||
|
||||
inline const char* frag = R"(
|
||||
#version 330 core
|
||||
layout (location = 0) out vec4 color;
|
||||
in DATA
|
||||
{
|
||||
vec4 color_out;
|
||||
} fs_in;
|
||||
void main()
|
||||
{
|
||||
if(fs_in.color_out.a < 0.1)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
color = fs_in.color_out;
|
||||
}
|
||||
)";
|
||||
135
TSE_OpenGlImpl/src/shader/basicTextureShader.cpp
Normal file
135
TSE_OpenGlImpl/src/shader/basicTextureShader.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
#include "GL/gl3w.h"
|
||||
#include "GL/gl.h"
|
||||
#include "basicTextureShader.hpp"
|
||||
#include "basicTextureShaderGLSL.hpp"
|
||||
#include "BehaviourScripts/Renderable.hpp"
|
||||
#include "Color.hpp"
|
||||
#include "interfaces/ITexture.hpp"
|
||||
|
||||
#define SHADER_VERTEX_INDEX 0
|
||||
#define SHADER_COLOR_INDEX 1
|
||||
#define SHADER_UV_INDEX 2
|
||||
#define SHADER_TID_INDEX 3
|
||||
|
||||
#define SHADER_PACKAGE_SIZE (sizeof(float) * (3 + 4 + 2 + 1))
|
||||
|
||||
TSE::OpenGL::BasicTextureShader* TSE::OpenGL::BasicTextureShader::instance = nullptr;
|
||||
std::map<TSE::uint, float> TSE::OpenGL::BasicTextureShader::textureSlots;
|
||||
|
||||
TSE::OpenGL::BasicTextureShader *TSE::OpenGL::BasicTextureShader::Instance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTextureShader::Destroy()
|
||||
{
|
||||
if(instance != nullptr)
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTextureShader::Init(float width, float height)
|
||||
{
|
||||
std::vector<std::unique_ptr<ShaderPart>> parts;
|
||||
parts.push_back(ShaderPart::LoadFromString(vertT, GL_VERTEX_SHADER));
|
||||
parts.push_back(ShaderPart::LoadFromString(fragT, GL_FRAGMENT_SHADER));
|
||||
instance = new BasicTextureShader(std::move(parts));
|
||||
|
||||
instance->Enable();
|
||||
int texIDs[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
|
||||
instance->SetUniform("textures", &texIDs[0], 33);
|
||||
instance->Disable();
|
||||
}
|
||||
|
||||
TSE::OpenGL::BasicTextureShader::BasicTextureShader(std::vector<std::unique_ptr<ShaderPart>> &&parts) : Shader(parts)
|
||||
{
|
||||
PackageSize = SHADER_PACKAGE_SIZE;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTextureShader::OnEnable() const
|
||||
{
|
||||
glEnableVertexAttribArray(SHADER_VERTEX_INDEX);
|
||||
glVertexAttribPointer(SHADER_VERTEX_INDEX, 3, GL_FLOAT, false, SHADER_PACKAGE_SIZE, (void*)0);
|
||||
glEnableVertexAttribArray(SHADER_COLOR_INDEX);
|
||||
glVertexAttribPointer(SHADER_COLOR_INDEX, 4, GL_FLOAT, false, SHADER_PACKAGE_SIZE, (void*)(sizeof(float) * 3));
|
||||
glEnableVertexAttribArray(SHADER_UV_INDEX);
|
||||
glVertexAttribPointer(SHADER_UV_INDEX, 2, GL_FLOAT, false, SHADER_PACKAGE_SIZE, (void*)(sizeof(float) * 7));
|
||||
glEnableVertexAttribArray(SHADER_TID_INDEX);
|
||||
glVertexAttribPointer(SHADER_TID_INDEX, 1, GL_FLOAT, false, SHADER_PACKAGE_SIZE, (void*)(sizeof(float) * 9));
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTextureShader::OnDisable() const
|
||||
{
|
||||
glDisableVertexAttribArray(SHADER_VERTEX_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_COLOR_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_UV_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_TID_INDEX);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTextureShader::OnFlush()
|
||||
{
|
||||
auto it = textureSlots.begin();
|
||||
for (int i = 0; i < textureSlots.size(); i++, it++)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
glBindTexture(GL_TEXTURE_2D, it->first);
|
||||
}
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTextureShader::OnDrawCall(int indexCount)
|
||||
{
|
||||
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, NULL);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTextureShader::OnSubmit(const Transformable &t, float *&target, TransformationStack &stack, void (*restartDrawcall)(IRenderer &), IRenderer &rnd)
|
||||
{
|
||||
auto* r = dynamic_cast<Renderable*>(t.GetBehaviourScript(RENDERABLE));
|
||||
if (!r) return;
|
||||
if(!r->GetMaterial()->HasValue("mainTex")) return;
|
||||
const ITexture* tex = r->GetMaterial()->GetValue<ITexture*>("mainTex");
|
||||
float ts = 0;
|
||||
auto it = textureSlots.find(tex->GetTextureId());
|
||||
if (it != textureSlots.end())
|
||||
{
|
||||
ts = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(textureSlots.size() == 32)
|
||||
{
|
||||
restartDrawcall(rnd);
|
||||
textureSlots.clear();
|
||||
}
|
||||
textureSlots[tex->GetTextureId()] = textureSlots.size();
|
||||
ts = textureSlots[tex->GetTextureId()];
|
||||
}
|
||||
|
||||
|
||||
const Vector3* verts = r->GetVertices();
|
||||
const Vector2* uvs = r->GetUVs();
|
||||
ushort vCount = r->GetVertexCount();
|
||||
const Color color = r->GetMaterial()->GetValue<Color>("mainColor");
|
||||
Matrix4x4 matr = t.GetLocalMatrix();
|
||||
|
||||
stack.Push(matr);
|
||||
const Matrix4x4& top = stack.Top();
|
||||
|
||||
//Todo transformable.lastmatrix hier ergänzen
|
||||
|
||||
for (ushort i = 0; i < vCount; i++) {
|
||||
Vector3 p = top * verts[i];
|
||||
Vector2 uv = uvs[i];
|
||||
*target++ = p.x;
|
||||
*target++ = p.y;
|
||||
*target++ = p.z;
|
||||
*target++ = color.r;
|
||||
*target++ = color.g;
|
||||
*target++ = color.b;
|
||||
*target++ = color.a;
|
||||
*target++ = uv.x;
|
||||
*target++ = uv.y;
|
||||
*target++ = ts;
|
||||
}
|
||||
|
||||
stack.Pop();
|
||||
}
|
||||
28
TSE_OpenGlImpl/src/shader/basicTextureShader.hpp
Normal file
28
TSE_OpenGlImpl/src/shader/basicTextureShader.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "Shader.hpp"
|
||||
#include "Types.hpp"
|
||||
#include <map>
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
class BasicTextureShader : public Shader
|
||||
{
|
||||
private:
|
||||
static BasicTextureShader* instance;
|
||||
static std::map<uint, float> textureSlots;
|
||||
|
||||
public:
|
||||
static BasicTextureShader* Instance();
|
||||
static void Destroy();
|
||||
static void Init(float width, float height);
|
||||
BasicTextureShader(std::vector<std::unique_ptr<ShaderPart>>&& parts);
|
||||
|
||||
protected:
|
||||
void OnEnable() const override;
|
||||
void OnDisable() const override;
|
||||
void OnFlush() override;
|
||||
void OnDrawCall(int indexCount) override;
|
||||
void OnSubmit(const Transformable& t, float*& target, TransformationStack& stack, void (*restartDrawcall)(IRenderer&), IRenderer& rnd) override;
|
||||
};
|
||||
} // namespace TSE::OpenGL
|
||||
159
TSE_OpenGlImpl/src/shader/basicTextureShaderGLSL.hpp
Normal file
159
TSE_OpenGlImpl/src/shader/basicTextureShaderGLSL.hpp
Normal file
@@ -0,0 +1,159 @@
|
||||
#pragma once
|
||||
|
||||
inline const char* vertT = R"(
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 position;
|
||||
layout (location = 1) in vec4 color;
|
||||
layout (location = 2) in vec2 uv;
|
||||
layout (location = 3) in float tid;
|
||||
|
||||
|
||||
uniform mat4 prMatrix;
|
||||
uniform mat4 camMatrix;
|
||||
|
||||
out DATA
|
||||
{
|
||||
vec4 color_out;
|
||||
vec2 uv_out;
|
||||
float tid_out;
|
||||
} vs_out;
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = prMatrix * camMatrix * vec4(position.x, position.y, position.z, 1.0);
|
||||
vs_out.color_out = color;
|
||||
vs_out.uv_out = uv;
|
||||
vs_out.tid_out = tid;
|
||||
}
|
||||
)";
|
||||
|
||||
inline const char* fragT = R"(
|
||||
#version 330 core
|
||||
layout (location = 0) out vec4 color;
|
||||
|
||||
uniform sampler2D textures[32];
|
||||
|
||||
in DATA
|
||||
{
|
||||
vec4 color_out;
|
||||
vec2 uv_out;
|
||||
float tid_out;
|
||||
} fs_in;
|
||||
void main()
|
||||
{
|
||||
vec4 texColor = vec4(0,0,0,0);
|
||||
int tid = int(fs_in.tid_out);
|
||||
switch(tid)
|
||||
{
|
||||
case 0:
|
||||
texColor = texture(textures[0], fs_in.uv_out);
|
||||
break;
|
||||
case 1:
|
||||
texColor = texture(textures[1], fs_in.uv_out);
|
||||
break;
|
||||
case 2:
|
||||
texColor = texture(textures[2], fs_in.uv_out);
|
||||
break;
|
||||
case 3:
|
||||
texColor = texture(textures[3], fs_in.uv_out);
|
||||
break;
|
||||
case 4:
|
||||
texColor = texture(textures[4], fs_in.uv_out);
|
||||
break;
|
||||
case 5:
|
||||
texColor = texture(textures[5], fs_in.uv_out);
|
||||
break;
|
||||
case 6:
|
||||
texColor = texture(textures[6], fs_in.uv_out);
|
||||
break;
|
||||
case 7:
|
||||
texColor = texture(textures[7], fs_in.uv_out);
|
||||
break;
|
||||
case 8:
|
||||
texColor = texture(textures[8], fs_in.uv_out);
|
||||
break;
|
||||
case 9:
|
||||
texColor = texture(textures[9], fs_in.uv_out);
|
||||
break;
|
||||
case 10:
|
||||
texColor = texture(textures[10], fs_in.uv_out);
|
||||
break;
|
||||
case 11:
|
||||
texColor = texture(textures[11], fs_in.uv_out);
|
||||
break;
|
||||
case 12:
|
||||
texColor = texture(textures[12], fs_in.uv_out);
|
||||
break;
|
||||
case 13:
|
||||
texColor = texture(textures[13], fs_in.uv_out);
|
||||
break;
|
||||
case 14:
|
||||
texColor = texture(textures[14], fs_in.uv_out);
|
||||
break;
|
||||
case 15:
|
||||
texColor = texture(textures[15], fs_in.uv_out);
|
||||
break;
|
||||
case 16:
|
||||
texColor = texture(textures[16], fs_in.uv_out);
|
||||
break;
|
||||
case 17:
|
||||
texColor = texture(textures[17], fs_in.uv_out);
|
||||
break;
|
||||
case 18:
|
||||
texColor = texture(textures[18], fs_in.uv_out);
|
||||
break;
|
||||
case 19:
|
||||
texColor = texture(textures[19], fs_in.uv_out);
|
||||
break;
|
||||
case 20:
|
||||
texColor = texture(textures[20], fs_in.uv_out);
|
||||
break;
|
||||
case 21:
|
||||
texColor = texture(textures[21], fs_in.uv_out);
|
||||
break;
|
||||
case 22:
|
||||
texColor = texture(textures[22], fs_in.uv_out);
|
||||
break;
|
||||
case 23:
|
||||
texColor = texture(textures[23], fs_in.uv_out);
|
||||
break;
|
||||
case 24:
|
||||
texColor = texture(textures[24], fs_in.uv_out);
|
||||
break;
|
||||
case 25:
|
||||
texColor = texture(textures[25], fs_in.uv_out);
|
||||
break;
|
||||
case 26:
|
||||
texColor = texture(textures[26], fs_in.uv_out);
|
||||
break;
|
||||
case 27:
|
||||
texColor = texture(textures[27], fs_in.uv_out);
|
||||
break;
|
||||
case 28:
|
||||
texColor = texture(textures[28], fs_in.uv_out);
|
||||
break;
|
||||
case 29:
|
||||
texColor = texture(textures[29], fs_in.uv_out);
|
||||
break;
|
||||
case 30:
|
||||
texColor = texture(textures[30], fs_in.uv_out);
|
||||
break;
|
||||
case 31:
|
||||
texColor = texture(textures[31], fs_in.uv_out);
|
||||
break;
|
||||
default:
|
||||
texColor = vec4(1,1,1,1);
|
||||
break;
|
||||
}
|
||||
vec4 res = texColor * fs_in.color_out;
|
||||
|
||||
if(res.a < 0.1)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
|
||||
color = res;
|
||||
}
|
||||
)";
|
||||
209
TSE_OpenGlImpl/src/shader/basicTileMapShader.cpp
Normal file
209
TSE_OpenGlImpl/src/shader/basicTileMapShader.cpp
Normal file
@@ -0,0 +1,209 @@
|
||||
#include "basicTileMapShader.hpp"
|
||||
#include "BehaviourScripts/Renderable.hpp"
|
||||
#include "BehaviourScripts/TileMap.hpp"
|
||||
#include "Color.hpp"
|
||||
#include "basicTileMapShaderGLSL.hpp"
|
||||
|
||||
#define SHADER_MESH_INDEX 0
|
||||
#define SHADER_POS_INDEX 1
|
||||
#define SHADER_SPRITE_INDEX 2
|
||||
|
||||
#define SHADER_PACKAGE_SIZE sizeof(float) * (3 + 1)
|
||||
|
||||
TSE::OpenGL::BasicTileMapShader* TSE::OpenGL::BasicTileMapShader::instance = nullptr;
|
||||
|
||||
TSE::OpenGL::BasicTileMapShader *TSE::OpenGL::BasicTileMapShader::Instance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTileMapShader::Destroy()
|
||||
{
|
||||
if(instance != nullptr)
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTileMapShader::Init(float width, float height)
|
||||
{
|
||||
std::vector<std::unique_ptr<ShaderPart>> parts;
|
||||
parts.push_back(ShaderPart::LoadFromString(vertTile, GL_VERTEX_SHADER));
|
||||
parts.push_back(ShaderPart::LoadFromString(fragTile, GL_FRAGMENT_SHADER));
|
||||
instance = new BasicTileMapShader(std::move(parts));
|
||||
|
||||
instance->Enable();
|
||||
int texIDs[] = { 0 };
|
||||
instance->SetUniform("atlas", 0);
|
||||
instance->Disable();
|
||||
}
|
||||
|
||||
TSE::OpenGL::BasicTileMapShader::BasicTileMapShader(std::vector<std::unique_ptr<ShaderPart>> &&parts) : Shader(parts)
|
||||
{
|
||||
PackageSize = SHADER_PACKAGE_SIZE;
|
||||
}
|
||||
|
||||
TSE::OpenGL::BasicTileMapShader::~BasicTileMapShader()
|
||||
{
|
||||
if (meshVBO) glDeleteBuffers(1, &meshVBO);
|
||||
if (meshIBO) glDeleteBuffers(1, &meshIBO);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTileMapShader::SetMesh(const void *verts, int vertCount, int stride, int floatCountPerVertex, int posOffsetBytes, GLenum primitive, const void *indices, int indexCount, GLenum indexType)
|
||||
{
|
||||
GLint prevVAO = 0, prevArrayBuffer = 0, prevElementBuffer = 0;
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &prevVAO);
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prevArrayBuffer);
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &prevElementBuffer);
|
||||
|
||||
if (!meshVBO) glGenBuffers(1, &meshVBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, meshVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertCount * stride, verts, GL_STATIC_DRAW);
|
||||
|
||||
if (indices && indexCount > 0)
|
||||
{
|
||||
if (!meshIBO) glGenBuffers(1, &meshIBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIBO);
|
||||
GLsizeiptr idxSize =
|
||||
(indexType == GL_UNSIGNED_INT ? 4 :
|
||||
indexType == GL_UNSIGNED_SHORT? 2 : 1) * indexCount;
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, idxSize, indices, GL_STATIC_DRAW);
|
||||
meshIndexCount = indexCount;
|
||||
meshIndexType = indexType;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Kein Index-Buffer
|
||||
if (meshIBO) { glDeleteBuffers(1, &meshIBO); meshIBO = 0; }
|
||||
meshIndexCount = 0;
|
||||
}
|
||||
|
||||
meshVertexCount = vertCount;
|
||||
meshStride = stride;
|
||||
meshPosOffset = posOffsetBytes;
|
||||
meshPosSize = floatCountPerVertex;
|
||||
meshPrimitive = primitive;
|
||||
meshReady = true;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, prevArrayBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prevElementBuffer);
|
||||
glBindVertexArray(prevVAO);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTileMapShader::OnEnable() const
|
||||
{
|
||||
if (!meshReady)
|
||||
{
|
||||
// Fallback: unit-Quad als TRIANGLE_FAN (4 Vertices, 2D Positionen)
|
||||
const float quad[8] = { -0.5f,-0.5f, 0.5f,-0.5f, 0.5f,0.5f, -0.5f,0.5f };
|
||||
const_cast<BasicTileMapShader*>(this)->SetMesh(
|
||||
quad, 4, sizeof(float)*2, 2, 0, GL_TRIANGLE_FAN
|
||||
);
|
||||
}
|
||||
|
||||
GLint prevArrayBuffer = 0;
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prevArrayBuffer);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, meshVBO);
|
||||
glEnableVertexAttribArray(SHADER_MESH_INDEX); // LOC_QUAD/pos
|
||||
glVertexAttribPointer(SHADER_MESH_INDEX, meshPosSize, GL_FLOAT, GL_FALSE, meshStride, (void*)meshPosOffset);
|
||||
glVertexAttribDivisor(SHADER_MESH_INDEX, 0); // per-vertex (Mesh)
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, prevArrayBuffer);
|
||||
|
||||
// layout 1: position (vec3)
|
||||
glEnableVertexAttribArray(SHADER_POS_INDEX);
|
||||
glVertexAttribPointer(SHADER_POS_INDEX, 3, GL_FLOAT, GL_FALSE, PackageSize, (void*)0);
|
||||
glVertexAttribDivisor(SHADER_POS_INDEX, 1);
|
||||
|
||||
// layout 2: spriteindex (float)
|
||||
glEnableVertexAttribArray(SHADER_SPRITE_INDEX);
|
||||
glVertexAttribPointer(SHADER_SPRITE_INDEX, 1, GL_FLOAT, GL_FALSE, PackageSize, (void*)(sizeof(float)*3));
|
||||
glVertexAttribDivisor(SHADER_SPRITE_INDEX, 1);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTileMapShader::OnDisable() const
|
||||
{
|
||||
glDisableVertexAttribArray(SHADER_MESH_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_POS_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_SPRITE_INDEX);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTileMapShader::OnFlush()
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, TextureID);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTileMapShader::OnDrawCall(int indexCount)
|
||||
{
|
||||
if (instanceCount <= 0) return;
|
||||
SetUniform("spriteCount", &SpriteCount);
|
||||
SetUniform("spriteScale", &SpriteScale);
|
||||
|
||||
GLint prevElementBuffer = 0;
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &prevElementBuffer);
|
||||
|
||||
if (meshIBO && meshIndexCount > 0)
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIBO);
|
||||
glDrawElementsInstanced(meshPrimitive, meshIndexCount, meshIndexType, (void*)0, instanceCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glDrawArraysInstanced(meshPrimitive, 0, meshVertexCount, instanceCount);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GLuint)prevElementBuffer);
|
||||
instanceCount = 0;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::BasicTileMapShader::OnSubmit(const Transformable &t, float *&target, TransformationStack &stack, void (*restartDrawcall)(IRenderer &), IRenderer &rnd)
|
||||
{
|
||||
auto* r = dynamic_cast<Renderable*>(t.GetBehaviourScript(RENDERABLE));
|
||||
if (!r) return;
|
||||
|
||||
auto* tm = dynamic_cast<TileMap*>(t.GetBehaviourScript(TILE_MAP));
|
||||
if (!tm) return;
|
||||
|
||||
auto tileSet = tm->GetTileSet();
|
||||
TextureID = tileSet->GetTextueID();
|
||||
SpriteCount = tileSet->GetCount();
|
||||
SpriteScale = tm->SpriteScale;
|
||||
|
||||
const std::vector<Vector2> orderedChunks = *tm->GetChunkPositionsInOrder();
|
||||
|
||||
Matrix4x4 matr = t.GetLocalMatrix();
|
||||
|
||||
stack.Push(matr);
|
||||
|
||||
for(auto chunkPos : orderedChunks)
|
||||
{
|
||||
auto chunk = tm->GetChunk(chunkPos);
|
||||
const int spriteCount = chunk->GetSpriteCount();
|
||||
const std::vector<Vector2> spritePositions = *chunk->GetOrderedPositions();
|
||||
const std::vector<Vector2i> spriteIds = *chunk->GetOrderedSpriteIds();
|
||||
int chunkSize = chunk->GetChunksize();
|
||||
|
||||
for (int i = 0; i < spriteCount; i++)
|
||||
{
|
||||
Matrix4x4 mat = Matrix4x4::ToTranslationMatrix((chunkPos - chunk->nextLine * chunkPos.y) + spritePositions[i]) * Matrix4x4::ToRotationMatrix(Quaternion()) * Matrix4x4::ToScaleMatrix({1,1,1});
|
||||
stack.Push(mat);
|
||||
Vector3 pos = stack.Top() * Vector3(0,0,0);
|
||||
|
||||
*target++ = pos.x;
|
||||
*target++ = pos.y;
|
||||
*target++ = pos.z;
|
||||
*target++ = spriteIds[i].x;
|
||||
|
||||
++instanceCount;
|
||||
stack.Pop();
|
||||
|
||||
if(instanceCount >= 20000)
|
||||
restartDrawcall(rnd);
|
||||
}
|
||||
}
|
||||
|
||||
stack.Pop();
|
||||
restartDrawcall(rnd);
|
||||
}
|
||||
44
TSE_OpenGlImpl/src/shader/basicTileMapShader.hpp
Normal file
44
TSE_OpenGlImpl/src/shader/basicTileMapShader.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "GL/gl3w.h"
|
||||
#include "GL/gl.h"
|
||||
#include "Shader.hpp"
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
class BasicTileMapShader : public Shader
|
||||
{
|
||||
private:
|
||||
static BasicTileMapShader* instance;
|
||||
mutable bool meshReady = false;
|
||||
GLuint meshVBO = 0;
|
||||
GLuint meshIBO = 0;
|
||||
GLsizei meshVertexCount = 0; // für DrawArraysInstanced
|
||||
GLsizei meshIndexCount = 0; // für DrawElementsInstanced
|
||||
GLenum meshPrimitive = GL_TRIANGLES;
|
||||
GLenum meshIndexType = GL_UNSIGNED_SHORT;
|
||||
int instanceCount = 0; // eigener Instanzzähler
|
||||
GLint meshPosSize = 2; // 2D (Billboard-Formen), für 3D Meshes: 3
|
||||
GLsizei meshStride = sizeof(float) * 2;
|
||||
size_t meshPosOffset = 0;
|
||||
GLuint TextureID;
|
||||
Vector2 SpriteCount;
|
||||
Vector2 SpriteScale;
|
||||
|
||||
public:
|
||||
static BasicTileMapShader* Instance();
|
||||
static void Destroy();
|
||||
static void Init(float width, float height);
|
||||
BasicTileMapShader(std::vector<std::unique_ptr<ShaderPart>>&& parts);
|
||||
~BasicTileMapShader();
|
||||
void SetMesh(const void* verts, int vertCount, int stride, int floatCountPerVertex, int posOffsetBytes, GLenum primitive, const void* indices = nullptr, int indexCount = 0, GLenum indexType = GL_UNSIGNED_SHORT);
|
||||
|
||||
protected:
|
||||
void OnEnable() const override;
|
||||
void OnDisable() const override;
|
||||
void OnFlush() override;
|
||||
void OnDrawCall(int indexCount) override;
|
||||
void OnSubmit(const Transformable& t, float*& target, TransformationStack& stack, void (*restartDrawcall)(IRenderer&), IRenderer& rnd) override;
|
||||
};
|
||||
} // namespace TSE::OpenGL
|
||||
60
TSE_OpenGlImpl/src/shader/basicTileMapShaderGLSL.hpp
Normal file
60
TSE_OpenGlImpl/src/shader/basicTileMapShaderGLSL.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
inline const char* vertTile = R"(
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec2 aPos;
|
||||
|
||||
layout(location = 1) in vec3 iTilePos;
|
||||
layout(location = 2) in float iSpriteId;
|
||||
|
||||
uniform mat4 prMatrix;
|
||||
uniform mat4 camMatrix;
|
||||
uniform vec2 spriteCount;
|
||||
uniform vec2 spriteScale;
|
||||
|
||||
out vec2 vUV;
|
||||
flat out int vSpriteId;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 local = vec3(aPos.x, aPos.y, 0);
|
||||
vec2 baseUV = aPos + vec2(0.5);
|
||||
vec3 tileSize = vec3(spriteScale.x, spriteScale.y, 1);
|
||||
|
||||
vec3 worldPos = (iTilePos * tileSize) + (local * tileSize);
|
||||
|
||||
gl_Position = prMatrix * camMatrix * vec4(worldPos.x, worldPos.y, worldPos.z, 1.0);
|
||||
|
||||
vUV = baseUV;
|
||||
vSpriteId = int(iSpriteId + 0.5);
|
||||
}
|
||||
)";
|
||||
|
||||
inline const char* fragTile = R"(
|
||||
#version 330 core
|
||||
|
||||
in vec2 vUV;
|
||||
flat in int vSpriteId;
|
||||
|
||||
uniform sampler2D atlas;
|
||||
uniform vec2 spriteCount;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 tileUVSize = 1.0 / spriteCount;
|
||||
|
||||
int cols = int(spriteCount.x);
|
||||
int sx = vSpriteId % cols;
|
||||
int sy = vSpriteId / cols;
|
||||
|
||||
vec2 atlasOffset = vec2(float(sx), float(sy)) * tileUVSize;
|
||||
vec2 atlasUV = atlasOffset + (vUV * tileUVSize);
|
||||
vec4 c = texture(atlas, atlasUV);
|
||||
if (c.a < 0.01) discard;
|
||||
|
||||
FragColor = c;
|
||||
}
|
||||
)";
|
||||
40
TSE_OpenGlImpl/src/shader/defaultShaderHandler.cpp
Normal file
40
TSE_OpenGlImpl/src/shader/defaultShaderHandler.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "defaultShaderHandler.hpp"
|
||||
#include "basicShader.hpp"
|
||||
#include "basicTextureShader.hpp"
|
||||
#include "ditheringShader.hpp"
|
||||
#include "basicParticleShader.hpp"
|
||||
#include "basicTileMapShader.hpp"
|
||||
#include "basicOrderedSpriteSetShader.hpp"
|
||||
#include "elements/ShaderRegistry.hpp"
|
||||
|
||||
void TSE::OpenGL::LoadBasicShaders(float width, float height)
|
||||
{
|
||||
BasicShader::Init(width, height);
|
||||
BasicTextureShader::Init(width, height);
|
||||
DitheringShader::Init(width, height);
|
||||
BasicParticleShader::Init(width, height);
|
||||
BasicTileMapShader::Init(width, height);
|
||||
BasicOrderedSpriteSetShader::Init(width, height);
|
||||
ShaderRegistry::SetShader("Basic Unlit Shader", BasicShader::Instance());
|
||||
ShaderRegistry::SetShader("Basic Unlit Texture Shader", BasicTextureShader::Instance());
|
||||
ShaderRegistry::SetShader("Basic Unlit Dithering Shader", DitheringShader::Instance());
|
||||
ShaderRegistry::SetShader("Basic Unlit Particle Shader", BasicParticleShader::Instance());
|
||||
ShaderRegistry::SetShader("Basic Unlit TileMap Shader", BasicTileMapShader::Instance());
|
||||
ShaderRegistry::SetShader("Basic Ordered Sprite Set Shader", BasicOrderedSpriteSetShader::Instance());
|
||||
}
|
||||
|
||||
void TSE::OpenGL::UnLoadBasicShaders()
|
||||
{
|
||||
ShaderRegistry::RemoveShader("Basic Unlit Shader");
|
||||
ShaderRegistry::RemoveShader("Basic Unlit Texture Shader");
|
||||
ShaderRegistry::RemoveShader("Basic Unlit Dithering Shader");
|
||||
ShaderRegistry::RemoveShader("Basic Unlit Particle Shader");
|
||||
ShaderRegistry::RemoveShader("Basic Unlit TileMap Shader");
|
||||
ShaderRegistry::RemoveShader("Basic Ordered Sprite Set Shader");
|
||||
BasicShader::Destroy();
|
||||
BasicTextureShader::Destroy();
|
||||
DitheringShader::Destroy();
|
||||
BasicParticleShader::Destroy();
|
||||
BasicTileMapShader::Destroy();
|
||||
BasicOrderedSpriteSetShader::Destroy();
|
||||
}
|
||||
7
TSE_OpenGlImpl/src/shader/defaultShaderHandler.hpp
Normal file
7
TSE_OpenGlImpl/src/shader/defaultShaderHandler.hpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
void LoadBasicShaders(float width, float height);
|
||||
void UnLoadBasicShaders();
|
||||
} // namespace TSE::GLFW
|
||||
91
TSE_OpenGlImpl/src/shader/ditheringShader.cpp
Normal file
91
TSE_OpenGlImpl/src/shader/ditheringShader.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "GL/gl3w.h"
|
||||
#include "GL/gl.h"
|
||||
#include "ditheringShader.hpp"
|
||||
#include "ditheringShaderGLSL.hpp"
|
||||
#include "BehaviourScripts/Renderable.hpp"
|
||||
#include "Color.hpp"
|
||||
|
||||
#define SHADER_VERTEX_INDEX 0
|
||||
#define SHADER_COLOR_INDEX 1
|
||||
|
||||
#define SHADER_PACKAGE_SIZE sizeof(float) * (3 + 4)
|
||||
|
||||
TSE::OpenGL::DitheringShader* TSE::OpenGL::DitheringShader::instance = nullptr;
|
||||
|
||||
TSE::OpenGL::DitheringShader *TSE::OpenGL::DitheringShader::Instance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::DitheringShader::Destroy()
|
||||
{
|
||||
if(instance != nullptr)
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::DitheringShader::Init(float width, float height)
|
||||
{
|
||||
std::vector<std::unique_ptr<ShaderPart>> parts;
|
||||
parts.push_back(ShaderPart::LoadFromString(vertD, GL_VERTEX_SHADER));
|
||||
parts.push_back(ShaderPart::LoadFromString(fragD, GL_FRAGMENT_SHADER));
|
||||
instance = new DitheringShader(std::move(parts));
|
||||
}
|
||||
|
||||
TSE::OpenGL::DitheringShader::DitheringShader(std::vector<std::unique_ptr<ShaderPart>> &&parts) : Shader(parts)
|
||||
{
|
||||
PackageSize = SHADER_PACKAGE_SIZE;
|
||||
}
|
||||
|
||||
void TSE::OpenGL::DitheringShader::OnEnable() const
|
||||
{
|
||||
glEnableVertexAttribArray(SHADER_VERTEX_INDEX);
|
||||
glVertexAttribPointer(SHADER_VERTEX_INDEX, 3, GL_FLOAT, false, SHADER_PACKAGE_SIZE, (void*)0);
|
||||
glEnableVertexAttribArray(SHADER_COLOR_INDEX);
|
||||
glVertexAttribPointer(SHADER_COLOR_INDEX, 4, GL_FLOAT, false, SHADER_PACKAGE_SIZE, (void*)(sizeof(float) * 3));
|
||||
}
|
||||
|
||||
void TSE::OpenGL::DitheringShader::OnDisable() const
|
||||
{
|
||||
glDisableVertexAttribArray(SHADER_VERTEX_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_COLOR_INDEX);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::DitheringShader::OnFlush()
|
||||
{
|
||||
}
|
||||
|
||||
void TSE::OpenGL::DitheringShader::OnDrawCall(int indexCount)
|
||||
{
|
||||
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, NULL);
|
||||
}
|
||||
|
||||
void TSE::OpenGL::DitheringShader::OnSubmit(const Transformable &t, float *&target, TransformationStack &stack, void (*restartDrawcall)(IRenderer &), IRenderer &rnd)
|
||||
{
|
||||
auto* r = dynamic_cast<Renderable*>(t.GetBehaviourScript(RENDERABLE));
|
||||
if (!r) return;
|
||||
|
||||
|
||||
const Vector3* verts = r->GetVertices();
|
||||
ushort vCount = r->GetVertexCount();
|
||||
const Color color = r->GetMaterial()->GetValue<Color>("mainColor");
|
||||
Matrix4x4 matr = t.GetLocalMatrix();
|
||||
|
||||
stack.Push(matr);
|
||||
const Matrix4x4& top = stack.Top();
|
||||
|
||||
//Todo transformable.lastmatrix hier ergänzen
|
||||
|
||||
for (ushort i = 0; i < vCount; i++) {
|
||||
Vector3 p = top * verts[i];
|
||||
*target++ = p.x;
|
||||
*target++ = p.y;
|
||||
*target++ = p.z;
|
||||
*target++ = color.r;
|
||||
*target++ = color.g;
|
||||
*target++ = color.b;
|
||||
*target++ = color.a;
|
||||
}
|
||||
|
||||
stack.Pop();
|
||||
}
|
||||
25
TSE_OpenGlImpl/src/shader/ditheringShader.hpp
Normal file
25
TSE_OpenGlImpl/src/shader/ditheringShader.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "Shader.hpp"
|
||||
|
||||
namespace TSE::OpenGL
|
||||
{
|
||||
class DitheringShader : public Shader
|
||||
{
|
||||
private:
|
||||
static DitheringShader* instance;
|
||||
|
||||
public:
|
||||
static DitheringShader* Instance();
|
||||
static void Destroy();
|
||||
static void Init(float width, float height);
|
||||
DitheringShader(std::vector<std::unique_ptr<ShaderPart>>&& parts);
|
||||
|
||||
protected:
|
||||
void OnEnable() const override;
|
||||
void OnDisable() const override;
|
||||
void OnFlush() override;
|
||||
void OnDrawCall(int indexCount) override;
|
||||
void OnSubmit(const Transformable& t, float*& target, TransformationStack& stack, void (*restartDrawcall)(IRenderer&), IRenderer& rnd) override;
|
||||
};
|
||||
} // namespace TSE::OpenGL
|
||||
80
TSE_OpenGlImpl/src/shader/ditheringShaderGLSL.hpp
Normal file
80
TSE_OpenGlImpl/src/shader/ditheringShaderGLSL.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
inline const char* vertD = R"(
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 position;
|
||||
layout (location = 1) in vec4 color;
|
||||
|
||||
uniform mat4 prMatrix;
|
||||
uniform mat4 camMatrix;
|
||||
|
||||
out DATA
|
||||
{
|
||||
vec4 color_out;
|
||||
} vs_out;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = prMatrix * camMatrix * vec4(position, 1.0);
|
||||
vs_out.color_out = color;
|
||||
}
|
||||
)";
|
||||
|
||||
inline const char* fragD = R"(
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) out vec4 color;
|
||||
|
||||
in DATA
|
||||
{
|
||||
vec4 color_out;
|
||||
} fs_in;
|
||||
|
||||
// 8x8 Bayer-Matrix (0..63), als Schwellwerttabelle
|
||||
float bayer8(vec2 fragXY)
|
||||
{
|
||||
int x = int(mod(fragXY.x, 8.0));
|
||||
int y = int(mod(fragXY.y, 8.0));
|
||||
int idx = x + y * 8;
|
||||
|
||||
// Werte aus klassischer 8x8 Ordered-Dithering-Matrix
|
||||
int m[64] = int[64](
|
||||
0, 48, 12, 60, 3, 51, 15, 63,
|
||||
32, 16, 44, 28, 35, 19, 47, 31,
|
||||
8, 56, 4, 52, 11, 59, 7, 55,
|
||||
40, 24, 36, 20, 43, 27, 39, 23,
|
||||
2, 50, 14, 62, 1, 49, 13, 61,
|
||||
34, 18, 46, 30, 33, 17, 45, 29,
|
||||
10, 58, 6, 54, 9, 57, 5, 53,
|
||||
42, 26, 38, 22, 41, 25, 37, 21
|
||||
);
|
||||
|
||||
// +0.5, damit die Schwellen mittig zwischen Stufen liegen
|
||||
return (float(m[idx]) + 0.5) / 64.0;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 c = fs_in.color_out;
|
||||
|
||||
// alpha == 0 -> nichts rendern
|
||||
if (c.a <= 0.0)
|
||||
discard;
|
||||
|
||||
// Für alpha < 1 verwenden wir Ordered Dithering
|
||||
if (c.a < 1.0)
|
||||
{
|
||||
float threshold = bayer8(gl_FragCoord.xy);
|
||||
// ist der Schwellenwert größer als alpha? -> Pixel auslassen
|
||||
if (threshold > c.a)
|
||||
discard;
|
||||
}
|
||||
|
||||
// Wenn wir bis hier nicht verworfen haben, zeichnen wir den Pixel.
|
||||
// Typischerweise setzt man die ausgegebene Alpha dann auf 1.0,
|
||||
// weil die Transparenz bereits über das Dithering realisiert wurde.
|
||||
color = vec4(c.rgb, 1.0);
|
||||
}
|
||||
)";
|
||||
Reference in New Issue
Block a user