| 1 | /**************************************************************************** |
| 2 | * MeshLab o o * |
| 3 | * A versatile mesh processing toolbox o o * |
| 4 | * _ O _ * |
| 5 | * Copyright(C) 2008 \/)\/ * |
| 6 | * Visual Computing Lab /\/| * |
| 7 | * ISTI - Italian National Research Council | * |
| 8 | * \ * |
| 9 | * All rights reserved. * |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | * This program is distributed in the hope that it will be useful, * |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 19 | * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * |
| 20 | * for more details. * |
| 21 | * * |
| 22 | ****************************************************************************/ |
| 23 | /**************************************************************************** |
| 24 | History |
| 25 | $Log: rubberband.cpp,v $ |
| 26 | Revision 1.1 2008/02/16 12:00:35 benedetti |
| 27 | first version, adapted from meshlab's editmeasure plugin |
| 28 | |
| 29 | |
| 30 | ****************************************************************************/ |
| 31 | |
| 32 | #include <GL/glew.h> |
| 33 | #include <wrap/gl/space.h> |
| 34 | #include <wrap/gl/picking.h> |
| 35 | |
| 36 | #include "rubberband.h" |
| 37 | |
| 38 | using namespace vcg; |
| 39 | |
| 40 | Rubberband::Rubberband(Color4b c) |
| 41 | :color(c),currentphase(RUBBER_BEGIN),qt_cursor(), |
| 42 | start(0,0,0),end(0,0,0),have_to_pick(false),font() |
| 43 | { |
| 44 | font.setFamily("Helvetica"); |
| 45 | font.setPixelSize(10); |
| 46 | } |
| 47 | |
| 48 | void Rubberband::Render(QGLWidget* gla) |
| 49 | { |
| 50 | if(have_to_pick){ |
| 51 | assert(currentphase!=RUBBER_DRAGGED); |
| 52 | Point3f pick_point; |
| 53 | bool picked = Pick(qt_cursor.x(), gla->height() - qt_cursor.y(), pick_point); |
| 54 | if(picked){ // we have not picked the background |
| 55 | have_to_pick=false; |
| 56 | switch(currentphase){ |
| 57 | case RUBBER_BEGIN: |
| 58 | start = pick_point; |
| 59 | gla->setMouseTracking(true); |
| 60 | currentphase = RUBBER_DRAGGING; |
| 61 | break; |
| 62 | case RUBBER_DRAGGING: |
| 63 | if(pick_point==start){ |
| 64 | have_to_pick=true; |
| 65 | break; |
| 66 | } |
| 67 | end = pick_point; |
| 68 | gla->setMouseTracking(false); |
| 69 | currentphase = RUBBER_DRAGGED; |
| 70 | break; |
| 71 | default: |
| 72 | assert(0); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if(currentphase==RUBBER_BEGIN) return; |
| 78 | |
| 79 | // Drawing of the current line |
| 80 | glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT); |
| 81 | glDisable(GL_LIGHTING); |
| 82 | glDisable(GL_TEXTURE_2D); |
| 83 | glDepthMask(false); |
| 84 | glLineWidth(2.5); |
| 85 | glPointSize(5.0); |
| 86 | if(currentphase==RUBBER_DRAGGING ) { |
| 87 | Point3f qt_start_point; |
| 88 | qt_start_point = PixelConvert(start); |
| 89 | glColor(color); |
| 90 | glMatrixMode(GL_PROJECTION); |
| 91 | glPushMatrix(); |
| 92 | glLoadIdentity(); |
| 93 | gluOrtho2D(0,gla->width(),gla->height(),0); |
| 94 | glMatrixMode(GL_MODELVIEW); |
| 95 | glPushMatrix(); |
| 96 | glLoadIdentity(); |
| 97 | glDisable(GL_DEPTH_TEST); |
| 98 | glBegin(GL_LINES); |
| 99 | glVertex2f(qt_start_point[0],qt_start_point[1]); |
| 100 | glVertex2f(qt_cursor.x(),qt_cursor.y()); |
| 101 | glEnd(); |
| 102 | glEnable(GL_DEPTH_TEST); |
| 103 | glMatrixMode(GL_PROJECTION); |
| 104 | glPopMatrix(); |
| 105 | glMatrixMode(GL_MODELVIEW); |
| 106 | glPopMatrix(); |
| 107 | } else { |
| 108 | assert(currentphase == RUBBER_DRAGGED); |
| 109 | glEnable(GL_BLEND); |
| 110 | glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); |
| 111 | glEnable(GL_LINE_SMOOTH); |
| 112 | glEnable(GL_POINT_SMOOTH); |
| 113 | glColor(color); |
| 114 | glBegin(GL_LINES); |
| 115 | glVertex(start); |
| 116 | glVertex(end); |
| 117 | glEnd(); |
| 118 | glBegin(GL_POINTS); |
| 119 | glVertex(start); |
| 120 | glVertex(end); |
| 121 | glEnd(); |
| 122 | glDisable(GL_DEPTH_TEST); |
| 123 | glLineWidth(0.7); |
| 124 | glPointSize(1.4); |
| 125 | glBegin(GL_LINES); |
| 126 | glVertex(start); |
| 127 | glVertex(end); |
| 128 | glEnd(); |
| 129 | glBegin(GL_POINTS); |
| 130 | glVertex(start); |
| 131 | glVertex(end); |
| 132 | glEnd(); |
| 133 | } |
| 134 | glPopAttrib(); |
| 135 | assert(!glGetError()); |
| 136 | } |
| 137 | |
| 138 | void Rubberband::Drag(QPoint p) |
| 139 | { |
| 140 | if(currentphase==RUBBER_DRAGGING); |
| 141 | qt_cursor=p; |
| 142 | } |
| 143 | |
| 144 | void Rubberband::Pin(QPoint p) |
| 145 | { |
| 146 | if(IsReady()) |
| 147 | return; |
| 148 | qt_cursor=p; |
| 149 | have_to_pick=true; |
| 150 | } |
| 151 | |
| 152 | void Rubberband::Reset() |
| 153 | { |
| 154 | currentphase = RUBBER_BEGIN; |
| 155 | qt_cursor = QPoint(); |
| 156 | start = Point3f(0,0,0); |
| 157 | end = Point3f(0,0,0); |
| 158 | have_to_pick = false; |
| 159 | } |
| 160 | |
| 161 | bool Rubberband::IsReady() |
| 162 | { |
| 163 | return currentphase==RUBBER_DRAGGED; |
| 164 | } |
| 165 | |
| 166 | void Rubberband::GetPoints(Point3f &s,Point3f &e) |
| 167 | { |
| 168 | assert(IsReady()); |
| 169 | s=start; |
| 170 | e=end; |
| 171 | } |
| 172 | |
| 173 | void Rubberband::RenderLabel(QString text,QGLWidget* gla) |
| 174 | { |
| 175 | if(currentphase==RUBBER_BEGIN) return; |
| 176 | |
| 177 | int x,y; |
| 178 | if(currentphase==RUBBER_DRAGGING){ |
| 179 | x=qt_cursor.x()+16; |
| 180 | y=qt_cursor.y()+16; |
| 181 | } else { |
| 182 | Point3f qt_start = PixelConvert(start); |
| 183 | Point3f qt_end = PixelConvert(end); |
| 184 | if(qt_start[0]>qt_end[0]){ |
| 185 | x=int(qt_start[0]+5); |
| 186 | y=int(qt_start[1]); |
| 187 | }else{ |
| 188 | x=int(qt_end[0]+5); |
| 189 | y=int(qt_end[1]); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | QFontMetrics fm(font); |
| 194 | QRect brec=fm.boundingRect(text); |
| 195 | glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LINE_BIT ); |
| 196 | glDisable(GL_LIGHTING); |
| 197 | glDisable(GL_TEXTURE_2D); |
| 198 | glEnable(GL_BLEND); |
| 199 | glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); |
| 200 | |
| 201 | glMatrixMode(GL_PROJECTION); |
| 202 | glPushMatrix(); |
| 203 | glLoadIdentity(); |
| 204 | gluOrtho2D(0,gla->width(),gla->height(),0); |
| 205 | glMatrixMode(GL_MODELVIEW); |
| 206 | glPushMatrix(); |
| 207 | glLoadIdentity(); |
| 208 | glColor4f(0,0,0,0.5); |
| 209 | glBegin(GL_QUADS); |
| 210 | glVertex2f(x+brec.left(),y+brec.bottom()); |
| 211 | glVertex2f(x+brec.right(),y+brec.bottom()); |
| 212 | glVertex2f(x+brec.right(),y+brec.top()); |
| 213 | glVertex2f(x+brec.left(),y+brec.top()); |
| 214 | glEnd(); |
| 215 | int offset=2; |
| 216 | glColor4f(0,0,0,0.2); |
| 217 | glBegin(GL_QUADS); |
| 218 | glVertex2f(x+brec.left()-offset,y+brec.bottom()+offset); |
| 219 | glVertex2f(x+brec.right()+offset,y+brec.bottom()+offset); |
| 220 | glVertex2f(x+brec.right()+offset,y+brec.top()-offset); |
| 221 | glVertex2f(x+brec.left()-offset,y+brec.top()-offset); |
| 222 | glEnd(); |
| 223 | glColor3f(1,1,1); |
| 224 | gla->renderText(x,y,0.99f,text,font); |
| 225 | glMatrixMode(GL_PROJECTION); |
| 226 | glPopMatrix(); |
| 227 | glMatrixMode(GL_MODELVIEW); |
| 228 | glPopMatrix(); |
| 229 | glPopAttrib(); |
| 230 | } |
| 231 | |
| 232 | Point3f Rubberband::PixelConvert(const Point3f p) |
| 233 | { |
| 234 | GLint vm[4]; |
| 235 | GLdouble mm[16]; |
| 236 | GLdouble pm[16]; |
| 237 | glGetIntegerv(GL_VIEWPORT, vm); |
| 238 | glGetDoublev(GL_MODELVIEW_MATRIX, mm); |
| 239 | glGetDoublev(GL_PROJECTION_MATRIX, pm); |
| 240 | GLdouble wx,wy,wz; |
| 241 | gluProject(p[0], p[1], p[2], mm, pm, vm, &wx, &wy, &wz); |
| 242 | return Point3f(wx,vm[3]-wy,wz); |
| 243 | } |