GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
types.cpp
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 #include "types.hpp"
13 #include <iostream>
14 #include <cstddef>
15 
16 using namespace gl3;
17 
18 
19 void GlDrawable::addCone(float x, float y, float z,
20  float vx, float vy, float vz,
21  float cone_scale)
22 {
23  double rhos = sqrt (vx*vx+vy*vy+vz*vz);
24  float phi = acos(vz/rhos);
25  float theta = atan2 (vy, vx);
26 
27  glm::mat4 mtx(1.0);
28  mtx = glm::translate(mtx, glm::vec3(x, y, z));
29  mtx = glm::rotate(mtx, theta, glm::vec3(0.f, 0.f, 1.f));
30  mtx = glm::rotate(mtx, phi, glm::vec3(0.f, 1.f, 0.f));
31  mtx = glm::scale(mtx, glm::vec3(cone_scale/4.));
32  mtx = glm::translate(mtx, glm::vec3(0, 0, 4));
33  glm::mat3 norm(mtx);
34  norm = glm::inverseTranspose(norm);
35 
36  glm::vec3 start_vtx = glm::vec3(mtx * glm::vec4(0.f, 0.f, 0.f, 1.f));
37  glm::vec3 start_norm = glm::vec3(norm * glm::vec3(0.f, 0.f, 1.f));
38 
39  glm::vec3 base_pts[] =
40  {
41  glm::vec3(mtx * glm::vec4(1, 0, -4, 1)),
42  glm::vec3(mtx * glm::vec4(cos(2*M_PI/4), sin(2*M_PI/4), -4, 1)),
43  glm::vec3(mtx * glm::vec4(cos(4*M_PI/4), sin(4*M_PI/4), -4, 1)),
44  glm::vec3(mtx * glm::vec4(cos(6*M_PI/4), sin(6*M_PI/4), -4, 1)),
45  };
46 
47  float nz = (1.0/4.0);
48  glm::vec3 base_norms[] =
49  {
50  glm::vec3(norm * glm::vec3(1, 0, nz)),
51  glm::vec3(norm * glm::vec3(cos(2*M_PI/4), sin(2*M_PI/4), nz)),
52  glm::vec3(norm * glm::vec3(cos(4*M_PI/4), sin(4*M_PI/4), nz)),
53  glm::vec3(norm * glm::vec3(cos(6*M_PI/4), sin(6*M_PI/4), nz)),
54  };
55 
56  float* orig = glm::value_ptr(start_vtx);
57  float* orig_n = glm::value_ptr(start_norm);
58  float* base[4] =
59  {
60  glm::value_ptr(base_pts[0]),
61  glm::value_ptr(base_pts[1]),
62  glm::value_ptr(base_pts[2]),
63  glm::value_ptr(base_pts[3])
64  };
65  float* base_n[4] =
66  {
67  glm::value_ptr(base_norms[0]),
68  glm::value_ptr(base_norms[1]),
69  glm::value_ptr(base_norms[2]),
70  glm::value_ptr(base_norms[3])
71  };
72 
73  std::vector<float> cone_pts;
74  for (int i = 0; i < 4; i++)
75  {
78  {
79  {orig[0], orig[1], orig[2]},
80  {orig_n[0], orig_n[1], orig_n[2]}
81  },
83  {
84  {base[i][0], base[i][1], base[i][2]},
85  {base_n[i][0], base_n[i][1], base_n[i][2]}
86  },
88  {
89  {base[(i+1)%4][0], base[(i+1)%4][1], base[(i+1)%4][2]},
90  {base_n[(i+1)%4][0], base_n[(i+1)%4][1], base_n[(i+1)%4][2]}
91  }
92  );
93  }
94 }
95 
96 void GlBuilder::saveVertex(const GlBuilder::FFState& v)
97 {
98  GLenum dst_buf = is_line ? GL_LINES : GL_TRIANGLES;
99  if (is_line || !use_norm)
100  {
101  if (use_color)
102  {
103  parent_buf->getBuffer<VertexColor>(dst_buf)
104  ->addVertex(VertexColor{v.coords, v.color});
105  }
106  else if (use_tex)
107  {
108  parent_buf->getBuffer<VertexTex>(dst_buf)
109  ->addVertex(VertexTex{v.coords, v.texcoord});
110  }
111  else
112  {
113  parent_buf->getBuffer<Vertex>(dst_buf)
114  ->addVertex(Vertex{v.coords});
115  }
116  }
117  else
118  {
119  if (use_color)
120  {
121  parent_buf->getBuffer<VertexNormColor>(dst_buf)
122  ->addVertex(VertexNormColor{v.coords, v.norm, v.color});
123  }
124  else if (use_tex)
125  {
126  parent_buf->getBuffer<VertexNormTex>(dst_buf)
127  ->addVertex(VertexNormTex{v.coords, v.norm, v.texcoord});
128  }
129  else
130  {
131  parent_buf->getBuffer<VertexNorm>(dst_buf)
132  ->addVertex(VertexNorm{v.coords, v.norm});
133  }
134  }
135 }
std::array< uint8_t, 4 > color
Definition: types.hpp:217
void addCone(float x, float y, float z, float vx, float vy, float vz, float cone_scale=0.075)
Definition: types.cpp:19
void addTriangle(const Vert &v1, const Vert &v2, const Vert &v3)
Definition: types.hpp:685
std::array< float, 3 > norm
Definition: types.hpp:233
std::array< float, 3 > norm
Definition: types.hpp:241
std::array< float, 3 > norm
Definition: types.hpp:250