camera.cpp 4.94 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
#include "camera.h"
Sylvain Thery's avatar
Sylvain Thery committed
2
#include "mapHandler.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
3 4 5 6 7 8 9 10 11 12

namespace CGoGN
{

namespace SCHNApps
{

unsigned int Camera::cameraCount = 0;

Camera::Camera(const QString& name, SCHNApps* s) :
Sylvain Thery's avatar
Sylvain Thery committed
13
	m_drawBB(false),
Pierre Kraemer's avatar
Pierre Kraemer committed
14 15 16 17 18 19
	m_name(name),
	m_schnapps(s),
	m_draw(false),
	m_drawPath(false)
{
	++cameraCount;
Pierre Kraemer's avatar
Pierre Kraemer committed
20
	connect(this->frame(), SIGNAL(modified()), this, SLOT(frameModified()));
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

	connect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));

	foreach(MapHandlerGen* map, m_schnapps->getMapSet().values())
	{
		// do the code of BBModified for each linked map
		if (isLinkedToMap(map))
			updateParams();
		foreach(View* v, l_views)
		{
			v->updateGL();
		}
		// and do the connection for map added
		mapAdded(map);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
37 38 39 40 41 42 43 44
}

Camera::~Camera()
{}

void Camera::setProjectionType(int t)
{
	setType(qglviewer::Camera::Type(t));
45
	DEBUG_EMIT("projectionTypeChanged");
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47 48 49 50 51 52 53
	emit(projectionTypeChanged(t));
	foreach(View* view, m_schnapps->getViewSet().values())
		view->updateGL();
}

void Camera::setDraw(bool b)
{
	m_draw = b;
54
	DEBUG_EMIT("drawChanged");
Pierre Kraemer's avatar
Pierre Kraemer committed
55 56 57 58 59 60 61 62
	emit(drawChanged(b));
	foreach(View* view, m_schnapps->getViewSet().values())
		view->updateGL();
}

void Camera::setDrawPath(bool b)
{
	m_drawPath = b;
63
	DEBUG_EMIT("drawPathChanged");
Pierre Kraemer's avatar
Pierre Kraemer committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	emit(drawPathChanged(b));
	foreach(View* view, m_schnapps->getViewSet().values())
		view->updateGL();
}

void Camera::linkView(View* view)
{
	if(view && !l_views.contains(view))
		l_views.push_back(view);
}

void Camera::unlinkView(View* view)
{
	l_views.removeOne(view);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93
void Camera::frameModified()
{
	if(m_draw || m_drawPath)
	{
		foreach(View* view, m_schnapps->getViewSet().values())
			view->updateGL();
	}
	else
	{
		foreach(View* view, l_views)
			view->updateGL();
	}
}

Sylvain Thery's avatar
Sylvain Thery 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

void Camera::updateParams()
{
	// get all maps viewed by camera
	QSet<MapHandlerGen*> q_maps;
	foreach(View* view, l_views)
	{
		QList<MapHandlerGen*> l_maps = view->getLinkedMaps();
		foreach(MapHandlerGen* mhg, l_maps)
			q_maps.insert(mhg);
	}

	if (!q_maps.empty())
	{
// to help debug I store bb min/max in camera for camera bb drawing
//		qglviewer::Vec m_bbMin;
//		qglviewer::Vec m_bbMax;

		(*q_maps.begin())->transformedBB(m_bbMin,m_bbMax);

		foreach(MapHandlerGen* mhg, q_maps)
		{
			qglviewer::Vec minbb;
			qglviewer::Vec maxbb;
			mhg->transformedBB(minbb,maxbb);
			for(unsigned int dim = 0; dim < 3; ++dim)
			{
				if(minbb[dim] < m_bbMin[dim])
					m_bbMin[dim] = minbb[dim];
				if(maxbb[dim] > m_bbMax[dim])
					m_bbMax[dim] = maxbb[dim];
			}
		}
		this->setSceneBoundingBox(m_bbMin, m_bbMax);
		this->showEntireScene();
	}

}


bool Camera::isLinkedToMap(MapHandlerGen* mhg) const
{
	const QString& mapName = mhg->getName();
	foreach(View* v, l_views)
	{
		if (v->isLinkedToMap(mapName))
			return true;
	}
	return false;
}


void Camera::mapAdded(MapHandlerGen* mhg)
{
	// all maps signals to camera its existence
	// selection if done by BBModified
	connect(mhg, SIGNAL(BBisModified()), this, SLOT(BBModified()));
}

void Camera::mapRemoved(MapHandlerGen* mhg)
{
	disconnect(mhg, SIGNAL(BBisModified()), this, SLOT(BBModified()));
}

void Camera::BBModified()
{
	DEBUG_SLOT();
	MapHandlerGen* mhg = static_cast<MapHandlerGen*>(QObject::sender());

	if (isLinkedToMap(mhg))
		updateParams();
	foreach(View* v, l_views)
	{
		v->updateGL();
	}
}


void Camera::drawBBCam()
{
	if (!m_drawBB)
		return;

177 178 179 180 181 182 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
//TODO with Drawer ?

//	float shift = 0.01f*(m_bbMax - m_bbMin).norm();

//	glDisable(GL_LIGHTING);
//	glColor3f(0.5f,1.0f,0.0f);
//	glLineWidth(1.0f);
//	glBegin(GL_LINE_LOOP);
//	glVertex3f(m_bbMin[0]-shift, m_bbMin[1]-shift, m_bbMin[2]-shift);
//	glVertex3f(m_bbMin[0]-shift, m_bbMax[1]+shift, m_bbMin[2]-shift);
//	glVertex3f(m_bbMax[0]+shift, m_bbMax[1]+shift, m_bbMin[2]-shift);
//	glVertex3f(m_bbMax[0]+shift, m_bbMin[1]-shift, m_bbMin[2]-shift);
//	glVertex3f(m_bbMin[0]-shift, m_bbMin[1]-shift, m_bbMin[2]-shift);
//	glEnd();
//	glBegin(GL_LINE_LOOP);
//	glVertex3f(m_bbMax[0]+shift, m_bbMax[1]+shift, m_bbMax[2]+shift);
//	glVertex3f(m_bbMax[0]+shift, m_bbMin[1]-shift, m_bbMax[2]+shift);
//	glVertex3f(m_bbMin[0]-shift, m_bbMin[1]-shift, m_bbMax[2]+shift);
//	glVertex3f(m_bbMin[0]-shift, m_bbMax[1]+shift, m_bbMax[2]+shift);
//	glVertex3f(m_bbMax[0]+shift, m_bbMax[1]+shift, m_bbMax[2]+shift);
//	glEnd();
//	glBegin(GL_LINES);
//	glVertex3f(m_bbMin[0]-shift, m_bbMin[1]-shift, m_bbMin[2]-shift);
//	glVertex3f(m_bbMin[0]-shift, m_bbMin[1]-shift, m_bbMax[2]+shift);
//	glVertex3f(m_bbMin[0]-shift, m_bbMax[1]+shift, m_bbMin[2]-shift);
//	glVertex3f(m_bbMin[0]-shift, m_bbMax[1]+shift, m_bbMax[2]+shift);
//	glVertex3f(m_bbMax[0]+shift, m_bbMax[1]+shift, m_bbMin[2]-shift);
//	glVertex3f(m_bbMax[0]+shift, m_bbMax[1]+shift, m_bbMax[2]+shift);
//	glVertex3f(m_bbMax[0]+shift, m_bbMin[1]-shift, m_bbMin[2]-shift);
//	glVertex3f(m_bbMax[0]+shift, m_bbMin[1]-shift, m_bbMax[2]+shift);
//	glEnd();
//	glColor3f(1.0f,1.0f,1.0f);
//	glEnable(GL_LIGHTING);
Sylvain Thery's avatar
Sylvain Thery committed
210 211
}

Pierre Kraemer's avatar
Pierre Kraemer committed
212 213 214
} // namespace SCHNApps

} // namespace CGoGN