RigsofRods
Soft-body Physics Simulation
ColorGradient.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of SkyX.
4 Visit http://www.paradise-studios.net/products/skyx/
5 
6 Copyright (C) 2009-2012 Xavier Verguín González <xavyiy@gmail.com>
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt.
21 --------------------------------------------------------------------------------
22 */
23 
24 #include "ColorGradient.h"
25 
26 namespace SkyX
27 {
29  : mMalFormed(true)
30  {
31  }
32 
34  {
35  }
36 
37  const Ogre::Vector3 ColorGradient::getColor(const Ogre::Real& p) const
38  {
39  if (mMalFormed)
40  {
41  SkyXLOG("Mal-formed ColorGradient");
42  return Ogre::Vector3(0,0,0);
43  }
44 
45  if (CFrameVector.size() == 0)
46  {
47  return Ogre::Vector3(0,0,0);
48  }
49  else if (CFrameVector.size() == 1)
50  {
51  return CFrameVector.at(0).first;
52  }
53 
54  std::pair<int, Ogre::Real> minBound, maxBound;
55 
56  // Min value
57  minBound.first = 0;
58  minBound.second = -1;
59  for (unsigned int k = 0; k < CFrameVector.size(); k++)
60  {
61  if (CFrameVector.at(k).second < p && CFrameVector.at(k).second > minBound.second)
62  {
63  minBound.first = k;
64  minBound.second = CFrameVector.at(k).second;
65  }
66  }
67 
68  // Max value
69  maxBound.first = 0;
70  maxBound.second = 2;
71  for (unsigned int k = 0; k < CFrameVector.size(); k++)
72  {
73  if (CFrameVector.at(k).second > p && CFrameVector.at(k).second < maxBound.second)
74  {
75  maxBound.first = k;
76  maxBound.second = CFrameVector.at(k).second;
77  }
78  }
79 
80  float range = maxBound.second - minBound.second,
81  rangepoint = (p - minBound.second) / range;
82 
83  return CFrameVector.at(minBound.first).first*(1-rangepoint) + CFrameVector.at(maxBound.first).first*rangepoint;
84  }
85 
86  const bool ColorGradient::_checkBounds() const
87  {
88  std::pair<bool, bool> existbounds;
89  existbounds.first = false; existbounds.second = false;
90 
91  for (unsigned int k = 0; k < CFrameVector.size(); k++)
92  {
93  if (CFrameVector.at(k).second == 0)
94  {
95  // More than one min bound
96  if (existbounds.first)
97  {
98  return false;
99  }
100 
101  existbounds.first = true;
102  }
103 
104  if (CFrameVector.at(k).second < 0 || CFrameVector.at(k).second > 1)
105  {
106  return false;
107  }
108  }
109 
110  for (unsigned int k = 0; k < CFrameVector.size(); k++)
111  {
112  if (CFrameVector.at(k).second == 1)
113  {
114  // More than one min bound
115  if (existbounds.second)
116  {
117  return false;
118  }
119 
120  existbounds.second = true;
121  }
122  }
123 
124  if (!existbounds.first || !existbounds.second)
125  {
126  return false;
127  }
128 
129  return true;
130  }
131 }
ColorGradient.h
SkyX::ColorGradient::getColor
const Ogre::Vector3 getColor(const Ogre::Real &p) const
Get color value.
Definition: ColorGradient.cpp:37
SkyX::ColorGradient::mMalFormed
bool mMalFormed
Mal formed color gradient?
Definition: ColorGradient.h:78
SkyX
Definition: AtmosphereManager.cpp:30
SkyX::ColorGradient::ColorGradient
ColorGradient()
Constructor.
Definition: ColorGradient.cpp:28
SkyX::ColorGradient::~ColorGradient
~ColorGradient()
Destructor.
Definition: ColorGradient.cpp:33
SkyX::ColorGradient::CFrameVector
std::vector< ColorFrame > CFrameVector
Color frame vector.
Definition: ColorGradient.h:81
SkyX::ColorGradient::_checkBounds
const bool _checkBounds() const
Check bounds.
Definition: ColorGradient.cpp:86
SkyXLOG
#define SkyXLOG(msg)
Include external headers.
Definition: Prerequisites.h:34