Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vertex_decl.cpp « graphics - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aa0ef3a0a9e0b1c9d2772022ff3cd360d5bc4ba5 (plain)
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
#include "graphics/vertex_decl.hpp"
#include "graphics/defines.hpp"
#include "base/assert.hpp"
#include "base/logging.hpp"

namespace graphics
{
  VertexAttrib::VertexAttrib(ESemantic semantic,
                             size_t offset,
                             EDataType elemType,
                             size_t elemCount,
                             size_t stride)
    : m_semantic(semantic),
      m_offset(offset),
      m_elemType(elemType),
      m_elemCount(elemCount),
      m_stride(stride)
  {}

  template <typename T>
  struct FillPosition
  {
    static void fill(VertexAttrib const * va, PosField<T> * p, unsigned char * v, unsigned stride)
    {
      if (va->m_elemCount > 3)
      {
        p->m_w = reinterpret_cast<T*>(v + sizeof(T) * 3);
        p->m_wStride = stride;
      }
      if (va->m_elemCount > 2)
      {
        p->m_z = reinterpret_cast<T*>(v + sizeof(T) * 2);
        p->m_zStride = stride;
      }
      if (va->m_elemCount > 1)
      {
        p->m_y = reinterpret_cast<T*>(v + sizeof(T) * 1);
        p->m_yStride = stride;
      }
      if (va->m_elemCount > 0)
      {
        p->m_x = reinterpret_cast<T*>(v);
        p->m_xStride = stride;
      }
    }
  };

  template <ESemantic sem>
  struct FillSemantic
  {
    static void fill(VertexAttrib const * va, VertexStream * vs, unsigned char * v);
  };

  template <>
  struct FillSemantic<ESemPosition>
  {
    static void fill(VertexAttrib const * va, VertexStream * vs, unsigned char * v, unsigned stride)
    {
      switch (va->m_elemType)
      {
      case EFloat:
        FillPosition<float>::fill(va, &vs->m_fPos, v, stride);
        break;
      default:
        break;
      }
    }
  };

  template <>
  struct FillSemantic<ESemNormal>
  {
    static void fill(VertexAttrib const * va, VertexStream * vs, unsigned char * v, unsigned stride)
    {
      switch (va->m_elemType)
      {
      case EFloat:
        FillPosition<float>::fill(va, &vs->m_fNormal, v, stride);
        break;
      default:
        ASSERT(false, ("Not supported"));
      }
    }
  };

  template <typename T>
  struct FillTexCoord
  {
    static void fill(VertexAttrib const * va, TexField<T> * p, unsigned char * v, unsigned stride)
    {
      if (va->m_elemCount > 1)
      {
        p->m_v = reinterpret_cast<T*>(v + sizeof(T));
        p->m_vStride = stride;
      }
      if (va->m_elemCount > 0)
      {
        p->m_u = reinterpret_cast<T*>(v);
        p->m_uStride = stride;
      }
    }
  };

  template <>
  struct FillSemantic<ESemTexCoord0>
  {
    static void fill(VertexAttrib const * va, VertexStream * vs, unsigned char * v, unsigned stride)
    {
      switch (va->m_elemType)
      {
      case EFloat:
        FillTexCoord<float>::fill(va, &vs->m_fTex, v, stride);
        break;
      default:
        ASSERT(false, ("Not supported"));
      };
    }
  };

  void VertexAttrib::initStream(VertexStream * vs, unsigned char * v, unsigned stride) const
  {
    switch (m_semantic)
    {
    case ESemPosition:
      FillSemantic<ESemPosition>::fill(this, vs, v, stride);
      break;
    case ESemNormal:
      FillSemantic<ESemNormal>::fill(this, vs, v, stride);
      break;
    case ESemTexCoord0:
      FillSemantic<ESemTexCoord0>::fill(this, vs, v, stride);
      break;
    default:
      LOG(LERROR, ("Unknown semantic specified"));
      break;
    };
  }


  VertexDecl::VertexDecl(VertexAttrib const * attrs, size_t cnt)
  {
    copy(attrs, attrs + cnt, back_inserter(m_attrs));

    m_elemSize = 0;

    for (unsigned i = 0; i < m_attrs.size(); ++i)
    {
      VertexAttrib & va = m_attrs[i];
      m_elemSize += graphics::elemSize(va.m_elemType) * va.m_elemCount;
    }
  }

  VertexAttrib const * VertexDecl::getAttr(size_t i) const
  {
    return &m_attrs[i];
  }

  size_t VertexDecl::attrCount() const
  {
    return m_attrs.size();
  }

  size_t VertexDecl::elemSize() const
  {
    return m_elemSize;
  }

  void VertexDecl::initStream(VertexStream *vs, unsigned char * v) const
  {
    *vs = VertexStream();
    unsigned char * fieldOffs = v;
    for (size_t i = 0; i < m_attrs.size(); ++i)
    {
      VertexAttrib const * va = &m_attrs[i];
      va->initStream(vs, fieldOffs + va->m_offset, m_elemSize);
    }
  }
}