61 #include <ACG/GL/acg_glew.hh> 62 #include "ArrowNode.hh" 63 #include <ACG/GL/IRenderer.hh> 64 #include <OpenMesh/Core/Utils/vector_cast.hh> 77 localArrowMin_(0.0f, 0.0f, 0.0f),
78 localArrowMax_(0.0f, 0.0f, 0.0f),
79 invalidateInstanceData_(true),
80 invalidateInstanceBuffer_(true),
81 supportsInstancing_(-1)
96 float radius = std::max(localArrowMax_[1] - localArrowMin_[1], localArrowMax_[2] - localArrowMin_[2]) * 0.5f;
98 size_t n = arrows_.size();
99 for (
size_t i = 0; i < n; ++i)
101 const Arrow* a = &arrows_[i];
105 Vec3f e = a->start + a->dir * a->scale[1];
114 _bbMin.
minimize(sd - volEnlargeOffset);
115 _bbMin.
minimize(ed - volEnlargeOffset);
117 _bbMax.
maximize(sd + volEnlargeOffset);
118 _bbMax.
maximize(ed + volEnlargeOffset);
132 void ArrowNode::createArrowMesh()
140 0.681818f, -0.034091f, -0.068182f, 0.000000f, -1.000000f, 0.000000f,
141 0.681818f, -0.034091f, 0.068182f, 0.000000f, -1.000000f, 0.000000f,
142 0.000000f, -0.034091f, 0.000000f, 0.000000f, -1.000000f, 0.000000f,
143 1.000000f, -0.034091f, 0.000000f, 0.000000f, -1.000000f, 0.000000f,
144 0.681818f, -0.034091f, -0.227273f, 0.000000f, -1.000000f, 0.000000f,
145 0.681818f, -0.034091f, 0.227273f, 0.000000f, -1.000000f, 0.000000f,
146 0.681818f, 0.034091f, -0.068182f, 0.000000f, 1.000000f, -0.000000f,
147 1.000000f, 0.034091f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
148 0.681818f, 0.034091f, -0.227273f, -0.000000f, 1.000000f, 0.000000f,
149 0.681818f, 0.034091f, 0.068182f, 0.000000f, 1.000000f, -0.000000f,
150 0.000000f, 0.034091f, 0.000000f, 0.000000f, 1.000000f, -0.000000f,
151 0.681818f, 0.034091f, 0.227273f, 0.000000f, 1.000000f, 0.000000f,
152 1.000000f, -0.034091f, 0.000000f, 0.581238f, 0.000000f, -0.813734f,
153 0.681818f, 0.034091f, -0.227273f, 0.581238f, 0.000000f, -0.813734f,
154 1.000000f, 0.034091f, 0.000000f, 0.581238f, 0.000000f, -0.813734f,
155 0.681818f, -0.034091f, -0.227273f, 0.581238f, 0.000000f, -0.813734f,
156 0.681818f, -0.034091f, 0.227273f, 0.581238f, 0.000000f, 0.813734f,
157 1.000000f, 0.034091f, 0.000000f, 0.581238f, 0.000000f, 0.813734f,
158 0.681818f, 0.034091f, 0.227273f, 0.581238f, -0.000000f, 0.813734f,
159 1.000000f, -0.034091f, 0.000000f, 0.581238f, 0.000000f, 0.813734f,
160 0.681818f, -0.034091f, 0.068182f, -1.000000f, -0.000000f, 0.000000f,
161 0.681818f, 0.034091f, 0.227273f, -1.000000f, -0.000000f, 0.000000f,
162 0.681818f, 0.034091f, 0.068182f, -1.000000f, 0.000000f, 0.000000f,
163 0.681818f, -0.034091f, 0.227273f, -1.000000f, -0.000000f, 0.000000f,
164 0.681818f, -0.034091f, -0.227273f, -1.000000f, -0.000000f, 0.000000f,
165 0.681818f, 0.034091f, -0.068182f, -1.000000f, -0.000000f, 0.000000f,
166 0.681818f, 0.034091f, -0.227273f, -1.000000f, 0.000000f, 0.000000f,
167 0.681818f, -0.034091f, -0.068182f, -1.000000f, -0.000000f, 0.000000f,
168 0.000000f, -0.034091f, 0.000000f, -0.099504f, 0.000000f, 0.995037f,
169 0.681818f, 0.034091f, 0.068182f, -0.099504f, 0.000000f, 0.995037f,
170 0.000000f, 0.034091f, 0.000000f, -0.099504f, 0.000000f, 0.995037f,
171 0.681818f, -0.034091f, 0.068182f, -0.099504f, 0.000000f, 0.995037f,
172 0.681818f, -0.034091f, -0.068182f, -0.099504f, -0.000000f, -0.995037f,
173 0.000000f, 0.034091f, 0.000000f, -0.099504f, -0.000000f, -0.995037f,
174 0.681818f, 0.034091f, -0.068182f, -0.099504f, 0.000000f, -0.995037f,
175 0.000000f, -0.034091f, 0.000000f, -0.099504f, -0.000000f, -0.995037f,
211 numIndices_ =
sizeof(idata) /
sizeof(idata[0]);
214 vertexBuffer_.upload(
sizeof(vdata), vdata, GL_STATIC_DRAW);
215 indexBuffer_.upload(
sizeof(idata), idata, GL_STATIC_DRAW);
218 localArrowMin_ =
Vec3f(vdata[0], vdata[1], vdata[2]);
219 localArrowMax_ = localArrowMin_;
221 for (
int i = 1; i < numVertices_; ++i)
223 Vec3f v =
Vec3f(vdata[i * 6], vdata[i * 6 + 1], vdata[i * 6 + 2]);
235 updateInstanceData();
237 vertexBuffer_.bind();
244 glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&viewMatrix);
248 glEnable(GL_COLOR_MATERIAL);
249 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
251 for (
size_t i = 0; i < arrows_.size(); ++i)
254 GLMatrixf worldView = viewMatrix * readWorldMatrix(i);
255 glLoadMatrixf(worldView.data());
257 Vec4uc c = arrows_[i].color;
258 glColor4ub(c[0], c[1], c[2], c[3]);
260 glDrawElements(GL_TRIANGLES, numIndices_, GL_UNSIGNED_INT, 0);
264 glLoadMatrixf(viewMatrix.data());
266 glDisable(GL_COLOR_MATERIAL);
278 updateInstanceBuffer();
283 obj.depthTest =
true;
290 if (supportsInstancing_ < 0)
296 if (supportsInstancing_)
300 obj.
shaderDesc.vertexTemplateFile =
"ArrowNode/instancing_vs.glsl";
302 obj.glDrawElementsInstanced(GL_TRIANGLES, numIndices_, GL_UNSIGNED_INT, 0, arrows_.size());
316 for (
size_t i = 0; i < arrows_.size(); ++i)
318 const Arrow* a = &arrows_[i];
320 obj.
modelview = viewMatrix * readWorldMatrix(i);
321 obj.
diffuse =
Vec3f(a->color[0] / 255.0f, a->color[1] / 255.0f, a->color[2] / 255.0f);
322 obj.alpha = a->color[3] / 255.0f;
324 obj.glDrawElements(GL_TRIANGLES, numIndices_, GL_UNSIGNED_INT, 0);
348 return int(arrows_.size());
353 GLMatrixf ArrowNode::computeWorldMatrix(
int _arrow)
const 355 const Arrow* a = &arrows_[_arrow];
364 Vec3f binormal = a->dir % a->normal;
365 for (
int i = 0; i < 3; ++i)
367 align(i, 0) = a->dir[i];
368 align(i, 1) = a->normal[i];
369 align(i, 2) = binormal[i];
373 align.
scale(a->scale[1], a->scale[2], a->scale[0]);
391 arrows_.push_back(a);
393 invalidateInstanceData_ =
true;
394 invalidateInstanceBuffer_ =
true;
396 return int(arrows_.size()) - 1;
404 for (
int i = 0; i < 4; ++i)
405 c[i] = std::min(std::max(
int(_color[i] * 255.0f), 0), 255);
406 return addArrow(_start, _dir, _normal, _scale, c);
411 void ArrowNode::Arrow::orthonormalize()
416 if (normal.sqrnorm() < 1e-6f)
417 normal =
Vec3f(0.0f, 1.0f, 0.0f);
421 while (std::fabs(dir | normal) > 0.99f || normal.sqrnorm() < 0.01f)
423 for (
int i = 0; i < 3; ++i)
424 normal[i] =
float(rand()) /
float(RAND_MAX) * 2.0f - 1.0f;
428 Vec3f binormal = dir % normal;
429 normal = (binormal % dir).normalized();
436 return addArrow(OpenMesh::vector_cast<Vec3f>(_start), OpenMesh::vector_cast<Vec3f>(_dir), OpenMesh::vector_cast<Vec3f>(_normal), OpenMesh::vector_cast<Vec3f>(_scale), _color);
444 for (
int i = 0; i < 4; ++i)
445 c[i] = std::min(std::max(
int(_color[i] * 255.0f), 0), 255);
446 return addArrow(_start, _dir, _normal, _scale, c);
451 void ArrowNode::updateInstanceData()
453 if (invalidateInstanceData_)
455 const int numArrows = arrows_.size();
458 const int instanceSize = instanceDataSize();
460 instanceData_.resize(numArrows * instanceSize);
462 for (
int i = 0; i < numArrows; ++i)
466 int offset = instanceDataOffset(i);
469 for (
int r = 0; r < 3; ++r)
470 for (
int c = 0; c < 4; ++c)
471 instanceData_[offset + r*4 + c] = m(r,c);
475 for (
int r = 0; r < 3; ++r)
476 for (
int c = 0; c < 3; ++c)
477 instanceData_[offset + 4 * 3 + r * 3 + c] = m(c, r);
481 memcpy(&instanceData_[offset + 4*3 + 3*3], arrows_[i].color.data(), 4);
486 invalidateInstanceData_ =
false;
494 int offset = instanceDataOffset(_arrow);
497 for (
int r = 0; r < 3; ++r)
498 for (
int c = 0; c < 4; ++c)
499 m(r,c) = instanceData_[offset + r*4 + c];
502 m(3,0) = 0.0f; m(3,1) = 0.0f; m(3,2) = 0.0f; m(3,3) = 1.0f;
509 void ArrowNode::updateInstanceBuffer()
511 if (invalidateInstanceBuffer_ || invalidateInstanceData_)
513 updateInstanceData();
515 if (!instanceData_.empty())
516 instanceBuffer_.upload(4 * instanceData_.size(), &instanceData_[0], GL_STATIC_DRAW);
534 invalidateInstanceBuffer_ =
false;
542 return arrows_[_arrowID].start;
547 arrows_[_arrowID].start = _start;
548 invalidateInstanceData_ =
true;
553 return arrows_[_arrowID].dir;
558 arrows_[_arrowID].dir = _dir;
559 arrows_[_arrowID].orthonormalize();
560 invalidateInstanceData_ =
true;
565 return arrows_[_arrowID].normal;
570 arrows_[_arrowID].normal = _normal;
571 arrows_[_arrowID].orthonormalize();
572 invalidateInstanceData_ =
true;
577 return arrows_[_arrowID].scale;
582 arrows_[_arrowID].scale = _scale;
583 invalidateInstanceData_ =
true;
588 return arrows_[_arrowID].color;
593 arrows_[_arrowID].color = _color;
594 invalidateInstanceData_ =
true;
Vec3f diffuse
material definitions
unsigned int getVertexStride(unsigned int i=0) const
int addArrow(const Vec3f &_start, const Vec3f &_dir, const Vec3f &_normal=Vec3f(0.0f, 1.0f, 0.0f), const Vec3f &_scale=Vec3f(1.0f, 1.0f, 1.0f), const Vec4uc &_color=Vec4uc(82, 82, 82, 255))
Add an arrow to the node.
DrawMode SOLID_FLAT_SHADED
draw flat shaded faces (requires face normals)
Vec3f arrowNormal(int _arrowID) const
Return the normal of an arrow.
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
DrawModes::DrawMode drawMode() const
Return the own draw modes of this node.
ArrowNode(BaseNode *_parent=0, std::string _name="<ArrowNode>")
default constructor
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
void activateFixedFunction() const
unsigned int getNumElements() const
GLuint indexBuffer
Use vertex array object.
defined by user via VertexElement::shaderInputName_
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
Vec3f arrowScale(int _arrowID) const
Return the scale of an arrow.
Vec4uc arrowColor(int _arrowID) const
Return the color of an arrow.
GLMatrixd modelview
Modelview transform.
void identity()
setup an identity matrix
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
VectorT< float, 3 > Vec3f
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
Vec3f arrowStart(int _arrowID) const
Return the start position of an arrow.
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const ACG::SceneGraph::Material *_mat)
Add the objects to the given renderer.
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
draw arrows
DrawModes::DrawMode availableDrawModes() const
return available draw modes
void deactivateFixedFunction() const
Namespace providing different geometric functions concerning angles.
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
void addElement(const VertexElement *_pElement)
bool invert()
matrix inversion (returns true on success)
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
bool checkExtensionSupported(const std::string &_extension)
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax)
update bounding box
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Interface class between scenegraph and renderer.
Vec3f arrowDir(int _arrowID) const
Return the direction of an arrow.
int n_arrows() const
number of arrows
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
ShaderGenDesc shaderDesc
Drawmode and other shader params.
void reserve(int _n)
reserve mem for _n arrows