CVertexBuffer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include <vector>
  6. #include "IVertexBuffer.h"
  7. // Define to receive warnings when violating the hw mapping hints
  8. //#define VERTEXBUFFER_HINT_DEBUG
  9. #ifdef VERTEXBUFFER_HINT_DEBUG
  10. #include "../src/os.h"
  11. #endif
  12. namespace irr
  13. {
  14. namespace scene
  15. {
  16. //! Template implementation of the IVertexBuffer interface
  17. template <class T>
  18. class CVertexBuffer final : public IVertexBuffer
  19. {
  20. public:
  21. //! Default constructor for empty buffer
  22. CVertexBuffer()
  23. {
  24. #ifdef _DEBUG
  25. setDebugName("CVertexBuffer");
  26. #endif
  27. }
  28. const void *getData() const override
  29. {
  30. return Data.data();
  31. }
  32. void *getData() override
  33. {
  34. return Data.data();
  35. }
  36. u32 getCount() const override
  37. {
  38. return static_cast<u32>(Data.size());
  39. }
  40. video::E_VERTEX_TYPE getType() const override
  41. {
  42. return T::getType();
  43. }
  44. const core::vector3df &getPosition(u32 i) const override
  45. {
  46. return Data[i].Pos;
  47. }
  48. core::vector3df &getPosition(u32 i) override
  49. {
  50. return Data[i].Pos;
  51. }
  52. const core::vector3df &getNormal(u32 i) const override
  53. {
  54. return Data[i].Normal;
  55. }
  56. core::vector3df &getNormal(u32 i) override
  57. {
  58. return Data[i].Normal;
  59. }
  60. const core::vector2df &getTCoords(u32 i) const override
  61. {
  62. return Data[i].TCoords;
  63. }
  64. core::vector2df &getTCoords(u32 i) override
  65. {
  66. return Data[i].TCoords;
  67. }
  68. E_HARDWARE_MAPPING getHardwareMappingHint() const override
  69. {
  70. return MappingHint;
  71. }
  72. void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint) override
  73. {
  74. MappingHint = NewMappingHint;
  75. }
  76. void setDirty() override
  77. {
  78. ++ChangedID;
  79. #ifdef VERTEXBUFFER_HINT_DEBUG
  80. if (MappingHint == EHM_STATIC && HWBuffer) {
  81. char buf[100];
  82. snprintf_irr(buf, sizeof(buf), "CVertexBuffer @ %p modified, but it has a static hint", this);
  83. os::Printer::log(buf, ELL_WARNING);
  84. }
  85. #endif
  86. }
  87. u32 getChangedID() const override { return ChangedID; }
  88. void setHWBuffer(void *ptr) const override
  89. {
  90. HWBuffer = ptr;
  91. }
  92. void *getHWBuffer() const override
  93. {
  94. return HWBuffer;
  95. }
  96. u32 ChangedID = 1;
  97. //! hardware mapping hint
  98. E_HARDWARE_MAPPING MappingHint = EHM_NEVER;
  99. mutable void *HWBuffer = nullptr;
  100. //! Vertices of this buffer
  101. std::vector<T> Data;
  102. };
  103. //! Standard buffer
  104. typedef CVertexBuffer<video::S3DVertex> SVertexBuffer;
  105. //! Buffer with two texture coords per vertex, e.g. for lightmaps
  106. typedef CVertexBuffer<video::S3DVertex2TCoords> SVertexBufferLightMap;
  107. //! Buffer with vertices having tangents stored, e.g. for normal mapping
  108. typedef CVertexBuffer<video::S3DVertexTangents> SVertexBufferTangents;
  109. } // end namespace scene
  110. } // end namespace irr