62 #include "GlutPrimitiveNode.hh"
64 #include <ACG/GL/IRenderer.hh>
70 namespace SceneGraph {
75 GlutPrimitiveNode::GlutPrimitiveNode(
BaseNode* _parent,
80 const int slices = 20;
81 const int stacks = 20;
88 GlutPrimitiveNode::GlutPrimitiveNode(GlutPrimitiveType _type,
94 const int slices = 20;
95 const int stacks = 20;
99 primitives_.push_back(p);
108 if (_idx > -1 && _idx < (
int)primitives_.size())
109 primitives_[_idx].position = _p;
118 if (_idx > -1 && _idx < (
int)primitives_.size())
119 return primitives_[_idx].position;
121 return Vec3d(-1,-1,-1);
130 if (_idx > -1 && _idx < (
int)primitives_.size())
131 primitives_[_idx].size = _s;
140 if (_idx > -1 && _idx < (
int)primitives_.size())
141 return primitives_[_idx].size;
151 for (
int i = 0; i < (int)primitives_.size(); ++i)
153 Vec3d sizeVec(primitives_[i].size, primitives_[i].size, primitives_[i].size);
154 _bbMax.maximize(primitives_[i].position + sizeVec);
155 _bbMin.minimize(primitives_[i].position - sizeVec);
179 float backupColorDiffuse[4];
180 float backupColorAmbient[4];
182 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
183 glGetFloatv(GL_CURRENT_COLOR,backupColorDiffuse);
184 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
185 glGetFloatv(GL_CURRENT_COLOR,backupColorAmbient);
187 for (
size_t i = 0; i < primitives_.size(); ++i)
192 glTranslatef(primitives_[i].position[0], primitives_[i].position[1], primitives_[i].position[2]);
199 glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
201 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
209 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
211 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
218 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
221 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
227 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
237 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
242 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
247 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
258 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
273 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
282 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
283 glColor4fv(backupColorDiffuse);
284 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
285 glColor4fv(backupColorAmbient);
297 primitives_.push_back(p);
298 return primitives_.size() - 1;
304 GlutPrimitiveNode::draw_obj(
int _idx)
const
306 if (_idx < 0 || _idx >= (
int)primitives_.size())
309 Vec3d axis = primitives_[_idx].axis;
310 double size = axis.norm();
316 Vec3d direction = axis;
321 direction.normalize();
322 rot_angle = acos((z_axis | direction)) * 180 / M_PI;
323 rot_normal = ((z_axis % direction).normalize());
326 if (fabs(rot_angle) > 0.0001 && fabs(180 - rot_angle) > 0.0001)
327 glRotatef(rot_angle,rot_normal[0], rot_normal[1], rot_normal[2]);
329 glRotatef(rot_angle,1,0,0);
332 switch (primitives_[_idx].type)
335 glutSolidCone(primitives_[_idx].size, primitives_[_idx].innersize, primitives_[_idx].slices, primitives_[_idx].stacks);
339 glutSolidCube(primitives_[_idx].size);
343 glutSolidDodecahedron();
347 glutSolidIcosahedron();
351 glutSolidOctahedron();
355 glutSolidSphere(primitives_[_idx].size, primitives_[_idx].slices, primitives_[_idx].stacks);
359 glutSolidTeapot(primitives_[_idx].size);
363 glutSolidTetrahedron();
367 glutSolidTorus(primitives_[_idx].innersize, primitives_[_idx].size, primitives_[_idx].slices, primitives_[_idx].stacks);
384 std::cerr <<
"Strange pickSetMaximum failed for index " << primitives_.size() <<
" in GlutPrimitiveNode\n";
393 for (
int i = 0; i < (int)primitives_.size(); ++i)
397 glTranslatef(primitives_[i].position[0], primitives_[i].position[1], primitives_[i].position[2]);
426 for (
int i = 0; i < (int)primitives_.size(); ++i)
431 _state.
translate(primitives_[i].position);
441 localMaterial.
baseColor(primitives_[i].color * .5);
444 ro.setMaterial(&localMaterial);
447 switch (primitives_[i].type) {
451 ro.debugName = (QString(
"glutprimitive.sphere no %1: ").arg(i)+QString(
name().c_str())).toLatin1();
453 sphere_->addToRenderer(_renderer, &ro, primitives_[i].size);
459 std::cerr <<
"Sorry, but the glut renderer objects are not available for this renderer yet!" << std::endl;
ShaderGenDesc shaderDesc
Drawmode and other shader params.
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
drawing the primitive
Namespace providing different geometric functions concerning angles.
static void enable(GLenum _cap)
replaces glEnable, but supports locking
DrawMode HIDDENLINE
draw hidden line (2 rendering passes needed)
std::string name() const
Returns: name of node (needs not be unique)
void pick_set_name(unsigned int _idx)
sets the current name/color (like glLoadName(_idx))
PickTarget
What target to use for picking.
const Vec4f & clear_color() const
get background color
static void disable(GLenum _cap)
replaces glDisable, but supports locking
bool pick_set_maximum(unsigned int _idx)
Set the maximal number of primitives/components of your object.
picks faces (should be implemented for all nodes)
pick any of the prior targets (should be implemented for all nodes)
Interface class between scenegraph and renderer.
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
DrawModes::DrawMode availableDrawModes() const
return available draw modes
DrawMode SOLID_FLAT_SHADED
draw flat shaded faces (requires face normals)
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
static void shadeModel(GLenum _mode)
replaces glShadeModel, supports locking
VectorT< double, 3 > Vec3d
const GLMatrixd & modelview() const
get modelview matrix
const Vec3d get_position(int _idx=0) const
get position
DrawMode SOLID_SMOOTH_SHADED
draw smooth shaded (Gouraud shaded) faces (requires halfedge normals)
void glColor(const Vec3f &_v)
Wrapper: glColor for Vec3f.
void diffuseColor(const Vec4f &_d)
set the diffuse color.
void push_modelview_matrix()
push modelview matrix
void set_size(double _s, int _idx=0)
set size
void pick(GLState &_state, PickTarget _target)
picking
DrawMode POINTS
draw unlighted points using the default base color
double get_size(int _idx=0) const
get size
void pop_modelview_matrix()
pop modelview matrix
GlutPrimitiveType
Lists all available primivites.
void baseColor(const Vec4f &_c)
set the base color
DrawMode WIREFRAME
draw wireframe
DrawMode SOLID_FACES_COLORED
draw colored, but not lighted faces using face colors
GLMatrixd modelview
Modelview transform.
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const Material *_mat)
Add the objects to the given renderer.
size_t add_primitive(GlutPrimitiveType _type, Vec3d _pos, Vec3d _axis, ACG::Vec4f _color)
int priority
Priority to allow sorting of objects.
static void depthRange(GLclampd _zNear, GLclampd _zFar)
replaces glDepthRange, supports locking
void set_position(const Vec3d &_p, int _idx=0)
set position
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax)
update bounding box
const Vec4f & base_color() const
get base color (used when lighting is off)