Skip to content

Instantly share code, notes, and snippets.

@hsdk123
Created August 15, 2025 02:42
Show Gist options
  • Select an option

  • Save hsdk123/c2f097e6d29c26d35ead21724367e333 to your computer and use it in GitHub Desktop.

Select an option

Save hsdk123/c2f097e6d29c26d35ead21724367e333 to your computer and use it in GitHub Desktop.
class CVertexArrayBufferImpl
{
public:
CVertexArrayBufferImpl()
{
_mesh = std::make_shared<Magnum::GL::Mesh>(Magnum::GL::MeshPrimitive::Triangles);
//_shader = std::make_shared<VertexArrayBufferShaderImpl>();
}
void Render(
NState_Abstract& state,
const CoreMath::TransformMatrix& matrix_local2projection,
const CoreMath::Colour& rgba,
const std::vector<NShader_SpriteImpl*> custom_shaders,
const CVertexArrayBuffer::RenderConfiguration& config)
{
using namespace Magnum;
using namespace Math::Literals;
auto& mesh = *std::static_pointer_cast<Magnum::GL::Mesh>(_mesh);
const auto rgba_impl = Magnum::Color4(
1.f * rgba.r / CoreMath::Colour::ValOne,
1.f * rgba.g / CoreMath::Colour::ValOne,
1.f * rgba.b / CoreMath::Colour::ValOne,
1.f * rgba.a / CoreMath::Colour::ValOne);
if (rgba_impl.a() > 0)
{
SUCCESS_CHECK_RETURN_IF_FAILED(state.HasComponent<CMask>());
auto& mask_data = state.GetComponent<CMask>().GetData();
SECORE_CHECK_RENDERER_STATUS();
static const auto uv_transform_current = Magnum::Matrix3(Magnum::Math::IdentityInit);
const auto& prev_blend = CRenderMgr::GetInstance().GetCurrentBlendMode();
CRenderMgr::GetInstance().ApplyBlendMode(BlendMode::BlendAlpha);
{
auto& shader = *(std::static_pointer_cast<NShader_SpriteAnimationImpl>(CShaderMgr::GetInstance().loadShader(L"vn_NShader_SpriteAnimationImpl")));
const bool can_render = shader.set_mask_variables(*state.GetOwnerObj(), mask_data);
if (can_render)
{
shader
.bindFirstTexture(*(static_cast<Magnum::GL::Texture2D*>(config.texture->getTextureToRender())))
.bindNextTransitionTexture(*(static_cast<Magnum::GL::Texture2D*>(config.texture->getTextureToRender())))
//.bindAlphaTransitionTexture(sprite_animation.GetAlphaTransitionTexture())
.setTextureMatrix_FirstTexture(uv_transform_current)
.setTextureMatrix_NextTexture(uv_transform_current)
//.setTimePassedFraction(animation_passed_ratio)
.setColor(rgba_impl)
.setTransformationProjectionMatrix(matrix_local2projection)
;
shader.draw(mesh);
}
}
CRenderMgr::GetInstance().ApplyBlendMode(prev_blend);
SECORE_CHECK_RENDERER_STATUS();
}
// wireframe shader
CRenderMgr::GetInstance().RenderWireframe(
matrix_local2projection
// local: scale
* CoreMath::TransformMatrix::scaling({ 2.f, 2.f, 2.f }),
CoreMath::Colour(0, 0, CoreMath::Colour::ValOne, CoreMath::Colour::ValOne)
);
}
public:
std::shared_ptr<void> _mesh;
};
lvn::CVertexArrayBuffer::CVertexArrayBuffer()
{
}
lvn::CVertexArrayBuffer::~CVertexArrayBuffer()
{
}
std::unique_ptr<CComponent> lvn::CVertexArrayBuffer::Clone()
{
return std::unique_ptr<CComponent>();
}
std::unique_ptr<CVertexArrayBuffer> lvn::CVertexArrayBuffer::Create()
{
return std::make_unique<CVertexArrayBuffer>();
}
void CVertexArrayBuffer::Render(
NObj& container,
const std::vector<Vertex>& vertices,
const std::vector<uint16_t>& indices,
const RenderConfiguration& config)
{
auto& state = container.GetAbstractState();
if (!_buffer) {
_buffer = std::make_shared<CVertexArrayBufferImpl>();
}
// matrix. todo: I should be passing in the camerarelationship, instead of using a fixed value.
const auto matrix_local2projection = CCameraMgr::GetInstance().getWorldViewProjectionMatrix(
state.GetComponent<CTransform>(), state.GetComponent<CRenderable>()._camera_relationship);
// colour
auto rgba = CoreMath::Colour::White;
if (state.HasComponent<CColour>()) {
rgba = state.GetComponent<CColour>().GetVal();
}
// construct mesh
auto& buffer = *static_cast<CVertexArrayBufferImpl*>(_buffer.get());
{
using namespace Magnum;
using namespace Math::Literals;
if (!config.texture) {
return;
}
struct TriangleVertex {
NShader_SpriteAnimationImpl::Position::Type position;
NShader_SpriteAnimationImpl::TextureCoordinates::Type textureCoordinates;
};
std::vector<TriangleVertex> vertices_;
{
for (const auto& vertex : vertices) {
auto& vertex_ = vertices_.emplace_back(TriangleVertex());
vertex_.position = { vertex.x, vertex.y, 0.f };
vertex_.textureCoordinates = { vertex.u, vertex.v };
}
}
Corrade::Containers::ArrayView<const void> data_vertices(vertices_);
GL::Buffer buffer_vertex;
buffer_vertex.setData(data_vertices, GL::BufferUsage::StaticDraw);
Containers::Pair<Containers::Array<char>, MeshIndexType> compressed_indices =
MeshTools::compressIndices(indices);
GL::Buffer buffer_indices(compressed_indices.first());
// reference: https://github.com/EsotericSoftware/spine-runtimes/blob/4.2/spine-glfw/src/spine-glfw.cpp#L82
buffer._mesh = std::make_shared<Magnum::GL::Mesh>(Magnum::GL::MeshPrimitive::Triangles);
std::static_pointer_cast<Magnum::GL::Mesh>(buffer._mesh)->
setCount(static_cast<Magnum::Int>(indices.size()))
.addVertexBuffer(std::move(buffer_vertex) /* transfer ownership to mesh */, 0,
NShader_SpriteAnimationImpl::Position{},
NShader_SpriteAnimationImpl::TextureCoordinates{})
.setIndexBuffer(std::move(buffer_indices), 0, compressed_indices.second());
}
buffer.Render(
state,
matrix_local2projection,
rgba,
{},
config
);
/*auto& buffer = *static_cast<CVertexArrayBufferImpl*>(_buffer.get());
buffer.Render(
matrix_local2projection,
*static_cast<Magnum::GL::CubeMapTexture*>(texture.getTextureToRender()),
rgba,
{}
);*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment