added TileMaps to TSE
This commit is contained in:
212
TSE_GlfwOpenGlImpl/src/shader/basicTileMapShader.cpp
Normal file
212
TSE_GlfwOpenGlImpl/src/shader/basicTileMapShader.cpp
Normal file
@@ -0,0 +1,212 @@
|
||||
#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::GLFW::BasicTileMapShader* TSE::GLFW::BasicTileMapShader::instance = nullptr;
|
||||
|
||||
TSE::GLFW::BasicTileMapShader *TSE::GLFW::BasicTileMapShader::Instance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
void TSE::GLFW::BasicTileMapShader::Destroy()
|
||||
{
|
||||
if(instance != nullptr)
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
void TSE::GLFW::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::GLFW::BasicTileMapShader::BasicTileMapShader(std::vector<std::unique_ptr<ShaderPart>> &&parts) : Shader(parts)
|
||||
{
|
||||
PackageSize = SHADER_PACKAGE_SIZE;
|
||||
}
|
||||
|
||||
TSE::GLFW::BasicTileMapShader::~BasicTileMapShader()
|
||||
{
|
||||
if (meshVBO) glDeleteBuffers(1, &meshVBO);
|
||||
if (meshIBO) glDeleteBuffers(1, &meshIBO);
|
||||
}
|
||||
|
||||
void TSE::GLFW::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::GLFW::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::GLFW::BasicTileMapShader::OnDisable() const
|
||||
{
|
||||
glDisableVertexAttribArray(SHADER_MESH_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_POS_INDEX);
|
||||
glDisableVertexAttribArray(SHADER_SPRITE_INDEX);
|
||||
}
|
||||
|
||||
void TSE::GLFW::BasicTileMapShader::OnFlush()
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, TextureID);
|
||||
}
|
||||
|
||||
void TSE::GLFW::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::GLFW::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;
|
||||
|
||||
int chunkcount = tm->GetChunkCount();
|
||||
Vector2 orderedChunks[chunkcount];
|
||||
tm->GetChunkPositionsInOrder(orderedChunks);
|
||||
|
||||
Matrix4x4 matr = t.GetLocalMatrix();
|
||||
|
||||
stack.Push(matr);
|
||||
|
||||
for(auto chunkPos : orderedChunks)
|
||||
{
|
||||
auto chunk = tm->GetChunk(chunkPos);
|
||||
int spriteCount = chunk->GetSpriteCount();
|
||||
Vector2 spritePositions[spriteCount];
|
||||
int spriteIds[spriteCount];
|
||||
chunk->GetOrderedPositions(spritePositions);
|
||||
chunk->GetOrderedSpriteIds(spriteIds);
|
||||
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];
|
||||
|
||||
++instanceCount;
|
||||
stack.Pop();
|
||||
|
||||
if(instanceCount >= 20000)
|
||||
restartDrawcall(rnd);
|
||||
}
|
||||
}
|
||||
|
||||
restartDrawcall(rnd);
|
||||
}
|
||||
44
TSE_GlfwOpenGlImpl/src/shader/basicTileMapShader.hpp
Normal file
44
TSE_GlfwOpenGlImpl/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::GLFW
|
||||
{
|
||||
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::GLFW
|
||||
60
TSE_GlfwOpenGlImpl/src/shader/basicTileMapShaderGLSL.hpp
Normal file
60
TSE_GlfwOpenGlImpl/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;
|
||||
}
|
||||
)";
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "basicTextureShader.hpp"
|
||||
#include "ditheringShader.hpp"
|
||||
#include "basicParticleShader.hpp"
|
||||
#include "basicTileMapShader.hpp"
|
||||
#include "elements/ShaderRegistry.hpp"
|
||||
|
||||
void TSE::GLFW::LoadBasicShaders(float width, float height)
|
||||
@@ -11,10 +12,12 @@ void TSE::GLFW::LoadBasicShaders(float width, float height)
|
||||
BasicTextureShader::Init(width, height);
|
||||
DitheringShader::Init(width, height);
|
||||
BasicParticleShader::Init(width, height);
|
||||
BasicTileMapShader::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());
|
||||
}
|
||||
|
||||
void TSE::GLFW::UnLoadBasicShaders()
|
||||
@@ -23,8 +26,10 @@ void TSE::GLFW::UnLoadBasicShaders()
|
||||
ShaderRegistry::RemoveShader("Basic Unlit Texture Shader");
|
||||
ShaderRegistry::RemoveShader("Basic Unlit Dithering Shader");
|
||||
ShaderRegistry::RemoveShader("Basic Unlit Particle Shader");
|
||||
ShaderRegistry::RemoveShader("Basic Unlit TileMap Shader");
|
||||
BasicShader::Destroy();
|
||||
BasicTextureShader::Destroy();
|
||||
DitheringShader::Destroy();
|
||||
BasicParticleShader::Destroy();
|
||||
BasicTileMapShader::Destroy();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user