00001 #ifndef VRS_SG_SCENEPICKER_H__
00002 #define VRS_SG_SCENEPICKER_H__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <vrs/config.h>
00022 #include <vrs/sg/interactiontechnique.h>
00023 #include <vrs/callback.h>
00024 #include <vrs/so.h>
00025 #include <vrs/matrix.h>
00026 #include <vrs/vertexdata.h>
00027
00028
00029 namespace VRS {
00030
00031 class SceneThing;
00032
00033 class ButtonEvent;
00034 class FrameBufferObjectGL;
00035 class BufferClearance;
00036 class RenderTextureGL;
00037 class RenderBufferGL;
00038 class Image;
00039 class GLCanvas;
00040 class PolygonSet;
00041 class Texture2DGL;
00042 class ImageTexture2DGL;
00043 class Shape;
00044 class TextureUnitSelectorGL;
00045
00046 template<class C> class Array2D;
00047
00048 namespace GL2{
00049 class ProgramObject;
00050 class VertexShaderObject;
00051 class FragmentShaderObject;
00052 class SamplerVariable;
00053 template<class C> class UniformVariable;
00054 }
00055
00056
00064 class VRS_CORE_API ScenePicker : public InteractionTechnique{
00065
00066 public:
00067 static const MouseButtonFunctionID pickFunction;
00068
00069 struct PickResult {
00070 PickResult(unsigned id, Vector& position, bool hit, InputEvent::Button but, InputEvent::Modifier mod):id(id),position(position),hit(hit),button(but),mod(mod){};
00071 PickResult(){id = 0; position = Vector::origin; hit = false;};
00072 unsigned int id;
00073 Vector position;
00074 bool hit;
00075 InputEvent::Modifier mod;
00076 InputEvent::Button button;
00077 };
00078
00079 class VRS_CORE_API GLImage : public VRS::SharedObj{
00080 public:
00081 GLImage(unsigned int width, unsigned int height);
00082
00083 ~GLImage();
00084
00085 float getValue(unsigned int xPos,unsigned int yPos, unsigned int component);
00086 float* getData(){return data;};
00087 unsigned int getPixelCount(){return width*height;};
00088 unsigned int getHeight(){return height;};
00089 unsigned int getWidth(){return width;};
00090 Color getColor(unsigned int xPos, unsigned int yPos);
00091 VertexData<4,float> getVector(unsigned int xPos, unsigned int yPos);
00092 SO<Image> toVRSImage();
00093 private:
00094 float* data;
00095 unsigned int width;
00096 unsigned int height;
00097 };
00098
00099 typedef Callback3<SO<Iterator<PickResult> >,unsigned int, unsigned int> SelectionCallback;
00100 typedef CallbackR1<unsigned int,SO<Shape> > IdCreationCallback;
00101
00114 ScenePicker(SO<SceneThing> scene,
00115 SO<IdCreationCallback> idCreationCallback,
00116 SO<SelectionCallback> pickingCallback,
00117 SO<GLCanvas> canvas,
00118 unsigned int textureUnit,
00119 unsigned int numberOfIdsToReserve = 10000,
00120 bool renderPositionBuffer = true,
00121 SO<Iterator<ScenePicker::MouseButtonFunctionID> > buttonFunctionsToRegister = 0);
00122
00123 void setupFBO( SO<GLCanvas> canvas );
00124
00125 virtual void onActivation();
00126 virtual void onDeactivation();
00127
00128 virtual void onDragStart(int x, int y, SO<ButtonEvent>, MouseButtonFunctionID id);
00129 virtual void onDragMotion(int x, int y, SO<MotionEvent>, MouseButtonFunctionID id, int startX, int startY);
00130 virtual void onDragEnd(int x, int y, SO<ButtonEvent>, MouseButtonFunctionID id, int startX, int startY);
00131
00132 virtual void onEvent(SO<Event> e);
00133
00137 void updatePickingBuffers();
00138
00143 inline bool preservingViewDirectionRequired(){ return true;}
00144
00145
00146 bool getRangeSelectionEnabled() const { return rangeSelectionEnabled_; }
00147
00152 void setRangeSelectionEnabled(bool val) { rangeSelectionEnabled_ = val; }
00153
00154
00155
00164 void registerSelectionCallback(const SO<SelectionCallback> newSelectionCallback);
00165 void unregisterSelectionCallback(const SO<SelectionCallback> exisitingSelectioncallback);
00166 void clearSelectionCallbacks();
00167
00176 void assignIdValues(SO<SceneThing> scene,SO<IdCreationCallback> idCreationCallback, unsigned int numberOfIdValues, unsigned int startId = 0);
00177
00184 void setSceneForRender(SO<SceneThing> scene);
00185
00194 const Vector addPolygonSet(unsigned int id,SO<PolygonSet> pset, bool updateLookupTexture = false);
00195
00196 void incrementTextureCoordinate();
00205 SO<Iterator<Vector> > addPolygonSets(SO<Iterator<unsigned int> > ids,SO<Iterator<SO<PolygonSet> > > psets, bool updateLookupTexture = false);
00206
00215 SO<Iterator<Vector> > addPolygonSets(SO<Iterator<unsigned int> > ids,SO<Iterator<SO<Iterator<SO<PolygonSet> > > > > psets, bool updateLookupTexture = false);
00216
00224 void addSceneThing(SO<SceneThing> scene, unsigned int id, bool updateLookupTexture = false);
00225
00226 const SO<GLImage> getPixelPositions() const {return this->positionPickingBufferData;};
00227 const SO<GLImage> getIdBufferImage();
00228
00229
00238 const bool getCameraSpaceCoordinateForPixel(unsigned int x, unsigned int y,Vector& coordinate) const;
00246 const unsigned int getIdForPixel(unsigned int x,unsigned int y) const;
00247
00248 bool getCallOnNoHit() const { return callOnNoHit_; }
00249 void setCallOnNoHit(bool val) { callOnNoHit_ = val; }
00250
00251 static VRS::UINT32 getIdForColor(const Color& c);
00252 static Color getColorForId(VRS::UINT32 id);
00253
00254
00261 static unsigned int ScenePicker::getPixelForCoordinate(float coordinate, unsigned int size);
00262 static float getCoordinateForPixel(unsigned pixel, unsigned int size);
00263
00264 bool ScenePicker::hasUnassignedIds(unsigned int numberOfRequiredIds);
00265
00266 unsigned int getTextureUnit(){return this->textureUnit_;};
00267 VRS_TYPEINFO(ScenePicker,InteractionTechnique);
00268
00269 protected:
00270
00271 private:
00272
00276 void createRenderScene();
00277
00281 void handleResize(unsigned int newWidth,unsigned int newHeight);
00282
00283
00284 void initIdImageTexture(unsigned int numberOfIdValues);
00285
00286
00287 SO<Array<SO<SelectionCallback> > > selectionCallbacks_;
00288
00289 unsigned int textureUnit_;
00290
00291 SO<FrameBufferObjectGL> fbo_;
00292
00293 SO<Image> idImage_;
00294
00295 SO<ImageTexture2DGL> idTexture_;
00296
00297 SO<Texture2DGL> idColorScreenTexture_;
00298
00299
00300 SO<RenderTextureGL> idColorRenderTarget_;
00301
00302 SO<Texture2DGL> pixelPositionScreenTexture_;
00303
00304 SO<RenderTextureGL> pixelPositionRenderTarget_;
00305 SO<BufferClearance> clearance_;
00306 SO<RenderBufferGL> depthBuffer;
00307 SO<GL2::ProgramObject> idShaderProgram_;
00308
00309 SO<TextureUnitSelectorGL> unitZeroSelector;
00310
00311 SO<SceneThing> renderScene_;
00312
00313
00314 SO<SceneThing> contentScene_;
00315
00316 SO<GLCanvas> canvas_;
00317
00318 bool rangeSelectionEnabled_;
00319
00320
00321 bool renderPositionBuffer_;
00322
00323 bool callOnNoHit_;
00324
00325
00326 unsigned int imageWidth;
00327 unsigned int numberOfIdsGiven_;
00328 float currentStep_;
00329 float currentXVal_, currentYVal_;
00330
00331 bool dragging_;
00332
00333
00334 SO<GLImage> idPickingBufferData;
00335 SO<GLImage> positionPickingBufferData;
00336 unsigned int pixelsInIdTexture;
00337
00338 };
00339
00340
00341 };
00342
00343 #endif // VRS_SG_OBJECTPICKER_H__