/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Vladimír Vondruš 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 #include "Magnum/Math/Color.h" #include "Magnum/Image.h" #include "Magnum/ImageView.h" #include "Magnum/PixelFormat.h" #ifdef MAGNUM_TARGET_GL #include "Magnum/ResourceManager.h" #include "Magnum/GL/AbstractShaderProgram.h" #include "Magnum/GL/Mesh.h" #include "Magnum/GL/PixelFormat.h" #include "Magnum/GL/Texture.h" #endif using namespace Magnum; using namespace Magnum::Math::Literals; #ifdef MAGNUM_TARGET_GL Containers::Pointer mesh; bool found = false; /* [AbstractResourceLoader-implementation] */ class MeshResourceLoader: public AbstractResourceLoader { void doLoad(ResourceKey key) override { // Load the mesh... // Not found if(!found) { setNotFound(key); return; } // Found, pass it to resource manager set(key, std::move(mesh)); } }; /* [AbstractResourceLoader-implementation] */ #endif int main() { { std::nullptr_t data{}; /* [Image-pixels] */ Image2D image{PixelFormat::RGB8Unorm, {128, 128}, data}; Containers::StridedArrayView2D pixels = image.pixels(); for(auto row: pixels.slice({48, 48}, {80, 80})) { for(Color3ub& pixel: row) pixel *= 1.1f; } /* [Image-pixels] */ } { char data[3]; /* [ImageView-usage] */ ImageView2D image{PixelFormat::RGBA8Unorm, {512, 256}, data}; /* [ImageView-usage] */ } { char evenFrameData[3], oddFrameData[3]; /* [ImageView-usage-streaming] */ ImageView2D frame{PixelFormat::RGBA8Unorm, {512, 256}}; frame.setData(evenFrameData); // Use even frame data ... frame.setData(oddFrameData); // Use odd frame data ... /* [ImageView-usage-streaming] */ } { char data[3]; /* [ImageView-usage-storage] */ ImageView2D image{ PixelStorage{} .setRowLength(75) .setAlignment(4) .setSkip({25, 25, 0}), PixelFormat::RGBA8Unorm, {25, 25}, data}; /* [ImageView-usage-storage] */ } #ifdef MAGNUM_TARGET_GL { char data[3]; /* [ImageView-usage-gl] */ ImageView2D image{GL::PixelFormat::DepthComponent, GL::PixelType::UnsignedInt, {512, 256}, data}; /* [ImageView-usage-gl] */ /* [ImageView-usage-gl-extract] */ auto format = pixelFormatUnwrap(image.format()); auto type = GLenum(image.formatExtra()); /* [ImageView-usage-gl-extract] */ static_cast(format); static_cast(type); } #endif { char data[3]; #define MTLPixelFormatRGBA8Unorm_sRGB 71 /* [ImageView-usage-metal] */ /* Default pixel storage, 8-bit sRGB + alpha, four bytes per pixel */ ImageView2D view{{}, MTLPixelFormatRGBA8Unorm_sRGB, {}, 4, {256, 256}, data}; /* [ImageView-usage-metal] */ } { char data[3]; /* [CompressedImageView-usage] */ CompressedImageView2D image{CompressedPixelFormat::Bc1RGBUnorm, {512, 256}, data}; /* [CompressedImageView-usage] */ } { char evenFrameData[3], oddFrameData[3]; /* [CompressedImageView-usage-streaming] */ CompressedImageView2D frame{CompressedPixelFormat::Bc1RGBUnorm, {512, 256}}; frame.setData(evenFrameData); // Use even frame data ... frame.setData(oddFrameData); // Use odd frame data ... /* [CompressedImageView-usage-streaming] */ } { char data[3]; /* [CompressedImageView-usage-storage] */ CompressedImageView2D image{ CompressedPixelStorage{} .setRowLength(64) .setCompressedBlockSize({4, 4, 1}) .setCompressedBlockDataSize(8) .setSkip({32, 32, 0}), CompressedPixelFormat::Bc1RGBUnorm, {32, 32}, data}; /* [CompressedImageView-usage-storage] */ } #if defined(MAGNUM_TARGET_GL) && !defined(MAGNUM_TARGET_GLES) { char data[3]; /* [CompressedImageView-usage-gl] */ CompressedImageView2D image{GL::CompressedPixelFormat::SignedRGRgtc2, {512, 256}, data}; /* [CompressedImageView-usage-gl] */ /* [CompressedImageView-usage-gl-extract] */ auto format = compressedPixelFormatUnwrap(image.format()); /* [CompressedImageView-usage-gl-extract] */ static_cast(format); } #endif { /* [Image-usage] */ Containers::Array data; Image2D image{PixelFormat::RGBA8Unorm, {512, 256}, std::move(data)}; /* [Image-usage] */ } #if defined(MAGNUM_TARGET_GL) && !defined(MAGNUM_TARGET_GLES) { /* [Image-usage-query] */ GL::Texture2D texture; Image2D image = texture.image(0, {GL::PixelFormat::DepthComponent, GL::PixelType::UnsignedInt}); /* [Image-usage-query] */ } #endif { /* [CompressedImage-usage] */ Containers::Array data; CompressedImage2D image{CompressedPixelFormat::Bc1RGBUnorm, {512, 256}, std::move(data)}; /* [CompressedImage-usage] */ } #if defined(MAGNUM_TARGET_GL) && !defined(MAGNUM_TARGET_GLES) { /* [CompressedImage-usage-query] */ GL::Texture2D texture; CompressedImage2D image = texture.compressedImage(0, {}); /* [CompressedImage-usage-query] */ } #endif #ifdef MAGNUM_TARGET_GL { /* [ResourceManager-typedef] */ typedef ResourceManager MyResourceManager; MyResourceManager manager; /* [ResourceManager-typedef] */ } { typedef ResourceManager MyResourceManager; struct MyShader: GL::AbstractShaderProgram { void bindTexture(GL::Texture2D&) {} }; /* [ResourceManager-fill] */ MyResourceManager& manager = MyResourceManager::instance(); Resource texture{manager.get("texture")}; Resource shader = manager.get("shader"); Resource cube = manager.get("cube"); // The manager doesn't have data for the cube yet, add them if(!cube) { GL::Mesh mesh; // ... manager.set(cube.key(), std::move(mesh)); } /* [ResourceManager-fill] */ /* [ResourceManager-use] */ shader->bindTexture(*texture); cube->draw(*shader); /* [ResourceManager-use] */ } { ResourceManager manager; struct MyShader: GL::AbstractShaderProgram {}; /* [ResourceManager-get-derived] */ Resource shader = manager.get("shader"); /* [ResourceManager-get-derived] */ } { typedef ResourceManager MyResourceManager; /* [AbstractResourceLoader-use] */ MyResourceManager manager; Containers::Pointer loader; manager.setLoader(std::move(loader)); // This will now automatically request the mesh from loader by calling load() Resource myMesh = manager.get("my-mesh"); /* [AbstractResourceLoader-use] */ } #endif }