Visualization.cpp
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @file Visualization.h 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @date Fall 2008 00005 00006 @brief 00007 Extracts out non-essential functionality (visualizations, user 00008 interaction) from Canvases 00009 <!-------------------------------------------------------------------->**/ 00010 00011 #include "Visualization.h" 00012 #include "OpenGLCanvas.h" 00013 #include "ImageCanvas.h" 00014 #include "Gui.h" 00015 00016 #include <milton.h> 00017 #include <QtGui> 00018 00019 InteractionListener::InteractionListener(Canvas *parent) 00020 : m_parent(parent) 00021 { 00022 ASSERT(parent); 00023 00024 // register with parent 00025 m_parent->registerInteractionListener(this); 00026 } 00027 00028 InteractionListener::~InteractionListener() 00029 { } 00030 00031 // Callbacks upon user interaction (empty by default) 00032 void InteractionListener::mousePressEvent (InteractionInfo &info) 00033 { } 00034 void InteractionListener::mouseReleaseEvent(InteractionInfo &info) 00035 { } 00036 void InteractionListener::mouseMoveEvent (InteractionInfo &info) 00037 { } 00038 00039 void InteractionListener::keyPressEvent (InteractionInfo &info) 00040 { } 00041 void InteractionListener::keyReleaseEvent (InteractionInfo &info) 00042 { } 00043 00044 // Called for OpenGL drawing during paintEvent in parent Canvas 00045 // (empty by default) 00046 void InteractionListener::paintGL() 00047 { } 00048 00049 // Called during paintEvent in parent Canvas (empty by default) 00050 void InteractionListener::paint(QPainter *e) 00051 { } 00052 00053 00054 CameraUIListener::CameraUIListener(OpenGLCanvas *parent) 00055 : InteractionListener(parent), m_visualizeAccel(false) 00056 { } 00057 00058 CameraUIListener::~CameraUIListener() 00059 { } 00060 00061 // Callbacks upon user interaction 00062 void CameraUIListener::mousePressEvent (InteractionInfo &info) { 00063 QMouseEvent *event = static_cast<QMouseEvent *>(info.event); 00064 00065 m_lastMousePos = m_mouseDownPos = Point2(event->x(), event->y()); 00066 00067 ThinLensCamera *camera = dynamic_cast<ThinLensCamera *>(m_parent->getCamera()); 00068 if (camera != NULL) { 00069 m_origEye = camera->getEye(); 00070 m_origUp = camera->getUp(); 00071 m_origLook = camera->getLook(); 00072 m_origU = camera->getU(); 00073 } 00074 } 00075 00076 void CameraUIListener::mouseReleaseEvent(InteractionInfo &info) 00077 { } 00078 00079 void CameraUIListener::mouseMoveEvent (InteractionInfo &info) { 00080 QMouseEvent *event = static_cast<QMouseEvent *>(info.event); 00081 00082 if (event->modifiers() & Qt::ControlModifier) { 00083 Qt::MouseButtons buttons = event->buttons(); 00084 const Vector2 &diff = Point2(event->x(), event->y()) - m_lastMousePos; 00085 00086 ThinLensCamera *camera = dynamic_cast<ThinLensCamera *>(m_parent->getCamera()); 00087 if (camera != NULL) { 00088 if (buttons & Qt::LeftButton) { 00089 // Camera trackball rotation 00090 // -------------------------------------------- 00091 const Vector2 &diff = 00092 Point2(event->x(), event->y()) - m_mouseDownPos; 00093 Point3 origin; 00094 00095 const real_t scaleFactor = -0.015f; 00096 const Matrix4x4 &leftRight = getRotMat(origin, m_origUp, 00097 scaleFactor * diff[0]); 00098 const Matrix4x4 &upDown = getRotMat(origin, m_origU, 00099 scaleFactor * diff[1]); 00100 00101 // Calculate new camera parameters 00102 const Point3 &eyeNew = upDown * leftRight * m_origEye; 00103 const Vector3 &upNew = upDown * leftRight * m_origUp; 00104 const Vector3 &lookNew = upDown * leftRight * m_origLook; 00105 00106 camera->setOrientation(eyeNew, lookNew, upNew); 00107 } else if (buttons & Qt::RightButton) { 00108 // Camera dolly (translation along look vector) 00109 // -------------------------------------------- 00110 const real_t amount = diff[0] / 200.0; 00111 const Vector3 &offset = camera->getLook() * amount; 00112 00113 const Point3 &eyeNew = camera->getEye() + offset; 00114 camera->setOrientation(eyeNew, camera->getLook(), camera->getUp()); 00115 } else if (buttons & Qt::MidButton) { 00116 // Camera pan (translation along uv film plane) 00117 // -------------------------------------------- 00118 const Point3 &eyeNew = camera->getEye() + 00119 (camera->getU() * (-diff[0] / 64)) + 00120 (camera->getV() * (diff[1] / 64)); 00121 00122 camera->setOrientation(eyeNew, camera->getLook(), camera->getUp()); 00123 } 00124 00125 m_parent->redraw(); 00126 } 00127 } 00128 00129 m_lastMousePos = Point2(event->x(), event->y()); 00130 } 00131 00132 void CameraUIListener::keyPressEvent (InteractionInfo &info) { 00133 QKeyEvent *event = static_cast<QKeyEvent *>(info.event); 00134 00135 m_visualizeAccel = (event->key() == Qt::Key_A); 00136 00137 if (m_visualizeAccel) { 00138 //Scene *scene = m_parent->getScene(); 00139 //ASSERT(scene); 00140 00141 // TODO 00142 //scene-> 00143 } 00144 } 00145 00146 void CameraUIListener::keyReleaseEvent (InteractionInfo &info) { 00147 QKeyEvent *event = static_cast<QKeyEvent *>(info.event); 00148 00149 if (m_visualizeAccel) 00150 m_visualizeAccel = !(event->key() == Qt::Key_A); 00151 } 00152 00153 void CameraUIListener::paintGL() { 00154 if (m_visualizeAccel) { 00155 00156 } 00157 } 00158 00159 00160 IntersectDebugTester::IntersectDebugTester(Canvas *parent) 00161 : InteractionListener(parent), m_index(0) 00162 { 00163 ResourceManager::insert<unsigned>("kdTreeIndex", m_index); 00164 } 00165 00166 IntersectDebugTester::~IntersectDebugTester() 00167 { } 00168 00169 void IntersectDebugTester::mousePressEvent (InteractionInfo &info) { 00170 QMouseEvent *event = static_cast<QMouseEvent *>(info.event); 00171 00172 if (event->modifiers() & Qt::ControlModifier) 00173 return; 00174 00175 Camera *camera = m_parent->getCamera(); 00176 ASSERT(camera); 00177 Scene *scene = m_parent->getScene(); 00178 ASSERT(scene); 00179 00180 const real_t width = m_parent->getWidth(), height = m_parent->getHeight(); 00181 00182 SurfacePoint pt; 00183 Ray ray(camera->getWorldRay(Point2(event->x() / width, event->y() / height))); 00184 00185 m_index = 0; 00186 //ResourceManager::insert<bool>("flushPreview", true); 00187 const real_t t = scene->getIntersection(ray, pt); 00188 00189 cerr << event->x() << ", " << event->y() << ": "; 00190 if (pt.init(ray, t)) { // intersection found 00191 cerr << pt << endl; 00192 00193 DirectIllumination *d = m_parent->getRenderer()->getDirectIllumination(); 00194 ASSERT(d); 00195 00196 const SpectralSampleSet &direct = d->evaluate(pt); 00197 00198 cerr << "direct illum = " << direct << endl; 00199 } else { 00200 //cerr << "no intersection" << endl; 00201 } 00202 00203 //m_parent->redraw(); 00204 } 00205 00206 void IntersectDebugTester::keyPressEvent(InteractionInfo &info) { 00207 QKeyEvent *event = static_cast<QKeyEvent *>(info.event); 00208 00209 if (event->modifiers() & Qt::ControlModifier) 00210 return; 00211 00212 if (event->key() == Qt::Key_Plus) 00213 ++m_index; 00214 else if (event->key() == Qt::Key_Minus && m_index > 0) 00215 --m_index; 00216 else 00217 return; 00218 00219 //cerr << "kdTreeIndex: " << m_index << endl; 00220 //ResourceManager::insert<unsigned>("kdTreeIndex", m_index); 00221 //ResourceManager::insert<bool>("flushPreview", true); 00222 00223 m_parent->redraw(); 00224 } 00225
Generated on 28 Feb 2009 for Milton by
1.5.6