AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
SL.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include "AUI/Enum/ImageRendering.h"
   15#include "AUI/Image/AImage.h"
   16#include "glm/common.hpp"
   17#include "glm/ext/quaternion_common.hpp"
   18#include "glm/fwd.hpp"
   19#include <glm/glm.hpp>
   20#include <AUI/Render/ITexture.h>
   21
   22namespace aui::sl_gen {
   23    struct Texture2D {
   24    public:
   25        Texture2D(AImageView texture, ImageRendering imageRendering) noexcept: mTexture(texture), mImageRendering(imageRendering) {}
   26        Texture2D(AImageView texture) noexcept: Texture2D(texture, ImageRendering::PIXELATED) {}
   27
   28        glm::vec4 operator[](glm::vec2 uv) const {
   29            switch (mImageRendering) {
   30                case ImageRendering::PIXELATED:
   31                    return clamped(glm::ivec2(uv * glm::vec2(mTexture.size())));
   32                case ImageRendering::SMOOTH: {
   33                    const auto pos = glm::ivec2(uv * glm::vec2(mTexture.size()));
   34                    const auto texelSize = glm::vec2(1.f) / glm::vec2(mTexture.size());
   35                    const auto topLeft = clamped(pos);
   36                    const auto topRight = clamped(pos + glm::ivec2(1, 0));
   37                    const auto bottomLeft = clamped(pos + glm::ivec2(0, 1));
   38                    const auto bottomRight = clamped(pos + glm::ivec2(1, 1));
   39                    const auto texelSpaceUv = glm::mod(uv, texelSize) / texelSize;
   40
   41                    const auto top = glm::mix(topLeft, topRight, texelSpaceUv.x);
   42                    const auto bottom = glm::mix(bottomLeft, bottomRight, texelSpaceUv.x);
   43                    return glm::mix(top, bottom, texelSpaceUv.y);
   44                }
   45            }
   46            return {};
   47        }
   48
   49    private:
   50        glm::vec4 clamped(glm::ivec2 pos) const noexcept {
   51            return mTexture.get(glm::clamp(pos, glm::ivec2(0), glm::ivec2(mTexture.size()) - 1));
   52        }
   53        AImageView mTexture;
   54        ImageRendering mImageRendering;
   55    };
   56}
   57
   58    inline glm::vec2 gl_SamplePosition = {0, 0};
   59
Non-owning read-only image representation of some format.
Definition AImageView.h:58
ImageRendering
Controls the image rendering type.
Definition ImageRendering.h:25