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
|
// 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 <array>
#include <vector>
#include <iostream>
#include "renderer_ff.hpp"
#include "attr_traits.hpp"
#include "../aux_vis.hpp"
namespace gl3
{
template<typename TVtx>
void setupFFVertexArray(TVtx* buffer)
{
static_assert(AttrCoord<TVtx>::exists,
"Invalid vertex type, requires at least TVtx::coord to be present.");
AttrCoord<TVtx>::setupLegacy(buffer);
AttrNormal<TVtx>::setupLegacy(buffer);
AttrColor<TVtx>::setupLegacy(buffer);
glClientActiveTexture(GL_TEXTURE0);
AttrTexcoord<TVtx>::setupLegacy(buffer);
glClientActiveTexture(GL_TEXTURE1);
AttrTexcoord<TVtx>::setupLegacy(buffer);
}
template<typename TVtx>
void clearFFVertexArray()
{
AttrCoord<TVtx>::clearLegacy();
AttrNormal<TVtx>::clearLegacy();
AttrColor<TVtx>::clearLegacy();
glClientActiveTexture(GL_TEXTURE0);
AttrTexcoord<TVtx>::clearLegacy();
glClientActiveTexture(GL_TEXTURE1);
AttrTexcoord<TVtx>::clearLegacy();
}
template<typename TVtx>
void FFGLDevice::bufferFFDeviceImpl(const VertexBuffer<TVtx>& buf)
{
glNewList(disp_lists[buf.getHandle()].list, GL_COMPILE);
setupFFVertexArray((TVtx*)buf.getData());
glDrawArrays(buf.getShape(), 0, buf.count());
glEndList();
clearFFVertexArray<TVtx>();
}
template<typename TVtx>
void FFGLDevice::bufferFFDeviceImpl(const IndexedVertexBuffer<TVtx>& buf)
{
glNewList(disp_lists[buf.getHandle()].list, GL_COMPILE);
setupFFVertexArray((TVtx*)buf.getData());
glDrawElements(buf.getShape(), buf.getIndices().size(), GL_UNSIGNED_INT,
buf.getIndices().data());
glEndList();
clearFFVertexArray<TVtx>();
}
void FFGLDevice::init()
{
GLDevice::init();
// Fixed-function pipeline parameters
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
// Texturing is set up such that the output color is computed as follows:
// - color_out.rgb = color_in.rgb * tex0.rgb
// - color_out.a = tex1.a
// Texture unit 0 should contain the color palette, while texture unit 1
// contains either the transparency alpha channel, or the font texture
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
// With a GL_ALPHA format texture loaded, GL_MODULATE will pass through the
// fragment rgb value.
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
void FFGLDevice::setTransformMatrices(glm::mat4 model_view,
glm::mat4 projection)
{
GLDevice::setTransformMatrices(model_view, projection);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(model_view));
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(projection));
}
void FFGLDevice::setNumLights(int i)
{
if (i == 0)
{
glDisable(GL_LIGHTING);
return;
}
glEnable(GL_LIGHTING);
for (int light_id = 0; light_id < i; light_id++)
{
glEnable(GL_LIGHT0 + light_id);
}
for (int light_id = i; light_id < LIGHTS_MAX; light_id++)
{
glDisable(GL_LIGHT0 + light_id);
}
}
void FFGLDevice::setMaterial(Material mat)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat.diffuse.data());
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat.ambient.data());
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat.specular.data());
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat.shininess);
GLfloat memis[] = { 0.0, 0.0, 0.0, 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, memis);
}
void FFGLDevice::setPointLight(int i, Light lt)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glLightfv(GL_LIGHT0 + i, GL_POSITION, lt.position.data());
GLfloat lambi[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0 + i, GL_AMBIENT, lambi);
glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lt.diffuse.data());
glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lt.specular.data());
glPopMatrix();
}
void FFGLDevice::setAmbientLight(const std::array<float, 4>& amb)
{
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &amb[0]);
}
void FFGLDevice::setClipPlaneUse(bool enable)
{
if (enable) { glEnable(GL_CLIP_PLANE0); }
else { glDisable(GL_CLIP_PLANE0); }
}
void FFGLDevice::setClipPlaneEqn(const std::array<double, 4>& eqn)
{
glClipPlane(GL_CLIP_PLANE0, eqn.data());
}
void FFGLDevice::bufferToDevice(array_layout layout, IVertexBuffer& buf)
{
if (buf.getHandle() == 0)
{
if (buf.count() == 0) { return; }
GLuint new_hnd = glGenLists(1);
buf.setHandle(disp_lists.size());
disp_lists.emplace_back(DispListData_{new_hnd, buf.getShape(), buf.count(), layout});
}
else
{
disp_lists[buf.getHandle()].count = buf.count();
}
switch (layout)
{
case Vertex::layout:
bufferFFDeviceImpl(static_cast<const VertexBuffer<Vertex>&>(buf));
break;
case VertexColor::layout:
bufferFFDeviceImpl(static_cast<const VertexBuffer<VertexColor>&>(buf));
break;
case VertexTex::layout:
bufferFFDeviceImpl(static_cast<const VertexBuffer<VertexTex>&>(buf));
break;
case VertexNorm::layout:
bufferFFDeviceImpl(static_cast<const VertexBuffer<VertexNorm>&>(buf));
break;
case VertexNormColor::layout:
bufferFFDeviceImpl(static_cast<const VertexBuffer<VertexNormColor>&>(buf));
break;
case VertexNormTex::layout:
bufferFFDeviceImpl(static_cast<const VertexBuffer<VertexNormTex>&>(buf));
break;
default:
std::cerr << "WARNING: Unhandled vertex layout " << layout << std::endl;
}
}
void FFGLDevice::bufferToDevice(array_layout layout, IIndexedBuffer& buf)
{
if (buf.getHandle() == 0)
{
if (buf.count() == 0) { return; }
GLuint new_hnd = glGenLists(1);
buf.setHandle(disp_lists.size());
disp_lists.emplace_back(DispListData_{new_hnd, buf.getShape(), buf.getIndices().size(), layout});
}
else
{
disp_lists[buf.getHandle()].count = buf.getIndices().size();
}
switch (layout)
{
case Vertex::layout:
bufferFFDeviceImpl(static_cast<const IndexedVertexBuffer<Vertex>&>(buf));
break;
case VertexColor::layout:
bufferFFDeviceImpl(static_cast<const IndexedVertexBuffer<VertexColor>&>(buf));
break;
case VertexTex::layout:
bufferFFDeviceImpl(static_cast<const IndexedVertexBuffer<VertexTex>&>(buf));
break;
case VertexNorm::layout:
bufferFFDeviceImpl(static_cast<const IndexedVertexBuffer<VertexNorm>&>(buf));
break;
case VertexNormColor::layout:
bufferFFDeviceImpl(static_cast<const IndexedVertexBuffer<VertexNormColor>&>
(buf));
break;
case VertexNormTex::layout:
bufferFFDeviceImpl(static_cast<const IndexedVertexBuffer<VertexNormTex>&>(buf));
break;
default:
std::cerr << "WARNING: Unhandled vertex layout " << layout << std::endl;
}
}
void FFGLDevice::bufferToDevice(TextBuffer&)
{
// we can't really do anything here can only compute offset matrix at draw
}
void FFGLDevice::drawDeviceBuffer(int hnd)
{
if (hnd == 0) { return; }
if (disp_lists[hnd].count == 0) { return; }
if (disp_lists[hnd].layout == Vertex::layout
|| disp_lists[hnd].layout == VertexNorm::layout)
{
glColor4fv(static_color.data());
}
else
{
glColor4f(1.f, 1.f, 1.f, 1.f);
}
if (!( disp_lists[hnd].layout == VertexNorm::layout
|| disp_lists[hnd].layout == VertexNormColor::layout
|| disp_lists[hnd].layout == VertexNormTex::layout))
{
glNormal3f(0.f, 0.f, 1.f);
}
glCallList(disp_lists[hnd].list);
// reset texturing parameters
// glMultiTexCoord2f(GL_TEXTURE0, 0.f, 0.f);
// glMultiTexCoord2f(GL_TEXTURE1, 0.f, 0.f);
}
void FFGLDevice::drawDeviceBuffer(const TextBuffer& buf)
{
glColor4fv(static_color.data());
glNormal3f(0.f, 0.f, 1.f);
glMultiTexCoord2f(GL_TEXTURE0, 0.f, 0.f);
float tex_w = GetFont()->getAtlasWidth();
float tex_h = GetFont()->getAtlasHeight();
// Model-view transform:
// - scale bounding boxes to relative clip-space/NDC coords
// - add z-offset of -0.005 to reduce text hiding by mesh
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glScalef(2.f / vp_width, 2.f / vp_height, 0.f);
glTranslatef(0.f, 0.f, -0.005);
glMatrixMode(GL_PROJECTION);
for (const TextBuffer::Entry& e : buf)
{
glm::vec4 pos(e.rx, e.ry, e.rz, 1.0);
// transform text starting position into NDC
pos = model_view_mtx * pos;
pos = proj_mtx * pos;
pos = pos / pos.w;
// Projection transform:
// - add starting offset in NDC
glPushMatrix();
glLoadIdentity();
glTranslatef(pos.x, pos.y, pos.z);
float pen_x = e.ox, pen_y = e.oy;
char prev_c = '\0';
for (char c : e.text)
{
const GlVisFont::glyph &g = GetFont()->GetTexChar(c);
pen_x += GetFont()->GetKerning(prev_c, c);
// note: subtract 1 to account for the padding in the texture glyphs
float cur_x = pen_x + g.bear_x - 1;
float cur_y = -pen_y - g.bear_y - 1;
pen_x += g.adv_x;
pen_y += g.adv_y;
if (!g.w || !g.h)
{
continue;
}
glBegin(GL_TRIANGLE_STRIP);
glMultiTexCoord2f(GL_TEXTURE1, g.tex_x, 0);
glVertex2f(cur_x, -cur_y);
glMultiTexCoord2f(GL_TEXTURE1, g.tex_x + g.w / tex_w, 0);
glVertex2f(cur_x + g.w, -cur_y);
glMultiTexCoord2f(GL_TEXTURE1, g.tex_x, g.h / tex_h);
glVertex2f(cur_x, -cur_y - g.h);
glMultiTexCoord2f(GL_TEXTURE1, g.tex_x + g.w / tex_w, g.h / tex_h);
glVertex2f(cur_x + g.w, -cur_y - g.h);
glEnd();
}
glPopMatrix();
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
void FFGLDevice::captureXfbBuffer(PaletteState& pal, CaptureBuffer& cbuf,
int hnd)
{
if (hnd == 0) { return; }
if (disp_lists[hnd].count == 0) { return; }
GLenum fbType;
int fbStride;
if (disp_lists[hnd].layout == VertexTex::layout
|| disp_lists[hnd].layout == VertexNormTex::layout)
{
// capture texture values too
// [ X Y Z ] [ R G B A ] [ U V - - ]
fbType = GL_3D_COLOR_TEXTURE;
fbStride = 11;
}
else
{
// only capture pos and color
// [ X Y Z ] [ R G B A ]
fbType = GL_3D_COLOR;
fbStride = 7;
}
// compute feedback buffer size
int sizebuf = 0;
if (disp_lists[hnd].shape == GL_LINES)
{
// for each line: LINE_TOKEN [Vtx] [Vtx]
sizebuf = (disp_lists[hnd].count / 2) + disp_lists[hnd].count * fbStride;
}
else if (disp_lists[hnd].shape == GL_TRIANGLES)
{
// for each tri: POLY_TOKEN 3 [Vtx] [Vtx] [Vtx]
// NOTE: when clip plane is enabled, we might get two triangles
// or a quad for an input triangle. However, the other clipped
// triangles get discarded, so this *should* be enough space.
sizebuf = (disp_lists[hnd].count / 3) * (2 + fbStride * 4);
}
else
{
std::cerr << "Warning: unhandled primitive type in FFPrinter::preDraw()" <<
std::endl;
return;
}
// allocate feedback buffer
std::vector<float> xfb_buf;
xfb_buf.resize(sizebuf);
glFeedbackBuffer(sizebuf, fbType, xfb_buf.data());
// draw with feedback capture
glRenderMode(GL_FEEDBACK);
drawDeviceBuffer(hnd);
#ifndef GLVIS_DEBUG
glRenderMode(GL_RENDER);
#else
if (glRenderMode(GL_RENDER) < 0)
{
std::cerr << "Warning: feedback data exceeded available buffer size" <<
std::endl;
}
#endif
size_t tok_idx = 0;
// process feedback buffer
while (tok_idx < xfb_buf.size())
{
switch ((GLuint)xfb_buf[tok_idx])
{
case GL_LINE_TOKEN:
case GL_LINE_RESET_TOKEN:
{
tok_idx++;
glm::vec3 coord0 = glm::make_vec3(&xfb_buf[tok_idx]),
coord1 = glm::make_vec3(&xfb_buf[tok_idx + fbStride]);
glm::vec4 color0 = glm::make_vec4(&xfb_buf[tok_idx + 3]),
color1 = glm::make_vec4(&xfb_buf[tok_idx + 3 + fbStride]);
if (fbStride == 11)
{
// get texture
pal.GetColorFromVal(xfb_buf[tok_idx + 7], glm::value_ptr(color0));
pal.GetColorFromVal(xfb_buf[tok_idx + 7 + fbStride], glm::value_ptr(color1));
}
cbuf.lines.emplace_back(coord0, color0);
cbuf.lines.emplace_back(coord1, color1);
tok_idx += fbStride * 2;
}
break;
case GL_POLYGON_TOKEN:
{
int n = xfb_buf[tok_idx + 1];
tok_idx += 2;
// get vertex 0, 1
glm::vec3 coord0 = glm::make_vec3(&xfb_buf[tok_idx]),
coord1 = glm::make_vec3(&xfb_buf[tok_idx + fbStride]);
glm::vec4 color0 = glm::make_vec4(&xfb_buf[tok_idx + 3]),
color1 = glm::make_vec4(&xfb_buf[tok_idx + 3 + fbStride]);
if (fbStride == 11)
{
// get texture
pal.GetColorFromVal(xfb_buf[tok_idx + 7], glm::value_ptr(color0));
pal.GetColorFromVal(xfb_buf[tok_idx + 7 + fbStride], glm::value_ptr(color1));
}
// decompose polygon into n-2 triangles [0 1 2] [0 2 3] ...
for (int i = 0; i < n-2; i++)
{
// get last vertex of current triangle
int vtxStart = fbStride * (2 + 3*i);
glm::vec3 coord2 = glm::make_vec3(&xfb_buf[tok_idx + vtxStart]);
glm::vec4 color2 = glm::make_vec4(&xfb_buf[tok_idx + 3 + vtxStart]);
if (fbStride == 11)
{
pal.GetColorFromVal(xfb_buf[tok_idx + 7 + vtxStart], glm::value_ptr(color2));
}
cbuf.triangles.emplace_back(coord0, color0);
cbuf.triangles.emplace_back(coord1, color1);
cbuf.triangles.emplace_back(coord2, color2);
// last vertex becomes second vertex of next triangle
coord1 = coord2;
color1 = color2;
}
tok_idx += n * fbStride;
}
break;
case GL_POINT_TOKEN:
case GL_BITMAP_TOKEN:
case GL_DRAW_PIXEL_TOKEN:
case GL_COPY_PIXEL_TOKEN:
default:
// commands containing the token + a single vertex ignore for now
tok_idx += 1 + fbStride;
break;
}
}
}
}
|