GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
attr_traits.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_ATTR_TRAITS_HPP
13 #define GLVIS_ATTR_TRAITS_HPP
14 
15 #include "types.hpp"
16 #include "renderer_core.hpp"
17 #include <type_traits>
18 
19 namespace gl3
20 {
21 
22 struct AttrNone
23 {
24  static void setup() { }
25  static void setupLegacy(void*) { }
26  static void clear() { }
27  static void clearLegacy() { }
28  enum { exists = false };
29 };
30 
31 // Base class to generate vertex attribute setup functions.
32 template<
33  typename TV, typename TAttr, TAttr TV::*Attrib, typename TAttrInfo>
34 struct AttrBase
35 {
36  constexpr static bool NormalizeAttr =
37  std::is_integral<typename TAttr::value_type>::value;
38 
39  constexpr static TAttr* getAttrOffset()
40  {
41  return &(((TV*)0)->*Attrib);
42  }
43 
44  // Sets up vertex attributes in the currently-bound buffer object.
45  static void setup()
46  {
47  glEnableVertexAttribArray(TAttrInfo::ShaderIdx);
48  glVertexAttribPointer(TAttrInfo::ShaderIdx,
49  std::tuple_size<TAttr>::value,
50  TAttrInfo::AttrGLType,
52  sizeof(TV),
53  (void*) getAttrOffset());
54  }
55 
56  // Sets up client-side vertex pointers for the given buffer.
57  static void setupLegacy(TV* buffer)
58  {
59  glEnableClientState(TAttrInfo::FFArrayIdx);
60  TAttrInfo::FFSetupFunc(std::tuple_size<TAttr>::value,
61  TAttrInfo::AttrGLType,
62  sizeof(TV),
63  (char*) buffer + (size_t) getAttrOffset());
64  }
65 
66  // Disables the attribute array.
67  static void clear()
68  {
69  glDisableVertexAttribArray(TAttrInfo::ShaderIdx);
70  }
71 
72  // Disables the client-side vertex array.
73  static void clearLegacy()
74  {
75  glDisableClientState(TAttrInfo::FFArrayIdx);
76  }
77 
78  enum { exists = true };
79 };
80 
81 // Default attribute traits for vertex types. Provides no-op setup/clear
82 // functions if an attribute doesn't exist.
83 template<typename TV, typename = int>
84 struct AttrCoord : AttrNone { };
85 
86 template<typename TV, typename = int>
87 struct AttrNormal : AttrNone { };
88 
89 template<typename TV, typename = int>
90 struct AttrColor : AttrNone { };
91 
92 template<typename TV, typename = int>
93 struct AttrTexcoord : AttrNone { };
94 
95 // Template specializations for attribute traits. If an attribute exists in a
96 // vertex, generates setup and clear functions which setup OpenGL vertex
97 // attributes.
98 //
99 // Each attribute specialization defines static parameters which are passed to
100 // the AttrBase base class via CRTP to generate the attribute setup functions:
101 // - AttrGLType: the OpenGL type of the attribute data
102 // - ShaderIdx: the index of the generic vertex attribute
103 // - FFArrayIdx: the index of the client-side vertex attribute array
104 // - FFSetupFunc: the function to use when setting up the FF array pointer;
105 // this can either be a direct pointer to a gl*Pointer function or a custom
106 // function
107 template<typename TV>
108 struct AttrCoord<TV, decltype((void)TV::coord, 0)>
109 : AttrBase<TV, decltype(TV::coord), &TV::coord,
110 AttrCoord<TV, decltype((void)TV::coord, 0)>>
111 {
112  const static GLenum AttrGLType = GL_FLOAT;
113  const static int ShaderIdx = CoreGLDevice::ATTR_VERTEX;
114  const static GLenum FFArrayIdx = GL_VERTEX_ARRAY;
115  constexpr static auto FFSetupFunc = glVertexPointer;
116 };
117 
118 template<typename TV>
119 struct AttrNormal<TV, decltype((void)TV::norm, 0)>
120 : AttrBase<TV, decltype(TV::norm), &TV::norm,
121 AttrNormal<TV, decltype((void)TV::norm, 0)>>
122 {
123  const static GLenum AttrGLType = GL_FLOAT;
124  const static int ShaderIdx = CoreGLDevice::ATTR_NORMAL;
125  const static GLenum FFArrayIdx = GL_NORMAL_ARRAY;
126  static void FFSetupFunc(GLint /*size*/, GLenum type, GLsizei stride,
127  const GLvoid* ptr)
128  {
129  glNormalPointer(type, stride, ptr);
130  }
131 };
132 
133 template<typename TV>
134 struct AttrColor<TV, decltype((void)TV::color, 0)>
135 : AttrBase<TV, decltype(TV::color), &TV::color,
136 AttrColor<TV, decltype((void)TV::color, 0)>>
137 {
138  const static GLenum AttrGLType = GL_UNSIGNED_BYTE;
139  const static int ShaderIdx = CoreGLDevice::ATTR_COLOR;
140  const static GLenum FFArrayIdx = GL_COLOR_ARRAY;
141  constexpr static auto FFSetupFunc = glColorPointer;
142 };
143 
144 template<typename TV>
145 struct AttrTexcoord<TV, decltype((void)TV::texCoord, 0)>
146 : AttrBase<TV, decltype(TV::texCoord), &TV::texCoord,
147 AttrTexcoord<TV, decltype((void)TV::texCoord, 0)>>
148 {
149  const static GLenum AttrGLType = GL_FLOAT;
150  const static int ShaderIdx = CoreGLDevice::ATTR_TEXCOORD0;
151  const static GLenum FFArrayIdx = GL_TEXTURE_COORD_ARRAY;
152  constexpr static auto FFSetupFunc = glTexCoordPointer;
153 };
154 
155 }
156 
157 #endif
static void clearLegacy()
Definition: attr_traits.hpp:27
static void clear()
Definition: attr_traits.hpp:26
static void setupLegacy(void *)
Definition: attr_traits.hpp:25
static void clearLegacy()
Definition: attr_traits.hpp:73
static void clear()
Definition: attr_traits.hpp:67
static void setup()
Definition: attr_traits.hpp:24
static constexpr bool NormalizeAttr
Definition: attr_traits.hpp:36
static void setup()
Definition: attr_traits.hpp:45
static constexpr TAttr * getAttrOffset()
Definition: attr_traits.hpp:39
static void FFSetupFunc(GLint, GLenum type, GLsizei stride, const GLvoid *ptr)
static void setupLegacy(TV *buffer)
Definition: attr_traits.hpp:57