Skip to content

Commit

Permalink
primitives: cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Aug 2, 2014
1 parent 0746996 commit 4ba9644
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
10 changes: 5 additions & 5 deletions doc/primitives.dox
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ cube rendered in proper Z-order. Enabling face culling is not needed for proper
rendering, but it will speed things up as only front-facing faces will be
rendered.
@skip PrimitivesExample::PrimitivesExample
@until Renderer::setFeature(Renderer::Feature::FaceCulling
@until Renderer::enable(Renderer::Feature::FaceCulling

We now use pre-made @ref Primitives::Cube "cube primitive" and create mesh from
it. The mesh is indexed and contains position and normal data. As said earlier,
interleaving the data gives us best memory access performance. We can do it by
hand as in previous example, but using @ref MeshTools::interleave() is much
more convenient. We upload the interleaved data directly to vertex buffer.
@skip Trade::MeshData3D
@until vertexBuffer.setData(
@until _vertexBuffer.setData(

Why do we need indexed mesh and what it actually is? In most meshes the
same vertex data are shared among more than one vertex, even a simple square
Expand All @@ -81,20 +81,20 @@ again does all the boring work for us -- it checks index range and creates
array consisting of @ref UnsignedByte, @ref UnsignedShort or @ref UnsignedInt
based on that.
@skip Containers::Array<char>
@until std::tie
@until _indexBuffer.

Everything is now ready for configuring the mesh. We set primitive type, index
count, add our vertex buffer and specify index buffer. The `indexStart` and
`indexEnd` parameters are purely optional, but they might improve memory access
performance on desktop GL as the GPU will know what subset of vertex data are
used.
@skip mesh.setPrimitive
@skip _mesh.setPrimitive
@until .setIndexBuffer

We now specify initial transformation, color and projection. See
@ref matrix-vector and @ref transformations for more thorough introduction to
transformations.
@skip transformation =
@skip _transformation =
@until }

@section examples-primitives-rendering Rendering
Expand Down
31 changes: 17 additions & 14 deletions src/primitives/PrimitivesExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class PrimitivesExample: public Platform::Application {
Color3 _color;
};

PrimitivesExample::PrimitivesExample(const Arguments& arguments): Platform::Application(arguments, Configuration().setTitle("Magnum Primitives Example")) {
PrimitivesExample::PrimitivesExample(const Arguments& arguments): Platform::Application{arguments, Configuration{}.setTitle("Magnum Primitives Example")} {
Renderer::enable(Renderer::Feature::DepthTest);
Renderer::enable(Renderer::Feature::FaceCulling);

Trade::MeshData3D cube = Primitives::Cube::solid();
const Trade::MeshData3D cube = Primitives::Cube::solid();

_vertexBuffer.setData(MeshTools::interleave(cube.positions(0), cube.normals(0)), BufferUsage::StaticDraw);

Expand All @@ -73,23 +73,23 @@ PrimitivesExample::PrimitivesExample(const Arguments& arguments): Platform::Appl
.addVertexBuffer(_vertexBuffer, 0, Shaders::Phong::Position{}, Shaders::Phong::Normal{})
.setIndexBuffer(_indexBuffer, 0, indexType, indexStart, indexEnd);

_transformation = Matrix4::rotationX(Deg(30.0f))*
Matrix4::rotationY(Deg(40.0f));
_color = Color3::fromHSV(Deg(35.0f), 1.0f, 1.0f);
_transformation = Matrix4::rotationX(30.0_degf)*
Matrix4::rotationY(40.0_degf);
_color = Color3::fromHSV(35.0_degf, 1.0f, 1.0f);

_projection = Matrix4::perspectiveProjection(Deg(35.0f), Vector2(defaultFramebuffer.viewport().size()).aspectRatio(), 0.01f, 100.0f)*
Matrix4::translation(Vector3::zAxis(-10.0f));
_projection = Matrix4::perspectiveProjection(35.0_degf, Vector2{defaultFramebuffer.viewport().size()}.aspectRatio(), 0.01f, 100.0f)*
Matrix4::translation(Vector3::zAxis(-10.0f));
}

void PrimitivesExample::drawEvent() {
defaultFramebuffer.clear(FramebufferClear::Color|FramebufferClear::Depth);

_shader.setLightPosition({7.0f, 5.0f, 2.5f})
.setLightColor(Color3(1.0f))
.setLightColor(Color3{1.0f})
.setDiffuseColor(_color)
.setAmbientColor(Color3::fromHSV(_color.hue(), 1.0f, 0.3f))
.setTransformationMatrix(_transformation)
.setNormalMatrix(_transformation.rotationScaling()) /** @todo better solution? */
.setNormalMatrix(_transformation.rotationScaling())
.setProjectionMatrix(_projection);
_mesh.draw(_shader);

Expand All @@ -104,7 +104,7 @@ void PrimitivesExample::mousePressEvent(MouseEvent& event) {
}

void PrimitivesExample::mouseReleaseEvent(MouseEvent& event) {
_color = Color3::fromHSV(_color.hue() + Deg(50.0), 1.0f, 1.0f);
_color = Color3::fromHSV(_color.hue() + 50.0_degf, 1.0f, 1.0f);

event.setAccepted();
redraw();
Expand All @@ -113,11 +113,14 @@ void PrimitivesExample::mouseReleaseEvent(MouseEvent& event) {
void PrimitivesExample::mouseMoveEvent(MouseMoveEvent& event) {
if(!(event.buttons() & MouseMoveEvent::Button::Left)) return;

Vector2 delta = 3.0f*Vector2(event.position() - _previousMousePosition)/Vector2(defaultFramebuffer.viewport().size());
const Vector2 delta = 3.0f*
Vector2{event.position() - _previousMousePosition}/
Vector2{defaultFramebuffer.viewport().size()};

_transformation =
Matrix4::rotationX(Rad(delta.y()))*
_transformation *
Matrix4::rotationY(Rad(delta.x()));
Matrix4::rotationX(Rad{delta.y()})*
_transformation*
Matrix4::rotationY(Rad{delta.x()});

_previousMousePosition = event.position();
event.setAccepted();
Expand Down

0 comments on commit 4ba9644

Please sign in to comment.