Created
November 1, 2013 10:58
-
-
Save mosra/7263870 to your computer and use it in GitHub Desktop.
Very simple 2D scenegraph example. Based on `scenegraph2D` branch of https://github.com/mosra/magnum-bootstrap and `triangle` example at https://github.com/mosra/magnum-examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find_package(Magnum REQUIRED | |
SceneGraph | |
Shaders | |
Sdl2Application) | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CORRADE_CXX_FLAGS}") | |
include_directories(${MAGNUM_INCLUDE_DIRS} ${MAGNUM_APPLICATION_INCLUDE_DIRS}) | |
add_executable(MyApplication MyApplication.cpp) | |
target_link_libraries(MyApplication | |
${MAGNUM_LIBRARIES} | |
${MAGNUM_SCENEGRAPH_LIBRARIES} | |
${MAGNUM_SHADERS_LIBRARIES} | |
${MAGNUM_APPLICATION_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Platform/Sdl2Application.h> | |
#include <DefaultFramebuffer.h> | |
#include <Mesh.h> | |
#include <Shaders/Flat.h> | |
#include <SceneGraph/Scene.h> | |
#include <SceneGraph/MatrixTransformation2D.h> | |
#include <SceneGraph/Camera2D.h> | |
#include <SceneGraph/Drawable.h> | |
using namespace Magnum; | |
typedef SceneGraph::Object<SceneGraph::MatrixTransformation2D> Object2D; | |
typedef SceneGraph::Scene<SceneGraph::MatrixTransformation2D> Scene2D; | |
class MyApplication: public Platform::Application { | |
public: | |
explicit MyApplication(const Arguments& arguments); | |
protected: | |
void viewportEvent(const Vector2i& size) override; | |
void drawEvent() override; | |
private: | |
Scene2D scene; | |
Object2D* cameraObject; | |
SceneGraph::Camera2D* camera; | |
SceneGraph::DrawableGroup2D drawables; | |
}; | |
class MyObject: public Object2D, public SceneGraph::Drawable2D { | |
public: | |
explicit MyObject(Object2D* parent, SceneGraph::DrawableGroup2D* drawables = nullptr); | |
private: | |
void draw(const Matrix3& transformationMatrix, SceneGraph::AbstractCamera2D& camera) override; | |
Buffer buffer; | |
Mesh mesh; | |
Shaders::Flat2D shader; | |
}; | |
MyApplication::MyApplication(const Arguments& arguments): Platform::Application(arguments) { | |
/* Configure camera */ | |
cameraObject = new Object2D(&scene); | |
camera = new SceneGraph::Camera2D(*cameraObject); | |
camera->setAspectRatioPolicy(SceneGraph::AspectRatioPolicy::Extend) | |
.setProjection({4.0f/3.0f, 1.0f}) | |
.setViewport(defaultFramebuffer.viewport().size()); | |
/* Add a few object instances. Note that it's done this way only for | |
brevity and is not efficient, as the data (buffer, mesh, shader) are | |
not shared among the instances. Consider using e.g. ResourceManager (see | |
Viewer example) */ | |
(new MyObject(&scene, &drawables))->scale(Vector2(0.3f)).translate(Vector2::yAxis(0.2f)); | |
(new MyObject(&scene, &drawables))->scale(Vector2(0.3f)).translate({-0.3f, -0.2f}); | |
(new MyObject(&scene, &drawables))->scale(Vector2(0.3f)).translate({+0.3f, -0.2f}); | |
} | |
void MyApplication::viewportEvent(const Vector2i& size) { | |
defaultFramebuffer.setViewport({{}, size}); | |
camera->setViewport(size); | |
} | |
void MyApplication::drawEvent() { | |
defaultFramebuffer.clear(FramebufferClear::Color); | |
camera->draw(drawables); | |
swapBuffers(); | |
} | |
MyObject::MyObject(Object2D* parent, SceneGraph::DrawableGroup2D* drawables): Object2D(parent), SceneGraph::Drawable2D(*this, drawables) { | |
constexpr static Vector2 data[] = { | |
{-0.5f, -0.5f}, /* Left vertex */ | |
{ 0.5f, -0.5f}, /* Right vertex */ | |
{ 0.0f, 0.5f} /* Top vertex */ | |
}; | |
buffer.setData(data, Buffer::Usage::StaticDraw); | |
mesh.setPrimitive(Mesh::Primitive::Triangles) | |
.setVertexCount(3) | |
.addVertexBuffer(buffer, 0, Shaders::Flat2D::Position()); | |
} | |
void MyObject::draw(const Matrix3& transformationMatrix, SceneGraph::AbstractCamera2D& camera) { | |
shader.setTransformationProjectionMatrix(camera.projectionMatrix()*transformationMatrix) | |
.setColor(Color3::fromHSV(32.0_degf, 0.9f, 1.0f)) | |
.use(); | |
mesh.draw(); | |
} | |
MAGNUM_APPLICATION_MAIN(MyApplication) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment