GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
openglvis.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_OPENGLVIS_HPP
13 #define GLVIS_OPENGLVIS_HPP
14 
15 #include <cmath>
16 #include "gl/types.hpp"
17 #include "material.hpp"
18 #include "palettes.hpp"
19 #include "mfem.hpp"
20 #include "geom_utils.hpp"
21 #include "sdl.hpp"
22 #include "gltf.hpp"
23 
24 // Visualization header file
25 
26 class Camera
27 {
28 private:
29  double eye[3], dir[3], up[3];
30  double left[3];
31 
32  void MoveEye(double dist, const double dir_[])
33  { LinearCombination(1.0, eye, dist, dir_, eye); }
34 
35 public:
36  Camera() { Reset(); }
37 
38  void Reset();
39  void Set(const double cam[]);
40 
41  const double *GetEye() { return eye; }
42  const double *GetDir() { return dir; }
43  const double *GetUp() { return up; }
44  const double *GetLeft() { CrossProd(up, dir, left); return left; }
45 
46  void MoveForwardBackward(double dist) { MoveEye(dist, dir); }
47  void MoveLeftRight(double dist) { MoveEye(dist, GetLeft()); }
48  void MoveUpDown(double dist) { MoveEye(dist, up); }
49 
50  void TiltLeftRight(double angle);
51 
52  void TurnLeftRight(double angle);
53  void TurnUpDown(double angle);
54 
55  void Print();
56 
57  glm::mat4 RotMatrix();
58  glm::mat4 TransposeRotMatrix();
59  glm::mat4 TranslateMatrix();
60 };
61 
63 {
64 protected:
65  // How to scale the visualized object(s)
66  double xscale, yscale, zscale;
67 
69 
70  glm::mat4 proj_mtx;
71 
72  enum
73  {
74  BG_BLK = 0,
76  } background;
77 
78  const Material BLK_MAT =
79  {
80  {{ 0.0, 0.0, 0.0, 1.0 }},
81  {{ 0.0, 0.0, 0.0, 1.0 }},
82  {{ 0.0, 0.0, 0.0, 1.0 }},
83  0.0
84  };
85 
86  std::array<float, 4> _l0_pos;
89  bool use_light;
90 
92  glm::mat4 GetModelViewMtx();
93 
94  std::array<float, 4> GetLineColor()
95  {
96  if (background == BG_BLK)
97  {
98  return { 1.f, 1.f, 1.f, 1.f };
99  }
100  else
101  {
102  return { 0.f, 0.f, 0.f, 1.f };
103  }
104  }
105 
106  void MySetColor (gl3::GlBuilder& builder, double val, double min, double max)
107  {
108  MySetColor(builder, palette.GetColorCoord(val, min, max));
109  }
110 
111  void MySetColor (gl3::GlBuilder& builder, double val)
112  {
113  if (val < 0.0) { val = 0.0; }
114  if (val > 1.0) { val = 1.0; }
115 
116  builder.glTexCoord2f(val, 1.0);
117  }
118 
119  // We only need 3 points, but the array is 4x3
120  void DrawTriangle(gl3::GlDrawable& buff,
121  const double (&pts)[4][3], const double (&cv)[4],
122  const double minv, const double maxv);
123 
124  void DrawQuad(gl3::GlDrawable& buff,
125  const double (&pts)[4][3], const double (&cv)[4],
126  const double minv, const double maxv);
127 
131  void DrawCutTriangle(gl3::GlDrawable& buff,
132  const double (&pts)[4][3], const double (&cv)[4],
133  const double minv, const double maxv);
134 
138  void DrawCutQuad(gl3::GlDrawable& buff,
139  const double (&pts)[4][3], const double (&cv)[4],
140  const double minv, const double maxv);
141 
142  void DrawPatch(gl3::GlDrawable& buff, const mfem::DenseMatrix &pts,
143  mfem::Vector &vals, mfem::DenseMatrix &normals,
144  const int n, const mfem::Array<int> &ind, const double minv,
145  const double maxv, const int normals_opt = 0);
146 
150  glTF_Builder &bld, glTF_Builder::material_id palette_mat);
151 
153  const std::string &nodeName);
154 
155  // returns number of triangles
156  int AddTriangles(glTF_Builder &bld,
159  glTF_Builder::material_id material,
160  const gl3::GlDrawable &gl_drawable);
161 
162  // returns number of lines
163  int AddLines(glTF_Builder &bld,
166  glTF_Builder::material_id material,
167  const gl3::GlDrawable &gl_drawable);
168 
169 public:
171  virtual ~VisualizationScene();
172 
176 
179 
181  struct
182  {
183  double x[2], y[2], z[2];
184  } bb;
185 
187  double cut_lambda;
190 
191  glm::mat4 rotmat;
192  glm::mat4 translmat;
193 
194  float matAlpha = 1.0;
195  float matAlphaCenter = 0.5;
196 
197  virtual gl3::SceneInfo GetSceneObjs() = 0;
198 
199  void SetView(double theta, double phi);
200  void Zoom(double factor);
201 
202  void Rotate(double angle, double x, double y, double z);
203  void PreRotate(double angle, double x, double y, double z);
204 
205  void Rotate(double angley, double anglex);
206  void Translate(double x, double y, double z = 0.0);
207  void Scale(double s);
208  void Scale(double s1, double s2, double s3);
209 
210  void CenterObject();
211  void CenterObject2D();
212 
213  void SetProjectionMtx(glm::mat4 projection) { proj_mtx = projection; }
214  void SetLightMatIdx(unsigned i);
215  int GetLightMatIdx() { return light_mat_idx; }
216 
217  void SetLight0CustomPos(std::array<float, 4> pos);
218  void ToggleBackground();
219 
222 
224  int view;
225 };
226 
227 #endif
double cut_lambda
Amount of face cutting with keys Ctrl-F3/F4 (0: no cut, 1: cut to edges)
Definition: openglvis.hpp:187
bool cut_updated
Have the reference geometries been updated for the cut?
Definition: openglvis.hpp:189
gl3::RenderParams GetMeshDrawParams()
Definition: openglvis.cpp:988
std::array< float, 4 > _l0_pos
Definition: openglvis.hpp:86
int view
This is set by SetVisualizationScene.
Definition: openglvis.hpp:224
int AddTriangles(glTF_Builder &bld, glTF_Builder::mesh_id mesh, glTF_Builder::buffer_id buffer, glTF_Builder::material_id material, const gl3::GlDrawable &gl_drawable)
Definition: openglvis.cpp:583
struct VisualizationScene::@3 bb
Bounding box.
glm::mat4 translmat
Definition: openglvis.hpp:192
void Set(const double cam[])
Definition: openglvis.cpp:37
int AddLines(glTF_Builder &bld, glTF_Builder::mesh_id mesh, glTF_Builder::buffer_id buffer, glTF_Builder::material_id material, const gl3::GlDrawable &gl_drawable)
Definition: openglvis.cpp:805
void Translate(double x, double y, double z=0.0)
Definition: openglvis.cpp:1078
void Zoom(double factor)
Definition: openglvis.cpp:1125
void TiltLeftRight(double angle)
Definition: openglvis.cpp:46
PaletteState palette
Definition: openglvis.hpp:178
const double * GetLeft()
Definition: openglvis.hpp:44
void TurnUpDown(double angle)
Definition: openglvis.cpp:59
void DrawPatch(gl3::GlDrawable &buff, const mfem::DenseMatrix &pts, mfem::Vector &vals, mfem::DenseMatrix &normals, const int n, const mfem::Array< int > &ind, const double minv, const double maxv, const int normals_opt=0)
Definition: openglvis.cpp:309
glTF_Builder::node_id AddModelNode(glTF_Builder &bld, const std::string &nodeName)
Definition: openglvis.cpp:535
glm::mat4 TranslateMatrix()
Definition: openglvis.cpp:100
glTF_Builder::material_id AddBlackMaterial(glTF_Builder &bld)
Definition: openglvis.cpp:499
void PreRotate(double angle, double x, double y, double z)
Definition: openglvis.cpp:1060
void SetProjectionMtx(glm::mat4 projection)
Definition: openglvis.hpp:213
void MoveUpDown(double dist)
Definition: openglvis.hpp:48
const double * GetDir()
Definition: openglvis.hpp:42
double GetColorCoord(double val, double min, double max)
Definition: palettes.cpp:7763
glm::mat4 proj_mtx
Definition: openglvis.hpp:70
glTF_Builder::material_id AddPaletteLinesMaterial(glTF_Builder &bld, glTF_Builder::material_id palette_mat)
Definition: openglvis.cpp:519
void CrossProd(const double a[], const double b[], double cp[])
Definition: geom_utils.hpp:31
void MySetColor(gl3::GlBuilder &builder, double val, double min, double max)
Definition: openglvis.hpp:106
void DrawTriangle(gl3::GlDrawable &buff, const double(&pts)[4][3], const double(&cv)[4], const double minv, const double maxv)
Definition: openglvis.cpp:254
std::array< float, 4 > GetLineColor()
Definition: openglvis.hpp:94
void SetLight0CustomPos(std::array< float, 4 > pos)
Definition: openglvis.cpp:1029
virtual ~VisualizationScene()
Definition: openglvis.cpp:141
void Print()
Definition: openglvis.cpp:106
void SetView(double theta, double phi)
Definition: openglvis.cpp:1115
SdlWindow * wnd
Definition: openglvis.hpp:68
virtual gl3::SceneInfo GetSceneObjs()=0
void glTexCoord2f(float coord_u, float coord_v)
Definition: types.hpp:429
void Scale(double s)
Definition: openglvis.cpp:1087
void SetLightMatIdx(unsigned i)
Definition: openglvis.cpp:1020
Crude fixed-function OpenGL emulation helper.
Definition: types.hpp:261
glm::mat4 GetModelViewMtx()
Definition: openglvis.cpp:1138
void DrawQuad(gl3::GlDrawable &buff, const double(&pts)[4][3], const double(&cv)[4], const double minv, const double maxv)
Definition: openglvis.cpp:281
void MoveLeftRight(double dist)
Definition: openglvis.hpp:47
void MySetColor(gl3::GlBuilder &builder, double val)
Definition: openglvis.hpp:111
void GenerateAlphaTexture()
Definition: openglvis.hpp:220
glTF_Builder::material_id AddPaletteMaterial(glTF_Builder &bld)
Definition: openglvis.cpp:425
glm::mat4 TransposeRotMatrix()
Definition: openglvis.cpp:85
Camera()
Definition: openglvis.hpp:36
void DrawCutTriangle(gl3::GlDrawable &buff, const double(&pts)[4][3], const double(&cv)[4], const double minv, const double maxv)
Definition: openglvis.cpp:145
void MoveForwardBackward(double dist)
Definition: openglvis.hpp:46
const double * GetEye()
Definition: openglvis.hpp:41
void TurnLeftRight(double angle)
Definition: openglvis.cpp:52
const double * GetUp()
Definition: openglvis.hpp:43
void LinearCombination(const double a, const double x[], const double b, const double y[], double z[])
Definition: geom_utils.hpp:18
enum VisualizationScene::@2 background
void DrawCutQuad(gl3::GlDrawable &buff, const double(&pts)[4][3], const double(&cv)[4], const double minv, const double maxv)
Definition: openglvis.cpp:196
glm::mat4 RotMatrix()
Definition: openglvis.cpp:69
void GenerateAlphaTexture(float matAlpha, float matAlphaCenter)
Definition: palettes.cpp:7824
void Rotate(double angle, double x, double y, double z)
Definition: openglvis.cpp:1049
const Material BLK_MAT
Definition: openglvis.hpp:78
void Reset()
Definition: openglvis.cpp:25