added rest of the openGL implementation theoreticaly the engine can render now

This commit is contained in:
2026-01-17 23:04:56 +01:00
parent 0f8c21278a
commit 551b790243
31 changed files with 1648 additions and 6 deletions

View File

@@ -0,0 +1,134 @@
#include "basicTextureShader.hpp"
#include "basicTextureShaderGLSL.hpp"
#include "BehaviourScripts/Renderable.hpp"
#include "Color.hpp"
#include "interfaces/ITexture.hpp"
#include "GL/gl3w.h"
#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::GLFW::BasicTextureShader* TSE::GLFW::BasicTextureShader::instance = nullptr;
std::map<uint, float> TSE::GLFW::BasicTextureShader::textureSlots;
TSE::GLFW::BasicTextureShader *TSE::GLFW::BasicTextureShader::Instance()
{
return instance;
}
void TSE::GLFW::BasicTextureShader::Destroy()
{
if(instance != nullptr)
delete instance;
instance = nullptr;
}
void TSE::GLFW::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::GLFW::BasicTextureShader::BasicTextureShader(std::vector<std::unique_ptr<ShaderPart>> &&parts) : Shader(parts)
{
PackageSize = SHADER_PACKAGE_SIZE;
}
void TSE::GLFW::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::GLFW::BasicTextureShader::OnDisable() const
{
glDisableVertexAttribArray(SHADER_VERTEX_INDEX);
glDisableVertexAttribArray(SHADER_COLOR_INDEX);
glDisableVertexAttribArray(SHADER_UV_INDEX);
glDisableVertexAttribArray(SHADER_TID_INDEX);
}
void TSE::GLFW::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::GLFW::BasicTextureShader::OnDrawCall(int indexCount)
{
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, NULL);
}
void TSE::GLFW::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();
}