Skip to content

Instantly share code, notes, and snippets.

@dranikpg
Created July 31, 2022 15:32
Show Gist options
  • Save dranikpg/a49c4d4d29ba2ef239e77fd916e7da21 to your computer and use it in GitHub Desktop.
Save dranikpg/a49c4d4d29ba2ef239e77fd916e7da21 to your computer and use it in GitHub Desktop.
#include <Magnum/GL/Context.h>
#include <Magnum/Math/Color.h>
#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/Shaders/FlatGL.h>
#include <iostream>
using namespace Magnum;
class MyApplication: public Platform::Application {
public:
explicit MyApplication(const Arguments& arguments);
private:
void drawEvent() override;
GL::Buffer _buffer;
GL::Mesh _mesh;
Shaders::FlatGL2D _shader{NoCreate};
};
using namespace Math::Literals;
const float data[] = { -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 0.5f};
MyApplication::MyApplication(const Arguments& arguments): Platform::Application{arguments} {
auto cs = Shaders::FlatGL2D::compile({}, 1, 1);
// Uncommenting this makes it work as expected.
//
// while (!cs.isLinkFinished()) { std::cout << "."; std::cout.flush();}
//
std::cout << "constructor 1: " << cs.id() << " & " << cs.isLinkFinished() << std::endl;
_shader = Shaders::FlatGL2D{std::move(cs)};
std::cout << "constructor 2: " << _shader.id() << " & " << _shader.isLinkFinished() << std::endl;
_shader.setColor(0x00ff00_rgbf);
_buffer.setData(data, GL::BufferUsage::StaticDraw);
_mesh.setPrimitive(GL::MeshPrimitive::Triangles)
.setCount(3)
.addVertexBuffer(_buffer, 0,
Shaders::FlatGL2D::Position{});
}
void MyApplication::drawEvent() {
GL::defaultFramebuffer.clear(GL::FramebufferClear::Color);
_mesh.draw(_shader);
// This might print false on the first pass
std::cout << "drawEvent: " << _shader.id() << " & " << _shader.isLinkFinished() << std::endl;
swapBuffers();
}
MAGNUM_APPLICATION_MAIN(MyApplication)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment