Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This file is part of Magnum.
- Original authors — credit is appreciated but not required:
- 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
- Vladimír Vondruš <mosra@centrum.cz>
- This is free and unencumbered software released into the public domain.
- Anyone is free to copy, modify, publish, use, compile, sell, or distribute
- this software, either in source code form or as a compiled binary, for any
- purpose, commercial or non-commercial, and by any means.
- In jurisdictions that recognize copyright laws, the author or authors of
- this software dedicate any and all copyright interest in the software to
- the public domain. We make this dedication for the benefit of the public
- at large and to the detriment of our heirs and successors. We intend this
- dedication to be an overt act of relinquishment in perpetuity of all
- present and future rights to this software under copyright law.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- #include <Magnum/GL/Buffer.h>
- #include <Magnum/GL/DefaultFramebuffer.h>
- #include <Magnum/GL/Mesh.h>
- #include <Magnum/GL/Renderer.h>
- #include <Magnum/MeshTools/Interleave.h>
- #include <Magnum/MeshTools/CompressIndices.h>
- #include <Magnum/Platform/Sdl2Application.h>
- #include <Magnum/Primitives/Cube.h>
- #include <Magnum/Shaders/Phong.h>
- #include <Magnum/Trade/MeshData3D.h>
- #include <iostream>
- namespace Magnum {
- namespace Examples {
- using namespace Magnum::Math::Literals;
- class PrimitivesExample : public Platform::Application {
- public:
- explicit PrimitivesExample(const Arguments &arguments);
- private:
- void drawEvent() override;
- GL::Buffer _indexBuffer, _vertexBuffer;
- GL::Mesh _mesh;
- Shaders::Phong _shader;
- Matrix4 _transformation, _projection;
- Color3 _color;
- };
- PrimitivesExample::PrimitivesExample(const Arguments &arguments) :
- Platform::Application{arguments, Configuration{}.setTitle("Magnum Primitives Example")} {
- GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
- GL::Renderer::enable(GL::Renderer::Feature::FaceCulling);
- const Trade::MeshData3D cube = Primitives::cubeSolid();
- _vertexBuffer.setData(MeshTools::interleave(cube.positions(0), cube.normals(0)));
- Containers::Array<char> indexData;
- MeshIndexType indexType;
- UnsignedInt indexStart, indexEnd;
- std::tie(indexData, indexType, indexStart, indexEnd) =
- MeshTools::compressIndices(cube.indices());
- _indexBuffer.setData(indexData);
- _mesh.setPrimitive(cube.primitive())
- .setCount(cube.indices().size())
- .addVertexBuffer(_vertexBuffer, 0, Shaders::Phong::Position{},
- Shaders::Phong::Normal{})
- .setIndexBuffer(_indexBuffer, 0, indexType, indexStart, indexEnd);
- _projection =
- Matrix4::perspectiveProjection(
- 90.0_degf, Vector2{windowSize()}.aspectRatio(), 0.01f, 100.0f) *
- Matrix4::translation(Vector3::zAxis(-10.0f));
- _color = Color3::fromHsv({35.0_degf, 1.0f, 1.0f});
- }
- void PrimitivesExample::drawEvent() {
- GL::defaultFramebuffer.clear(
- GL::FramebufferClear::Color | GL::FramebufferClear::Depth);
- static int direction = 1;
- static float localSmoothX = -10;
- localSmoothX += direction * 0.14f;
- if (localSmoothX >= 10)
- direction = -1;
- if (localSmoothX <= -10)
- direction = 1;
- _transformation = Matrix4::translation(
- {localSmoothX, 0, 0});
- _shader.setLightPosition({7.0f, 5.0f, 2.5f})
- .setLightColor(Color3{1.0f})
- .setDiffuseColor(_color)
- .setAmbientColor(Color3::fromHsv({_color.hue(), 1.0f, 0.3f}))
- .setTransformationMatrix(_transformation)
- .setNormalMatrix(_transformation.rotationScaling())
- .setProjectionMatrix(_projection);
- _mesh.draw(_shader);
- swapBuffers();
- redraw();
- }
- }
- }
- MAGNUM_APPLICATION_MAIN(Magnum::Examples::PrimitivesExample)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement