CHAI3D  3.0.0
CVertexArray.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2015, 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.0.0 $Rev: 1646 $
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 render()
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  //--------------------------------------------------------------------------
965  //--------------------------------------------------------------------------
966  inline void allocateData(const int a_numberOfVertices,
967  const bool a_useNormalData,
968  const bool a_useTexCoordData,
969  const bool a_useColorData,
970  const bool a_useTangentData,
971  const bool a_useBitangentData,
972  const bool a_useUserData)
973  {
974  // increment counter
975  m_numVertices = m_numVertices + a_numberOfVertices;
976 
977  // update position data allocation
978  cVector3d pos(0.0, 0.0, 0.0);
979  m_localPos.resize(m_numVertices, pos);
980  m_globalPos.resize(m_numVertices, pos);
981 
982  // update normal data allocation
983  m_useNormalData = a_useNormalData;
984  if (m_useNormalData)
985  {
986  cVector3d normal(1.0, 0.0, 0.0);
987  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
988  {
989  m_normal.resize(m_numVertices, normal);
990  }
991  else
992  {
993  m_normal.push_back(normal);
994  }
995  m_flagNormalData = true;
996  }
997  else
998  {
999  m_normal.clear();
1000  }
1001 
1002  // texture coordinate data allocation
1003  m_useTexCoordData = a_useTexCoordData;
1004  if (m_useTexCoordData)
1005  {
1006  cVector3d texCoord(0.0, 0.0, 0.0);
1007  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1008  {
1009  m_texCoord.resize(m_numVertices, texCoord);
1010  }
1011  else
1012  {
1013  m_texCoord.push_back(texCoord);
1014  }
1015  m_flagTexCoordData = true;
1016  }
1017  else
1018  {
1019  m_texCoord.clear();
1020  }
1021 
1022  // update color data allocation
1023  m_useColorData = a_useColorData;
1024  if (m_useColorData)
1025  {
1026  cColorf color(0.0, 0.0, 0.0, 1.0);
1027  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1028  {
1029  m_color.resize(m_numVertices, color);
1030  }
1031  else
1032  {
1033  m_color.push_back(color);
1034  }
1035  m_flagColorData = true;
1036  }
1037  else
1038  {
1039  m_color.clear();
1040  }
1041 
1042  // update tangent data allocation
1043  m_useTangentData = a_useTangentData;
1044  if (m_useTangentData)
1045  {
1046  cVector3d tangent(1.0, 0.0, 0.0);
1047  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1048  {
1049  m_tangent.resize(m_numVertices, tangent);
1050  }
1051  else
1052  {
1053  m_tangent.push_back(tangent);
1054  }
1055  m_flagTangentData = true;
1056  }
1057  else
1058  {
1059  m_tangent.clear();
1060  }
1061 
1062  // update bitangent data allocation
1063  m_useBitangentData = a_useBitangentData;
1064  if (m_useBitangentData)
1065  {
1066  cVector3d bitangent(0.0, 1.0, 0.0);
1067  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1068  {
1069  m_bitangent.resize(m_numVertices, bitangent);
1070  }
1071  else
1072  {
1073  m_bitangent.push_back(bitangent);
1074  }
1075  m_flagBitangentData = true;
1076  }
1077  else
1078  {
1079  m_bitangent.clear();
1080  }
1081 
1082  // update user data allocation
1083  m_useUserData = a_useUserData;
1084  if (m_useUserData)
1085  {
1086  int data = 0;
1087  if ((a_numberOfVertices > 1) || (a_numberOfVertices == 0))
1088  {
1089  m_userData.resize(m_numVertices, data);
1090  }
1091  else
1092  {
1093  m_userData.push_back(data);
1094  }
1095  m_flagUserData = true;
1096  }
1097  else
1098  {
1099  m_userData.clear();
1100  }
1101 
1102  // update buffer size
1103  m_flagBufferResize = true;
1104  }
1105 
1106 
1107  //--------------------------------------------------------------------------
1108  // PUBLIC MEMBERS:
1109  //--------------------------------------------------------------------------
1110 
1111 public:
1112 
1114  std::vector<cVector3d> m_localPos;
1115 
1117  std::vector<cVector3d> m_globalPos;
1118 
1120  std::vector<cVector3d> m_normal;
1121 
1123  std::vector<cVector3d> m_texCoord;
1124 
1126  std::vector<cColorf> m_color;
1127 
1129  std::vector<cVector3d> m_tangent;
1130 
1132  std::vector<cVector3d> m_bitangent;
1133 
1135  std::vector<int> m_userData;
1136 
1137 
1138  //--------------------------------------------------------------------------
1139  // PROTECTED MEMBERS:
1140  //--------------------------------------------------------------------------
1141 
1142 protected:
1143 
1145  unsigned int m_numVertices;
1146 
1149 
1152 
1155 
1158 
1161 
1164 
1165 
1166  //--------------------------------------------------------------------------
1167  // PUBLIC MEMBERS:
1168  //--------------------------------------------------------------------------
1169 
1170 public:
1171 
1174 
1177 
1180 
1183 
1186 
1189 
1192 
1195 
1196 
1197  //--------------------------------------------------------------------------
1198  // PUBLIC MEMBERS: (OPENGL)
1199  //--------------------------------------------------------------------------
1200 
1201 public:
1202 
1205 
1208 
1211 
1214 
1217 
1220 };
1221 
1222 //------------------------------------------------------------------------------
1223 } // namespace chai3d
1224 //------------------------------------------------------------------------------
1225 
1226 //------------------------------------------------------------------------------
1227 #endif
1228 //------------------------------------------------------------------------------
This class implements a 3D vector.
Definition: CVector3d.h:85
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:1160
std::vector< int > m_userData
User data of vertices.
Definition: CVertexArray.h:1135
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:1132
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:1151
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:1126
This class implements array of 3D vertices.
Definition: CVertexArray.h:133
std::vector< cVector3d > m_tangent
Surface tangent of vertices.
Definition: CVertexArray.h:1129
void render()
Definition: CVertexArray.h:771
bool m_flagBufferResize
If true, then data buffer need to be updated in size.
Definition: CVertexArray.h:1194
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:966
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:1219
bool m_useBitangentData
Definition: CVertexArray.h:102
bool m_flagColorData
If true then vertex color data has been modified.
Definition: CVertexArray.h:1182
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:1185
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:1120
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:1176
bool m_useColorData
If true then vertex color data will be allocated for each new vertex.
Definition: CVertexArray.h:1154
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:1191
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:1216
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:1210
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:1117
std::vector< cVector3d > m_localPos
Local position of vertices.
Definition: CVertexArray.h:1114
#define C_VB_TANGENT
Definition: CShader.h:76
GLuint m_positionBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1204
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:1157
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
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:1173
bool m_flagTexCoordData
If true then texture coordinate data has been modified.
Definition: CVertexArray.h:1179
bool m_useNormalData
If true then normal data will be allocated for each new vertex.
Definition: CVertexArray.h:1148
#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:1188
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:1123
Definition: CAudioBuffer.cpp:56
GLuint m_colorBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1213
GLuint m_normalBuffer
OpenGL Buffer for storing triangle indices.
Definition: CVertexArray.h:1207
#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
unsigned int m_numVertices
Number of vertices.
Definition: CVertexArray.h:1145
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:1163
cVector3d getNormal(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:448
cVector3d getTangent(const unsigned int a_vertexIndex) const
Definition: CVertexArray.h:597

CHAI3D 3.0.0 documentation
Please address any questions to support@chai3d.org
(C) 2003-2015 - CHAI3D
All Rights Reserved.