1 /***********************************************************************
11 HISTORIQUE DES MODIFICATIONS :
12 --------------------------------
13 xx-xx-xx : xxx ; Creation.
14 25-06-96 : FMN ; Suppression utilisation de glScissor.
15 03-07-96 : FMN ; Suppression TelGetViewportAtLocation.
17 ************************************************************************/
19 /*----------------------------------------------------------------------*/
24 #include <OpenGl_tgl_all.hxx>
30 #include <OpenGl_tsm.hxx>
31 #include <OpenGl_tsm_ws.hxx>
32 #include <OpenGl_telem_pick.hxx>
33 #include <OpenGl_telem_view.hxx>
34 #include <OpenGl_Memory.hxx>
37 /*----------------------------------------------------------------------*/
42 #define ARRAY_GROW_SIZE 10
45 /*----------------------------------------------------------------------*/
50 static GLuint *tgl_pick_buffer; /* buffer to be given to GL */
51 static Tint tgl_pick_bufsize;
54 /*----------------------------------------------------------------------*/
57 allocate( Tint *cur_size, Tint size_reqd, void **arr, Tint elem_size )
59 size_reqd = ( ( size_reqd / ARRAY_GROW_SIZE ) + 1 ) * ARRAY_GROW_SIZE;
61 if( !( *cur_size ) ) {
62 *arr = malloc( size_reqd * elem_size );
63 memset( *arr, 0, size_reqd * elem_size );
66 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
67 *arr = realloc( *arr, size_reqd * elem_size );
69 *arr = (void*)cmn_resizemem<char>( (char*)*arr, size_reqd * elem_size );
75 *cur_size = size_reqd;
80 Initialize( Tint wsid, Tint *view_stid )
84 Tint buf_siz_reqd = 0;
86 TsmGetWSAttri( wsid, WSViewStid, &key );
90 *view_stid = key.ldata;
91 TsmGetStructureDepth( *view_stid, &depth );
93 buf_siz_reqd = depth*6 + 2;
95 if( tgl_pick_bufsize < buf_siz_reqd )
97 if( allocate( &tgl_pick_bufsize, buf_siz_reqd, (void **)&tgl_pick_buffer,
98 sizeof( int ) ) == TFailure )
104 /*----------------------------------------------------------------------*/
107 fill_pick_report( tel_pick_report rep, TPickOrder order, Tint depth )
112 rep->depth = tgl_pick_buffer[0]/6;
114 if( order == TTopFirst )
116 for( i = 0, j = 3; i < ( Tint )(tgl_pick_buffer[0]/6) && i < depth; i++ )
117 { /* skip hit numb. zmin, zmax -> j = 3 */
118 pn.s[0] = tgl_pick_buffer[j++];
119 pn.s[1] = tgl_pick_buffer[j++];
120 rep->pick_path[i].struct_id = pn.i;
121 pn.s[0] = tgl_pick_buffer[j++];
122 pn.s[1] = tgl_pick_buffer[j++];
123 rep->pick_path[i].pick_id = pn.i;
124 pn.s[0] = tgl_pick_buffer[j++];
125 pn.s[1] = tgl_pick_buffer[j++];
126 rep->pick_path[i].el_num = pn.i;
133 is = rep->depth < depth ? rep->depth : depth;
134 js = 3 + ( rep->depth - is )*6; /* skip hit numb. zmin, zmax -> j = 3 */
137 for( i = is, j = js; i >= 0; i-- )
139 pn.s[0] = tgl_pick_buffer[j++];
140 pn.s[1] = tgl_pick_buffer[j++];
141 rep->pick_path[i].struct_id = pn.i;
142 pn.s[0] = tgl_pick_buffer[j++];
143 pn.s[1] = tgl_pick_buffer[j++];
144 rep->pick_path[i].pick_id = pn.i;
145 pn.s[0] = tgl_pick_buffer[j++];
146 pn.s[1] = tgl_pick_buffer[j++];
147 rep->pick_path[i].el_num = pn.i;
153 /*----------------------------------------------------------------------*/
155 Tint TglVpBeingPicked = -1;
158 TPick( Tint Wsid, Tint x, Tint y, Tfloat apw, Tfloat aph,
159 TPickOrder order, Tint depth, tel_pick_report rep )
165 TEL_VIEW_REP ovrep, vrep;
167 if( Initialize( Wsid, &(data.ldata) ) == TFailure )
175 Tfloat xsf, ysf, x1, x2, y1, y2, W, H, xm, xp, ym, yp, cx, cy;
177 TEL_VIEW_MAPPING map;
180 TglVpBeingPicked = vid;
181 TelGetViewRepresentation( Wsid, vid, &vrep );
184 TsmGetWSAttri( Wsid, WSWidth, &key1 );
185 W = ( float )key1.ldata;
186 TsmGetWSAttri( Wsid, WSHeight, &key1 );
187 H = ( float )key1.ldata;
189 xm = vrep.extra.map.window.xmax, xp = vrep.extra.map.window.xmin;
190 ym = vrep.extra.map.window.ymax, yp = vrep.extra.map.window.ymin;
191 cx = xm + xp, cx /= 2;
192 cy = ym + yp, cy /= 2;
193 x1 = x - apw/2, x2 = x1 + apw, y1 = y - aph/2, y2 = y1 + aph;
194 xsf = xm - xp, xsf /= W;
195 ysf = ym - yp, ysf /= H;
201 map = vrep.extra.map;
202 map.window.xmin = x1, map.window.xmax = x2;
203 map.window.ymin = y1, map.window.ymax = y2;
204 TelEvalViewMappingMatrixPick( &map, &err, vrep.mapping_matrix, cx, cy );
206 printf( "Error in Mapping pick\n" );
208 if( TelSetViewRepresentation( Wsid, vid, &vrep ) == TFailure )
209 printf( "Error in Set vrep for pick\n" );
212 glMatrixMode(GL_MODELVIEW);
213 tgl_pick_buffer[0] = 0;
214 glSelectBuffer( tgl_pick_bufsize , tgl_pick_buffer);
215 glRenderMode(GL_SELECT);
217 TsmSendMessage( TelExecuteStructure, PickTraverse, data, 1, &key );
218 glRenderMode(GL_RENDER);
219 TglVpBeingPicked = -1;
222 fill_pick_report( rep, order, depth );
223 TelSetViewRepresentation( Wsid, vid, &ovrep );
227 /*----------------------------------------------------------------------*/