Commit 0cba6dbe authored by Jan Möbius's avatar Jan Möbius

Christopher: CoordsysNode with render objects.

refs #901

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@14982 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 36e2b88b
......@@ -41,6 +41,8 @@
\*===========================================================================*/
#include "GLPrimitives.hh"
#include <ACG/GL/IRenderer.hh>
namespace ACG {
......@@ -57,6 +59,9 @@ GLPrimitive::GLPrimitive() :
vbo_(0)
{
vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_POSITION);
vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_NORMAL);
vertexDecl_.addElement(GL_FLOAT, 2, VERTEX_USAGE_TEXCOORD);
}
//------------------------------------------------------------------------
......@@ -104,9 +109,33 @@ void GLPrimitive::addTriangleToVBO(const ACG::Vec3f* _p, const ACG::Vec3f* _n, c
void GLPrimitive::bindVBO()
{
if (checkVBO())
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_);
glVertexPointer(3, GL_FLOAT, 32, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 32, (GLvoid*) 12);
glEnableClientState(GL_NORMAL_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glTexCoordPointer(2, GL_FLOAT, 32, (GLvoid*) 24);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
//------------------------------------------------------------------------
bool GLPrimitive::checkVBO()
{
// create vbo if not done yet
// update vbo data and upload to gpu if needed
// return false iff vbo empty
if (!vbo_) {
if (!vboData_ || !numTris_)
return;
return false;
// create vbo
glGenBuffersARB(1, &vbo_);
......@@ -120,19 +149,11 @@ void GLPrimitive::bindVBO()
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, numTris_ * 3 * 8 * 4, vboData_, GL_STATIC_DRAW_ARB);
vboDataInvalid_ = false;
} else
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_);
glVertexPointer(3, GL_FLOAT, 32, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 32, (GLvoid*) 12);
glEnableClientState(GL_NORMAL_ARRAY);
}
glClientActiveTexture(GL_TEXTURE0);
glTexCoordPointer(2, GL_FLOAT, 32, (GLvoid*) 24);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
return true;
}
//------------------------------------------------------------------------
void GLPrimitive::unBindVBO()
......@@ -156,6 +177,21 @@ void GLPrimitive::draw()
//------------------------------------------------------------------------
void GLPrimitive::addToRenderer( class IRenderer* _renderer, RenderObject* _ro )
{
if (checkVBO())
{
_ro->vertexBuffer = vbo_;
_ro->vertexDecl = &vertexDecl_;
_ro->glDrawArrays(GL_TRIANGLES, 0, getNumTriangles() * 3);
_renderer->addRenderObject(_ro);
}
}
//------------------------------------------------------------------------
void GLPrimitive::updateVBOData() {
curTriPtr_ = 0;
......@@ -201,6 +237,19 @@ void GLSphere::draw(GLState& _state, float _radius, const ACG::Vec3f& _center)
_state.pop_modelview_matrix();
}
//------------------------------------------------------------------------
void GLSphere::addToRenderer( IRenderer* _renderer, const RenderObject* _base, float _radius, const ACG::Vec3f& _center /*= ACG::Vec3f(0.0f, 0.0f, 0.0f)*/ )
{
RenderObject ro = *_base;
ro.modelview.translate(_center);
ro.modelview.scale(_radius, _radius, _radius);
GLPrimitive::addToRenderer(_renderer, &ro);
}
//------------------------------------------------------------------------
int GLSphere::getNumTriangles()
......@@ -378,6 +427,53 @@ void GLCone::draw(GLState& _state, float _height, const ACG::Vec3f& _center, ACG
//------------------------------------------------------------------------
void GLCone::addToRenderer(IRenderer* _renderer,
const RenderObject* _base,
float _height,
const ACG::Vec3f& _center,
ACG::Vec3f _upDir)
{
RenderObject ro = *_base;
// translate
ro.modelview.translate(ACG::Vec3f(_center));
_upDir.normalize();
// compute rotation matrix mAlign
// such that vBindDir rotates to _upDir
ACG::GLMatrixd mAlign;
mAlign.identity();
ACG::Vec3f vBindDir(0.0f, 0.0f, 1.0f);
ACG::Vec3f vRotAxis = OpenMesh::cross(_upDir, vBindDir);
vRotAxis.normalize();
ACG::Vec3f vUp = OpenMesh::cross(_upDir, vRotAxis);
// rotate
for (int i = 0; i < 3; ++i) {
mAlign(i, 0) = vRotAxis[i];
mAlign(i, 1) = vUp[i];
mAlign(i, 2) = _upDir[i];
}
ACG::Vec3f vDelta = vBindDir - _upDir;
if (fabsf(OpenMesh::dot(vDelta, vDelta) < 1e-3f))
mAlign.identity();
// scale
mAlign.scale(1.0, 1.0, _height);
ro.modelview *= mAlign;
GLPrimitive::addToRenderer(_renderer, &ro);
}
//------------------------------------------------------------------------
int GLCone::getNumTriangles()
{
int numTris = stacks_ * slices_ * 2;
......
......@@ -58,6 +58,7 @@
#include <ACG/Config/ACGDefines.hh>
#include <ACG/GL/GLState.hh>
#include <ACG/Math/VectorT.hh>
#include <ACG/GL/VertexDeclaration.hh>
//== NAMESPACES ===============================================================
......@@ -75,6 +76,9 @@ public:
// bind vbo + gl draw call
void draw();
// add to deferred draw call to renderer
void addToRenderer(class IRenderer* _renderer, struct RenderObject* _ro);
// triangle count must be known before updateVBO
virtual int getNumTriangles() = 0;
......@@ -89,6 +93,8 @@ protected:
void bindVBO();
bool checkVBO();
void unBindVBO();
bool vboDataInvalid_;
......@@ -103,6 +109,8 @@ private:
float* vboData_;
int curTriPtr_;
VertexDeclaration vertexDecl_;
unsigned int vbo_;
};
......@@ -116,6 +124,8 @@ public:
void draw(GLState& _state, float _radius, const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f));
void addToRenderer(class IRenderer* _renderer, const struct RenderObject* _base, float _radius, const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f));
int getNumTriangles();
private:
......@@ -148,6 +158,12 @@ public:
const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f),
ACG::Vec3f _upDir = ACG::Vec3f(0.0f, 0.0f, 1.0f));
void addToRenderer(class IRenderer* _renderer, const struct RenderObject* _base,
float _height,
const ACG::Vec3f& _center = ACG::Vec3f(0.0f, 0.0f, 0.0f),
ACG::Vec3f _upDir = ACG::Vec3f(0.0f, 0.0f, 1.0f));
int getNumTriangles();
void updateVBO();
......
......@@ -199,6 +199,8 @@ struct ACGDLLEXPORT RenderObject
GLuint texture;
/// used internally for renderer debugging
int debugID;
......
......@@ -40,6 +40,10 @@
* *
\*===========================================================================*/
#ifndef ACG_VERTEXDECLARATION_HH
#define ACG_VERTEXDECLARATION_HH
#include <vector>
#include <list>
#include <ACG/Config/ACGDefines.hh>
......@@ -270,3 +274,7 @@ private:
//=============================================================================
} // namespace ACG
//=============================================================================
#endif // ACG_VERTEXDECLARATION_HH defined
\ No newline at end of file
......@@ -53,6 +53,7 @@
#include "CoordsysNode.hh"
#include <ACG/GL/gl.hh>
#include <ACG/GL/IRenderer.hh>
#include <iostream>
#include <math.h>
......@@ -178,6 +179,55 @@ drawCoordsys( GLState& _state) {
}
void CoordsysNode::drawCoordsys(IRenderer* _renderer, RenderObject* _baseRO)
{
// save model view matrix
GLMatrixf mModelView = _baseRO->modelview;
const double arrowLength = 0.03;
const double bodyLength = 0.06;
const double sphereRadius = 0.01;
// Origin
_baseRO->emissive = Vec3f(1.0f, 1.0f, 1.0f);
sphere_->addToRenderer(_renderer, _baseRO, sphereRadius);
// X-Axis
_baseRO->emissive = Vec3f(1.0f, 0.0f, 0.0f);
_baseRO->modelview = mModelView;
_baseRO->modelview.rotate (-90, 0, 1, 0);
_baseRO->modelview.translate ( 0, 0, -bodyLength );
cylinder_->addToRenderer(_renderer, _baseRO, bodyLength);
_baseRO->modelview.translate ( 0, 0, -arrowLength );
cone_->addToRenderer(_renderer, _baseRO, arrowLength);
// Y-Axis
_baseRO->emissive = Vec3f(0.0f, 1.0f, 0.0f);
_baseRO->modelview = mModelView;
_baseRO->modelview.rotate (90, 1, 0, 0);
_baseRO->modelview.translate ( 0, 0, -bodyLength );
cylinder_->addToRenderer(_renderer, _baseRO, bodyLength);
_baseRO->modelview.translate ( 0, 0, -arrowLength );
cone_->addToRenderer(_renderer, _baseRO, arrowLength);
// Z-Axis
_baseRO->emissive = Vec3f(0.0f, 0.0f, 1.0f);
_baseRO->modelview = mModelView;
_baseRO->modelview.rotate (180, 0, 1, 0);
_baseRO->modelview.translate ( 0, 0, -bodyLength );
cylinder_->addToRenderer(_renderer, _baseRO, bodyLength);
_baseRO->modelview.translate ( 0, 0, -arrowLength );
cone_->addToRenderer(_renderer, _baseRO, arrowLength);
}
//============================================================================
void
......@@ -371,6 +421,150 @@ draw(GLState& _state , const DrawModes::DrawMode& /*_drawMode*/)
}
void CoordsysNode::getRenderObjects( IRenderer* _renderer, GLState& _state, const DrawModes::DrawMode& _drawMode )
{
// Init state - changes when mode_ != POSITION
Vec3d pos3D(0.0,0.0,0.0);
_state.push_modelview_matrix();
// init base renderobject
RenderObject ro;
memset(&ro, 0, sizeof(RenderObject));
ro.initFromState(&_state);
ro.depthTest = true;
ro.depthWrite = true;
if ( mode_ == SCREENPOS ) {
int left, bottom, width, height;
double aspect = _state.aspect();
_state.get_viewport(left, bottom, width, height);
// Projection reset
_state.push_projection_matrix();
_state.reset_projection();
if (projectionMode_ == PERSPECTIVE_PROJECTION)
_state.perspective(45.0, aspect, 0.8, 20.0);
else
_state.ortho(-0.65*aspect, 0.65*aspect, -0.65, 0.65, 0.8, 20.0);
_state.push_modelview_matrix();
_state.reset_modelview();
float rel_size = 50.0;
float projdist = sqrt ( (width*height) / rel_size );
float posx = left + width - projdist ;
float posy = bottom + height - projdist ;
// get our desired coordsys position in scene coordinates
pos3D = _state.unproject (Vec3d (posx, posy, 0.5));
_state.pop_modelview_matrix();
// reset scene translation
// we want only the scene rotation to rotate the coordsys
GLMatrixd modelview = _state.modelview();
modelview(0,3) = 0.0;
modelview(1,3) = 0.0;
modelview(2,3) = 0.0;
_state.set_modelview (modelview);
_state.translate (pos3D[0], pos3D[1], pos3D[2], MULT_FROM_LEFT);
// grab new transforms
ro.proj = _state.projection();
ro.modelview = _state.modelview();
// colored by emission only
ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;
ro.shaderDesc.textured = false;
ro.shaderDesc.vertexColors = false;
// clear the depth buffer behind the coordsys
ro.priority = -4;
ro.depthRange = Vec2f(1.0f, 1.0f);
ro.depthFunc = GL_ALWAYS;
drawCoordsys(_renderer, &ro);
// regrab of transforms needed, drawCoordsys overwrites this
ro.modelview = _state.modelview();
ro.priority = -3;
ro.depthRange = Vec2f(0.0f, 1.0f);
ro.depthFunc = GL_LESS;
// draw coordsys
drawCoordsys(_renderer, &ro);
// set depth buffer to 0 so that nothing can paint over cordsys
ro.modelview = _state.modelview();
ro.priority = -2;
ro.depthRange = Vec2f(0.0, 0.0);
ro.depthFunc = GL_ALWAYS;
ro.glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
// Koordinatensystem zeichnen
drawCoordsys(_renderer, &ro);
// Projection reload
_state.pop_projection_matrix();
} else if (mode_ == POSITION) { /* mode_ == POSITION */
GLMatrixd modelview = _state.modelview();
modelview(0,3) = 0.0;
modelview(1,3) = 0.0;
modelview(2,3) = 0.0;
_state.set_modelview (modelview);
// clear depth buffer in coordsys region
ACG::GLState::depthRange (1.0, 1.0);
ACG::GLState::depthFunc (GL_ALWAYS);
// Koordinatensystem zeichnen
drawCoordsys(_renderer, &ro);
// draw coordsys in normal mode
ACG::GLState::depthRange (0.0, 1.0);
ACG::GLState::depthFunc (GL_LESS);
// Koordinatensystem zeichnen
drawCoordsys(_renderer, &ro);
// set depth buffer to 0 so that nothing can paint over cordsys
ACG::GLState::depthRange (0.0, 0.0);
ACG::GLState::depthFunc (GL_ALWAYS);
glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
// Koordinatensystem zeichnen
drawCoordsys(_renderer, &ro);
}
_state.pop_modelview_matrix();
}
void
CoordsysNode::
setMode(const CoordsysMode _mode)
......
......@@ -120,6 +120,9 @@ public:
/// draw Coordsys
void draw(GLState& _state, const DrawModes::DrawMode& _drawMode);
/// add renderobjects for shader pipeline renderer
void getRenderObjects(IRenderer* _renderer, GLState& _state, const DrawModes::DrawMode& _drawMode);
/// draw Coordsys for object picking
void pick(GLState& _state, PickTarget _target);
......@@ -142,6 +145,7 @@ public:
private:
void drawCoordsys(GLState& _state);
void drawCoordsys(IRenderer* _renderer, RenderObject* _baseRO);
void drawCoordsysPick(GLState& _state);
void clearPickArea(GLState& _state, bool _draw, GLfloat _depth);
void boundingCircle(std::vector<Vec2f> &_in, Vec2f &_center, float &_radius);
......
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