TypeSplatCloud.cc 10.2 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

//================================================================
//
//  CLASS TypeSplatCloudPlugin - IMPLEMENTATION
//
//================================================================


//== INCLUDES ====================================================


#include "TypeSplatCloud.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

57 58 59
#include <OpenFlipper/common/BackupData.hh>
#include "SplatCloudBackup.hh"

Jan Möbius's avatar
Jan Möbius committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include <ACG/GL/GLState.hh>


//== IMPLEMENTATION ==============================================


void TypeSplatCloudPlugin::slotViewChanged()
{
	// get drawmodes
	ACG::SceneGraph::DrawModes::DrawMode splatsDrawMode = ACG::SceneGraph::DrawModes::getDrawMode( "Splats" );
	ACG::SceneGraph::DrawModes::DrawMode dotsDrawMode   = ACG::SceneGraph::DrawModes::getDrawMode( "Dots"   );

	// if drawmodes don't exist something went wrong
	if( splatsDrawMode == ACG::SceneGraph::DrawModes::NONE || 
		dotsDrawMode   == ACG::SceneGraph::DrawModes::NONE )
	{
		emit log(LOGERR,tr("Shader DrawModes for SplatCloud not existent!"));
		return;
	}

	// get current glState
81
	const ACG::GLState &glstate = PluginFunctions::viewerProperties().glState();
Jan Möbius's avatar
Jan Möbius committed
82 83 84

	// get viewport
	int left, bottom, width, height;
85
	glstate.get_viewport( left, bottom, width, height );
Jan Möbius's avatar
Jan Möbius committed
86 87 88 89 90 91 92

	float x = (float) left;
	float y = (float) bottom;
	float w = (float) width;
	float h = (float) height;

	// get depthrange
93
	// TODO: use glstate.get_depth_range when implemented
Jan Möbius's avatar
Jan Möbius committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	GLfloat	depthRange[2];
	glGetFloatv( GL_DEPTH_RANGE, depthRange );
	float z = (float) depthRange[0];
	float d = (float) depthRange[1] - z;

	// check if we are safe
	if( w<=0.0f || h<=0.0f || d<=0.0f )
		return;

	// calculate window-coordinates to normalized-device-coordinates scale
	ACG::Vec4f invVPs;
	invVPs[0] = 2.0f / w;
	invVPs[1] = 2.0f / h;
	invVPs[2] = 2.0f / d;
	invVPs[3] = 0.0f;

	// calculate window-coordinates to normalized-device-coordinates transpose
	ACG::Vec4f invVPt;
	invVPt[0] = - ( x * invVPs[0] + 1.0f );
	invVPt[1] = - ( y * invVPs[1] + 1.0f );
	invVPt[2] = - ( z * invVPs[2] + 1.0f );
	invVPt[3] = 1.0f;

	// calculate normalized-device-coordinates to window-coordinates scale and transpose
	GLfloat VPs_z = 0.5f * d;
	GLfloat VPt_z = z + VPs_z;

Jan Möbius's avatar
Jan Möbius committed
121 122 123 124 125 126 127
	// calculate scaling factor of modelview matrix
	static const double RCP_3 = 1.0 / 3.0;
	const ACG::GLMatrixd &mv = glstate.modelview();
	double detMV = mv(0,0) * (mv(1,1)*mv(2,2) - mv(1,2)*mv(2,1)) 
	             + mv(0,1) * (mv(1,2)*mv(2,0) - mv(1,0)*mv(2,2)) 
	             + mv(0,2) * (mv(1,0)*mv(2,1) - mv(1,1)*mv(2,0));
	GLfloat MVs = (GLfloat) pow( fabs( detMV ), RCP_3 );
128

Jan Möbius's avatar
Jan Möbius committed
129
	// calculate scale for pointsizes in eye-coordinates according to fovy and transformation to window-coordinates
130 131
	GLfloat VPsFov_y = glstate.projection()(1,1) * (0.5f * h);

Jan Möbius's avatar
Jan Möbius committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	// for all splatcloud-objects...
	PluginFunctions::ObjectIterator objIter( PluginFunctions::ALL_OBJECTS, DATA_SPLATCLOUD );
	for( ; objIter != PluginFunctions::objectsEnd(); ++objIter )
	{
		// get scenegraph shader-node
		ShaderNode *shaderNode = PluginFunctions::splatShaderNode( *objIter );

		// get standard shaders and picking shaders
		GLSL::PtrProgram splatsShader     = shaderNode->getShader( splatsDrawMode, false );
		GLSL::PtrProgram splatsPickShader = shaderNode->getShader( splatsDrawMode, true  );
		GLSL::PtrProgram dotsShader       = shaderNode->getShader( dotsDrawMode,   false );
		GLSL::PtrProgram dotsPickShader   = shaderNode->getShader( dotsDrawMode,   true  );

		// update shader uniforms concerning viewport and depthrange

		if( splatsShader )
		{
			splatsShader->use();
			splatsShader->setUniform( "invViewportScale",   invVPs   );
			splatsShader->setUniform( "invViewportTransp",  invVPt   );
			splatsShader->setUniform( "viewportScale_z",    VPs_z    );
			splatsShader->setUniform( "viewportTransp_z",   VPt_z    );
154
			splatsShader->setUniform( "modelviewScale",     MVs      );
Jan Möbius's avatar
Jan Möbius committed
155 156 157 158 159 160 161 162 163 164 165
			splatsShader->setUniform( "viewportScaleFov_y", VPsFov_y );
			splatsShader->disable();
		}

		if( splatsPickShader )
		{
			splatsPickShader->use();
			splatsPickShader->setUniform( "invViewportScale",   invVPs   );
			splatsPickShader->setUniform( "invViewportTransp",  invVPt   );
			splatsPickShader->setUniform( "viewportScale_z",    VPs_z    );
			splatsPickShader->setUniform( "viewportTransp_z",   VPt_z    );
166
			splatsPickShader->setUniform( "modelviewScale",     MVs      );
Jan Möbius's avatar
Jan Möbius committed
167 168 169 170 171 172 173
			splatsPickShader->setUniform( "viewportScaleFov_y", VPsFov_y );
			splatsPickShader->disable();
		}

		if( dotsShader )
		{
			dotsShader->use();
174
			dotsShader->setUniform( "modelviewScale",     MVs      );
Jan Möbius's avatar
Jan Möbius committed
175 176 177 178 179 180 181
			dotsShader->setUniform( "viewportScaleFov_y", VPsFov_y );
			dotsShader->disable();
		}

		if( dotsPickShader )
		{
			dotsPickShader->use();
182
			dotsPickShader->setUniform( "modelviewScale",     MVs      );
Jan Möbius's avatar
Jan Möbius committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
			dotsPickShader->setUniform( "viewportScaleFov_y", VPsFov_y );
			dotsPickShader->disable();
		}
	}
}


//----------------------------------------------------------------


bool TypeSplatCloudPlugin::registerType()
{
	addDataType( "SplatCloud", tr( "SplatCloud" ) );
	setTypeIcon( "SplatCloud", "SplatCloudType.png" );
	return true;
}


//----------------------------------------------------------------


int TypeSplatCloudPlugin::addEmpty()
{
	// new object data struct
	SplatCloudObject *object = new SplatCloudObject();

	if( PluginFunctions::objectCount() == 1 )
		object->target( true );

	if( PluginFunctions::targetCount() == 0 )
		object->target( true );

	QString name = get_unique_name( object );

	// call the local function to update names
	QFileInfo f( name );
	object->setName( f.fileName() );

	object->update();

	object->show();

	emit emptyObjectAdded( object->id() );

	return object->id();
}


//----------------------------------------------------------------


QString TypeSplatCloudPlugin::get_unique_name( SplatCloudObject *_object )
{
	bool name_unique = false;

	int cur_idx = _object->id();

	while( !name_unique )
	{
		name_unique = true;

		QString cur_name = QString( tr( "SplatCloud %1.spl" ).arg( cur_idx ) );

		PluginFunctions::ObjectIterator o_it( PluginFunctions::ALL_OBJECTS, DATA_SPLATCLOUD );
		for( ; o_it != PluginFunctions::objectsEnd(); ++o_it )
		{
			if( o_it->name() == cur_name )
			{
				name_unique = false;
				cur_idx += 10;
				break;
			}
		}
	}

	return QString( tr( "SplatCloud %1.spl" ).arg( cur_idx ) );
}


262 263 264
//----------------------------------------------------------------


265
void TypeSplatCloudPlugin::generateBackup( int _objectId, QString _name, UpdateType _type )
266
{
267 268 269
	if( _objectId == -1 )
		return;

270
	BaseObjectData *object = 0;
271
	PluginFunctions::getObject( _objectId, object );
272

273
	SplatCloudObject *splatCloudObject = PluginFunctions::splatCloudObject( object );
274

275
	if( splatCloudObject )
276 277 278 279 280
	{
		// get backup object data
		BackupData *backupData = 0;

		if( object->hasObjectData( OBJECT_BACKUPS ) )
281
		{
282
			backupData = dynamic_cast<BackupData *>( object->objectData( OBJECT_BACKUPS ) );
283
		}
284 285 286 287 288 289 290 291
		else
		{
			// add backup data
			backupData = new BackupData( object );
			object->setObjectData( OBJECT_BACKUPS, backupData );
		}

		// store a new backup
292
		SplatCloudBackup *backup = new SplatCloudBackup( splatCloudObject, _name, _type );
293 294 295 296 297 298

		backupData->storeBackup( backup );
	}
}


Jan Möbius's avatar
Jan Möbius committed
299 300 301 302
//================================================================


Q_EXPORT_PLUGIN2( typesplatcloudplugin, TypeSplatCloudPlugin );