1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
// LICENSE and NOTICE for details. LLNL-CODE-443271.
//
// This file is part of the GLVis visualization tool and library. For more
// information and source code availability see https://glvis.org.
//
// GLVis is free software; you can redistribute it and/or modify it under the
// terms of the BSD-3 license. We welcome feedback and contributions, see file
// CONTRIBUTING.md for details.
#include "palettes_base.hpp"
#include "gl/renderer.hpp"
#include <iomanip>
#include <fstream>
using namespace std;
void RGBAf::Print(ostream& os) const
{
os << std::fixed << std::setprecision(6)
<< std::setw(10) << r << " "
<< std::setw(10) << g << " "
<< std::setw(10) << b << " "
<< std::setw(10) << a;
}
template <size_t N>
Palette::Palette(const std::string& name,
const array<array<float,3>,N>& arr) : name(name)
{
colors.resize(N);
for (size_t i = 0; i < N; ++i)
{
colors[i] = RGBAf(arr[i][0], arr[i][1], arr[i][2]);
}
}
template <size_t N>
Palette::Palette(const std::string& name,
const array<array<float,4>,N>& arr) : name(name)
{
colors.resize(N);
for (size_t i = 0; i < N; ++i)
{
colors[i] = RGBAf(arr[i][0], arr[i][1], arr[i][2], arr[i][3]);
}
}
void Palette::AddColor(float r, float g, float b, float a)
{
colors.push_back(RGBAf(r, g, b, a));
}
void Palette::Print(ostream& os) const
{
os << "palette " << name << " RGBf" << endl;
for (const auto& color : colors)
{
color.Print(os);
os << endl;
}
os << endl;
}
RGBAf Palette::Color(int i, bool reversed) const
{
int j = reversed ? Size() - 1 - i : i;
return colors[j];
}
vector<array<float,4>> Palette::GetData(bool reversed) const
{
vector<array<float,4>> rgba_data(Size());
for (int i = 0; i < Size(); ++i)
{
rgba_data[i] = Color(i, reversed).AsArray();
}
return rgba_data;
}
bool Palette::IsTranslucent() const
{
for (const auto& color : colors)
{
if (color.a != 1.0) { return true; }
}
return false;
}
// Initialize GL parameters
int Texture::max_texture_size = -1;
// WebGL 2 requires sized internal format for float texture
GLenum Texture::alpha_internal = GL_R32F;
GLenum Texture::alpha_channel = GL_RED;
GLenum Texture::rgba_internal = GL_RGBA32F;
GLenum Texture::rgba_channel = GL_RGBA;
Texture::Texture(const Palette* palette,
TextureType textype,
int cycles, int colors)
: palette(palette), textype(textype)
{
// Initialize static GL parameters
InitStaticGL();
// Input sanitization/init
UpdateParameters(cycles, colors);
// Generate the texture id
GLuint texid;
glGenTextures(1, &texid);
texture = texid;
}
Texture::Texture(float matAlpha, float matAlphaCenter)
: palette(nullptr), textype(TextureType::ALPHAMAP)
{
// Initialize static GL parameters
InitStaticGL();
// Input sanitization/init
UpdateAlphaParameters(matAlpha, matAlphaCenter);
// Generate the texture id
GLuint texid;
glGenTextures(1, &texid);
texture = texid;
// set alpha texture to 1.0
std::vector<float> texture_data(Texture::max_texture_size);
std::fill(texture_data.begin(), texture_data.end(), 1.0f);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, Texture::alpha_internal,
Texture::max_texture_size, 1, 0,
Texture::alpha_channel, GL_FLOAT, texture_data.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glActiveTexture(GL_TEXTURE0);
}
void Texture::InitStaticGL()
{
if (Texture::max_texture_size < 0)
{
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &Texture::max_texture_size);
if (gl3::GLDevice::useLegacyTextureFmts())
{
Texture::alpha_internal = GL_ALPHA;
Texture::alpha_channel = GL_ALPHA;
Texture::rgba_internal = GL_RGBA;
}
}
}
vector<float> Texture::GenerateAlphaTextureData()
{
vector<float> texture_data(Texture::max_texture_size);
if (alpha >= 1.0)
{
// transparency off
std::fill(texture_data.begin(), texture_data.end(), 1.0f);
}
else
{
for (int i = 0; i < Texture::max_texture_size; i++)
{
float val = float(2*i + 1)/(2*Texture::max_texture_size); // midpoint of texel
texture_data[i] = alpha * std::exp(-std::abs(val - alpha_center));
}
}
return texture_data;
}
vector<array<float,4>> Texture::GenerateTextureData()
{
// Original palette size
int plt_size = palette->Size();
// Initialize the texture data
vector<array<float,4>> texture_data(tsize);
// Discrete texture
if ( textype == TextureType::DISCRETE )
{
// Generate the texture data
for (int rpt = 0; rpt < nrepeat; rpt++)
{
bool reverse = (reversed + rpt) % 2 != 0;
for (int i = 0; i < ncolors; i++)
{
int j = std::min(i * plt_size / (ncolors - 1), plt_size - 1);
texture_data[rpt*ncolors + i] = palette->Color(j, reverse).AsArray();
}
}
}
// Smooth texture (interpolates colors)
else if ( textype == TextureType::SMOOTH )
{
// Generate the texture data
for (int rpt = 0; rpt < nrepeat; rpt++)
{
bool reverse = (reversed + rpt) % 2 != 0;
for (int i = 0; i < ncolors; i++)
{
float t = i * (plt_size - 1) / (ncolors - 1);
int j = std::min((int)t, plt_size - 2);
t -= j;
array<float,4> col1 = palette->Color(j, reverse).AsArray();
array<float,4> col2 = palette->Color(j+1, reverse).AsArray();
texture_data[rpt*ncolors + i] =
{
(1-t) * col1[0] + t * col2[0],
(1-t) * col1[1] + t * col2[1],
(1-t) * col1[2] + t * col2[2],
(1-t) * col1[3] + t * col2[3]
};
}
}
}
return texture_data;
}
void Texture::SetCycles(int cycles)
{
if (cycles == 0)
{
cycles = 1;
}
reversed = cycles < 0;
nrepeat = abs(cycles);
}
void Texture::SetColors(int colors)
{
ncolors = colors <= 0 ? palette->Size() : colors;
}
void Texture::UpdateTextureSize()
{
tsize = nrepeat * ncolors;
if (tsize > Texture::max_texture_size)
{
cerr << "Warning: Texture size "
<< "(" << tsize << ")" << " exceeds maximum "
<< "(" << Texture::max_texture_size << ")" << endl;
if (ncolors >= Texture::max_texture_size)
{
ncolors = Texture::max_texture_size;
nrepeat = 1;
}
else
{
nrepeat = Texture::max_texture_size / ncolors;
}
tsize = nrepeat * ncolors;
}
}
void Texture::UpdateAlphaParameters(float matAlpha, float matAlphaCenter)
{
alpha = std::max(0.0f, std::min(1.0f, matAlpha));
alpha_center = std::max(0.0f, std::min(1.0f, matAlphaCenter));
tsize = Texture::max_texture_size;
}
void Texture::UpdateParameters(int cycles, int colors)
{
SetCycles(cycles);
SetColors(colors);
UpdateTextureSize();
}
void Texture::Generate()
{
// Get texture data and formats (different for alpha textures)
// Define the texture image
if ( textype == TextureType::ALPHAMAP )
{
vector<float> texture_data = GenerateAlphaTextureData();
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
tsize, 1,
Texture::alpha_channel, GL_FLOAT, texture_data.data());
glActiveTexture(GL_TEXTURE0);
}
else
{
vector<array<float,4>> texture_data = GenerateTextureData();
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, Texture::rgba_internal,
tsize, 1, 0,
Texture::rgba_channel, GL_FLOAT, texture_data.data());
}
// Discrete
if ( textype == TextureType::DISCRETE)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
// Smooth
else if ( textype == TextureType::SMOOTH )
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
}
int PaletteRegistry::GetIndexByName(const std::string& name) const
{
for (int i = 0; i < NumPalettes(); i++)
{
if (Get(i)->name == name) { return i; }
}
return -1;
}
PaletteRegistry::PaletteRegistry(const vector<Palette>& paletteRefs)
{
for (const Palette& palette : paletteRefs)
{
if (IsNameUnique(palette.name))
{
palettes.push_back(as_unique<Palette>(palette));
}
}
}
void PaletteRegistry::AddPalette(Palette& palette)
{
if (IsNameUnique(palette.name))
{
palettes.push_back(as_unique<Palette>(palette));
}
}
void PaletteRegistry::AddPalette(const std::string& name)
{
if (IsNameUnique(name))
{
palettes.push_back(as_unique<Palette>(name));
}
}
bool PaletteRegistry::IsNameUnique(const std::string& name) const
{
// palette name is unique || container is empty
if (GetIndexByName(name) == -1 || palettes.empty())
{
return true;
}
else
{
cout << "Palette with name: '" << name << "' already exists in registry.";
return false;
}
}
Palette* PaletteRegistry::Get(int index) const
{
if (0 <= index && index <= NumPalettes()-1)
{
return palettes[index].get();
}
cout << "Palette (index = " << index+1 << ") out of range. Available palettes:"
<< endl;
this->PrintSummary();
return palettes.back().get();
}
Palette* PaletteRegistry::Get(const std::string& name) const
{
int idx = GetIndexByName(name);
if (idx != -1)
{
return palettes[idx].get();
}
cout << "Palette (name = " << name << ") not found. Available palettes:" <<
endl;
this->PrintSummary();
return palettes.back().get();
}
void PaletteRegistry::SetDefault(const std::string& name)
{
const int idx = GetIndexByName(name);
if (idx < 0)
{
cout << "Palette (name = " << name << ") not found. Available palettes:"
<< endl;
PrintSummary();
}
else
{
default_palette = idx;
cout << "Default palette set to: " << default_palette << ") "
<< Get(default_palette)->name << endl;
}
}
void PaletteRegistry::PrintSummary(ostream& os) const
{
for (int i = 0; i < NumPalettes(); i++)
{
os << setw(3) << i+1 << ") "
<< left << setw(12) << Get(i)->name << right;
if ((i+1)%5 == 0)
{
os << endl;
}
}
os << endl;
}
void PaletteRegistry::PrintAll(ostream& os) const
{
for (int i = 0; i < NumPalettes(); i++)
{
Get(i)->Print(os);
}
}
void PaletteRegistry::Load(const std::string& palette_filename)
{
ifstream pfile(palette_filename);
if (!pfile)
{
cout << "Could not open palette file: " << palette_filename << endl;
return;
}
std::string word, palname, channeltype;
int idx = -1;
// read initializing commands
while (1)
{
pfile >> ws;
if (!pfile.good())
{
break;
}
if (pfile.peek() == '#')
{
getline(pfile, word);
continue;
}
pfile >> word;
if (word == "palette")
{
pfile >> palname >> channeltype;
idx = GetIndexByName(palname);
if (idx != -1)
{
cout << "Warning: palette name <" << palname
<< "> already exists. Overriding..." << endl;
palettes.erase(palettes.begin() + idx);
}
// Add new palette
AddPalette(palname);
idx = GetIndexByName(palname);
cout << "Reading palette: (" << idx+1 << ") " << palname << endl;
}
else if (channeltype == "RGBf" && idx != -1)
{
float r, g, b;
r = stof(word);
pfile >> g >> b;
Get(idx)->AddColor(r,g,b);
}
else if (channeltype == "RGBAf" && idx != -1)
{
float r, g, b, a;
r = stof(word);
pfile >> g >> b >> a;
Get(idx)->AddColor(r,g,b,a);
}
else
{
cout << "Error reading palette file: " << palette_filename << endl;
break;
}
}
cout << "Finished loading palettes from file: " << palette_filename << endl;
}
|