File indexing completed on 2024-11-10 04:56:54
0001 /* 0002 KWin - the KDE window manager 0003 This file is part of the KDE project. 0004 0005 SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@gmail.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 #include "gllut3D.h" 0010 0011 #include <vector> 0012 0013 namespace KWin 0014 { 0015 0016 GlLookUpTable3D::GlLookUpTable3D(GLuint handle, size_t xSize, size_t ySize, size_t zSize) 0017 : m_handle(handle) 0018 , m_xSize(xSize) 0019 , m_ySize(ySize) 0020 , m_zSize(zSize) 0021 { 0022 } 0023 0024 GlLookUpTable3D::~GlLookUpTable3D() 0025 { 0026 glDeleteTextures(1, &m_handle); 0027 } 0028 0029 GLuint GlLookUpTable3D::handle() const 0030 { 0031 return m_handle; 0032 } 0033 0034 size_t GlLookUpTable3D::xSize() const 0035 { 0036 return m_xSize; 0037 } 0038 0039 size_t GlLookUpTable3D::ySize() const 0040 { 0041 return m_ySize; 0042 } 0043 0044 size_t GlLookUpTable3D::zSize() const 0045 { 0046 return m_zSize; 0047 } 0048 0049 void GlLookUpTable3D::bind() 0050 { 0051 glBindTexture(GL_TEXTURE_3D, m_handle); 0052 } 0053 0054 std::unique_ptr<GlLookUpTable3D> GlLookUpTable3D::create(const std::function<QVector3D(size_t x, size_t y, size_t z)> &mapping, size_t xSize, size_t ySize, size_t zSize) 0055 { 0056 GLuint handle = 0; 0057 glGenTextures(1, &handle); 0058 if (!handle) { 0059 return nullptr; 0060 } 0061 glBindTexture(GL_TEXTURE_3D, handle); 0062 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0); 0063 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_LOD, 0); 0064 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LOD, 0); 0065 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 0066 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 0067 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 0068 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 0069 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); 0070 QVector<float> data; 0071 data.reserve(4 * xSize * ySize * zSize); 0072 for (size_t z = 0; z < zSize; z++) { 0073 for (size_t y = 0; y < ySize; y++) { 0074 for (size_t x = 0; x < xSize; x++) { 0075 const auto color = mapping(x, y, z); 0076 data.push_back(color.x()); 0077 data.push_back(color.y()); 0078 data.push_back(color.z()); 0079 data.push_back(1); 0080 } 0081 } 0082 } 0083 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F, xSize, ySize, zSize, 0, GL_RGBA, GL_FLOAT, data.data()); 0084 glBindTexture(GL_TEXTURE_3D, 0); 0085 return std::make_unique<GlLookUpTable3D>(handle, xSize, ySize, zSize); 0086 } 0087 0088 }