Commit 135e621e authored by Ellen Dekkers's avatar Ellen Dekkers
Browse files

LineNode, PointNode: float -> double

GlutPrimitiveNode: float -> double, can handle more than one primitive now

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@8876 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 4b898d07
......@@ -64,22 +64,71 @@ namespace SceneGraph {
//== IMPLEMENTATION ==========================================================
void
GlutPrimitiveNode::
set_position(const Vec3d& _p, int _idx)
{
if (_idx > -1 && _idx < (int)primitives_.size())
primitives_[_idx].position = _p;
}
//----------------------------------------------------------------------------
const Vec3d
GlutPrimitiveNode::
get_position(int _idx) const
{
if (_idx > -1 && _idx < (int)primitives_.size())
return primitives_[_idx].position;
return Vec3d(-1,-1,-1);
}
//----------------------------------------------------------------------------
void
GlutPrimitiveNode::
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
set_size(double _s, int _idx)
{
if (_bbMin[0] > position_[0]-size_) _bbMin[0] = position_[0] - size_;
if (_bbMin[1] > position_[1]-size_) _bbMin[1] = position_[1] - size_;
if (_bbMin[2] > position_[2]-size_) _bbMin[2] = position_[2] - size_;
if (_bbMax[0] < position_[0]+ size_) _bbMax[0] = position_[0] + size_;
if (_bbMax[1] < position_[1]+ size_) _bbMax[1] = position_[1] + size_;
if (_bbMax[2] < position_[2]+ size_) _bbMax[2] = position_[2] + size_;
if (_idx > -1 && _idx < (int)primitives_.size())
primitives_[_idx].size = _s;
}
//----------------------------------------------------------------------------
double
GlutPrimitiveNode::
get_size(int _idx) const
{
if (_idx > -1 && _idx < (int)primitives_.size())
return primitives_[_idx].size;
return -1;
}
//----------------------------------------------------------------------------
void
GlutPrimitiveNode::
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
{
Vec3d bbMin(FLT_MAX,FLT_MAX,FLT_MAX);
Vec3d bbMax(-FLT_MAX,-FLT_MAX,-FLT_MAX);
for (int i = 0; i < (int)primitives_.size(); ++i)
{
Vec3d sizeVec(primitives_[i].size, primitives_[i].size, primitives_[i].size);
bbMax.maximize(primitives_[i].position + sizeVec);
bbMin.minimize(primitives_[i].position - sizeVec);
}
Vec3d bbMind = ACG::Vec3d(bbMin);
Vec3d bbMaxd = ACG::Vec3d(bbMax);
_bbMin.minimize(bbMind);
_bbMax.maximize(bbMaxd);
}
//----------------------------------------------------------------------------
DrawModes::DrawMode
GlutPrimitiveNode::
......@@ -92,95 +141,119 @@ availableDrawModes() const
DrawModes::SOLID_SMOOTH_SHADED );
}
//----------------------------------------------------------------------------
void
GlutPrimitiveNode::
draw(GLState& _state, DrawModes::DrawMode _drawMode)
{
glDepthFunc(depthFunc());
glPushMatrix();
glTranslatef(position_[0], position_[1], position_[2]);
for (int i = 0; i < (int)primitives_.size(); ++i)
{
glPushMatrix();
glTranslatef(primitives_[i].position[0], primitives_[i].position[1], primitives_[i].position[2]);
if (_drawMode & DrawModes::POINTS)
{
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
draw_obj();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (_drawMode & DrawModes::POINTS)
{
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
draw_obj(i);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (_drawMode & DrawModes::WIREFRAME)
{
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
draw_obj();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (_drawMode & DrawModes::WIREFRAME)
{
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
draw_obj(i);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (_drawMode & DrawModes::HIDDENLINE)
{
Vec4f base_color_backup = _state.base_color();
if (_drawMode & DrawModes::HIDDENLINE)
{
Vec4f base_color_backup = _state.base_color();
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor(_state.clear_color());
glDepthRange(0.01, 1.0);
draw_obj();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor(_state.clear_color());
glDepthRange(0.01, 1.0);
draw_obj(i);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor(base_color_backup);
glDepthRange(0.0, 1.0);
draw_obj();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor(base_color_backup);
glDepthRange(0.0, 1.0);
draw_obj(i);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (_drawMode & DrawModes::SOLID_FLAT_SHADED)
{
glEnable(GL_LIGHTING);
glShadeModel(GL_FLAT);
draw_obj();
}
if (_drawMode & DrawModes::SOLID_FLAT_SHADED)
{
glEnable( GL_COLOR_MATERIAL );
glEnable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glColor(primitives_[i].color);
draw_obj(i);
}
if (_drawMode & DrawModes::SOLID_SMOOTH_SHADED)
{
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
draw_obj();
}
if (_drawMode & DrawModes::SOLID_SMOOTH_SHADED)
{
glEnable( GL_COLOR_MATERIAL );
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glColor(primitives_[i].color);
draw_obj(i);
}
glPopMatrix();
glPopMatrix();
} // end of primitives iter
glDepthFunc(GL_LESS);
}
//----------------------------------------------------------------------------
void
GlutPrimitiveNode::
add_primitive(GlutPrimitiveType _type, Vec3d _pos, Vec3d _axis, ACG::Vec3uc _color)
{
Primitive p(_type, _pos, _axis, _color);
primitives_.push_back(p);
}
//----------------------------------------------------------------------------
void
GlutPrimitiveNode::draw_obj() const
GlutPrimitiveNode::draw_obj(int _idx) const
{
switch (type_)
if (_idx < 0 || _idx >= (int)primitives_.size()) // range check
return;
switch (primitives_[_idx].type)
{
case CONE:
glutSolidCone(size_, innersize_, slices_, stacks_);
glutSolidCone(primitives_[_idx].size, primitives_[_idx].innersize, primitives_[_idx].slices, primitives_[_idx].stacks);
break;
case CUBE:
glutSolidCube(size_);
glutSolidCube(primitives_[_idx].size);
break;
case DODECAHEDRON:
......@@ -196,51 +269,49 @@ GlutPrimitiveNode::draw_obj() const
break;
case SPHERE:
glutSolidSphere(size_, slices_, stacks_);
glutSolidSphere(primitives_[_idx].size, primitives_[_idx].slices, primitives_[_idx].stacks);
break;
case TEAPOT:
glutSolidTeapot(size_);
glutSolidTeapot(primitives_[_idx].size);
break;
case TETRAHEDRON:
glutSolidTetrahedron();
break;
case TORUS:
glutSolidTorus(innersize_, size_, slices_, stacks_);
glutSolidTorus(primitives_[_idx].innersize, primitives_[_idx].size, primitives_[_idx].slices, primitives_[_idx].stacks);
break;
};
}
//----------------------------------------------------------------------------
void
GlutPrimitiveNode::
pick(GLState& _state , PickTarget _target)
{
// initialize picking stack
if (!_state.pick_set_maximum (1))
if (!_state.pick_set_maximum (primitives_.size()))
{
std::cerr << "Strange pickSetMaximum failed for index 1 in GlutPrimitiveNode\n";
std::cerr << "Strange pickSetMaximum failed for index " << primitives_.size() << " in GlutPrimitiveNode\n";
return;
}
_state.pick_set_name(0);
switch (_target)
{
case PICK_ANYTHING:
case PICK_FACE:
{
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glPushMatrix();
glTranslatef(position_[0], position_[1], position_[2]);
draw_obj();
glPopMatrix();
for (int i = 0; i < (int)primitives_.size(); ++i)
{
_state.pick_set_name(i);
glPushMatrix();
glTranslatef(primitives_[i].position[0], primitives_[i].position[1], primitives_[i].position[2]);
draw_obj(i);
glPopMatrix();
}
break;
}
......
......@@ -94,39 +94,110 @@ public:
TETRAHEDRON,
TORUS
};
struct Primitive
{
Vec3d position; // position
Vec3d axis; // direction / axis vector
GlutPrimitiveType type; // glut primitive type
ACG::Vec3uc color; // color
// glut primitive resolution
double size;
double innersize; // size of inner loop for torus, height for cone
unsigned int slices, stacks;
// Constructor
Primitive()
{
// set default resolution
size = 1.0;
innersize = 0.5;
slices = 20;
stacks = 20;
}
Primitive(GlutPrimitiveType _t, Vec3d _p, Vec3d _v, ACG::Vec3uc _c)
{
type = _t;
position = _p;
axis = _v;
color = _c;
// set default resolution
size = 1.0;
innersize = 0.5;
slices = 20;
stacks = 20;
}
// Copy Constructor
Primitive( const Primitive& _p)
{
// use defined = operator
*this = _p;
}
// = operator
Primitive& operator=(const Primitive& _p)
{
type = _p.type;
position = _p.position;
axis = _p.axis;
color = _p.color;
size = _p.size;
innersize = _p.innersize;
slices = _p.slices;
stacks = _p.stacks;
return *this;
}
};
/// Default constructor.
GlutPrimitiveNode( GlutPrimitiveType _type,
BaseNode* _parent=0,
std::string _name="<GlutPrimitive>" )
: BaseNode(_parent, _name),
type_(_type),
size_(1.0),
innersize_(0.5),
slices_(20),
stacks_(20),
position_(0.0, 0.0, 0.0)
{}
GlutPrimitiveNode( BaseNode* _parent=0,
std::string _name="<GlutPrimitive>" )
: BaseNode(_parent, _name)
{};
GlutPrimitiveNode( GlutPrimitiveType _type,
BaseNode* _parent=0,
std::string _name="<GlutPrimitive>" )
: BaseNode(_parent, _name)
{
// add a single primitive of the given type
Primitive p;
p.type = _type;
primitives_.push_back(p);
}
/// destructor
virtual ~GlutPrimitiveNode() {}
void add_primitive(GlutPrimitiveType _type, Vec3d _pos, Vec3d _axis, ACG::Vec3uc _color);
void clear(){primitives_.clear();};
/// set position
void set_position(const Vec3f& _p) { position_ = _p; }
void set_position(const Vec3d& _p, int _idx = 0);
/// get position
const Vec3f& get_position() const { return position_; }
const Vec3d get_position(int _idx = 0) const;
/// get a primitive
Primitive& get_primitive(int _idx){return primitives_[_idx];};
/// set size
void set_size(float _s) { size_ = _s; }
void set_size(double _s, int _idx = 0);
/// get size
float get_size() const { return size_; }
double get_size(int _idx = 0) const;
ACG_CLASSNAME(GlutPrimitiveNode);
/// return available draw modes
DrawModes::DrawMode availableDrawModes() const;
......@@ -135,17 +206,15 @@ public:
/// drawing the primitive
void draw(GLState& _state, DrawModes::DrawMode _drawMode);
void draw_obj() const;
void draw_obj(int _idx) const;
/// picking
void pick(GLState& _state, PickTarget _target);
private:
GlutPrimitiveType type_;
float size_, innersize_;
unsigned int slices_, stacks_;
Vec3f position_;
std::vector<Primitive> primitives_;
};
......
......@@ -67,21 +67,12 @@ void
LineNode::
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
{
Vec3f bbMin(FLT_MAX,FLT_MAX,FLT_MAX);
Vec3f bbMax(-FLT_MAX,-FLT_MAX,-FLT_MAX);
ConstPointIter p_it=points_.begin(), p_end=points_.end();
for (; p_it!=p_end; ++p_it)
{
bbMax.maximize(*p_it);
bbMin.minimize(*p_it);
_bbMax.maximize(*p_it);
_bbMin.minimize(*p_it);
}
Vec3d bbMind = ACG::Vec3d(bbMin);
Vec3d bbMaxd = ACG::Vec3d(bbMax);
_bbMin.minimize(bbMind);
_bbMax.maximize(bbMaxd);
}
......
......@@ -83,7 +83,7 @@ class ACGDLLEXPORT LineNode : public MaterialNode
public:
// typedefs
typedef std::vector<Vec3f> PointVector;
typedef std::vector<Vec3d> PointVector;
typedef PointVector::iterator PointIter;
typedef PointVector::const_iterator ConstPointIter;
typedef std::vector<ACG::Vec3uc> ColorVector;
......@@ -147,10 +147,10 @@ public:
/// add point (for LineMode == PolygonMode)
void add_point(const Vec3f& _v) { points_.push_back(_v); }
void add_point(const Vec3d& _v) { points_.push_back(_v); }
/// add line (for LineMode == LineSegmentsMode)
void add_line(const Vec3f& _v0, const Vec3f& _v1) {
void add_line(const Vec3d& _v0, const Vec3d& _v1) {
add_point(_v0); add_point(_v1);
}
/// add color (only for LineMode == LineSegmentsMode)
......@@ -168,10 +168,10 @@ public:
/// STL conformance
void push_back(const Vec3f& _v) { points_.push_back(_v); }
typedef Vec3f value_type;
typedef Vec3f& reference;
typedef const Vec3f& const_reference;
void push_back(const Vec3d& _v) { points_.push_back(_v); }
typedef Vec3d value_type;
typedef Vec3d& reference;
typedef const Vec3d& const_reference;
protected:
......
......@@ -68,20 +68,11 @@ void
PointNode::
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
{
Vec3f bbMin(FLT_MAX,FLT_MAX,FLT_MAX);
Vec3f bbMax(-FLT_MAX,-FLT_MAX,-FLT_MAX);
ConstPointIter p_it=points_.begin(), p_end=points_.end();
for (; p_it!=p_end; ++p_it) {
bbMin.minimize(*p_it);
bbMax.maximize(*p_it);
_bbMin.minimize(*p_it);
_bbMax.maximize(*p_it);
}
Vec3d bbMind = ACG::Vec3d(bbMin);
Vec3d bbMaxd = ACG::Vec3d(bbMax);
_bbMin.minimize(bbMind);
_bbMax.maximize(bbMaxd);
}
......
......@@ -82,7 +82,7 @@ class ACGDLLEXPORT PointNode : public BaseNode
public:
// typedefs
typedef std::vector<ACG::Vec3f> PointVector;
typedef std::vector<ACG::Vec3d> PointVector;
typedef PointVector::iterator PointIter;
typedef PointVector::const_iterator ConstPointIter;
typedef std::vector<ACG::Vec3uc> ColorVector;
......@@ -117,9 +117,9 @@ public:
}
/// add point
void add_point(const ACG::Vec3f& _p) { points_.push_back(_p); }
void add_point(const ACG::Vec3d& _p) { points_.push_back(_p); }
/// add normal
void add_normal(const ACG::Vec3f& _n) { normals_.push_back(_n); }
void add_normal(const ACG::Vec3d& _n) { normals_.push_back(_n); }
/// add color
void add_color(const ACG::Vec3uc& _c) { colors_.push_back(_c); }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment