Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <GL/glew.h>
00030 #include <wrap/gl/space.h>
00031 #include <wrap/gl/picking.h>
00032 #include <wrap/qt/device_to_logical.h>
00033
00034 #include "rubberband.h"
00035
00036 using namespace vcg;
00037
00038 Rubberband::Rubberband(Color4b c)
00039 :color(c)
00040 {
00041 this->Reset();
00042 }
00043
00044 void Rubberband::Render(QGLWidget* gla)
00045 {
00046 if(have_to_pick){
00047 assert(currentphase!=RUBBER_DRAGGED);
00048 Point3f pick_point;
00049 bool picked = Pick(QTLogicalToDevice(gla, qt_cursor.x()), QTLogicalToDevice(gla, gla->height() - qt_cursor.y()), pick_point);
00050 if(picked){
00051 have_to_pick=false;
00052 switch(currentphase){
00053 case RUBBER_BEGIN:
00054 start = pick_point;
00055 gla->setMouseTracking(true);
00056 currentphase = RUBBER_DRAGGING;
00057 break;
00058 case RUBBER_DRAGGING:
00059 if(pick_point==start){
00060 have_to_pick=true;
00061 break;
00062 }
00063 end = pick_point;
00064 gla->setMouseTracking(false);
00065 currentphase = RUBBER_DRAGGED;
00066 break;
00067 default:
00068 assert(0);
00069 }
00070 }
00071 }
00072
00073 if(currentphase==RUBBER_BEGIN) return;
00074
00075
00076 glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
00077 glDisable(GL_LIGHTING);
00078 glDisable(GL_TEXTURE_2D);
00079 glDepthMask(false);
00080 glLineWidth(2.5);
00081 glPointSize(5.0);
00082
00083 if(currentphase==RUBBER_DRAGGING )
00084 {
00085 Point2f qt_start_point = DevicePixelConvert(start);
00086 glColor(color);
00087 glMatrixMode(GL_PROJECTION);
00088 glPushMatrix();
00089 glLoadIdentity();
00090 gluOrtho2D(0, QTLogicalToDevice(gla,gla->width()), QTLogicalToDevice(gla,gla->height()), 0);
00091 glMatrixMode(GL_MODELVIEW);
00092 glPushMatrix();
00093 glLoadIdentity();
00094 glDisable(GL_DEPTH_TEST);
00095 glBegin(GL_LINES);
00096 glVertex(qt_start_point);
00097 glVertex2f(QTLogicalToDevice(gla, qt_cursor.x()), QTLogicalToDevice(gla, qt_cursor.y()));
00098 glEnd();
00099 glEnable(GL_DEPTH_TEST);
00100 glMatrixMode(GL_PROJECTION);
00101 glPopMatrix();
00102 glMatrixMode(GL_MODELVIEW);
00103 glPopMatrix();
00104 }
00105 else
00106 {
00107 assert(currentphase == RUBBER_DRAGGED);
00108 glEnable(GL_BLEND);
00109 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR);
00110 glEnable(GL_LINE_SMOOTH);
00111 glEnable(GL_POINT_SMOOTH);
00112 glColor(color);
00113 glLineWidth(2.0);
00114 glPointSize(4.0);
00115 glBegin(GL_LINES);
00116 glVertex(start);
00117 glVertex(end);
00118 glEnd();
00119 glBegin(GL_POINTS);
00120 glVertex(start);
00121 glVertex(end);
00122 glEnd();
00123 glDepthFunc(GL_GREATER);
00124 glLineWidth(1.0f);
00125 glPointSize(2.0f);
00126 glBegin(GL_LINES);
00127 glVertex(start);
00128 glVertex(end);
00129 glEnd();
00130 glBegin(GL_POINTS);
00131 glVertex(start);
00132 glVertex(end);
00133 glEnd();
00134 glDepthFunc(GL_LESS);
00135 }
00136
00137
00138 glPopAttrib();
00139 assert(!glGetError());
00140 }
00141
00142 void Rubberband::RenderLine(QGLWidget* gla, Point3f AA, Point3f BB)
00143 {
00144
00145 glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
00146 glDisable(GL_LIGHTING);
00147 glDisable(GL_TEXTURE_2D);
00148 glDepthMask(false);
00149 glLineWidth(2.5);
00150 glPointSize(6.0);
00151 glEnable(GL_BLEND);
00152 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR);
00153 glEnable(GL_LINE_SMOOTH);
00154 glEnable(GL_POINT_SMOOTH);
00155 glColor(color);
00156 glLineWidth(2.0);
00157 glPointSize(5.0);
00158 glBegin(GL_LINES);
00159 glVertex(AA);
00160 glVertex(BB);
00161 glEnd();
00162 glBegin(GL_POINTS);
00163 glVertex(AA);
00164 glVertex(BB);
00165 glEnd();
00166 glDepthFunc(GL_GREATER);
00167 glLineWidth(1.0f);
00168 glPointSize(2.0f);
00169 glBegin(GL_LINES);
00170 glVertex(AA);
00171 glVertex(BB);
00172 glEnd();
00173 glBegin(GL_POINTS);
00174 glVertex(AA);
00175 glVertex(BB);
00176 glEnd();
00177 glDepthFunc(GL_LESS);
00178
00179 glPopAttrib();
00180 assert(!glGetError());
00181 }
00182
00183 void Rubberband::Drag(QPoint p)
00184 {
00185 if(currentphase==RUBBER_DRAGGING)
00186 qt_cursor=p;
00187 }
00188
00189 void Rubberband::Pin(QPoint p)
00190 {
00191 if(IsReady())
00192 return;
00193 qt_cursor=p;
00194 have_to_pick=true;
00195 }
00196
00197 void Rubberband::Reset()
00198 {
00199 currentphase = RUBBER_BEGIN;
00200 qt_cursor = QPoint();
00201 start = Point3f(0,0,0);
00202 end = Point3f(0,0,0);
00203 have_to_pick = false;
00204 }
00205
00206 bool Rubberband::IsReady()
00207 {
00208 return currentphase==RUBBER_DRAGGED;
00209 }
00210
00211 void Rubberband::GetPoints(Point3f &s,Point3f &e)
00212 {
00213 assert(IsReady());
00214 s=start;
00215 e=end;
00216 }
00217
00218 Point2f Rubberband::DevicePixelConvert(const Point3f p)
00219 {
00220 GLint vm[4];
00221 GLdouble mm[16];
00222 GLdouble pm[16];
00223 glGetIntegerv(GL_VIEWPORT, vm);
00224 glGetDoublev(GL_MODELVIEW_MATRIX, mm);
00225 glGetDoublev(GL_PROJECTION_MATRIX, pm);
00226 GLdouble wx,wy,wz;
00227 gluProject(p[0], p[1], p[2], mm, pm, vm, &wx, &wy, &wz);
00228 return Point2f(wx,vm[3]-wy);
00229 }