File: PickWidget.cs

package info (click to toggle)
gtkglarea-sharp 0.0.17-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,144 kB
  • ctags: 492
  • sloc: sh: 3,376; cs: 2,633; makefile: 421; xml: 126
file content (242 lines) | stat: -rw-r--r-- 6,299 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;

using Tao.OpenGl;
using gl=Tao.OpenGl.Gl;
using glu=Tao.OpenGl.Glu;
using GtkGL;

using Gtk;

namespace GtkGL {

	public enum InputMode {
		TrackballMode,
		PickMode,
		TranslateMode,
		ScaleMode
	}

	public class PickWidget: GtkGL.GLWidget {
	      
	    //Trackball.Trackball tb = new Trackball.Trackball();
		double beginX = 0;
	  	double beginY = 0;
	  	bool button1Pressed = false;
		GtkGL.Quaternion quat = GtkGL.Quaternion.Identity;
		
		InputMode currentInputMode = InputMode.PickMode;
		//InputMode currentInputMode = InputMode.TrackballMode;
		public InputMode CurrentInputMode {
			get { return currentInputMode; }
			set { currentInputMode = value; }
		}
		
		
	    public PickWidget() : base()
	    {
			base.GLSetup += GtkGL.GLWidget.EnableLighting;
	    
	    	// Create and initialize the quaternion
			quat = new GtkGL.Quaternion(0.0, 0.0, 0.0, 1.0);

			connectHandlers();
	    }
	    
	    void connectHandlers()
	    {
	    	this.Events |=
	    		Gdk.EventMask.Button1MotionMask |
		    	Gdk.EventMask.Button2MotionMask |
	    		Gdk.EventMask.ButtonPressMask |
	    		Gdk.EventMask.ButtonReleaseMask |
	    		Gdk.EventMask.VisibilityNotifyMask |
	    		Gdk.EventMask.PointerMotionMask |
	    		Gdk.EventMask.PointerMotionHintMask ;
	    
	    	this.ButtonPressEvent += OnButtonPress;
			this.ButtonReleaseEvent += OnButtonRelease;
			this.MotionNotifyEvent += OnMotionNotify;

	    }
	        
	   	// Track mouse dragging
		void OnMotionNotify (object o, Gtk.MotionNotifyEventArgs e)
		{
			int ix, iy;
			double x, y;
			Gdk.ModifierType m;
			
			// Find the current mouse X and Y positions
			if (e.Event.IsHint) {
				e.Event.Window.GetPointer(out ix, out iy, out m);
				x = (double)ix;
				y = (double)iy;
			} else {
	    		x = e.Event.X;
	    		y = e.Event.Y;
	  		}
		}
		
		void OnButtonPress (object o, Gtk.ButtonPressEventArgs e)
		{
			if(e.Event.Button != 1)
				return;

			button1Pressed = true;
				
			/* potential beginning of drag, reset mouse position */
			beginX = e.Event.X;
			beginY = e.Event.Y;
		}
		
		struct hitStruct {
			public int numNames;
			public double minZ;
			public double maxZ;
			public int[] nameStack;
		}

		void processHits (int hits, int[] buffer)
		{
			uint i, j=0;
			uint names, minZ, maxZ, numberOfNames;
			
			for (i = 0; i < hits; i++) {
				hitStruct hit = new hitStruct();
				
				// Get the number of names hit
				hit.numNames = buffer[j++];

				// I don't know if this is correct...
				// Findour min and max Z values
				hit.minZ = (double) (buffer[j++] / 0x7fffffff);
				hit.maxZ = (double) (buffer[j++] / 0x7fffffff);
				
				// Console.WriteLine("Hit occured between [{0} .. {1}]", hit.minZ, hit.maxZ);
				
				// Allocate some space for our hit names
				hit.nameStack = new int[hit.numNames];
				
				for(int k = 0; k < hit.numNames; k++){
					hit.nameStack[k] = buffer[j++];
					
					// Select the objects that were hit
					System.Collections.IEnumerator enumerator = GLObjectList.GetEnumerator();
	  	
		  			while(enumerator.MoveNext()){
		  				if(( (GtkGL.IGLObject) enumerator.Current ).ID == hit.nameStack[k]){
		  					( (GtkGL.IGLObject) enumerator.Current ).Selected = true;
		  					Console.WriteLine("Object {0} selected.", hit.nameStack[k]);
		  				}
		  			}
				}
			}
		}
		
		void OnButtonRelease (object o, Gtk.ButtonReleaseEventArgs e)
		{
			if(e.Event.Button != 1)
				return;

			button1Pressed = false;

			if(this.CurrentInputMode == GtkGL.InputMode.PickMode){
				// This document is based on the following example:
				// http://www.lighthouse3d.com/opengl/picking/
				// Console.WriteLine("Entering Pick mode...");

				// Console.WriteLine("Deselecting all objects");				
				System.Collections.IEnumerator enumerator = GLObjectList.GetEnumerator();
  	
	  			while(enumerator.MoveNext()){
					( (GtkGL.IGLObject) enumerator.Current ).Selected = false;
	  			}
		  			
				// Get Mouse coordinates
	    		double x = e.Event.X, y = e.Event.Y;
		
				if( this.MakeCurrent() == 0)
					return;
					
				double xCenter, yCenter,
					   width, height;
					   
				if(x > beginX){
					width = x - beginX;
					xCenter = beginX + ( width / 2 );
				}else{
					width = beginX - x;
					xCenter = x + ( width / 2 );
				}
				
				if(y > beginY){
					height = y - beginY;
					yCenter = beginY + ( height / 2 );
				}else{
					height = beginY - y;
					yCenter = y + ( height / 2 );
				}
				
				// Establish a buffer for selection mode values
				int[] selectBuf = new int[64];
				// http://www.mevis.de/opengl/glSelectBuffer.html
				gl.glSelectBuffer (64, selectBuf);
				
				// http://www.mevis.de/opengl/glRenderMode.html
				gl.glRenderMode(gl.GL_SELECT); // Enter the SELECT render mode

				gl.glMatrixMode(gl.GL_PROJECTION);				// Select The Projection Matrix
				gl.glPushMatrix();                              // Save our state
				gl.glLoadIdentity();							// Reset The Projection Matrix

				// Get the viewport
				int[] viewport = new int[4];
				gl.glGetIntegerv(gl.GL_VIEWPORT, viewport);
					
				glu.gluPickMatrix(xCenter, viewport[3] - yCenter, width+1, height+1, viewport);
				
				int windowWidth, windowHeight;
				e.Event.Window.GetSize(out windowWidth, out windowHeight);
				
				glu.gluPerspective(45,windowWidth / windowHeight,0.1,100);
				gl.glTranslatef(0.0f,0.0f,-6.0f);				// Move away from the drawing area 6.0				
				
				gl.glMatrixMode(gl.GL_MODELVIEW);
				
				// http://www.mevis.de/opengl/glInitNames.html				
				gl.glInitNames();  // Initialize the names stack
				
				// Draw the objects
				enumerator = GLObjectList.GetEnumerator();
	  	
	  			while(enumerator.MoveNext()){
	  				( (GtkGL.IGLObject) enumerator.Current ).Draw();
	  			}
	  			
	  			gl.glMatrixMode(gl.GL_PROJECTION);
	  			gl.glPopMatrix();
	  			
	  			gl.glMatrixMode(gl.GL_MODELVIEW);
	  			gl.glFlush();
	  			
				// Return to normal rendering mode
				int hits = gl.glRenderMode(gl.GL_RENDER);
				
				processHits(hits, selectBuf);

				// Console.WriteLine("Exited picking mode");
			}
	    }
	    
		private void OnQuit (object o, System.EventArgs e){
			Application.Quit();
		}

		private void OnWindowDeleteEvent (object sender, Gtk.DeleteEventArgs a) 
		{
			Application.Quit ();
			a.RetVal = true;
		}	    
	    
	}
}