2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_RENDER_H)
23 #define INCLUDED_RENDER_H
26 /// \brief High-level constructs for efficient OpenGL rendering.
31 #include "container/array.h"
32 #include "math/vector.h"
37 typedef unsigned int RenderIndex;
38 const GLenum RenderIndexTypeID = GL_UNSIGNED_INT;
40 /// \brief A resizable buffer of indices.
43 typedef std::vector<RenderIndex> Indices;
46 typedef Indices::iterator iterator;
47 typedef Indices::const_iterator const_iterator;
51 return m_data.begin();
53 const_iterator begin() const
55 return m_data.begin();
61 const_iterator end() const
68 return m_data.empty();
70 std::size_t size() const
74 const RenderIndex* data() const
76 return &(*m_data.begin());
78 RenderIndex& operator[](std::size_t index)
82 const RenderIndex& operator[](std::size_t index) const
90 void reserve(std::size_t max_indices)
92 m_data.reserve(max_indices);
94 void insert(RenderIndex index)
96 m_data.push_back(index);
98 void swap(IndexBuffer& other)
100 std::swap(m_data, m_data);
106 /// \brief Swaps the values of \p self and \p other.
107 /// Overloads std::swap.
108 inline void swap(IndexBuffer& self, IndexBuffer& other)
114 /// \brief A resizable buffer of vertices.
115 /// \param Vertex The vertex data type.
116 template<typename Vertex>
119 typedef typename std::vector<Vertex> Vertices;
122 typedef typename Vertices::iterator iterator;
123 typedef typename Vertices::const_iterator const_iterator;
127 return m_data.begin();
133 const_iterator begin() const
135 return m_data.begin();
137 const_iterator end() const
144 return m_data.empty();
146 RenderIndex size() const
148 return RenderIndex(m_data.size());
150 const Vertex* data() const
152 return &(*m_data.begin());
154 Vertex& operator[](std::size_t index)
156 return m_data[index];
158 const Vertex& operator[](std::size_t index) const
160 return m_data[index];
167 void reserve(std::size_t max_vertices)
169 m_data.reserve(max_vertices);
171 void push_back(const Vertex& vertex)
173 m_data.push_back(vertex);
177 /// \brief A wrapper around a VertexBuffer which inserts only vertices which have not already been inserted.
178 /// \param Vertex The vertex data type. Must support operator<, operator== and operator!=.
179 /// For best performance, quantise vertices before inserting them.
180 template<typename Vertex>
181 class UniqueVertexBuffer
183 typedef VertexBuffer<Vertex> Vertices;
189 : m_left(0), m_right(0)
196 std::vector<bnode> m_btree;
201 const RenderIndex find_or_insert(const Vertex& vertex)
203 RenderIndex index = 0;
207 if(vertex < m_data[index])
209 bnode& node = m_btree[index];
217 node.m_left = RenderIndex(m_btree.size());
218 m_btree.push_back(bnode());
219 m_data.push_back(vertex);
220 return RenderIndex(m_btree.size()-1);
223 if(m_data[index] < vertex)
225 bnode& node = m_btree[index];
226 if(node.m_right != 0)
228 index = node.m_right;
233 node.m_right = RenderIndex(m_btree.size());
234 m_btree.push_back(bnode());
235 m_data.push_back(vertex);
236 return RenderIndex(m_btree.size()-1);
244 UniqueVertexBuffer(Vertices& data)
245 : m_data(data), m_prev0(0), m_prev1(0), m_prev2(0)
249 typedef typename Vertices::const_iterator iterator;
251 iterator begin() const
253 return m_data.begin();
260 std::size_t size() const
262 return m_data.size();
264 const Vertex* data() const
266 return &(*m_data.begin());
268 Vertex& operator[](std::size_t index)
270 return m_data[index];
272 const Vertex& operator[](std::size_t index) const
274 return m_data[index];
285 void reserve(std::size_t max_vertices)
287 m_data.reserve(max_vertices);
288 m_btree.reserve(max_vertices);
290 /// \brief Returns the index of the element equal to \p vertex.
291 RenderIndex insert(const Vertex& vertex)
295 m_data.push_back(vertex);
296 m_btree.push_back(bnode());
300 if(m_data[m_prev0] == vertex)
302 if(m_prev1 != m_prev0 && m_data[m_prev1] == vertex)
304 if(m_prev2 != m_prev0 && m_prev2 != m_prev1 && m_data[m_prev2] == vertex)
309 m_prev0 = find_or_insert(vertex);
316 /// \brief A 4-byte colour.
319 unsigned char r, g, b, a;
325 Colour4b(unsigned char _r, unsigned char _g, unsigned char _b, unsigned char _a)
326 : r(_r), g(_g), b(_b), a(_a)
331 inline bool operator<(const Colour4b& self, const Colour4b& other)
333 if(self.r != other.r)
335 return self.r < other.r;
337 if(self.g != other.g)
339 return self.g < other.g;
341 if(self.b != other.b)
343 return self.b < other.b;
345 if(self.a != other.a)
347 return self.a < other.a;
352 inline bool operator==(const Colour4b& self, const Colour4b& other)
354 return self.r == other.r && self.g == other.g && self.b == other.b && self.a == other.a;
357 inline bool operator!=(const Colour4b& self, const Colour4b& other)
359 return !operator==(self, other);
362 /// \brief A 3-float vertex.
363 struct Vertex3f : public Vector3
369 Vertex3f(float _x, float _y, float _z)
370 : Vector3(_x, _y, _z)
375 inline bool operator<(const Vertex3f& self, const Vertex3f& other)
377 if(self.x() != other.x())
379 return self.x() < other.x();
381 if(self.y() != other.y())
383 return self.y() < other.y();
385 if(self.z() != other.z())
387 return self.z() < other.z();
392 inline bool operator==(const Vertex3f& self, const Vertex3f& other)
394 return self.x() == other.x() && self.y() == other.y() && self.z() == other.z();
397 inline bool operator!=(const Vertex3f& self, const Vertex3f& other)
399 return !operator==(self, other);
403 inline Vertex3f vertex3f_from_array(const float* array)
405 return Vertex3f(array[0], array[1], array[2]);
408 inline float* vertex3f_to_array(Vertex3f& vertex)
410 return reinterpret_cast<float*>(&vertex);
413 inline const float* vertex3f_to_array(const Vertex3f& vertex)
415 return reinterpret_cast<const float*>(&vertex);
418 const Vertex3f vertex3f_identity(0, 0, 0);
420 inline Vertex3f vertex3f_for_vector3(const Vector3& vector3)
422 return Vertex3f(vector3.x(), vector3.y(), vector3.z());
425 inline const Vector3& vertex3f_to_vector3(const Vertex3f& vertex)
430 inline Vector3& vertex3f_to_vector3(Vertex3f& vertex)
436 /// \brief A 3-float normal.
437 struct Normal3f : public Vector3
443 Normal3f(float _x, float _y, float _z)
444 : Vector3(_x, _y, _z)
449 inline bool operator<(const Normal3f& self, const Normal3f& other)
451 if(self.x() != other.x())
453 return self.x() < other.x();
455 if(self.y() != other.y())
457 return self.y() < other.y();
459 if(self.z() != other.z())
461 return self.z() < other.z();
466 inline bool operator==(const Normal3f& self, const Normal3f& other)
468 return self.x() == other.x() && self.y() == other.y() && self.z() == other.z();
471 inline bool operator!=(const Normal3f& self, const Normal3f& other)
473 return !operator==(self, other);
477 inline Normal3f normal3f_from_array(const float* array)
479 return Normal3f(array[0], array[1], array[2]);
482 inline float* normal3f_to_array(Normal3f& normal)
484 return reinterpret_cast<float*>(&normal);
487 inline const float* normal3f_to_array(const Normal3f& normal)
489 return reinterpret_cast<const float*>(&normal);
492 inline Normal3f normal3f_for_vector3(const Vector3& vector3)
494 return Normal3f(vector3.x(), vector3.y(), vector3.z());
497 inline const Vector3& normal3f_to_vector3(const Normal3f& normal)
502 inline Vector3& normal3f_to_vector3(Normal3f& normal)
508 /// \brief A 2-float texture-coordinate set.
509 struct TexCoord2f : public Vector2
515 TexCoord2f(float _s, float _t)
524 const float& s() const
532 const float& t() const
538 inline bool operator<(const TexCoord2f& self, const TexCoord2f& other)
540 if(self.s() != other.s())
542 return self.s() < other.s();
544 if(self.t() != other.t())
546 return self.t() < other.t();
551 inline bool operator==(const TexCoord2f& self, const TexCoord2f& other)
553 return self.s() == other.s() && self.t() == other.t();
556 inline bool operator!=(const TexCoord2f& self, const TexCoord2f& other)
558 return !operator==(self, other);
562 inline float* texcoord2f_to_array(TexCoord2f& texcoord)
564 return reinterpret_cast<float*>(&texcoord);
567 inline const float* texcoord2f_to_array(const TexCoord2f& texcoord)
569 return reinterpret_cast<const float*>(&texcoord);
572 inline const TexCoord2f& texcoord2f_from_array(const float* array)
574 return *reinterpret_cast<const TexCoord2f*>(array);
577 inline TexCoord2f texcoord2f_for_vector2(const Vector2& vector2)
579 return TexCoord2f(vector2.x(), vector2.y());
582 inline const Vector2& texcoord2f_to_vector2(const TexCoord2f& vertex)
587 inline Vector2& texcoord2f_to_vector2(TexCoord2f& vertex)
592 /// \brief Returns \p normal rescaled to be unit-length.
593 inline Normal3f normal3f_normalised(const Normal3f& normal)
595 return normal3f_for_vector3(vector3_normalised(normal3f_to_vector3(normal)));
598 enum UnitSphereOctant
600 UNITSPHEREOCTANT_000 = 0 << 0 | 0 << 1 | 0 << 2,
601 UNITSPHEREOCTANT_001 = 0 << 0 | 0 << 1 | 1 << 2,
602 UNITSPHEREOCTANT_010 = 0 << 0 | 1 << 1 | 0 << 2,
603 UNITSPHEREOCTANT_011 = 0 << 0 | 1 << 1 | 1 << 2,
604 UNITSPHEREOCTANT_100 = 1 << 0 | 0 << 1 | 0 << 2,
605 UNITSPHEREOCTANT_101 = 1 << 0 | 0 << 1 | 1 << 2,
606 UNITSPHEREOCTANT_110 = 1 << 0 | 1 << 1 | 0 << 2,
607 UNITSPHEREOCTANT_111 = 1 << 0 | 1 << 1 | 1 << 2,
610 /// \brief Returns the octant for \p normal indicating the sign of the region of unit-sphere space it lies within.
611 inline UnitSphereOctant normal3f_classify_octant(const Normal3f& normal)
613 return static_cast<UnitSphereOctant>(
614 ((normal.x() > 0) << 0) | ((normal.y() > 0) << 1) | ((normal.z() > 0) << 2)
618 /// \brief Returns \p normal with its components signs made positive based on \p octant.
619 inline Normal3f normal3f_fold_octant(const Normal3f& normal, UnitSphereOctant octant)
623 case UNITSPHEREOCTANT_000:
624 return Normal3f(-normal.x(), -normal.y(), -normal.z());
625 case UNITSPHEREOCTANT_001:
626 return Normal3f(normal.x(), -normal.y(), -normal.z());
627 case UNITSPHEREOCTANT_010:
628 return Normal3f(-normal.x(), normal.y(), -normal.z());
629 case UNITSPHEREOCTANT_011:
630 return Normal3f(normal.x(), normal.y(), -normal.z());
631 case UNITSPHEREOCTANT_100:
632 return Normal3f(-normal.x(), -normal.y(), normal.z());
633 case UNITSPHEREOCTANT_101:
634 return Normal3f(normal.x(), -normal.y(), normal.z());
635 case UNITSPHEREOCTANT_110:
636 return Normal3f(-normal.x(), normal.y(), normal.z());
637 case UNITSPHEREOCTANT_111:
638 return Normal3f(normal.x(), normal.y(), normal.z());
643 /// \brief Reverses the effect of normal3f_fold_octant() on \p normal with \p octant.
644 /// \p normal must have been obtained with normal3f_fold_octant().
645 /// \p octant must have been obtained with normal3f_classify_octant().
646 inline Normal3f normal3f_unfold_octant(const Normal3f& normal, UnitSphereOctant octant)
648 return normal3f_fold_octant(normal, octant);
651 enum UnitSphereSextant
653 UNITSPHERESEXTANT_XYZ = 0,
654 UNITSPHERESEXTANT_XZY = 1,
655 UNITSPHERESEXTANT_YXZ = 2,
656 UNITSPHERESEXTANT_YZX = 3,
657 UNITSPHERESEXTANT_ZXY = 4,
658 UNITSPHERESEXTANT_ZYX = 5,
661 /// \brief Returns the sextant for \p normal indicating how to sort its components so that x > y > z.
662 /// All components of \p normal must be positive.
663 /// \p normal must be normalised.
664 inline UnitSphereSextant normal3f_classify_sextant(const Normal3f& normal)
667 normal.x() >= normal.y()
668 ? normal.x() >= normal.z()
669 ? normal.y() >= normal.z()
670 ? UNITSPHERESEXTANT_XYZ
671 : UNITSPHERESEXTANT_XZY
672 : UNITSPHERESEXTANT_ZXY
673 : normal.y() >= normal.z()
674 ? normal.x() >= normal.z()
675 ? UNITSPHERESEXTANT_YXZ
676 : UNITSPHERESEXTANT_YZX
677 : UNITSPHERESEXTANT_ZYX;
680 /// \brief Returns \p normal with its components sorted so that x > y > z based on \p sextant.
681 /// All components of \p normal must be positive.
682 /// \p normal must be normalised.
683 inline Normal3f normal3f_fold_sextant(const Normal3f& normal, UnitSphereSextant sextant)
687 case UNITSPHERESEXTANT_XYZ:
688 return Normal3f(normal.x(), normal.y(), normal.z());
689 case UNITSPHERESEXTANT_XZY:
690 return Normal3f(normal.x(), normal.z(), normal.y());
691 case UNITSPHERESEXTANT_YXZ:
692 return Normal3f(normal.y(), normal.x(), normal.z());
693 case UNITSPHERESEXTANT_YZX:
694 return Normal3f(normal.y(), normal.z(), normal.x());
695 case UNITSPHERESEXTANT_ZXY:
696 return Normal3f(normal.z(), normal.x(), normal.y());
697 case UNITSPHERESEXTANT_ZYX:
698 return Normal3f(normal.z(), normal.y(), normal.x());
703 /// \brief Reverses the effect of normal3f_fold_sextant() on \p normal with \p sextant.
704 /// \p normal must have been obtained with normal3f_fold_sextant().
705 /// \p sextant must have been obtained with normal3f_classify_sextant().
706 inline Normal3f normal3f_unfold_sextant(const Normal3f& normal, UnitSphereSextant sextant)
708 return normal3f_fold_sextant(normal, sextant);
711 const std::size_t c_quantise_normal = 1 << 6;
713 /// \brief All the components of \p folded must be positive and sorted so that x > y > z.
714 inline Normal3f normal3f_folded_quantised(const Normal3f& folded)
717 double scale = static_cast<float>(c_quantise_normal) / (folded.x() + folded.y() + folded.z());
718 unsigned int zbits = static_cast<unsigned int>(folded.z() * scale);
719 unsigned int ybits = static_cast<unsigned int>(folded.y() * scale);
722 return normal3f_normalised(Normal3f(
723 static_cast<float>(c_quantise_normal - zbits - ybits),
724 static_cast<float>(ybits),
725 static_cast<float>(zbits)
729 /// \brief Returns \p normal quantised by compressing and then decompressing its representation.
730 inline Normal3f normal3f_quantised_custom(const Normal3f& normal)
732 UnitSphereOctant octant = normal3f_classify_octant(normal);
733 Normal3f folded = normal3f_fold_octant(normal, octant);
734 UnitSphereSextant sextant = normal3f_classify_sextant(folded);
735 folded = normal3f_fold_sextant(folded, sextant);
736 return normal3f_unfold_octant(normal3f_unfold_sextant(normal3f_folded_quantised(folded), sextant), octant);
743 double longditude, latitude;
745 spherical_t(double _longditude, double _latitude)
746 : longditude(_longditude), latitude(_latitude)
754 phi = acos((2 * V) - 1);
757 V = (cos(phi) + 1) / 2;
760 longitude = atan(y / x);
763 struct uniformspherical_t
767 uniformspherical_t(double U_, double V_)
774 inline spherical_t spherical_from_normal3f(const Normal3f& normal)
776 return spherical_t(normal.x() == 0 ? c_pi / 2 : normal.x() > 0 ? atan(normal.y() / normal.x()) : atan(normal.y() / normal.x()) + c_pi, acos(normal.z()));
779 inline Normal3f normal3f_from_spherical(const spherical_t& spherical)
782 static_cast<float>(cos(spherical.longditude) * sin(spherical.latitude)),
783 static_cast<float>(sin(spherical.longditude) * sin(spherical.latitude)),
784 static_cast<float>(cos(spherical.latitude))
788 inline uniformspherical_t uniformspherical_from_spherical(const spherical_t& spherical)
790 return uniformspherical_t(spherical.longditude * c_inv_2pi, (cos(spherical.latitude) + 1) * 0.5);
793 inline spherical_t spherical_from_uniformspherical(const uniformspherical_t& uniformspherical)
795 return spherical_t(c_2pi * uniformspherical.U, acos((2 * uniformspherical.V) - 1));
798 inline uniformspherical_t uniformspherical_from_normal3f(const Normal3f& normal)
800 return uniformspherical_from_spherical(spherical_from_normal3f(normal));
801 //return uniformspherical_t(atan2(normal.y / normal.x) * c_inv_2pi, (normal.z + 1) * 0.5);
804 inline Normal3f normal3f_from_uniformspherical(const uniformspherical_t& uniformspherical)
806 return normal3f_from_spherical(spherical_from_uniformspherical(uniformspherical));
809 /// \brief Returns a single-precision \p component quantised to \p precision.
810 inline float float_quantise(float component, float precision)
812 return float_snapped(component, precision);
815 /// \brief Returns a double-precision \p component quantised to \p precision.
816 inline double double_quantise(double component, double precision)
818 return float_snapped(component, precision);
821 inline spherical_t spherical_quantised(const spherical_t& spherical, float snap)
823 return spherical_t(double_quantise(spherical.longditude, snap), double_quantise(spherical.latitude, snap));
826 inline uniformspherical_t uniformspherical_quantised(const uniformspherical_t& uniformspherical, float snap)
828 return uniformspherical_t(double_quantise(uniformspherical.U, snap), double_quantise(uniformspherical.V, snap));
831 /// \brief Returns a \p vertex quantised to \p precision.
832 inline Vertex3f vertex3f_quantised(const Vertex3f& vertex, float precision)
834 return Vertex3f(float_quantise(vertex.x(), precision), float_quantise(vertex.y(), precision), float_quantise(vertex.z(), precision));
837 /// \brief Returns a \p normal quantised to a fixed precision.
838 inline Normal3f normal3f_quantised(const Normal3f& normal)
840 return normal3f_quantised_custom(normal);
841 //return normal3f_from_spherical(spherical_quantised(spherical_from_normal3f(normal), snap));
842 //return normal3f_from_uniformspherical(uniformspherical_quantised(uniformspherical_from_normal3f(normal), snap));
843 // float_quantise(normal.x, snap), float_quantise(normal.y, snap), float_quantise(normal.y, snap));
846 /// \brief Returns a \p texcoord quantised to \p precision.
847 inline TexCoord2f texcoord2f_quantised(const TexCoord2f& texcoord, float precision)
849 return TexCoord2f(float_quantise(texcoord.s(), precision), float_quantise(texcoord.t(), precision));
852 /// \brief Standard vertex type for lines and points.
861 PointVertex(Vertex3f _vertex)
862 : colour(Colour4b(255, 255, 255, 255)), vertex(_vertex)
865 PointVertex(Vertex3f _vertex, Colour4b _colour)
866 : colour(_colour), vertex(_vertex)
871 inline bool operator<(const PointVertex& self, const PointVertex& other)
873 if(self.vertex != other.vertex)
875 return self.vertex < other.vertex;
877 if(self.colour != other.colour)
879 return self.colour < other.colour;
884 inline bool operator==(const PointVertex& self, const PointVertex& other)
886 return self.colour == other.colour && self.vertex == other.vertex;
889 inline bool operator!=(const PointVertex& self, const PointVertex& other)
891 return !operator==(self, other);
894 /// \brief Standard vertex type for lit/textured meshes.
895 struct ArbitraryMeshVertex
903 ArbitraryMeshVertex() : tangent(0, 0, 0), bitangent(0, 0, 0)
906 ArbitraryMeshVertex(Vertex3f _vertex, Normal3f _normal, TexCoord2f _texcoord)
907 : texcoord(_texcoord), normal(_normal), vertex(_vertex), tangent(0, 0, 0), bitangent(0, 0, 0)
912 inline bool operator<(const ArbitraryMeshVertex& self, const ArbitraryMeshVertex& other)
914 if(self.texcoord != other.texcoord)
916 return self.texcoord < other.texcoord;
918 if(self.normal != other.normal)
920 return self.normal < other.normal;
922 if(self.vertex != other.vertex)
924 return self.vertex < other.vertex;
929 inline bool operator==(const ArbitraryMeshVertex& self, const ArbitraryMeshVertex& other)
931 return self.texcoord == other.texcoord && self.normal == other.normal && self.vertex == other.vertex;
934 inline bool operator!=(const ArbitraryMeshVertex& self, const ArbitraryMeshVertex& other)
936 return !operator==(self, other);
939 const float c_quantise_vertex = 1.f / static_cast<float>(1 << 3);
941 /// \brief Returns \p v with vertex quantised to a fixed precision.
942 inline PointVertex pointvertex_quantised(const PointVertex& v)
944 return PointVertex(vertex3f_quantised(v.vertex, c_quantise_vertex), v.colour);
947 const float c_quantise_texcoord = 1.f / static_cast<float>(1 << 8);
949 /// \brief Returns \p v with vertex, normal and texcoord quantised to a fixed precision.
950 inline ArbitraryMeshVertex arbitrarymeshvertex_quantised(const ArbitraryMeshVertex& v)
952 return ArbitraryMeshVertex(vertex3f_quantised(v.vertex, c_quantise_vertex), normal3f_quantised(v.normal), texcoord2f_quantised(v.texcoord, c_quantise_texcoord));
956 /// \brief Sets up the OpenGL colour and vertex arrays for \p array.
957 inline void pointvertex_gl_array(const PointVertex* array)
959 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &array->colour);
960 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &array->vertex);
963 class RenderablePointArray : public OpenGLRenderable
965 const Array<PointVertex>& m_array;
968 RenderablePointArray(const Array<PointVertex>& array, GLenum mode)
969 : m_array(array), m_mode(mode)
972 void render(RenderStateFlags state) const
974 #define NV_DRIVER_BUG 1
976 glColorPointer(4, GL_UNSIGNED_BYTE, 0, 0);
977 glVertexPointer(3, GL_FLOAT, 0, 0);
978 glDrawArrays(GL_TRIANGLE_FAN, 0, 0);
980 pointvertex_gl_array(m_array.data());
981 glDrawArrays(m_mode, 0, GLsizei(m_array.size()));
985 class RenderablePointVector : public OpenGLRenderable
987 std::vector<PointVertex> m_vector;
990 RenderablePointVector(GLenum mode)
995 void render(RenderStateFlags state) const
997 pointvertex_gl_array(&m_vector.front());
998 glDrawArrays(m_mode, 0, GLsizei(m_vector.size()));
1001 std::size_t size() const
1003 return m_vector.size();
1007 return m_vector.empty();
1013 void reserve(std::size_t size)
1015 m_vector.reserve(size);
1017 void push_back(const PointVertex& point)
1019 m_vector.push_back(point);
1024 class RenderableVertexBuffer : public OpenGLRenderable
1026 const GLenum m_mode;
1027 const VertexBuffer<PointVertex>& m_vertices;
1029 RenderableVertexBuffer(GLenum mode, const VertexBuffer<PointVertex>& vertices)
1030 : m_mode(mode), m_vertices(vertices)
1034 void render(RenderStateFlags state) const
1036 pointvertex_gl_array(m_vertices.data());
1037 glDrawArrays(m_mode, 0, m_vertices.size());
1041 class RenderableIndexBuffer : public OpenGLRenderable
1043 const GLenum m_mode;
1044 const IndexBuffer& m_indices;
1045 const VertexBuffer<PointVertex>& m_vertices;
1047 RenderableIndexBuffer(GLenum mode, const IndexBuffer& indices, const VertexBuffer<PointVertex>& vertices)
1048 : m_mode(mode), m_indices(indices), m_vertices(vertices)
1052 void render(RenderStateFlags state) const
1055 pointvertex_gl_array(m_vertices.data());
1056 glDrawElements(m_mode, GLsizei(m_indices.size()), RenderIndexTypeID, m_indices.data());
1059 if(state & RENDER_COLOURARRAY != 0)
1061 for(std::size_t i = 0; i < m_indices.size(); ++i)
1063 glColor4ubv(&m_vertices[m_indices[i]].colour.r);
1064 glVertex3fv(&m_vertices[m_indices[i]].vertex.x);
1069 for(std::size_t i = 0; i < m_indices.size(); ++i)
1071 glVertex3fv(&m_vertices[m_indices[i]].vertex.x);
1083 static void set(Vertex3f& vertex, float x, float y, float z)
1094 static void set(Vertex3f& vertex, float x, float y, float z)
1105 static void set(Vertex3f& vertex, float x, float y, float z)
1113 template<typename remap_policy>
1114 inline void draw_circle(const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap)
1116 const double increment = c_pi / double(segments << 2);
1118 std::size_t count = 0;
1121 while(count < segments)
1123 PointVertex* i = vertices + count;
1124 PointVertex* j = vertices + ((segments << 1) - (count + 1));
1126 PointVertex* k = i + (segments << 1);
1127 PointVertex* l = j + (segments << 1);
1129 PointVertex* m = i + (segments << 2);
1130 PointVertex* n = j + (segments << 2);
1131 PointVertex* o = k + (segments << 2);
1132 PointVertex* p = l + (segments << 2);
1134 remap_policy::set(i->vertex, x,-y, 0);
1135 remap_policy::set(k->vertex,-y,-x, 0);
1136 remap_policy::set(m->vertex,-x, y, 0);
1137 remap_policy::set(o->vertex, y, x, 0);
1142 const double theta = increment * count;
1143 x = static_cast<float>(radius * cos(theta));
1144 y = static_cast<float>(radius * sin(theta));
1147 remap_policy::set(j->vertex, y,-x, 0);
1148 remap_policy::set(l->vertex,-x,-y, 0);
1149 remap_policy::set(n->vertex,-y, x, 0);
1150 remap_policy::set(p->vertex, x, y, 0);
1155 class PointVertexArrayIterator
1157 PointVertex* m_point;
1159 PointVertexArrayIterator(PointVertex* point)
1163 PointVertexArrayIterator& operator++()
1168 PointVertexArrayIterator operator++(int)
1170 PointVertexArrayIterator tmp(*this);
1174 Vertex3f& operator*()
1176 return m_point.vertex;
1178 Vertex3f* operator->()
1180 return &(operator*());
1184 template<typename remap_policy, typename iterator_type
1185 inline void draw_circle(const std::size_t segments, const float radius, iterator_type start, remap_policy remap)
1187 const float increment = c_pi / (double)(segments << 2);
1189 std::size_t count = 0;
1190 iterator_type pxpy(start);
1191 iterator_type pypx(pxpy + (segments << 1));
1192 iterator_type pynx(pxpy + (segments << 1));
1193 iterator_type nxpy(pypx + (segments << 1));
1194 iterator_type nxny(pypx + (segments << 1));
1195 iterator_type nynx(nxpy + (segments << 1));
1196 iterator_type nypx(nxpy + (segments << 1));
1197 iterator_type pxny(start);
1198 while(count < segments)
1200 const float theta = increment * count;
1201 const float x = radius * cos(theta);
1202 const float y = radius * sin(theta);
1204 remap_policy::set((*pxpy), x, y, 0);
1205 remap_policy::set((*pxny), x,-y, 0);
1206 remap_policy::set((*nxpy),-x, y, 0);
1207 remap_policy::set((*nxny),-x,-y, 0);
1209 remap_policy::set((*pypx), y, x, 0);
1210 remap_policy::set((*pynx), y,-x, 0);
1211 remap_policy::set((*nypx),-y, x, 0);
1212 remap_policy::set((*nynx),-y,-x, 0);
1216 template<typename remap_policy, typename iterator_type
1217 inline void draw_semicircle(const std::size_t segments, const float radius, iterator_type start, remap_policy remap)
1219 const float increment = c_pi / (double)(segments << 2);
1221 std::size_t count = 0;
1222 iterator_type pxpy(start);
1223 iterator_type pypx(pxpy + (segments << 1));
1224 iterator_type pynx(pxpy + (segments << 1));
1225 iterator_type nxpy(pypx + (segments << 1));
1226 iterator_type nxny(pypx + (segments << 1));
1227 iterator_type nynx(nxpy + (segments << 1));
1228 iterator_type nypx(nxpy + (segments << 1));
1229 iterator_type pxny(start);
1230 while(count < segments)
1232 const float theta = increment * count;
1233 const float x = radius * cos(theta);
1234 const float y = radius * sin(theta);
1236 remap_policy::set((*pxpy), x, y, 0);
1237 remap_policy::set((*pxny), x,-y, 0);
1238 remap_policy::set((*nxpy),-x, y, 0);
1239 remap_policy::set((*nxny),-x,-y, 0);
1241 //remap_policy::set((*pypx), y, x, 0);
1242 //remap_policy::set((*pynx), y,-x, 0);
1243 //remap_policy::set((*nypx),-y, x, 0);
1244 //remap_policy::set((*nynx),-y,-x, 0);
1251 inline void draw_quad(const float radius, PointVertex* quad)
1253 (*quad++).vertex = Vertex3f(-radius, radius, 0);
1254 (*quad++).vertex = Vertex3f(radius, radius, 0);
1255 (*quad++).vertex = Vertex3f(radius, -radius, 0);
1256 (*quad++).vertex = Vertex3f(-radius, -radius, 0);
1259 inline void draw_cube(const float radius, PointVertex* cube)
1261 (*cube++).vertex = Vertex3f(-radius, -radius, -radius);
1262 (*cube++).vertex = Vertex3f(radius, -radius, -radius);
1263 (*cube++).vertex = Vertex3f(-radius, radius, -radius);
1264 (*cube++).vertex = Vertex3f(radius, radius, -radius);
1265 (*cube++).vertex = Vertex3f(-radius, -radius, radius);
1266 (*cube++).vertex = Vertex3f(radius, -radius, radius);
1267 (*cube++).vertex = Vertex3f(-radius, radius, radius);
1268 (*cube++).vertex = Vertex3f(radius, radius, radius);
1272 /// \brief Calculates the tangent vectors for a triangle \p a, \p b, \p c and stores the tangent in \p s and the bitangent in \p t.
1273 inline void ArbitraryMeshTriangle_calcTangents(const ArbitraryMeshVertex& a, const ArbitraryMeshVertex& b, const ArbitraryMeshVertex& c, Vector3& s, Vector3& t)
1275 s = Vector3(0, 0, 0);
1276 t = Vector3(0, 0, 0);
1281 Vector3(b.vertex.x(), b.texcoord.s(), b.texcoord.t()),
1282 Vector3(a.vertex.x(), a.texcoord.s(), a.texcoord.t())
1285 Vector3(c.vertex.x(), c.texcoord.s(), c.texcoord.t()),
1286 Vector3(a.vertex.x(), a.texcoord.s(), a.texcoord.t())
1291 if(fabs(cross.x()) > 0.000001f)
1293 s.x() = -cross.y() / cross.x();
1296 if(fabs(cross.x()) > 0.000001f)
1298 t.x() = -cross.z() / cross.x();
1306 Vector3(b.vertex.y(), b.texcoord.s(), b.texcoord.t()),
1307 Vector3(a.vertex.y(), a.texcoord.s(), a.texcoord.t())
1310 Vector3(c.vertex.y(), c.texcoord.s(), c.texcoord.t()),
1311 Vector3(a.vertex.y(), a.texcoord.s(), a.texcoord.t())
1316 if(fabs(cross.x()) > 0.000001f)
1318 s.y() = -cross.y() / cross.x();
1321 if(fabs(cross.x()) > 0.000001f)
1323 t.y() = -cross.z() / cross.x();
1331 Vector3(b.vertex.z(), b.texcoord.s(), b.texcoord.t()),
1332 Vector3(a.vertex.z(), a.texcoord.s(), a.texcoord.t())
1335 Vector3(c.vertex.z(), c.texcoord.s(), c.texcoord.t()),
1336 Vector3(a.vertex.z(), a.texcoord.s(), a.texcoord.t())
1341 if(fabs(cross.x()) > 0.000001f)
1343 s.z() = -cross.y() / cross.x();
1346 if(fabs(cross.x()) > 0.000001f)
1348 t.z() = -cross.z() / cross.x();
1353 inline void ArbitraryMeshTriangle_sumTangents(ArbitraryMeshVertex& a, ArbitraryMeshVertex& b, ArbitraryMeshVertex& c)
1357 ArbitraryMeshTriangle_calcTangents(a, b, c, s, t);
1359 reinterpret_cast<Vector3&>(a.tangent) += s;
1360 reinterpret_cast<Vector3&>(b.tangent) += s;
1361 reinterpret_cast<Vector3&>(c.tangent) += s;
1363 reinterpret_cast<Vector3&>(a.bitangent) += t;
1364 reinterpret_cast<Vector3&>(b.bitangent) += t;
1365 reinterpret_cast<Vector3&>(c.bitangent) += t;