GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
renderer.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-443271.
4 //
5 // This file is part of the GLVis visualization tool and library. For more
6 // information and source code availability see https://glvis.org.
7 //
8 // GLVis is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef GLVIS_RENDERER_HPP
13 #define GLVIS_RENDERER_HPP
14 
15 #include <memory>
16 #include <vector>
17 #include <set>
18 #include <unordered_map>
19 
20 #include "platform_gl.hpp"
21 #include "types.hpp"
22 #include "../material.hpp"
23 #include "../palettes.hpp"
24 
25 namespace gl3
26 {
27 
28 using namespace resource;
29 
30 const int LIGHTS_MAX = 3;
31 #ifdef GLVIS_MS_LINEWIDTH
32 const float LINE_WIDTH_AA = GLVIS_MS_LINEWIDTH;
33 #else
34 const float LINE_WIDTH_AA = 1.4;
35 #endif
36 
38 {
39  // Transformation matrices
42 
43  // Lighting settings
46  std::array<Light, LIGHTS_MAX> lights;
47  std::array<float, 4> light_amb_scene;
48  std::array<float, 4> static_color;
49 
50  // Clip plane parameters
52  std::array<double, 4> clip_plane_eqn;
53 
54  // If true, batch contains translucent drawables
56 };
57 
58 typedef vector<pair<RenderParams, GlDrawable*>> RenderQueue;
59 
60 struct SceneInfo
61 {
62  vector<GlDrawable*> needs_buffering;
64 };
65 
67 {
68  glm::vec3 position;
69  glm::vec4 color;
70 
71  FeedbackVertex() = default;
72  FeedbackVertex(glm::vec3 pos, glm::vec4 c)
73  : position(pos), color(c) { }
74  FeedbackVertex(glm::vec4 pos, glm::vec4 c)
75  : position(pos), color(c) { }
76 };
77 
79 {
80  glm::vec3 offset;
81  glm::vec4 color;
82  std::string text;
83 
84  FeedbackText() = default;
85  FeedbackText(glm::vec3 t_off, glm::vec4 t_color, std::string txt)
86  : offset(t_off), color(t_color), text(std::move(txt)) { }
87 };
88 
90 {
91  vector<FeedbackVertex> lines;
92  vector<FeedbackVertex> triangles;
93  vector<FeedbackText> text;
94 };
95 
96 // OpenGL device interface representing rendering capabilities
97 class GLDevice
98 {
99 protected:
100  int vp_width;
102  glm::mat4 model_view_mtx;
103  glm::mat4 proj_mtx;
104 
105  std::array<float, 4> static_color;
106 
107 protected:
109 
110 public:
111 
113  {
116  CORE_DEVICE
117  };
118 
119  virtual ~GLDevice() = default;
120  const static int SAMPLER_COLOR = 0;
121  const static int SAMPLER_ALPHA = 1;
122 
123  void detachTexture(int tex_unit)
124  {
125  glActiveTexture(GL_TEXTURE0 + tex_unit);
126  glBindTexture(GL_TEXTURE_2D, passthrough_texture);
127  }
128  void attachTexture(int tex_unit, int tex_id)
129  {
130  glActiveTexture(GL_TEXTURE0 + tex_unit);
131  glBindTexture(GL_TEXTURE_2D, tex_id);
132  };
133 
134  // If true, use unsized internal formats and GL_ALPHA for single-channel
135  // data. Otherwise, use the newer sized internal formats and GL_RED.
136  static bool useLegacyTextureFmts();
137 
138  void enableBlend() { glEnable(GL_BLEND); }
139  void disableBlend() { glDisable(GL_BLEND); }
140  void enableDepthWrite() { glDepthMask(GL_TRUE); }
141  void disableDepthWrite() { glDepthMask(GL_FALSE); }
142  void setLineWidth(float w) { glLineWidth(w); }
143 
144  virtual void init();
145  virtual DeviceType getType() = 0;
146  // Sets the window viewport.
147  void setViewport(GLsizei w, GLsizei h);
148  // Gets the current window viewport.
149  void getViewport(GLint (&vp)[4]);
150  // Set the color to use, if a color attribute is not provided.
151  void setStaticColor(const std::array<float, 4>& rgba) { static_color = rgba; }
152 
153  // === Render pipeline functions ===
154 
155  // Set the current transform matrices.
156  virtual void setTransformMatrices(glm::mat4 model_view, glm::mat4 projection);
157  // Set the number of lights to use. Setting number of lights to 0 disables
158  // lighting.
159  virtual void setNumLights(int i) = 0;
160  // Set the parameters to use for the mesh material.
161  virtual void setMaterial(Material mat) = 0;
162  // Set the array of parameters to use for each light.
163  virtual void setPointLight(int i, Light lt) = 0;
164  // Set the color value of the global ambient light.
165  virtual void setAmbientLight(const std::array<float, 4>& amb) = 0;
166  // Set whether to enable or disable the clip plane.
167  virtual void setClipPlaneUse(bool enable) = 0;
168  // Set the equation to use for the clip plane.
169  virtual void setClipPlaneEqn(const std::array<double, 4>& eqn) = 0;
170 
171  // === Buffer management functions ===
172 
173  // Load a client-side vertex buffer into a device buffer.
174  virtual void bufferToDevice(array_layout layout, IVertexBuffer& buf) = 0;
175  virtual void bufferToDevice(array_layout layout, IIndexedBuffer& buf) = 0;
176  virtual void bufferToDevice(TextBuffer& t_buf) = 0;
177  // Draw the data loaded in a device buffer.
178  virtual void drawDeviceBuffer(int hnd) = 0;
179  virtual void drawDeviceBuffer(const TextBuffer& t_buf) = 0;
180 
181  // === Transform feedback functions ===
182 
183  // Initializes state needed for transform feedback.
184  virtual void initXfbMode() {}
185  // Prepares state when exiting transform feedback.
186  virtual void exitXfbMode() {}
187  // Capture the next drawn vertex buffer to a feedback buffer instead of
188  // drawing to screen.
189  virtual void captureXfbBuffer(PaletteState& pal, CaptureBuffer& capture,
190  int hnd) = 0;
191  // Capture the next text buffer instead of drawing to screen.
192  void captureXfbBuffer(CaptureBuffer& capture, const TextBuffer& t_buf);
193 
194 };
195 
197 {
198  unique_ptr<GLDevice> device;
199  bool msaa_enable;
200  int msaa_samples;
201  GLuint color_tex, alpha_tex, font_tex;
202  float line_w, line_w_aa;
203  PaletteState* palette;
204 
205  bool feat_use_fbo_antialias;
206  void init();
207 public:
209  : msaa_enable(false)
210  , msaa_samples(0)
211  , line_w(1.f)
212  , line_w_aa(LINE_WIDTH_AA) { init(); }
213 
214  template<typename TDevice>
215  void setDevice()
216  {
217  device.reset(new TDevice());
218  device->setLineWidth(line_w);
219  device->init();
220  msaa_enable = false;
221  }
222 
223  template<typename TDevice>
224  void setDevice(TDevice&& dev)
225  {
226  device.reset(new TDevice(dev));
227  }
228  void setPalette(PaletteState* pal) { this->palette = pal; }
229 
230  // Sets the texture handle of the color palette.
231  void setColorTexture(GLuint tex_h) { color_tex = tex_h; }
232  // Sets the texture handle of the alpha texture.
233  void setAlphaTexture(GLuint tex_h) { alpha_tex = tex_h; }
234  // Sets the texture handle of the font atlas.
235  void setFontTexture(GLuint tex_h) { font_tex = tex_h; }
236 
237  void setAntialiasing(bool aa_status);
238  bool getAntialiasing() { return msaa_enable; }
239  void setSamplesMSAA(int samples)
240  {
241  if (msaa_samples < samples)
242  {
243  std::cerr << "GL_MAX_SAMPLES = " << msaa_samples
244  << " but requested " << samples << "x MSAA. ";
245  std::cerr << "Setting antialiasing mode to "
246  << msaa_samples << "x MSAA." << endl;
247  }
248  else
249  {
250  msaa_samples = samples;
251  }
252  }
253  int getSamplesMSAA() { return msaa_samples; }
254 
255  void setLineWidth(float w);
256  float getLineWidth() { return line_w; }
257  void setLineWidthMS(float w);
258  float getLineWidthMS() { return line_w_aa; }
259 
260  void setClearColor(float r, float g, float b, float a) { glClearColor(r, g, b, a); }
261  void setViewport(GLsizei w, GLsizei h) { device->setViewport(w, h); }
262 
263  void render(const RenderQueue& queued);
264  CaptureBuffer capture(const RenderQueue& queued);
265 
266  void buffer(GlDrawable* buf);
267 };
268 
269 }
270 
271 #endif // GLVIS_RENDERER_HPP
void setLineWidth(float w)
Definition: renderer.hpp:142
glm::mat4 proj_mtx
Definition: renderer.hpp:103
void setDevice(TDevice &&dev)
Definition: renderer.hpp:224
void setClearColor(float r, float g, float b, float a)
Definition: renderer.hpp:260
std::string text
Definition: renderer.hpp:82
glm::mat4 model_view_mtx
Definition: renderer.hpp:102
GlMatrix projection
Definition: renderer.hpp:41
float line_w
Definition: aux_vis.cpp:47
void disableDepthWrite()
Definition: renderer.hpp:141
void setSamplesMSAA(int samples)
Definition: renderer.hpp:239
TextureHandle passthrough_texture
Definition: renderer.hpp:108
void setViewport(GLsizei w, GLsizei h)
Definition: renderer.hpp:261
void setPalette(PaletteState *pal)
Definition: renderer.hpp:228
void setAlphaTexture(GLuint tex_h)
Definition: renderer.hpp:233
FeedbackText(glm::vec3 t_off, glm::vec4 t_color, std::string txt)
Definition: renderer.hpp:85
glm::vec3 position
Definition: renderer.hpp:68
void attachTexture(int tex_unit, int tex_id)
Definition: renderer.hpp:128
std::array< float, 4 > light_amb_scene
Definition: renderer.hpp:47
vector< FeedbackText > text
Definition: renderer.hpp:93
std::array< Light, LIGHTS_MAX > lights
Definition: renderer.hpp:46
array_layout
Definition: types.hpp:170
void disableBlend()
Definition: renderer.hpp:139
float getLineWidth()
Definition: renderer.hpp:256
virtual void exitXfbMode()
Definition: renderer.hpp:186
std::array< float, 4 > static_color
Definition: renderer.hpp:105
glm::vec4 color
Definition: renderer.hpp:81
float getLineWidthMS()
Definition: renderer.hpp:258
FeedbackVertex(glm::vec4 pos, glm::vec4 c)
Definition: renderer.hpp:74
bool getAntialiasing()
Definition: renderer.hpp:238
void setFontTexture(GLuint tex_h)
Definition: renderer.hpp:235
const int LIGHTS_MAX
Definition: renderer.hpp:30
void setStaticColor(const std::array< float, 4 > &rgba)
Definition: renderer.hpp:151
void detachTexture(int tex_unit)
Definition: renderer.hpp:123
vector< FeedbackVertex > lines
Definition: renderer.hpp:91
vector< pair< RenderParams, GlDrawable * > > RenderQueue
Definition: renderer.hpp:58
bool contains_translucent
Definition: renderer.hpp:55
void setColorTexture(GLuint tex_h)
Definition: renderer.hpp:231
vector< GlDrawable * > needs_buffering
Definition: renderer.hpp:62
vector< FeedbackVertex > triangles
Definition: renderer.hpp:92
void enableDepthWrite()
Definition: renderer.hpp:140
std::array< float, 4 > static_color
Definition: renderer.hpp:48
float line_w_aa
Definition: aux_vis.cpp:48
virtual void initXfbMode()
Definition: renderer.hpp:184
RenderQueue queue
Definition: renderer.hpp:63
const float LINE_WIDTH_AA
Definition: renderer.hpp:32
GlMatrix model_view
Definition: renderer.hpp:40
Material mesh_material
Definition: renderer.hpp:44
std::array< double, 4 > clip_plane_eqn
Definition: renderer.hpp:52
glm::vec3 offset
Definition: renderer.hpp:80
FeedbackVertex(glm::vec3 pos, glm::vec4 c)
Definition: renderer.hpp:72
void enableBlend()
Definition: renderer.hpp:138