CVertexArray.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Francois Conti
40  \version 3.2.0 $Rev: 2161 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CVertexArrayH
46 #define CVertexArrayH
47 //------------------------------------------------------------------------------
48 #include "math/CVector3d.h"
49 #include "math/CMatrix3d.h"
50 #include "graphics/CColor.h"
51 #include "shaders/CShader.h"
52 //------------------------------------------------------------------------------
53 #include <vector>
54 #include <list>
55 //------------------------------------------------------------------------------
56 namespace chai3d {
57 //------------------------------------------------------------------------------
58 
59 //==============================================================================
66 //==============================================================================
67 
68 
69 //==============================================================================
77 //==============================================================================
79 {
80 
81 public:
82 
83  cVertexArrayOptions(const bool a_useNormalData,
84  const bool a_useTexCoordData,
85  const bool a_useColorData,
86  const bool a_useTangentData,
87  const bool a_useBitangentData,
88  const bool a_useUserData)
89  {
90  m_useNormalData = a_useNormalData;
91  m_useTexCoordData = a_useTexCoordData;
92  m_useColorData = a_useColorData;
93  m_useTangentData = a_useTangentData;
94  m_useBitangentData = a_useBitangentData;
95  m_useUserData = a_useUserData;
96  }
97 
104 };
105 
106 //------------------------------------------------------------------------------
108 typedef std::shared_ptr<cVertexArray> cVertexArrayPtr;
109 //------------------------------------------------------------------------------
110 
111 //==============================================================================
132 //==============================================================================
134 {
135  //--------------------------------------------------------------------------
136  // CONSTRUCTOR & DESTRUCTOR:
137  //--------------------------------------------------------------------------
138 
139 public:
140 
141  //--------------------------------------------------------------------------
149  //--------------------------------------------------------------------------
151  {
152  clear();
153  m_useNormalData = a_options.m_useNormalData;
155  m_useColorData = a_options.m_useColorData;
158  m_useUserData = a_options.m_useUserData;
159  m_flagPositionData = false;
160  m_flagNormalData = false;
161  m_flagTexCoordData = false;
162  m_flagColorData = false;
163  m_flagTangentData = false;
164  m_flagBitangentData = false;
165  m_flagUserData = false;
166  m_flagBufferResize = true;
167  m_positionBuffer = (GLuint)(-1);
168  m_normalBuffer = (GLuint)(-1);
169  m_texCoordBuffer = (GLuint)(-1);
170  m_colorBuffer = (GLuint)(-1);
171  m_tangentBuffer = (GLuint)(-1);
172  m_bitangentBuffer = (GLuint)(-1);
173  }
174 
175 
176  //--------------------------------------------------------------------------
180  //--------------------------------------------------------------------------
182 
183 
184  //--------------------------------------------------------------------------
197  //--------------------------------------------------------------------------
198  static cVertexArrayPtr create(const bool a_useNormalData,
199  const bool a_useTexCoordData,
200  const bool a_useColorData,
201  const bool a_useTangentData,
202  const bool a_useBitangentData,
203  const bool a_useUserData)
204  {
205  const cVertexArrayOptions options(a_useNormalData,
206  a_useTexCoordData,
207  a_useColorData,
208  a_useTangentData,
209  a_useBitangentData,
210  a_useUserData);
211 
212  return (std::make_shared<cVertexArray>(options));
213  }
214 
215 
216  //--------------------------------------------------------------------------
217  // PUBLIC METHODS:
218  //--------------------------------------------------------------------------
219 
220  //--------------------------------------------------------------------------
224  //--------------------------------------------------------------------------
225  inline void clear()
226  {
227  m_localPos.clear();
228  m_globalPos.clear();
229  m_normal.clear();
230  m_texCoord.clear();
231  m_color.clear();
232  m_tangent.clear();
233  m_bitangent.clear();
234  m_userData.clear();
235  m_numVertices = 0;
236  m_flagBufferResize = true;
237  }
238 
239 
240  //--------------------------------------------------------------------------
244  //--------------------------------------------------------------------------
245  cVertexArrayPtr copy()
246  {
247  cVertexArrayPtr vertexArray = cVertexArray::create(m_useNormalData,
252  m_useUserData);
253 
254  // copy arrays
255  vertexArray->m_localPos = m_localPos;
256  vertexArray->m_globalPos = m_globalPos;
257  vertexArray->m_normal = m_normal;
258  vertexArray->m_texCoord = m_texCoord;
259  vertexArray->m_color = m_color;
260  vertexArray->m_tangent = m_tangent;
261  vertexArray->m_bitangent = m_bitangent;
262  vertexArray->m_userData = m_userData;
263 
264  // copy data
265  vertexArray->m_useNormalData = m_useNormalData;
266  vertexArray->m_useTexCoordData = m_useTexCoordData;
267  vertexArray->m_useColorData = m_useColorData;
268  vertexArray->m_useTangentData = m_useTangentData;
269  vertexArray->m_useBitangentData = m_useBitangentData;
270  vertexArray->m_useUserData = m_useUserData;
271  vertexArray->m_numVertices = m_numVertices;
272 
273  // return new vertex array
274  return (vertexArray);
275  }
276 
277 
278  //--------------------------------------------------------------------------
284  //--------------------------------------------------------------------------
285  inline int newVertex()
286  {
287  return (newVertices(1));
288  }
289 
290 
291  //--------------------------------------------------------------------------
299  //--------------------------------------------------------------------------
300  inline int newVertices(unsigned int a_numberOfVertices)
301  {
302  // sanity check
303  if (a_numberOfVertices == 0) { return (-1); }
304 
305  // store current index
306  int index = m_numVertices;
307 
308  // allocate new vertices
310 
311  // return index of first allocated vertex
312  return (index);
313  }
314 
315 
316  //--------------------------------------------------------------------------
325  //--------------------------------------------------------------------------
326  inline void setLocalPos(const unsigned int a_vertexIndex,
327  const double& a_x,
328  const double& a_y,
329  const double& a_z)
330  {
331  m_localPos[a_vertexIndex].set(a_x, a_y, a_z);
332  m_flagPositionData = true;
333  }
334 
335 
336  //--------------------------------------------------------------------------
343  //--------------------------------------------------------------------------
344  inline void setLocalPos(const unsigned int a_vertexIndex,
345  const cVector3d& a_pos)
346  {
347  m_localPos[a_vertexIndex] = a_pos;
348  m_flagPositionData = true;
349  }
350 
351 
352  //--------------------------------------------------------------------------
360  //--------------------------------------------------------------------------
361  inline void translate(const unsigned int a_vertexIndex,
362  const cVector3d& a_translation)
363  {
364  m_localPos[a_vertexIndex].add(a_translation);
365  m_flagPositionData = true;
366  }
367 
368 
369  //--------------------------------------------------------------------------
376  //--------------------------------------------------------------------------
377  inline cVector3d getLocalPos(const unsigned int a_vertexIndex) const
378  {
379  return (m_localPos[a_vertexIndex]);
380  }
381 
382 
383  //--------------------------------------------------------------------------
392  //--------------------------------------------------------------------------
393  inline cVector3d getGlobalPos(const unsigned int a_vertexIndex) const
394  {
395  return (m_globalPos[a_vertexIndex]);
396  }
397 
398 
399  //--------------------------------------------------------------------------
406  //--------------------------------------------------------------------------
407  inline void setNormal(const unsigned int a_vertexIndex,
408  const cVector3d& a_normal)
409  {
410  if (m_useNormalData)
411  {
412  m_normal[a_vertexIndex] = a_normal;
413  m_flagNormalData = true;
414  }
415  }
416 
417 
418  //--------------------------------------------------------------------------
427  //--------------------------------------------------------------------------
428  inline void setNormal(const unsigned int a_vertexIndex,
429  const double& a_x,
430  const double& a_y,
431  const double& a_z)
432  {
433  if (m_useNormalData)
434  {
435  m_normal[a_vertexIndex].set(a_x, a_y, a_z);
436  m_flagNormalData = true;
437  }
438  }
439 
440 
441  //--------------------------------------------------------------------------
448  //--------------------------------------------------------------------------
449  inline cVector3d getNormal(const unsigned int a_vertexIndex) const
450  {
451  return (m_normal[a_vertexIndex]);
452  }
453 
454 
455  //--------------------------------------------------------------------------
462  //--------------------------------------------------------------------------
463  inline void setTexCoord(const unsigned int a_vertexIndex,
464  const cVector3d& a_texCoord)
465  {
466  if (m_useTexCoordData)
467  {
468  m_texCoord[a_vertexIndex] = a_texCoord;
469  m_flagTexCoordData = true;
470  }
471  }
472 
473 
474  //--------------------------------------------------------------------------
484  //--------------------------------------------------------------------------
485  inline void setTexCoord(const unsigned int a_vertexIndex,
486  const double& a_tx,
487  const double& a_ty = 0.0,
488  const double& a_tz = 0.0)
489  {
490  if (m_useTexCoordData)
491  {
492  m_texCoord[a_vertexIndex].set(a_tx, a_ty,a_tz);
493  m_flagTexCoordData = true;
494  }
495  }
496 
497 
498  //--------------------------------------------------------------------------
505  //--------------------------------------------------------------------------
506  inline cVector3d getTexCoord(const unsigned int a_vertexIndex) const
507  {
508  return (m_texCoord[a_vertexIndex]);
509  }
510 
511 
512  //--------------------------------------------------------------------------
519  //--------------------------------------------------------------------------
520  inline void setColor(const unsigned int a_vertexIndex,
521  const cColorf& a_color)
522  {
523  if (m_useColorData)
524  {
525  m_color[a_vertexIndex] = a_color;
526  m_flagColorData = true;
527  }
528  }
529 
530 
531  //--------------------------------------------------------------------------
542  //--------------------------------------------------------------------------
543  inline void setColor(const unsigned int a_vertexIndex,
544  const float& a_red,
545  const float& a_green,
546  const float& a_blue,
547  const float& a_alpha = 1.0 )
548  {
549  if (m_useColorData)
550  {
551  m_color[a_vertexIndex].set(a_red, a_green, a_blue, a_alpha);
552  m_flagColorData = true;
553  }
554  }
555 
556 
557  //--------------------------------------------------------------------------
564  //--------------------------------------------------------------------------
565  inline void setColor(const unsigned int a_vertexIndex,
566  const cColorb& a_color)
567  {
568  if (m_useColorData)
569  {
570  m_color[a_vertexIndex] = a_color.getColorf();
571  m_flagColorData = true;
572  }
573  }
574 
575 
576  //--------------------------------------------------------------------------
583  //--------------------------------------------------------------------------
584  inline cColorf getColor(const unsigned int a_vertexIndex) const
585  {
586  return (m_color[a_vertexIndex]);
587  }
588 
589 
590  //--------------------------------------------------------------------------
597  //--------------------------------------------------------------------------
598  inline void setTangent(const unsigned int a_vertexIndex,
599  const cVector3d& a_tangent)
600  {
601  if (m_useTangentData)
602  {
603  m_tangent[a_vertexIndex] = a_tangent;
604  m_flagTangentData = true;
605  }
606  }
607 
608 
609  //--------------------------------------------------------------------------
618  //--------------------------------------------------------------------------
619  inline void setTangent(const unsigned int a_vertexIndex,
620  const double& a_x,
621  const double& a_y,
622  const double& a_z)
623  {
624  if (m_useTangentData)
625  {
626  m_tangent[a_vertexIndex].set(a_x, a_y, a_z);
627  m_flagTangentData = true;
628  }
629  }
630 
631 
632  //--------------------------------------------------------------------------
639  //--------------------------------------------------------------------------
640  inline cVector3d getTangent(const unsigned int a_vertexIndex) const
641  {
642  return (m_tangent[a_vertexIndex]);
643  }
644 
645 
646  //--------------------------------------------------------------------------
653  //--------------------------------------------------------------------------
654  inline void setBitangent(const unsigned int a_vertexIndex,
655  const cVector3d& a_bitangent)
656  {
657  if (m_useBitangentData)
658  {
659  m_bitangent[a_vertexIndex] = a_bitangent;
660  m_flagBitangentData = true;
661  }
662  }
663 
664 
665  //--------------------------------------------------------------------------
674  //--------------------------------------------------------------------------
675  inline void setBitangent(const unsigned int a_vertexIndex,
676  const double& a_x,
677  const double& a_y,
678  const double& a_z)
679  {
680  if (m_useBitangentData)
681  {
682  m_bitangent[a_vertexIndex].set(a_x, a_y, a_z);
683  m_flagBitangentData = true;
684  }
685  }
686 
687 
688  //--------------------------------------------------------------------------
695  //--------------------------------------------------------------------------
696  inline cVector3d getBitangent(const unsigned int a_vertexIndex) const
697  {
698  return (m_bitangent[a_vertexIndex]);
699  }
700 
701 
702  //--------------------------------------------------------------------------
709  //--------------------------------------------------------------------------
710  inline void setUserData(const unsigned int a_vertexIndex,
711  const int a_userData)
712  {
713  if (m_useUserData)
714  {
715  m_userData[a_vertexIndex] = a_userData;
716  m_flagUserData = true;
717  }
718  }
719 
720 
721  //--------------------------------------------------------------------------
728  //--------------------------------------------------------------------------
729  inline int getUserData(const unsigned int a_vertexIndex) const
730  {
731  return (m_userData[a_vertexIndex]);
732  }
733 
734 
735  //--------------------------------------------------------------------------
744  //--------------------------------------------------------------------------
745  inline void computeGlobalPosition(const unsigned int a_vertexIndex,
746  const cVector3d& a_globalPos,
747  const cMatrix3d& a_globalRot)
748  {
749  a_globalRot.mulr(m_localPos[a_vertexIndex], m_globalPos[a_vertexIndex]);
750  m_globalPos[a_vertexIndex].add(a_globalPos);
751  }
752 
753 
754  //--------------------------------------------------------------------------
760  //--------------------------------------------------------------------------
761  inline unsigned int getNumElements() const
762  {
763  return (m_numVertices);
764  }
765 
766 
767  //--------------------------------------------------------------------------
774  //--------------------------------------------------------------------------
775  inline bool getUseNormalData() const
776  {
777  return (m_useNormalData);
778  }
779 
780 
781  //--------------------------------------------------------------------------
788  //--------------------------------------------------------------------------
789  inline bool getUseTexCoordData() const
790  {
791  return (m_useTexCoordData);
792  }
793 
794 
795  //--------------------------------------------------------------------------
802  //--------------------------------------------------------------------------
803  inline bool getUseColorData() const
804  {
805  return (m_useColorData);
806  }
807 
808 
809  //--------------------------------------------------------------------------
816  //--------------------------------------------------------------------------
817  inline bool getUseTangentData() const
818  {
819  return (m_useTangentData);
820  }
821 
822 
823  //--------------------------------------------------------------------------
830  //--------------------------------------------------------------------------
831  inline bool getUseBitangentData() const
832  {
833  return (m_useBitangentData);
834  }
835 
836 
837  //--------------------------------------------------------------------------
844  //--------------------------------------------------------------------------
845  inline bool getUseUserData() const
846  {
847  return (m_useUserData);
848  }
849 
850 
851  //--------------------------------------------------------------------------
855  //--------------------------------------------------------------------------
856  inline void renderInitialize()
857  {
858 #ifdef C_USE_OPENGL
859  // create buffers first time
860  if (m_positionBuffer == (GLuint)(-1))
861  {
862  glGenBuffers(1, &m_positionBuffer);
863  }
864 
865  if ((m_normalBuffer == (GLuint)(-1)) && (m_useNormalData))
866  {
867  glGenBuffers(1, &m_normalBuffer);
868  }
869 
870  if ((m_texCoordBuffer == (GLuint)(-1)) && (m_useTexCoordData))
871  {
872  glGenBuffers(1, &m_texCoordBuffer);
873  }
874 
875  if ((m_colorBuffer == (GLuint)(-1)) && (m_useColorData))
876  {
877  glGenBuffers(1, &m_colorBuffer);
878  }
879 
880  if ((m_tangentBuffer == (GLuint)(-1)) && (m_useTangentData))
881  {
882  glGenBuffers(1, &m_tangentBuffer);
883  }
884 
885  if ((m_bitangentBuffer == (GLuint)(-1)) && (m_useBitangentData))
886  {
887  glGenBuffers(1, &m_bitangentBuffer);
888  }
889 
890  // resize buffers
891  if (m_flagBufferResize)
892  {
893  if (true)
894  {
895  glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
896  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_localPos[0]), GL_STATIC_DRAW);
897  }
898 
899  if (m_useNormalData)
900  {
901  glBindBuffer(GL_ARRAY_BUFFER, m_normalBuffer);
902  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_normal[0]), GL_STATIC_DRAW);
903  }
904 
905  if (m_useTexCoordData)
906  {
907  glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
908  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_texCoord[0]), GL_STATIC_DRAW);
909  }
910 
911  if (m_useColorData)
912  {
913  glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
914  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cColorf), &(m_color[0]), GL_STATIC_DRAW);
915  }
916 
917  if (m_useTangentData)
918  {
919  glBindBuffer(GL_ARRAY_BUFFER, m_tangentBuffer);
920  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_tangent[0]), GL_STATIC_DRAW);
921  }
922 
923  if (m_useBitangentData)
924  {
925  glBindBuffer(GL_ARRAY_BUFFER, m_bitangentBuffer);
926  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_bitangent[0]), GL_STATIC_DRAW);
927  }
928 
929  m_flagBufferResize = false;
930  }
931 
932  // update buffers if needed
933  if (m_flagPositionData)
934  {
935  glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
936  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_localPos[0]));
937  m_flagPositionData = false;
938  }
939  if (m_flagNormalData)
940  {
941  glBindBuffer(GL_ARRAY_BUFFER, m_normalBuffer);
942  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_normal[0]));
943  m_flagNormalData = false;
944  }
945  if (m_flagTexCoordData)
946  {
947  glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
948  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_texCoord[0]));
949  m_flagTexCoordData = false;
950  }
951  if (m_flagColorData)
952  {
953  glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
954  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cColorf), &(m_color[0]));
955  m_flagColorData = false;
956  }
957  if (m_flagTangentData)
958  {
959  glBindBuffer(GL_ARRAY_BUFFER, m_tangentBuffer);
960  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_tangent[0]));
961  m_flagTangentData = false;
962  }
963  if (m_flagBitangentData)
964  {
965  glBindBuffer(GL_ARRAY_BUFFER, m_bitangentBuffer);
966  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_bitangent[0]));
967  m_flagBitangentData = false;
968  }
969 
970  // bind buffers and set client state
971  {
972  glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
973  glEnableVertexAttribArray(C_VB_POSITION);
974  glVertexAttribPointer(C_VB_POSITION, 3, GL_DOUBLE, GL_FALSE, 0, 0);
975  glVertexPointer(3, GL_DOUBLE, 0, 0);
976  }
977 
978  if (m_useNormalData)
979  {
980  glBindBuffer(GL_ARRAY_BUFFER, m_normalBuffer);
981  glEnableVertexAttribArray(C_VB_NORMAL);
982  glVertexAttribPointer(C_VB_NORMAL, 3, GL_DOUBLE, GL_FALSE, 0, 0);
983  }
984  else
985  {
986  glDisableVertexAttribArray(C_VB_NORMAL);
987  }
988 
989  if (m_useTexCoordData)
990  {
991  glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
992  glEnableVertexAttribArray(C_VB_TEXCOORD);
993  glVertexAttribPointer(C_VB_TEXCOORD, 3, GL_DOUBLE, GL_FALSE, 0, 0);
994  }
995  else
996  {
997  glDisableVertexAttribArray(C_VB_TEXCOORD);
998  }
999 
1000  if (m_useColorData)
1001  {
1002  glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
1003  glEnableVertexAttribArray(C_VB_COLOR);
1004  glVertexAttribPointer(C_VB_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(cColorf), 0);
1005  }
1006  else
1007  {
1008  glDisableVertexAttribArray(C_VB_COLOR);
1009  }
1010 
1011  if (m_useTangentData)
1012  {
1013  glBindBuffer(GL_ARRAY_BUFFER, m_tangentBuffer);
1014  glEnableVertexAttribArray(C_VB_TANGENT);
1015  glVertexAttribPointer(C_VB_TANGENT, 3, GL_DOUBLE, GL_FALSE, 0, 0);
1016  }
1017  else
1018  {
1019  glDisableVertexAttribArray(C_VB_TANGENT);
1020  }
1021 
1022  if (m_useBitangentData)
1023  {
1024  glBindBuffer(GL_ARRAY_BUFFER, m_bitangentBuffer);
1025  glEnableVertexAttribArray(C_VB_BITANGENT);
1026  glVertexAttribPointer(C_VB_BITANGENT, 3, GL_DOUBLE, GL_FALSE, 0, 0);
1027  }
1028  else
1029  {
1030  glDisableVertexAttribArray(C_VB_BITANGENT);
1031  }
1032 
1033  glBindBuffer(GL_ARRAY_BUFFER, 0);
1034 #endif
1035  }
1036 
1037 
1038  //--------------------------------------------------------------------------
1042  //--------------------------------------------------------------------------
1043  inline void renderFinalize()
1044  {
1045 #ifdef C_USE_OPENGL
1046  glDisableVertexAttribArray(C_VB_INDEX_BUFFER);
1047  glDisableVertexAttribArray(C_VB_POSITION);
1048  glDisableVertexAttribArray(C_VB_NORMAL);
1049  glDisableVertexAttribArray(C_VB_TEXCOORD);
1050  glDisableVertexAttribArray(C_VB_COLOR);
1051  glDisableVertexAttribArray(C_VB_TANGENT);
1052  glDisableVertexAttribArray(C_VB_BITANGENT);
1053 #endif
1054  }
1055 
1056  //--------------------------------------------------------------------------
1068  //--------------------------------------------------------------------------
1069  inline void allocateData(const int a_numberOfVertices,
1070  const bool a_useNormalData,
1071  const bool a_useTexCoordData,
1072  const bool a_useColorData,
1073  const bool a_useTangentData,
1074  const bool a_useBitangentData,
1075  const bool a_useUserData)
1076  {
1077  // increment counter
1078  m_numVertices = m_numVertices + a_numberOfVertices;
1079 
1080  // update position data allocation
1081  cVector3d pos(0.0, 0.0, 0.0);
1082  m_localPos.resize(m_numVertices, pos);
1083  m_globalPos.resize(m_numVertices, pos);
1084 
1085  // update normal data allocation
1086  m_useNormalData = a_useNormalData;
1087  if (m_useNormalData)
1088  {
1089  cVector3d normal(1.0, 0.0, 0.0);
1090  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1091  {
1092  m_normal.resize(m_numVertices, normal);
1093  }
1094  else
1095  {
1096  m_normal.push_back(normal);
1097  }
1098  m_flagNormalData = true;
1099  }
1100  else
1101  {
1102  m_normal.clear();
1103  }
1104 
1105  // texture coordinate data allocation
1106  m_useTexCoordData = a_useTexCoordData;
1107  if (m_useTexCoordData)
1108  {
1109  cVector3d texCoord(0.0, 0.0, 0.0);
1110  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1111  {
1112  m_texCoord.resize(m_numVertices, texCoord);
1113  }
1114  else
1115  {
1116  m_texCoord.push_back(texCoord);
1117  }
1118  m_flagTexCoordData = true;
1119  }
1120  else
1121  {
1122  m_texCoord.clear();
1123  }
1124 
1125  // update color data allocation
1126  m_useColorData = a_useColorData;
1127  if (m_useColorData)
1128  {
1129  cColorf color(0.0, 0.0, 0.0, 1.0);
1130  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1131  {
1132  m_color.resize(m_numVertices, color);
1133  }
1134  else
1135  {
1136  m_color.push_back(color);
1137  }
1138  m_flagColorData = true;
1139  }
1140  else
1141  {
1142  m_color.clear();
1143  }
1144 
1145  // update tangent data allocation
1146  m_useTangentData = a_useTangentData;
1147  if (m_useTangentData)
1148  {
1149  cVector3d tangent(1.0, 0.0, 0.0);
1150  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1151  {
1152  m_tangent.resize(m_numVertices, tangent);
1153  }
1154  else
1155  {
1156  m_tangent.push_back(tangent);
1157  }
1158  m_flagTangentData = true;
1159  }
1160  else
1161  {
1162  m_tangent.clear();
1163  }
1164 
1165  // update bitangent data allocation
1166  m_useBitangentData = a_useBitangentData;
1167  if (m_useBitangentData)
1168  {
1169  cVector3d bitangent(0.0, 1.0, 0.0);
1170  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1171  {
1172  m_bitangent.resize(m_numVertices, bitangent);
1173  }
1174  else
1175  {
1176  m_bitangent.push_back(bitangent);
1177  }
1178  m_flagBitangentData = true;
1179  }
1180  else
1181  {
1182  m_bitangent.clear();
1183  }
1184 
1185  // update user data allocation
1186  m_useUserData = a_useUserData;
1187  if (m_useUserData)
1188  {
1189  int data = 0;
1190  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1191  {
1192  m_userData.resize(m_numVertices, data);
1193  }
1194  else
1195  {
1196  m_userData.push_back(data);
1197  }
1198  m_flagUserData = true;
1199  }
1200  else
1201  {
1202  m_userData.clear();
1203  }
1204 
1205  // update buffer size
1206  m_flagBufferResize = true;
1207  }
1208 
1209 
1210  //--------------------------------------------------------------------------
1211  // PUBLIC MEMBERS:
1212  //--------------------------------------------------------------------------
1213 
1214 public:
1215 
1217  std::vector<cVector3d> m_localPos;
1218 
1220  std::vector<cVector3d> m_globalPos;
1221 
1223  std::vector<cVector3d> m_normal;
1224 
1226  std::vector<cVector3d> m_texCoord;
1227 
1229  std::vector<cColorf> m_color;
1230 
1232  std::vector<cVector3d> m_tangent;
1233 
1235  std::vector<cVector3d> m_bitangent;
1236 
1238  std::vector<int> m_userData;
1239 
1240 
1241  //--------------------------------------------------------------------------
1242  // PROTECTED MEMBERS:
1243  //--------------------------------------------------------------------------
1244 
1245 protected:
1246 
1248  unsigned int m_numVertices;
1249 
1252 
1255 
1258 
1261 
1264 
1267 
1268 
1269  //--------------------------------------------------------------------------
1270  // PUBLIC MEMBERS:
1271  //--------------------------------------------------------------------------
1272 
1273 public:
1274 
1277 
1280 
1283 
1286 
1289 
1292 
1295 
1298 
1299 
1300  //--------------------------------------------------------------------------
1301  // PUBLIC MEMBERS: (OPENGL)
1302  //--------------------------------------------------------------------------
1303 
1304 public:
1305 
1308 
1311 
1314 
1317 
1320 
1323 };
1324 
1325 //------------------------------------------------------------------------------
1326 } // namespace chai3d
1327 //------------------------------------------------------------------------------
1328 
1329 //------------------------------------------------------------------------------
1330 #endif
1331 //------------------------------------------------------------------------------
This class implements a 3D vector.
Definition: CVector3d.h:88
int newVertex()
Definition: CVertexArray.h:285
Implements an OpenGL shader primitive.
bool m_useBitangentData
If true then surface bitangent data will be allocated for each new vertex.
Definition: CVertexArray.h:1263
std::vector< int > m_userData
User data of vertices.
Definition: CVertexArray.h:1238
void mulr(const cMatrix3d &a_matrix, cMatrix3d &a_result) const
This function computes the multiplication of this matrix with another.
Definition: CMatrix3d.h:922
#define C_VB_BITANGENT
Definition: CShader.h:78
std::vector< cVector3d > m_bitangent
Surface bitangent of vertices.
Definition: CVertexArray.h:1235
void setTangent(const unsigned int a_vertexIndex, const cVector3d &a_tangent)
Definition: CVertexArray.h:598
cColorf getColor(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:584
bool m_useTexCoordData
Definition: CVertexArray.h:99
bool m_useTexCoordData
If true then texture coordinate data will be allocated for each new vertex.
Definition: CVertexArray.h:1254
bool m_useUserData
Definition: CVertexArray.h:103
cVector3d getTexCoord(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:506
std::vector< cColorf > m_color
Color of vertices.
Definition: CVertexArray.h:1229
This class implements array of 3D vertices.
Definition: CVertexArray.h:133
std::vector< cVector3d > m_tangent
Surface tangent of vertices.
Definition: CVertexArray.h:1232
bool m_flagBufferResize
If true, then data buffer need to be updated in size.
Definition: CVertexArray.h:1297
void allocateData(const int a_numberOfVertices, const bool a_useNormalData, const bool a_useTexCoordData, const bool a_useColorData, const bool a_useTangentData, const bool a_useBitangentData, const bool a_useUserData)
Definition: CVertexArray.h:1069
void setNormal(const unsigned int a_vertexIndex, const double &a_x, const double &a_y, const double &a_z)
Definition: CVertexArray.h:428
int getUserData(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:729
GLuint m_bitangentBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1322
bool m_useBitangentData
Definition: CVertexArray.h:102
void renderFinalize()
Definition: CVertexArray.h:1043
bool m_flagColorData
If true then vertex color data has been modified.
Definition: CVertexArray.h:1285
static cVertexArrayPtr create(const bool a_useNormalData, const bool a_useTexCoordData, const bool a_useColorData, const bool a_useTangentData, const bool a_useBitangentData, const bool a_useUserData)
Definition: CVertexArray.h:198
bool m_flagTangentData
If true then surface tangent data has been modified.
Definition: CVertexArray.h:1288
void setTexCoord(const unsigned int a_vertexIndex, const cVector3d &a_texCoord)
Definition: CVertexArray.h:463
std::vector< cVector3d > m_normal
Surface normal of vertices.
Definition: CVertexArray.h:1223
This structure manages user options when creating vertex arrays.
Definition: CVertexArray.h:78
bool m_flagNormalData
If true then normal data has been modified.
Definition: CVertexArray.h:1279
bool m_useColorData
If true then vertex color data will be allocated for each new vertex.
Definition: CVertexArray.h:1257
void setColor(const unsigned int a_vertexIndex, const cColorf &a_color)
Definition: CVertexArray.h:520
bool m_flagUserData
If true then user data has been modified.
Definition: CVertexArray.h:1294
This class implements a 3D matrix.
Definition: CMatrix3d.h:97
#define C_VB_COLOR
Definition: CShader.h:76
GLuint m_tangentBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1319
cVertexArrayPtr copy()
Definition: CVertexArray.h:245
cVector3d getBitangent(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:696
This class defines a color using a GLubyte representation for each component.
Definition: CColor.h:1184
~cVertexArray()
Definition: CVertexArray.h:181
bool getUseTangentData() const
Definition: CVertexArray.h:817
GLuint m_texCoordBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1313
unsigned int getNumElements() const
Definition: CVertexArray.h:761
bool m_useColorData
Definition: CVertexArray.h:100
void setLocalPos(const unsigned int a_vertexIndex, const double &a_x, const double &a_y, const double &a_z)
Definition: CVertexArray.h:326
cVertexArrayOptions(const bool a_useNormalData, const bool a_useTexCoordData, const bool a_useColorData, const bool a_useTangentData, const bool a_useBitangentData, const bool a_useUserData)
Definition: CVertexArray.h:83
Implements a 3D matrix.
#define C_VB_POSITION
Definition: CShader.h:73
std::vector< cVector3d > m_globalPos
Global position of vertices in world coordinates.
Definition: CVertexArray.h:1220
std::vector< cVector3d > m_localPos
Local position of vertices.
Definition: CVertexArray.h:1217
#define C_VB_TANGENT
Definition: CShader.h:77
GLuint m_positionBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1307
cVector3d getLocalPos(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:377
void setColor(const unsigned int a_vertexIndex, const cColorb &a_color)
Definition: CVertexArray.h:565
bool getUseBitangentData() const
Definition: CVertexArray.h:831
void setBitangent(const unsigned int a_vertexIndex, const double &a_x, const double &a_y, const double &a_z)
Definition: CVertexArray.h:675
std::shared_ptr< cVertexArray > cVertexArrayPtr
Definition: CVertexArray.h:107
bool m_useTangentData
If true then surface tangent data will be allocated for each new vertex.
Definition: CVertexArray.h:1260
This class defines a color using a GLfloat representation for each component.
Definition: CColor.h:138
Implements color properties.
bool getUseColorData() const
Definition: CVertexArray.h:803
void setUserData(const unsigned int a_vertexIndex, const int a_userData)
Definition: CVertexArray.h:710
cVertexArray(const cVertexArrayOptions &a_options)
Definition: CVertexArray.h:150
Implements a 3D vector.
void setTexCoord(const unsigned int a_vertexIndex, const double &a_tx, const double &a_ty=0.0, const double &a_tz=0.0)
Definition: CVertexArray.h:485
#define C_VB_INDEX_BUFFER
Definition: CShader.h:72
bool getUseUserData() const
Definition: CVertexArray.h:845
bool getUseTexCoordData() const
Definition: CVertexArray.h:789
bool m_flagPositionData
If true then position data has been modified.
Definition: CVertexArray.h:1276
bool m_flagTexCoordData
If true then texture coordinate data has been modified.
Definition: CVertexArray.h:1282
bool m_useNormalData
If true then normal data will be allocated for each new vertex.
Definition: CVertexArray.h:1251
#define C_VB_TEXCOORD
Definition: CShader.h:75
void setNormal(const unsigned int a_vertexIndex, const cVector3d &a_normal)
Definition: CVertexArray.h:407
void setTangent(const unsigned int a_vertexIndex, const double &a_x, const double &a_y, const double &a_z)
Definition: CVertexArray.h:619
bool m_flagBitangentData
If true then surface bitangent data has been modified.
Definition: CVertexArray.h:1291
bool m_useNormalData
Definition: CVertexArray.h:98
void clear()
Definition: CVertexArray.h:225
std::vector< cVector3d > m_texCoord
Texture coordinate (U,V,W) of vertices.
Definition: CVertexArray.h:1226
Definition: CAudioBuffer.cpp:56
GLuint m_colorBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1316
GLuint m_normalBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1310
#define C_VB_NORMAL
Definition: CShader.h:74
bool m_useTangentData
Definition: CVertexArray.h:101
cVector3d getGlobalPos(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:393
void renderInitialize()
Definition: CVertexArray.h:856
unsigned int m_numVertices
Number of vertices.
Definition: CVertexArray.h:1248
void setColor(const unsigned int a_vertexIndex, const float &a_red, const float &a_green, const float &a_blue, const float &a_alpha=1.0)
Definition: CVertexArray.h:543
int newVertices(unsigned int a_numberOfVertices)
Definition: CVertexArray.h:300
void computeGlobalPosition(const unsigned int a_vertexIndex, const cVector3d &a_globalPos, const cMatrix3d &a_globalRot)
Definition: CVertexArray.h:745
void translate(const unsigned int a_vertexIndex, const cVector3d &a_translation)
Definition: CVertexArray.h:361
void setLocalPos(const unsigned int a_vertexIndex, const cVector3d &a_pos)
Definition: CVertexArray.h:344
cColorf getColorf() const
This method returns this color converted into cColorf format.
Definition: CColor.cpp:76
void setBitangent(const unsigned int a_vertexIndex, const cVector3d &a_bitangent)
Definition: CVertexArray.h:654
bool getUseNormalData() const
Definition: CVertexArray.h:775
bool m_useUserData
If true then surface bitangent data will be allocated for each new vertex.
Definition: CVertexArray.h:1266
cVector3d getNormal(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:449
cVector3d getTangent(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:640