Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shape.cc
Go to the documentation of this file.
1 
18 #include "ion/gfx/shape.h"
19 
20 #include "ion/base/enumhelper.h"
21 #include "ion/base/invalid.h"
22 #include "ion/base/static_assert.h"
23 #include "ion/portgfx/glheaders.h"
24 
25 namespace ion {
26 namespace gfx {
27 
29  : primitive_type_(kTriangles), vertex_ranges_(*this), instance_count_(0) {}
30 
32 }
33 
34 size_t Shape::AddVertexRange(const math::Range1i& range) {
35  if (range.IsEmpty()) {
36  LOG(WARNING) << "Ignoring empty range passed to Shape::AddVertexRange.";
37  return base::kInvalidIndex;
38  } else {
39  const size_t index = vertex_ranges_.size();
40  vertex_ranges_.push_back(VertexRange(range, true, 0));
41  return index;
42  }
43 }
44 
45 void Shape::SetVertexRange(size_t i, const math::Range1i& range) {
46  if (CheckRangeIndex(i, "SetVertexRange")) {
47  if (range.IsEmpty())
48  LOG(WARNING) << "Ignoring empty range passed to Shape::SetVertexRange.";
49  else
50  vertex_ranges_[i].range = range;
51  }
52 }
53 
54 const math::Range1i Shape::GetVertexRange(size_t i) const {
55  if (CheckRangeIndex(i, "GetVertexRange"))
56  return vertex_ranges_[i].range;
57  return math::Range1i();
58 }
59 
60 void Shape::EnableVertexRange(size_t i, bool enable) {
61  if (CheckRangeIndex(i, "EnableVertexRange"))
62  vertex_ranges_[i].is_enabled = enable;
63 }
64 
65 bool Shape::IsVertexRangeEnabled(size_t i) const {
66  if (CheckRangeIndex(i, "IsVertexRangeEnabled"))
67  return vertex_ranges_[i].is_enabled;
68  return false;
69 }
70 
71 void Shape::SetVertexRangeInstanceCount(size_t i, int instance_count) {
72  if (CheckRangeIndex(i, "SetVertexRangeInstanceCount"))
73  vertex_ranges_[i].instance_count = instance_count;
74 }
75 
77  if (CheckRangeIndex(i, "GetVertexRangeInstanceCount"))
78  return vertex_ranges_[i].instance_count;
79  return 0;
80 }
81 
82 bool Shape::CheckRangeIndex(size_t i, const char* name) const {
85  if (i < vertex_ranges_.size()) {
86  return true;
87  } else {
88  LOG(WARNING) << "Out of bounds index " << i << " passed to "
89  << "Shape::" << name << "; shape has "
90  << vertex_ranges_.size() << " ranges";
91  return false;
92  }
93 }
94 
95 } // namespace gfx
96 
97 namespace base {
98 
99 using gfx::Shape;
100 
102 template <> ION_API const EnumHelper::EnumData<Shape::PrimitiveType>
103 EnumHelper::GetEnumData() {
104  static const GLenum kValues[] = {
105  GL_LINES, GL_LINE_LOOP, GL_LINE_STRIP, GL_POINTS, GL_TRIANGLES,
106  GL_TRIANGLE_FAN, GL_TRIANGLE_STRIP
107  };
108  static const char* kStrings[] = {
109  "Lines", "Line Loop", "Line Strip", "Points", "Triangles", "Triangle Fan",
110  "Triangle Strip",
111  };
112  ION_STATIC_ASSERT(ARRAYSIZE(kValues) == ARRAYSIZE(kStrings),
113  "EnumHelper size mismatch");
114  return EnumData<Shape::PrimitiveType>(
115  base::IndexMap<Shape::PrimitiveType, GLenum>(kValues, ARRAYSIZE(kValues)),
116  kStrings);
117 }
118 
119 } // namespace base
120 
121 } // namespace ion
void SetVertexRangeInstanceCount(size_t i, int instance_count)
Sets/Gets the instance count of vertex range.
Definition: shape.cc:71
void EnableVertexRange(size_t i, bool enable)
Enables or disables a specific vertex range.
Definition: shape.cc:60
Range< 1, int32 > Range1i
Definition: range.h:363
const size_t kInvalidIndex
kInvalidIndex is a size_t value that is very unlikely to be a valid index.
Definition: invalid.cc:23
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
int GetVertexRangeInstanceCount(size_t i) const
Returns the instance count that the vertex range is set to.
Definition: shape.cc:76
const math::Range1i GetVertexRange(size_t i) const
Returns the i-th vertex range.
Definition: shape.cc:54
This template class can be used to map between two kinds of indices when the following assumptions ap...
Definition: indexmap.h:40
size_t AddVertexRange(const math::Range1i &range)
By default, a Shape draws all of its vertices (if there is no IndexBuffer) or all of its indexed vert...
Definition: shape.cc:34
std::string name
Definition: printer.cc:324
void SetVertexRange(size_t i, const math::Range1i &range)
Modifies the specified vertex range if the passed index is valid (i.e., there is a range at i that ha...
Definition: shape.cc:45
~Shape() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
Definition: shape.cc:31
bool IsVertexRangeEnabled(size_t i) const
Returns whether the i-th vertex range is enabled, or false if i does not refer to a valid range...
Definition: shape.cc:65
#define ION_STATIC_ASSERT(expr, message)
Copyright 2016 Google Inc.
Definition: static_assert.h:35