Each shader now has sample image, example mesh configuration and example rendering setup. Also properly documented all attribute types and made introductory chapter for whole Shaders namespace.pull/87/head
@ -0,0 +1,60 @@
|
||||
# |
||||
# This file is part of Magnum. |
||||
# |
||||
# Copyright © 2010, 2011, 2012, 2013, 2014 |
||||
# Vladimír Vondruš <mosra@centrum.cz> |
||||
# |
||||
# Permission is hereby granted, free of charge, to any person obtaining a |
||||
# copy of this software and associated documentation files (the "Software"), |
||||
# to deal in the Software without restriction, including without limitation |
||||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
# and/or sell copies of the Software, and to permit persons to whom the |
||||
# Software is furnished to do so, subject to the following conditions: |
||||
# |
||||
# The above copyright notice and this permission notice shall be included |
||||
# in all copies or substantial portions of the Software. |
||||
# |
||||
# 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 OR COPYRIGHT HOLDERS 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. |
||||
# |
||||
|
||||
cmake_minimum_required(VERSION 2.8.9) |
||||
project(MyApplication) |
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../../modules/") |
||||
|
||||
find_package(Magnum REQUIRED |
||||
MeshTools |
||||
Primitives |
||||
Shaders) |
||||
|
||||
if(CORRADE_TARGET_APPLE) |
||||
find_package(Magnum REQUIRED WindowlessCglApplication) |
||||
elseif(CORRADE_TARGET_UNIX) |
||||
find_package(Magnum REQUIRED WindowlessGlxApplication) |
||||
elseif(CORRADE_TARGET_WINDOWS) |
||||
find_package(Magnum REQUIRED WindowlessWglApplication) |
||||
else() |
||||
message(FATAL_ERROR "No windowless application available on this platform") |
||||
endif() |
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake |
||||
${CMAKE_CURRENT_BINARY_DIR}/configure.h) |
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR}) |
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CORRADE_CXX_FLAGS}") |
||||
include_directories(${MAGNUM_INCLUDE_DIRS} |
||||
${MAGNUM_WINDOWLESSAPPLICATION_INCLUDE_DIRS}) |
||||
|
||||
add_executable(shaders shaders.cpp) |
||||
target_link_libraries(shaders |
||||
${MAGNUM_LIBRARIES} |
||||
${MAGNUM_MESHTOOLS_LIBRARIES} |
||||
${MAGNUM_PRIMITIVES_LIBRARIES} |
||||
${MAGNUM_SHADERS_LIBRARIES} |
||||
${MAGNUM_WINDOWLESSAPPLICATION_LIBRARIES}) |
||||
@ -0,0 +1,32 @@
|
||||
Source files for images in Magnum documentation |
||||
----------------------------------------------- |
||||
|
||||
Compile and install Magnum with windowless application for your platform and |
||||
`magnum-distancefieldconverter` utility and any `PngImporter` and |
||||
`PngImageConverter` plugins from Magnum Plugins. |
||||
|
||||
Create build dir, point CMake to this directory and compile the executables: |
||||
|
||||
mkdir build-doc |
||||
cd build-doc |
||||
cmake ../doc/generated |
||||
cmake --build . |
||||
|
||||
### Shader images |
||||
|
||||
Generated by the `shaders` executable. Must be run in this directory, the |
||||
output is put into `doc/` directory. The executable requires two textures: |
||||
|
||||
- `vector.png`, generated as full-page PNG output at 90 DPI from `vector.svg`, |
||||
converted to pure grayscale using imagemagick: |
||||
|
||||
```bash |
||||
mogrify -flatten -background '#ffffff' -format grayscale vector.png |
||||
``` |
||||
|
||||
- `vector-distancefield.png`, generated as full-page PNG output at 360 DPI |
||||
(1024x1024) and then processed through `magnum-distancefieldconverter` |
||||
|
||||
```bash |
||||
magnum-distancefieldconverter --importer PngImporter --converter PngImageConverter --output-size "64 64" --radius 16 vector-src.png vector-distancefield.png |
||||
``` |
||||
@ -0,0 +1,32 @@
|
||||
/* |
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014 |
||||
Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
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 OR COPYRIGHT HOLDERS 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. |
||||
*/ |
||||
|
||||
#ifdef CORRADE_IS_DEBUG_BUILD |
||||
#define MAGNUM_PLUGINS_IMAGECONVERTER_DIR "${MAGNUM_PLUGINS_IMAGECONVERTER_DEBUG_DIR}" |
||||
#define MAGNUM_PLUGINS_IMPORTER_DIR "${MAGNUM_PLUGINS_IMPORTER_DEBUG_DIR}" |
||||
#else |
||||
#define MAGNUM_PLUGINS_IMAGECONVERTER_DIR "${MAGNUM_PLUGINS_IMAGECONVERTER_DIR}" |
||||
#define MAGNUM_PLUGINS_IMPORTER_DIR "${MAGNUM_PLUGINS_IMPORTER_DIR}" |
||||
#endif |
||||
@ -0,0 +1,301 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014 |
||||
Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
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 OR COPYRIGHT HOLDERS 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 <Corrade/PluginManager/Manager.h> |
||||
#include <Corrade/Utility/Directory.h> |
||||
|
||||
#ifdef CORRADE_TARGET_APPLE |
||||
#include <Magnum/Platform/WindowlessCglApplication.h> |
||||
#elif defined(CORRADE_TARGET_UNIX) |
||||
#include <Magnum/Platform/WindowlessGlxApplication.h> |
||||
#elif defined(CORRADE_TARGET_WINDOWS) |
||||
#include <Magnum/Platform/WindowlessWglApplication.h> |
||||
#else |
||||
#error No windowless application available on this platform |
||||
#endif |
||||
|
||||
#include <Magnum/Buffer.h> |
||||
#include <Magnum/Framebuffer.h> |
||||
#include <Magnum/Image.h> |
||||
#include <Magnum/Mesh.h> |
||||
#include <Magnum/Renderbuffer.h> |
||||
#include <Magnum/RenderbufferFormat.h> |
||||
#include <Magnum/MeshTools/Compile.h> |
||||
#include <Magnum/MeshTools/Interleave.h> |
||||
#include <Magnum/Primitives/Square.h> |
||||
#include <Magnum/Primitives/Circle.h> |
||||
#include <Magnum/Primitives/Icosphere.h> |
||||
#include <Magnum/Primitives/UVSphere.h> |
||||
#include <Magnum/Shaders/Flat.h> |
||||
#include <Magnum/Shaders/MeshVisualizer.h> |
||||
#include <Magnum/Shaders/Phong.h> |
||||
#include <Magnum/Shaders/VertexColor.h> |
||||
#include <Magnum/Shaders/Vector.h> |
||||
#include <Magnum/Shaders/DistanceFieldVector.h> |
||||
#include <Magnum/Trade/AbstractImageConverter.h> |
||||
#include <Magnum/Trade/ImageData.h> |
||||
#include <Magnum/Trade/MeshData2D.h> |
||||
#include <Magnum/Trade/MeshData3D.h> |
||||
#include <Magnum/Trade/AbstractImporter.h> |
||||
#include <Magnum/Renderer.h> |
||||
#include <Magnum/ColorFormat.h> |
||||
#include <Magnum/Texture.h> |
||||
#include <Magnum/TextureFormat.h> |
||||
|
||||
#include "configure.h" |
||||
|
||||
using namespace Magnum; |
||||
|
||||
struct ShaderVisualizer: Platform::WindowlessApplication { |
||||
using Platform::WindowlessApplication::WindowlessApplication; |
||||
|
||||
int exec() override; |
||||
|
||||
std::string phong(); |
||||
std::string meshVisualizer(); |
||||
std::string flat(); |
||||
std::string vertexColor(); |
||||
|
||||
std::string vector(); |
||||
std::string distanceFieldVector(); |
||||
|
||||
std::unique_ptr<Trade::AbstractImporter> _importer; |
||||
}; |
||||
|
||||
namespace { |
||||
constexpr const Vector2i ImageSize{256}; |
||||
} |
||||
|
||||
int ShaderVisualizer::exec() { |
||||
PluginManager::Manager<Trade::AbstractImageConverter> converterManager{MAGNUM_PLUGINS_IMAGECONVERTER_DIR}; |
||||
std::unique_ptr<Trade::AbstractImageConverter> converter = converterManager.loadAndInstantiate("PngImageConverter"); |
||||
if(!converter) { |
||||
Error() << "Cannot load image converter plugin"; |
||||
std::exit(1); |
||||
} |
||||
|
||||
PluginManager::Manager<Trade::AbstractImporter> importerManager{MAGNUM_PLUGINS_IMPORTER_DIR}; |
||||
_importer = importerManager.loadAndInstantiate("PngImporter"); |
||||
if(!_importer) { |
||||
Error() << "Cannot load image importer plugin"; |
||||
std::exit(1); |
||||
} |
||||
|
||||
Renderbuffer multisampleColor, multisampleDepth; |
||||
multisampleColor.setStorageMultisample(16, RenderbufferFormat::RGBA8, ImageSize); |
||||
multisampleDepth.setStorageMultisample(16, RenderbufferFormat::DepthComponent24, ImageSize); |
||||
|
||||
Framebuffer multisampleFramebuffer{{{}, ImageSize}}; |
||||
multisampleFramebuffer.attachRenderbuffer(Framebuffer::ColorAttachment{0}, multisampleColor) |
||||
.attachRenderbuffer(Framebuffer::BufferAttachment::Depth, multisampleDepth) |
||||
.bind(FramebufferTarget::Draw); |
||||
CORRADE_INTERNAL_ASSERT(multisampleFramebuffer.checkStatus(FramebufferTarget::ReadDraw) == Framebuffer::Status::Complete); |
||||
|
||||
Renderbuffer color; |
||||
color.setStorage(RenderbufferFormat::RGBA8, ImageSize); |
||||
Framebuffer framebuffer{{{}, ImageSize}}; |
||||
framebuffer.attachRenderbuffer(Framebuffer::ColorAttachment{0}, color); |
||||
|
||||
Renderer::enable(Renderer::Feature::DepthTest); |
||||
|
||||
for(auto fun: {&ShaderVisualizer::phong, |
||||
&ShaderVisualizer::meshVisualizer, |
||||
&ShaderVisualizer::flat, |
||||
&ShaderVisualizer::vertexColor, |
||||
&ShaderVisualizer::vector, |
||||
&ShaderVisualizer::distanceFieldVector}) { |
||||
multisampleFramebuffer.clear(FramebufferClear::Color|FramebufferClear::Depth); |
||||
|
||||
std::string filename = (this->*fun)(); |
||||
|
||||
AbstractFramebuffer::blit(multisampleFramebuffer, framebuffer, framebuffer.viewport(), FramebufferBlit::Color); |
||||
Image2D result = framebuffer.read(framebuffer.viewport(), {ColorFormat::RGBA, ColorType::UnsignedByte}); |
||||
converter->exportToFile(result, Utility::Directory::join("../", "shaders-" + filename)); |
||||
} |
||||
|
||||
_importer.reset(); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
namespace { |
||||
const auto Projection = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); |
||||
const auto Transformation = Matrix4::translation(Vector3::zAxis(-5.0f)); |
||||
const auto BaseColor = Color3::fromHSV(216.0_degf, 0.85f, 1.0f); |
||||
const auto OutlineColor = Color3{0.95f}; |
||||
} |
||||
|
||||
std::string ShaderVisualizer::phong() { |
||||
std::unique_ptr<Buffer> vertices, indices; |
||||
Mesh mesh; |
||||
std::tie(mesh, vertices, indices) = MeshTools::compile(Primitives::UVSphere::solid(16, 32), BufferUsage::StaticDraw); |
||||
|
||||
Shaders::Phong shader; |
||||
shader.setAmbientColor(Color3(0.025f)) |
||||
.setDiffuseColor(BaseColor) |
||||
.setShininess(200.0f) |
||||
.setLightPosition({5.0f, 5.0f, 7.0f}) |
||||
.setProjectionMatrix(Projection) |
||||
.setTransformationMatrix(Transformation) |
||||
.setNormalMatrix(Transformation.rotationScaling()); |
||||
|
||||
mesh.draw(shader); |
||||
|
||||
return "phong.png"; |
||||
} |
||||
|
||||
std::string ShaderVisualizer::meshVisualizer() { |
||||
std::unique_ptr<Buffer> vertices, indices; |
||||
Mesh mesh; |
||||
std::tie(mesh, vertices, indices) = MeshTools::compile(Primitives::Icosphere::solid(1), BufferUsage::StaticDraw); |
||||
|
||||
const Matrix4 projection = Projection*Transformation* |
||||
Matrix4::rotationZ(13.7_degf)* |
||||
Matrix4::rotationX(-12.6_degf); |
||||
|
||||
Shaders::MeshVisualizer shader{Shaders::MeshVisualizer::Flag::Wireframe}; |
||||
shader.setColor(BaseColor) |
||||
.setWireframeColor(OutlineColor) |
||||
.setViewportSize(Vector2{ImageSize}) |
||||
.setTransformationProjectionMatrix(projection); |
||||
|
||||
mesh.draw(shader); |
||||
|
||||
return "meshvisualizer.png"; |
||||
} |
||||
|
||||
std::string ShaderVisualizer::flat() { |
||||
std::unique_ptr<Buffer> vertices, indices; |
||||
Mesh mesh; |
||||
std::tie(mesh, vertices, indices) = MeshTools::compile(Primitives::UVSphere::solid(16, 32), BufferUsage::StaticDraw); |
||||
|
||||
Shaders::Flat3D shader; |
||||
shader.setColor(BaseColor) |
||||
.setTransformationProjectionMatrix(Projection*Transformation); |
||||
|
||||
mesh.draw(shader); |
||||
|
||||
return "flat.png"; |
||||
} |
||||
|
||||
std::string ShaderVisualizer::vertexColor() { |
||||
Trade::MeshData3D sphere = Primitives::UVSphere::solid(32, 64); |
||||
|
||||
/* Color vertices nearest to given position */ |
||||
auto target = Vector3{2.0f, 2.0f, 7.0f}.normalized(); |
||||
std::vector<Color3> colors; |
||||
colors.reserve(sphere.positions(0).size()); |
||||
for(Vector3 position: sphere.positions(0)) |
||||
colors.push_back(Color3::fromHSV(Math::lerp(240.0_degf, 420.0_degf, Math::max(1.0f - (position - target).length(), 0.0f)), 0.85f, 0.85f)); |
||||
|
||||
Buffer vertices, indices; |
||||
vertices.setData(MeshTools::interleave(sphere.positions(0), colors), BufferUsage::StaticDraw); |
||||
indices.setData(sphere.indices(), BufferUsage::StaticDraw); |
||||
|
||||
Mesh mesh; |
||||
mesh.setPrimitive(MeshPrimitive::Triangles) |
||||
.setCount(sphere.indices().size()) |
||||
.addVertexBuffer(vertices, 0, Shaders::VertexColor3D::Position{}, Shaders::VertexColor3D::Color{}) |
||||
.setIndexBuffer(indices, 0, Mesh::IndexType::UnsignedInt); |
||||
|
||||
Shaders::VertexColor3D shader; |
||||
shader.setTransformationProjectionMatrix(Projection*Transformation); |
||||
|
||||
mesh.draw(shader); |
||||
|
||||
return "vertexcolor.png"; |
||||
} |
||||
|
||||
std::string ShaderVisualizer::vector() { |
||||
std::optional<Trade::ImageData2D> image; |
||||
if(!_importer->openFile("vector.png") || !(image = _importer->image2D(0))) { |
||||
Error() << "Cannot open vector.png"; |
||||
return "vector.png"; |
||||
} |
||||
|
||||
Texture2D texture; |
||||
texture.setMinificationFilter(Sampler::Filter::Linear) |
||||
.setMagnificationFilter(Sampler::Filter::Linear) |
||||
.setWrapping(Sampler::Wrapping::ClampToEdge) |
||||
.setStorage(1, TextureFormat::RGBA8, image->size()) |
||||
.setSubImage(0, {}, *image); |
||||
|
||||
Mesh mesh; |
||||
std::unique_ptr<Buffer> vertices; |
||||
std::tie(mesh, vertices, std::ignore) = MeshTools::compile(Primitives::Square::solid(Primitives::Square::TextureCoords::Generate), BufferUsage::StaticDraw); |
||||
|
||||
Shaders::Vector2D shader; |
||||
shader.setColor(BaseColor) |
||||
.setVectorTexture(texture) |
||||
.setTransformationProjectionMatrix({}); |
||||
|
||||
Renderer::enable(Renderer::Feature::Blending); |
||||
Renderer::setBlendFunction(Renderer::BlendFunction::One, Renderer::BlendFunction::OneMinusSourceAlpha); |
||||
Renderer::setBlendEquation(Renderer::BlendEquation::Add, Renderer::BlendEquation::Add); |
||||
|
||||
mesh.draw(shader); |
||||
|
||||
Renderer::disable(Renderer::Feature::Blending); |
||||
|
||||
return "vector.png"; |
||||
} |
||||
|
||||
std::string ShaderVisualizer::distanceFieldVector() { |
||||
std::optional<Trade::ImageData2D> image; |
||||
if(!_importer->openFile("vector-distancefield.png") || !(image = _importer->image2D(0))) { |
||||
Error() << "Cannot open vector-distancefield.png"; |
||||
return "distancefieldvector.png"; |
||||
} |
||||
|
||||
Texture2D texture; |
||||
texture.setMinificationFilter(Sampler::Filter::Linear) |
||||
.setMagnificationFilter(Sampler::Filter::Linear) |
||||
.setWrapping(Sampler::Wrapping::ClampToEdge) |
||||
.setStorage(1, TextureFormat::RGBA8, image->size()) |
||||
.setSubImage(0, {}, *image); |
||||
|
||||
Mesh mesh; |
||||
std::unique_ptr<Buffer> vertices; |
||||
std::tie(mesh, vertices, std::ignore) = MeshTools::compile(Primitives::Square::solid(Primitives::Square::TextureCoords::Generate), BufferUsage::StaticDraw); |
||||
|
||||
Shaders::DistanceFieldVector2D shader; |
||||
shader.setColor(BaseColor) |
||||
.setOutlineColor(OutlineColor) |
||||
.setOutlineRange(0.6f, 0.4f) |
||||
.setVectorTexture(texture) |
||||
.setTransformationProjectionMatrix({}); |
||||
|
||||
Renderer::enable(Renderer::Feature::Blending); |
||||
Renderer::setBlendFunction(Renderer::BlendFunction::One, Renderer::BlendFunction::OneMinusSourceAlpha); |
||||
Renderer::setBlendEquation(Renderer::BlendEquation::Add, Renderer::BlendEquation::Add); |
||||
|
||||
mesh.draw(shader); |
||||
|
||||
Renderer::disable(Renderer::Feature::Blending); |
||||
|
||||
return "distancefieldvector.png"; |
||||
} |
||||
|
||||
MAGNUM_WINDOWLESSAPPLICATION_MAIN(ShaderVisualizer) |
||||
|
After Width: | Height: | Size: 852 B |
|
After Width: | Height: | Size: 2.4 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 4.7 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 20 KiB |
@ -0,0 +1,136 @@
|
||||
/* |
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014 |
||||
Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
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 OR COPYRIGHT HOLDERS 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. |
||||
*/ |
||||
|
||||
namespace Magnum { |
||||
/** @page shaders Builtin shaders |
||||
@brief Overview and basic usage of builtin shaders. |
||||
|
||||
- Previous page: @ref plugins |
||||
- Next page: @ref scenegraph |
||||
|
||||
@tableofcontents |
||||
|
||||
Magnum contains a set of general-purpose shaders for easy prototyping, UI |
||||
rendering and data visualization/debugging in both 2D and 3D scenes. The |
||||
following shaders are available, see documentation of each class for sample |
||||
output and example setup: |
||||
|
||||
- @ref Shaders::Flat "Shaders::Flat*D" -- flat shading using single color or |
||||
texture |
||||
- @ref Shaders::Vector "Shaders::Vector*D" -- colored vector graphics |
||||
- @ref Shaders::DistanceFieldVector "Shaders::DistanceFieldVector*D" -- |
||||
colored and outlined vector graphics |
||||
- @ref Shaders::VertexColor "Shaders::VertexColor*D" -- vertex-colored meshes |
||||
- @ref Shaders::Phong -- Phong shading using colors or textures, 3D only |
||||
- @ref Shaders::MeshVisualizer -- wireframe visualization, 3D only |
||||
|
||||
All the builtin shaders can be used on unextended OpenGL 2.1 and OpenGL ES 2.0 |
||||
/ WebGL 1.0, but they try to use the most recent technology available to have |
||||
them as efficient as possible on every configuration. |
||||
|
||||
@section shaders-usage Usage |
||||
|
||||
Shader usage is divided into two parts: configuring vertex attributes in the |
||||
mesh and configuring the shader itself. |
||||
|
||||
Each shader expects some set of vertex attributes, thus when adding vertex |
||||
buffer into the mesh, you need to specify which shader attributes are on which |
||||
position in the buffer. See @ref Mesh::addVertexBuffer() for details and usage |
||||
examples. Example mesh configuration for @ref Shaders::Phong shader: |
||||
@code |
||||
struct Vertex { |
||||
Vector3 position; |
||||
Vector3 normal; |
||||
Vector2 textureCoordinates; |
||||
}; |
||||
Vertex data[] = { ... }; |
||||
|
||||
Buffer vertices; |
||||
vertices.setData(data, BufferUsage::StaticDraw); |
||||
|
||||
Mesh mesh; |
||||
mesh.addVertexBuffer(vertices, 0, |
||||
Shaders::Phong::Position{}, |
||||
Shaders::Phong::Normal{}, |
||||
Shaders::Phong::TextureCoordinates{}); |
||||
@endcode |
||||
|
||||
Each shader then has its own set of configuration functions. Some configuration |
||||
is static, specified commonly as flags in constructor, directly affecting |
||||
compiled shader code. Other configuration is specified through uniforms and |
||||
various binding points, commonly exposed through various setters. Example |
||||
configuration and rendering using @ref Shaders::Phong "Shaders::Phong": |
||||
@code |
||||
Matrix4 transformationMatrix, projectionMatrix; |
||||
Texture2D diffuseTexture, specularTexture; |
||||
|
||||
Shaders::Phong shader{Shaders::Phong::DiffuseTexture}; |
||||
shader.setDiffuseTexture(diffuseTexture) |
||||
.setLightPosition({5.0f, 5.0f, 7.0f}) |
||||
.setTransformationMatrix(transformationMatrix) |
||||
.setNormalMatrix(transformationMatrix.rotation()) |
||||
.setProjectionMatrix(projectionMatrix); |
||||
|
||||
mesh.draw(shader); |
||||
@endcode |
||||
|
||||
@section shaders-generic Generic vertex attributes |
||||
|
||||
Many shaders share the same vertex attribute definitions, such as positions, |
||||
normals, texture coordinates etc. It's thus possible to configure the mesh |
||||
for *generic* shader and then render it with any compatible shader. Definition |
||||
of generic attributes is available in @ref Shaders::Generic class. |
||||
Configuration of the above mesh using generic attributes could then look like |
||||
this: |
||||
@code |
||||
mesh.addVertexBuffer(vertices, 0, |
||||
Shaders::Generic3D::Position{}, |
||||
Shaders::Generic3D::Normal{}, |
||||
Shaders::Generic3D::TextureCoordinates{}); |
||||
@endcode |
||||
Note that in this particular case both configurations are equivalent, because |
||||
@ref Shaders::Phong also uses generic vertex attribute definitions. |
||||
|
||||
Then you can render the mesh using @ref Shaders::Phong shader like above, or |
||||
use for example @ref Shaders::Flat3D or even @ref Shaders::MeshVisualizer with |
||||
the same mesh reconfiguration. The unused attributes will be simply ignored. |
||||
@code |
||||
Shaders::MeshVisualizer visualizerShader{Shaders::MeshVisualizer::Wireframe}; |
||||
visualizerShader.setColor(Color3::fromHSV(216.0_degf, 0.85f, 1.0f)) |
||||
.setWireframeColor(Color3{0.95f}) |
||||
.setViewportSize(defaultFramebuffer.viewport().size()) |
||||
.setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); |
||||
|
||||
mesh.draw(visualizerShader); |
||||
@endcode |
||||
|
||||
The @ref MeshTools::compile() utility configures meshes using generic vertex |
||||
attribute definitions to make them usable with any shader. |
||||
|
||||
- Previous page: @ref plugins |
||||
- Next page: @ref scenegraph |
||||
|
||||
*/ |
||||
} |
||||