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.1.1 $Rev: 1928 $
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 
272  // return new vertex array
273  return (vertexArray);
274  }
275 
276 
277  //--------------------------------------------------------------------------
283  //--------------------------------------------------------------------------
284  inline int newVertex()
285  {
286  return (newVertices(1));
287  }
288 
289 
290  //--------------------------------------------------------------------------
298  //--------------------------------------------------------------------------
299  inline int newVertices(unsigned int a_numberOfVertices)
300  {
301  // sanity check
302  if (a_numberOfVertices == 0) { return (-1); }
303 
304  // store current index
305  int index = m_numVertices;
306 
307  // allocate new vertices
309 
310  // return index of first allocated vertex
311  return (index);
312  }
313 
314 
315  //--------------------------------------------------------------------------
324  //--------------------------------------------------------------------------
325  inline void setLocalPos(const unsigned int a_vertexIndex,
326  const double& a_x,
327  const double& a_y,
328  const double& a_z)
329  {
330  m_localPos[a_vertexIndex].set(a_x, a_y, a_z);
331  m_flagPositionData = true;
332  }
333 
334 
335  //--------------------------------------------------------------------------
342  //--------------------------------------------------------------------------
343  inline void setLocalPos(const unsigned int a_vertexIndex,
344  const cVector3d& a_pos)
345  {
346  m_localPos[a_vertexIndex] = a_pos;
347  m_flagPositionData = true;
348  }
349 
350 
351  //--------------------------------------------------------------------------
359  //--------------------------------------------------------------------------
360  inline void translate(const unsigned int a_vertexIndex,
361  const cVector3d& a_translation)
362  {
363  m_localPos[a_vertexIndex].add(a_translation);
364  m_flagPositionData = true;
365  }
366 
367 
368  //--------------------------------------------------------------------------
375  //--------------------------------------------------------------------------
376  inline cVector3d getLocalPos(const unsigned int a_vertexIndex) const
377  {
378  return (m_localPos[a_vertexIndex]);
379  }
380 
381 
382  //--------------------------------------------------------------------------
391  //--------------------------------------------------------------------------
392  inline cVector3d getGlobalPos(const unsigned int a_vertexIndex) const
393  {
394  return (m_globalPos[a_vertexIndex]);
395  }
396 
397 
398  //--------------------------------------------------------------------------
405  //--------------------------------------------------------------------------
406  inline void setNormal(const unsigned int a_vertexIndex,
407  const cVector3d& a_normal)
408  {
409  if (m_useNormalData)
410  {
411  m_normal[a_vertexIndex] = a_normal;
412  m_flagNormalData = true;
413  }
414  }
415 
416 
417  //--------------------------------------------------------------------------
426  //--------------------------------------------------------------------------
427  inline void setNormal(const unsigned int a_vertexIndex,
428  const double& a_x,
429  const double& a_y,
430  const double& a_z)
431  {
432  if (m_useNormalData)
433  {
434  m_normal[a_vertexIndex].set(a_x, a_y, a_z);
435  m_flagNormalData = true;
436  }
437  }
438 
439 
440  //--------------------------------------------------------------------------
447  //--------------------------------------------------------------------------
448  inline cVector3d getNormal(const unsigned int a_vertexIndex) const
449  {
450  return (m_normal[a_vertexIndex]);
451  }
452 
453 
454  //--------------------------------------------------------------------------
461  //--------------------------------------------------------------------------
462  inline void setTexCoord(const unsigned int a_vertexIndex,
463  const cVector3d& a_texCoord)
464  {
465  if (m_useTexCoordData)
466  {
467  m_texCoord[a_vertexIndex] = a_texCoord;
468  m_flagTexCoordData = true;
469  }
470  }
471 
472 
473  //--------------------------------------------------------------------------
483  //--------------------------------------------------------------------------
484  inline void setTexCoord(const unsigned int a_vertexIndex,
485  const double& a_tx,
486  const double& a_ty = 0.0,
487  const double& a_tz = 0.0)
488  {
489  if (m_useTexCoordData)
490  {
491  m_texCoord[a_vertexIndex].set(a_tx, a_ty,a_tz);
492  m_flagTexCoordData = true;
493  }
494  }
495 
496 
497  //--------------------------------------------------------------------------
504  //--------------------------------------------------------------------------
505  inline cVector3d getTexCoord(const unsigned int a_vertexIndex) const
506  {
507  return (m_texCoord[a_vertexIndex]);
508  }
509 
510 
511  //--------------------------------------------------------------------------
518  //--------------------------------------------------------------------------
519  inline void setColor(const unsigned int a_vertexIndex,
520  const cColorf& a_color)
521  {
522  if (m_useColorData)
523  {
524  m_color[a_vertexIndex] = a_color;
525  m_flagColorData = true;
526  }
527  }
528 
529 
530  //--------------------------------------------------------------------------
541  //--------------------------------------------------------------------------
542  inline void setColor(const unsigned int a_vertexIndex,
543  const float& a_red,
544  const float& a_green,
545  const float& a_blue,
546  const float& a_alpha = 1.0 )
547  {
548  if (m_useColorData)
549  {
550  m_color[a_vertexIndex].set(a_red, a_green, a_blue, a_alpha);
551  m_flagColorData = true;
552  }
553  }
554 
555 
556  //--------------------------------------------------------------------------
563  //--------------------------------------------------------------------------
564  inline void setColor(const unsigned int a_vertexIndex,
565  const cColorb& a_color)
566  {
567  if (m_useColorData)
568  {
569  m_color[a_vertexIndex] = a_color.getColorf();
570  m_flagColorData = true;
571  }
572  }
573 
574 
575  //--------------------------------------------------------------------------
582  //--------------------------------------------------------------------------
583  inline cColorf getColor(const unsigned int a_vertexIndex) const
584  {
585  return (m_color[a_vertexIndex]);
586  }
587 
588 
589  //--------------------------------------------------------------------------
596  //--------------------------------------------------------------------------
597  inline cVector3d getTangent(const unsigned int a_vertexIndex) const
598  {
599  return (m_tangent[a_vertexIndex]);
600  }
601 
602 
603  //--------------------------------------------------------------------------
610  //--------------------------------------------------------------------------
611  inline cVector3d getBitangent(const unsigned int a_vertexIndex) const
612  {
613  return (m_bitangent[a_vertexIndex]);
614  }
615 
616 
617  //--------------------------------------------------------------------------
624  //--------------------------------------------------------------------------
625  inline void setUserData(const unsigned int a_vertexIndex,
626  const int a_userData)
627  {
628  if (m_useUserData)
629  {
630  m_userData[a_vertexIndex] = a_userData;
631  m_flagUserData = true;
632  }
633  }
634 
635 
636  //--------------------------------------------------------------------------
643  //--------------------------------------------------------------------------
644  inline int getUserData(const unsigned int a_vertexIndex) const
645  {
646  return (m_userData[a_vertexIndex]);
647  }
648 
649 
650  //--------------------------------------------------------------------------
659  //--------------------------------------------------------------------------
660  inline void computeGlobalPosition(const unsigned int a_vertexIndex,
661  const cVector3d& a_globalPos,
662  const cMatrix3d& a_globalRot)
663  {
664  a_globalRot.mulr(m_localPos[a_vertexIndex], m_globalPos[a_vertexIndex]);
665  m_globalPos[a_vertexIndex].add(a_globalPos);
666  }
667 
668 
669  //--------------------------------------------------------------------------
675  //--------------------------------------------------------------------------
676  inline unsigned int getNumVertices() const
677  {
678  return (m_numVertices);
679  }
680 
681 
682  //--------------------------------------------------------------------------
689  //--------------------------------------------------------------------------
690  inline bool getUseNormalData() const
691  {
692  return (m_useNormalData);
693  }
694 
695 
696  //--------------------------------------------------------------------------
703  //--------------------------------------------------------------------------
704  inline bool getUseTexCoordData() const
705  {
706  return (m_useTexCoordData);
707  }
708 
709 
710  //--------------------------------------------------------------------------
717  //--------------------------------------------------------------------------
718  inline bool getUseColorData() const
719  {
720  return (m_useColorData);
721  }
722 
723 
724  //--------------------------------------------------------------------------
731  //--------------------------------------------------------------------------
732  inline bool getUseTangentData() const
733  {
734  return (m_useTangentData);
735  }
736 
737 
738  //--------------------------------------------------------------------------
745  //--------------------------------------------------------------------------
746  inline bool getUseBitangentData() const
747  {
748  return (m_useBitangentData);
749  }
750 
751 
752  //--------------------------------------------------------------------------
759  //--------------------------------------------------------------------------
760  inline bool getUseUserData() const
761  {
762  return (m_useUserData);
763  }
764 
765 
766  //--------------------------------------------------------------------------
770  //--------------------------------------------------------------------------
771  inline void renderInitialize()
772  {
773 #ifdef C_USE_OPENGL
774  // create buffers first time
775  if (m_positionBuffer == (GLuint)(-1))
776  {
777  glGenBuffers(1, &m_positionBuffer);
778  }
779 
780  if ((m_normalBuffer == (GLuint)(-1)) && (m_useNormalData))
781  {
782  glGenBuffers(1, &m_normalBuffer);
783  }
784 
785  if ((m_texCoordBuffer == (GLuint)(-1)) && (m_useTexCoordData))
786  {
787  glGenBuffers(1, &m_texCoordBuffer);
788  }
789 
790  if ((m_colorBuffer == (GLuint)(-1)) && (m_useColorData))
791  {
792  glGenBuffers(1, &m_colorBuffer);
793  }
794 
795  if ((m_tangentBuffer == (GLuint)(-1)) && (m_useTangentData))
796  {
797  glGenBuffers(1, &m_tangentBuffer);
798  }
799 
800  if ((m_bitangentBuffer == (GLuint)(-1)) && (m_useBitangentData))
801  {
802  glGenBuffers(1, &m_bitangentBuffer);
803  }
804 
805  // resize buffers
806  if (m_flagBufferResize)
807  {
808  if (true)
809  {
810  glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
811  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_localPos[0]), GL_STATIC_DRAW);
812  }
813 
814  if (m_useNormalData)
815  {
816  glBindBuffer(GL_ARRAY_BUFFER, m_normalBuffer);
817  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_normal[0]), GL_STATIC_DRAW);
818  }
819 
820  if (m_useTexCoordData)
821  {
822  glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
823  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_texCoord[0]), GL_STATIC_DRAW);
824  }
825 
826  if (m_useColorData)
827  {
828  glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
829  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cColorf), &(m_color[0]), GL_STATIC_DRAW);
830  }
831 
832  if (m_useTangentData)
833  {
834  glBindBuffer(GL_ARRAY_BUFFER, m_tangentBuffer);
835  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_tangent[0]), GL_STATIC_DRAW);
836  }
837 
838  if (m_useBitangentData)
839  {
840  glBindBuffer(GL_ARRAY_BUFFER, m_bitangentBuffer);
841  glBufferData(GL_ARRAY_BUFFER, m_numVertices * sizeof(cVector3d), &(m_bitangent[0]), GL_STATIC_DRAW);
842  }
843 
844  m_flagBufferResize = false;
845  }
846 
847  // update buffers if needed
848  if (m_flagPositionData)
849  {
850  glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
851  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_localPos[0]));
852  m_flagPositionData = false;
853  }
854  if (m_flagNormalData)
855  {
856  glBindBuffer(GL_ARRAY_BUFFER, m_normalBuffer);
857  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_normal[0]));
858  m_flagNormalData = false;
859  }
860  if (m_flagTexCoordData)
861  {
862  glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
863  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_texCoord[0]));
864  m_flagTexCoordData = false;
865  }
866  if (m_flagColorData)
867  {
868  glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
869  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cColorf), &(m_color[0]));
870  m_flagColorData = false;
871  }
872  if (m_flagTangentData)
873  {
874  glBindBuffer(GL_ARRAY_BUFFER, m_tangentBuffer);
875  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_tangent[0]));
876  m_flagTangentData = false;
877  }
879  {
880  glBindBuffer(GL_ARRAY_BUFFER, m_bitangentBuffer);
881  glBufferSubData(GL_ARRAY_BUFFER, 0, m_numVertices * sizeof(cVector3d), &(m_bitangent[0]));
882  m_flagBitangentData = false;
883  }
884 
885  // bind buffers and set client state
886  {
887  glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);
888  glEnableVertexAttribArray(C_VB_POSITION);
889  glVertexAttribPointer(C_VB_POSITION, 3, GL_DOUBLE, GL_FALSE, 0, 0);
890  glVertexPointer(3, GL_DOUBLE, 0, 0);
891  }
892 
893  if (m_useNormalData)
894  {
895  glBindBuffer(GL_ARRAY_BUFFER, m_normalBuffer);
896  glEnableVertexAttribArray(C_VB_NORMAL);
897  glVertexAttribPointer(C_VB_NORMAL, 3, GL_DOUBLE, GL_FALSE, 0, 0);
898  }
899  else
900  {
901  glDisableVertexAttribArray(C_VB_NORMAL);
902  }
903 
904  if (m_useTexCoordData)
905  {
906  glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
907  glEnableVertexAttribArray(C_VB_TEXCOORD);
908  glVertexAttribPointer(C_VB_TEXCOORD, 3, GL_DOUBLE, GL_FALSE, 0, 0);
909  }
910  else
911  {
912  glDisableVertexAttribArray(C_VB_TEXCOORD);
913  }
914 
915  if (m_useColorData)
916  {
917  glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
918  glEnableVertexAttribArray(C_VB_COLOR);
919  glVertexAttribPointer(C_VB_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(cColorf), 0);
920  }
921  else
922  {
923  glDisableVertexAttribArray(C_VB_COLOR);
924  }
925 
926  if (m_useTangentData)
927  {
928  glBindBuffer(GL_ARRAY_BUFFER, m_tangentBuffer);
929  glEnableVertexAttribArray(C_VB_TANGENT);
930  glVertexAttribPointer(C_VB_TANGENT, 3, GL_DOUBLE, GL_FALSE, 0, 0);
931  }
932  else
933  {
934  glDisableVertexAttribArray(C_VB_TANGENT);
935  }
936 
937  if (m_useBitangentData)
938  {
939  glBindBuffer(GL_ARRAY_BUFFER, m_bitangentBuffer);
940  glEnableVertexAttribArray(C_VB_BITANGENT);
941  glVertexAttribPointer(C_VB_BITANGENT, 3, GL_DOUBLE, GL_FALSE, 0, 0);
942  }
943  else
944  {
945  glDisableVertexAttribArray(C_VB_BITANGENT);
946  }
947 
948  glBindBuffer(GL_ARRAY_BUFFER, 0);
949 #endif
950  }
951 
952 
953  //--------------------------------------------------------------------------
957  //--------------------------------------------------------------------------
958  inline void renderFinalize()
959  {
960 #ifdef C_USE_OPENGL
961  glDisableVertexAttribArray(C_VB_INDEX_BUFFER);
962  glDisableVertexAttribArray(C_VB_POSITION);
963  glDisableVertexAttribArray(C_VB_NORMAL);
964  glDisableVertexAttribArray(C_VB_TEXCOORD);
965  glDisableVertexAttribArray(C_VB_COLOR);
966  glDisableVertexAttribArray(C_VB_TANGENT);
967  glDisableVertexAttribArray(C_VB_BITANGENT);
968 #endif
969  }
970 
971  //--------------------------------------------------------------------------
983  //--------------------------------------------------------------------------
984  inline void allocateData(const int a_numberOfVertices,
985  const bool a_useNormalData,
986  const bool a_useTexCoordData,
987  const bool a_useColorData,
988  const bool a_useTangentData,
989  const bool a_useBitangentData,
990  const bool a_useUserData)
991  {
992  // increment counter
993  m_numVertices = m_numVertices + a_numberOfVertices;
994 
995  // update position data allocation
996  cVector3d pos(0.0, 0.0, 0.0);
997  m_localPos.resize(m_numVertices, pos);
998  m_globalPos.resize(m_numVertices, pos);
999 
1000  // update normal data allocation
1001  m_useNormalData = a_useNormalData;
1002  if (m_useNormalData)
1003  {
1004  cVector3d normal(1.0, 0.0, 0.0);
1005  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1006  {
1007  m_normal.resize(m_numVertices, normal);
1008  }
1009  else
1010  {
1011  m_normal.push_back(normal);
1012  }
1013  m_flagNormalData = true;
1014  }
1015  else
1016  {
1017  m_normal.clear();
1018  }
1019 
1020  // texture coordinate data allocation
1021  m_useTexCoordData = a_useTexCoordData;
1022  if (m_useTexCoordData)
1023  {
1024  cVector3d texCoord(0.0, 0.0, 0.0);
1025  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1026  {
1027  m_texCoord.resize(m_numVertices, texCoord);
1028  }
1029  else
1030  {
1031  m_texCoord.push_back(texCoord);
1032  }
1033  m_flagTexCoordData = true;
1034  }
1035  else
1036  {
1037  m_texCoord.clear();
1038  }
1039 
1040  // update color data allocation
1041  m_useColorData = a_useColorData;
1042  if (m_useColorData)
1043  {
1044  cColorf color(0.0, 0.0, 0.0, 1.0);
1045  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1046  {
1047  m_color.resize(m_numVertices, color);
1048  }
1049  else
1050  {
1051  m_color.push_back(color);
1052  }
1053  m_flagColorData = true;
1054  }
1055  else
1056  {
1057  m_color.clear();
1058  }
1059 
1060  // update tangent data allocation
1061  m_useTangentData = a_useTangentData;
1062  if (m_useTangentData)
1063  {
1064  cVector3d tangent(1.0, 0.0, 0.0);
1065  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1066  {
1067  m_tangent.resize(m_numVertices, tangent);
1068  }
1069  else
1070  {
1071  m_tangent.push_back(tangent);
1072  }
1073  m_flagTangentData = true;
1074  }
1075  else
1076  {
1077  m_tangent.clear();
1078  }
1079 
1080  // update bitangent data allocation
1081  m_useBitangentData = a_useBitangentData;
1082  if (m_useBitangentData)
1083  {
1084  cVector3d bitangent(0.0, 1.0, 0.0);
1085  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1086  {
1087  m_bitangent.resize(m_numVertices, bitangent);
1088  }
1089  else
1090  {
1091  m_bitangent.push_back(bitangent);
1092  }
1093  m_flagBitangentData = true;
1094  }
1095  else
1096  {
1097  m_bitangent.clear();
1098  }
1099 
1100  // update user data allocation
1101  m_useUserData = a_useUserData;
1102  if (m_useUserData)
1103  {
1104  int data = 0;
1105  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1106  {
1107  m_userData.resize(m_numVertices, data);
1108  }
1109  else
1110  {
1111  m_userData.push_back(data);
1112  }
1113  m_flagUserData = true;
1114  }
1115  else
1116  {
1117  m_userData.clear();
1118  }
1119 
1120  // update buffer size
1121  m_flagBufferResize = true;
1122  }
1123 
1124 
1125  //--------------------------------------------------------------------------
1126  // PUBLIC MEMBERS:
1127  //--------------------------------------------------------------------------
1128 
1129 public:
1130 
1132  std::vector<cVector3d> m_localPos;
1133 
1135  std::vector<cVector3d> m_globalPos;
1136 
1138  std::vector<cVector3d> m_normal;
1139 
1141  std::vector<cVector3d> m_texCoord;
1142 
1144  std::vector<cColorf> m_color;
1145 
1147  std::vector<cVector3d> m_tangent;
1148 
1150  std::vector<cVector3d> m_bitangent;
1151 
1153  std::vector<int> m_userData;
1154 
1155 
1156  //--------------------------------------------------------------------------
1157  // PROTECTED MEMBERS:
1158  //--------------------------------------------------------------------------
1159 
1160 protected:
1161 
1163  unsigned int m_numVertices;
1164 
1167 
1170 
1173 
1176 
1179 
1182 
1183 
1184  //--------------------------------------------------------------------------
1185  // PUBLIC MEMBERS:
1186  //--------------------------------------------------------------------------
1187 
1188 public:
1189 
1192 
1195 
1198 
1201 
1204 
1207 
1210 
1213 
1214 
1215  //--------------------------------------------------------------------------
1216  // PUBLIC MEMBERS: (OPENGL)
1217  //--------------------------------------------------------------------------
1218 
1219 public:
1220 
1223 
1226 
1229 
1232 
1235 
1238 };
1239 
1240 //------------------------------------------------------------------------------
1241 } // namespace chai3d
1242 //------------------------------------------------------------------------------
1243 
1244 //------------------------------------------------------------------------------
1245 #endif
1246 //------------------------------------------------------------------------------
This class implements a 3D vector.
Definition: CVector3d.h:88
int newVertex()
Definition: CVertexArray.h:284
Implements an OpenGL shader primitive.
bool m_useBitangentData
If true then surface bitangent data will be allocated for each new vertex.
Definition: CVertexArray.h:1178
std::vector< int > m_userData
User data of vertices.
Definition: CVertexArray.h:1153
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:77
std::vector< cVector3d > m_bitangent
Surface bitangent of vertices.
Definition: CVertexArray.h:1150
cColorf getColor(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:583
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:1169
bool m_useUserData
Definition: CVertexArray.h:103
cVector3d getTexCoord(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:505
std::vector< cColorf > m_color
Color of vertices.
Definition: CVertexArray.h:1144
This class implements array of 3D vertices.
Definition: CVertexArray.h:133
std::vector< cVector3d > m_tangent
Surface tangent of vertices.
Definition: CVertexArray.h:1147
bool m_flagBufferResize
If true, then data buffer need to be updated in size.
Definition: CVertexArray.h:1212
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:984
void setNormal(const unsigned int a_vertexIndex, const double &a_x, const double &a_y, const double &a_z)
Definition: CVertexArray.h:427
unsigned int getNumVertices() const
Definition: CVertexArray.h:676
int getUserData(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:644
GLuint m_bitangentBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1237
bool m_useBitangentData
Definition: CVertexArray.h:102
void renderFinalize()
Definition: CVertexArray.h:958
bool m_flagColorData
If true then vertex color data has been modified.
Definition: CVertexArray.h:1200
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:1203
void setTexCoord(const unsigned int a_vertexIndex, const cVector3d &a_texCoord)
Definition: CVertexArray.h:462
std::vector< cVector3d > m_normal
Surface normal of vertices.
Definition: CVertexArray.h:1138
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:1194
bool m_useColorData
If true then vertex color data will be allocated for each new vertex.
Definition: CVertexArray.h:1172
void setColor(const unsigned int a_vertexIndex, const cColorf &a_color)
Definition: CVertexArray.h:519
bool m_flagUserData
If true then user data has been modified.
Definition: CVertexArray.h:1209
This class implements a 3D matrix.
Definition: CMatrix3d.h:97
#define C_VB_COLOR
Definition: CShader.h:75
GLuint m_tangentBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1234
cVertexArrayPtr copy()
Definition: CVertexArray.h:245
cVector3d getBitangent(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:611
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:732
GLuint m_texCoordBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1228
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:325
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:72
std::vector< cVector3d > m_globalPos
Global position of vertices in world coordinates.
Definition: CVertexArray.h:1135
std::vector< cVector3d > m_localPos
Local position of vertices.
Definition: CVertexArray.h:1132
#define C_VB_TANGENT
Definition: CShader.h:76
GLuint m_positionBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1222
cVector3d getLocalPos(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:376
void setColor(const unsigned int a_vertexIndex, const cColorb &a_color)
Definition: CVertexArray.h:564
bool getUseBitangentData() const
Definition: CVertexArray.h:746
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:1175
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:718
void setUserData(const unsigned int a_vertexIndex, const int a_userData)
Definition: CVertexArray.h:625
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:484
#define C_VB_INDEX_BUFFER
Definition: CShader.h:71
bool getUseUserData() const
Definition: CVertexArray.h:760
bool getUseTexCoordData() const
Definition: CVertexArray.h:704
bool m_flagPositionData
If true then position data has been modified.
Definition: CVertexArray.h:1191
bool m_flagTexCoordData
If true then texture coordinate data has been modified.
Definition: CVertexArray.h:1197
bool m_useNormalData
If true then normal data will be allocated for each new vertex.
Definition: CVertexArray.h:1166
#define C_VB_TEXCOORD
Definition: CShader.h:74
void setNormal(const unsigned int a_vertexIndex, const cVector3d &a_normal)
Definition: CVertexArray.h:406
bool m_flagBitangentData
If true then surface bitangent data has been modified.
Definition: CVertexArray.h:1206
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:1141
Definition: CAudioBuffer.cpp:56
GLuint m_colorBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1231
GLuint m_normalBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1225
#define C_VB_NORMAL
Definition: CShader.h:73
bool m_useTangentData
Definition: CVertexArray.h:101
cVector3d getGlobalPos(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:392
void renderInitialize()
Definition: CVertexArray.h:771
unsigned int m_numVertices
Number of vertices.
Definition: CVertexArray.h:1163
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:542
int newVertices(unsigned int a_numberOfVertices)
Definition: CVertexArray.h:299
void computeGlobalPosition(const unsigned int a_vertexIndex, const cVector3d &a_globalPos, const cMatrix3d &a_globalRot)
Definition: CVertexArray.h:660
void translate(const unsigned int a_vertexIndex, const cVector3d &a_translation)
Definition: CVertexArray.h:360
void setLocalPos(const unsigned int a_vertexIndex, const cVector3d &a_pos)
Definition: CVertexArray.h:343
cColorf getColorf() const
This method returns this color converted into cColorf format.
Definition: CColor.cpp:76
bool getUseNormalData() const
Definition: CVertexArray.h:690
bool m_useUserData
If true then surface bitangent data will be allocated for each new vertex.
Definition: CVertexArray.h:1181
cVector3d getNormal(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:448
cVector3d getTangent(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:597