Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. /*
  2. This file is part of Magnum.
  3.  
  4. Original authors — credit is appreciated but not required:
  5.  
  6. 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
  7. Vladimír Vondruš <mosra@centrum.cz>
  8.  
  9. This is free and unencumbered software released into the public domain.
  10.  
  11. Anyone is free to copy, modify, publish, use, compile, sell, or distribute
  12. this software, either in source code form or as a compiled binary, for any
  13. purpose, commercial or non-commercial, and by any means.
  14.  
  15. In jurisdictions that recognize copyright laws, the author or authors of
  16. this software dedicate any and all copyright interest in the software to
  17. the public domain. We make this dedication for the benefit of the public
  18. at large and to the detriment of our heirs and successors. We intend this
  19. dedication to be an overt act of relinquishment in perpetuity of all
  20. present and future rights to this software under copyright law.
  21.  
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  26. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29.  
  30. #include <Magnum/GL/Buffer.h>
  31. #include <Magnum/GL/DefaultFramebuffer.h>
  32. #include <Magnum/GL/Mesh.h>
  33. #include <Magnum/GL/Renderer.h>
  34. #include <Magnum/MeshTools/Interleave.h>
  35. #include <Magnum/MeshTools/CompressIndices.h>
  36. #include <Magnum/Platform/Sdl2Application.h>
  37. #include <Magnum/Primitives/Cube.h>
  38. #include <Magnum/Shaders/Phong.h>
  39. #include <Magnum/Trade/MeshData3D.h>
  40. #include <iostream>
  41.  
  42. namespace Magnum {
  43. namespace Examples {
  44.  
  45. using namespace Magnum::Math::Literals;
  46.  
  47. class PrimitivesExample : public Platform::Application {
  48. public:
  49. explicit PrimitivesExample(const Arguments &arguments);
  50.  
  51. private:
  52. void drawEvent() override;
  53.  
  54. GL::Buffer _indexBuffer, _vertexBuffer;
  55. GL::Mesh _mesh;
  56. Shaders::Phong _shader;
  57.  
  58. Matrix4 _transformation, _projection;
  59. Color3 _color;
  60. };
  61.  
  62. PrimitivesExample::PrimitivesExample(const Arguments &arguments) :
  63. Platform::Application{arguments, Configuration{}.setTitle("Magnum Primitives Example")} {
  64. GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
  65. GL::Renderer::enable(GL::Renderer::Feature::FaceCulling);
  66.  
  67. const Trade::MeshData3D cube = Primitives::cubeSolid();
  68.  
  69. _vertexBuffer.setData(MeshTools::interleave(cube.positions(0), cube.normals(0)));
  70.  
  71. Containers::Array<char> indexData;
  72. MeshIndexType indexType;
  73. UnsignedInt indexStart, indexEnd;
  74. std::tie(indexData, indexType, indexStart, indexEnd) =
  75. MeshTools::compressIndices(cube.indices());
  76. _indexBuffer.setData(indexData);
  77.  
  78. _mesh.setPrimitive(cube.primitive())
  79. .setCount(cube.indices().size())
  80. .addVertexBuffer(_vertexBuffer, 0, Shaders::Phong::Position{},
  81. Shaders::Phong::Normal{})
  82. .setIndexBuffer(_indexBuffer, 0, indexType, indexStart, indexEnd);
  83.  
  84. _projection =
  85. Matrix4::perspectiveProjection(
  86. 90.0_degf, Vector2{windowSize()}.aspectRatio(), 0.01f, 100.0f) *
  87. Matrix4::translation(Vector3::zAxis(-10.0f));
  88. _color = Color3::fromHsv({35.0_degf, 1.0f, 1.0f});
  89. }
  90.  
  91. void PrimitivesExample::drawEvent() {
  92. GL::defaultFramebuffer.clear(
  93. GL::FramebufferClear::Color | GL::FramebufferClear::Depth);
  94.  
  95. static int direction = 1;
  96. static float localSmoothX = -10;
  97.  
  98. localSmoothX += direction * 0.14f;
  99. if (localSmoothX >= 10)
  100. direction = -1;
  101. if (localSmoothX <= -10)
  102. direction = 1;
  103.  
  104. _transformation = Matrix4::translation(
  105. {localSmoothX, 0, 0});
  106.  
  107.  
  108. _shader.setLightPosition({7.0f, 5.0f, 2.5f})
  109. .setLightColor(Color3{1.0f})
  110. .setDiffuseColor(_color)
  111. .setAmbientColor(Color3::fromHsv({_color.hue(), 1.0f, 0.3f}))
  112. .setTransformationMatrix(_transformation)
  113. .setNormalMatrix(_transformation.rotationScaling())
  114. .setProjectionMatrix(_projection);
  115. _mesh.draw(_shader);
  116. swapBuffers();
  117. redraw();
  118. }
  119. }
  120. }
  121. MAGNUM_APPLICATION_MAIN(Magnum::Examples::PrimitivesExample)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement