Skip to content

Instantly share code, notes, and snippets.

@mosra
Created March 21, 2019 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosra/faee459393146650b3d1107e15b40fd4 to your computer and use it in GitHub Desktop.
Save mosra/faee459393146650b3d1107e15b40fd4 to your computer and use it in GitHub Desktop.
Triangle, but windowless
#include <Magnum/DebugTools/Screenshot.h>
#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/Framebuffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/GL/Renderbuffer.h>
#include <Magnum/GL/RenderbufferFormat.h>
#ifdef MAGNUM_TARGET_HEADLESS
#include <Magnum/Platform/WindowlessEglApplication.h>
#elif defined(CORRADE_TARGET_IOS)
#include <Magnum/Platform/WindowlessIosApplication.h>
#elif defined(CORRADE_TARGET_APPLE)
#include <Magnum/Platform/WindowlessCglApplication.h>
#elif defined(CORRADE_TARGET_UNIX)
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_DESKTOP_GLES)
#include <Magnum/Platform/WindowlessGlxApplication.h>
#else
#include <Magnum/Platform/WindowlessEglApplication.h>
#endif
#elif defined(CORRADE_TARGET_WINDOWS)
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_DESKTOP_GLES)
#include <Magnum/Platform/WindowlessWglApplication.h>
#else
#include <Magnum/Platform/WindowlessWindowsEglApplication.h>
#endif
#else
#error no windowless application available on this platform
#endif
#include <Magnum/Shaders/VertexColor.h>
using namespace Magnum;
class MyApplication: public Platform::WindowlessApplication {
public:
using Platform::WindowlessApplication::WindowlessApplication;
int exec() override;
};
int MyApplication::exec() {
using namespace Math::Literals;
struct TriangleVertex {
Vector2 position;
Color3 color;
};
const TriangleVertex data[]{
{{-0.5f, -0.5f}, 0xff0000_rgbf}, /* Left vertex, red color */
{{ 0.5f, -0.5f}, 0x00ff00_rgbf}, /* Right vertex, green color */
{{ 0.0f, 0.5f}, 0x0000ff_rgbf} /* Top vertex, blue color */
};
GL::Buffer buffer;
buffer.setData(data);
GL::Mesh mesh;
mesh.setCount(3)
.addVertexBuffer(std::move(buffer), 0,
Shaders::VertexColor2D::Position{},
Shaders::VertexColor2D::Color3{});
GL::Renderbuffer renderbuffer;
renderbuffer.setStorage(GL::RenderbufferFormat::RGBA8, {640, 480});
GL::Framebuffer framebuffer{{{}, {640, 480}}};
framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, renderbuffer)
.clear(GL::FramebufferClear::Color)
.bind();
Shaders::VertexColor2D shader;
mesh.draw(shader);
if(DebugTools::screenshot(framebuffer, "triangle.png"))
return 0;
else {
Error{} << "oh noes!";
return 1;
}
}
MAGNUM_WINDOWLESSAPPLICATION_MAIN(MyApplication)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment