summaryrefslogtreecommitdiff
path: root/src/OpenGl/OpenGl_pick.cxx
blob: bb5ef64abc2a4ba0be040bf67eeb21b5c9f286bc (plain)
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
/***********************************************************************

FONCTION :
----------
File OpenGl_pick :


REMARQUES:
----------       

HISTORIQUE DES MODIFICATIONS   :
--------------------------------
xx-xx-xx : xxx ; Creation.
25-06-96 : FMN ; Suppression utilisation de glScissor.
03-07-96 : FMN ; Suppression TelGetViewportAtLocation.

************************************************************************/

/* Workaround for internal bcc32 compiler bug. */						
/* Starting from bcc32 v6.40 this flag should be passed as a compiler option. */
#if defined(__BORLANDC__) && __BORLANDC__ < 0x0640 /* bcc32 v6.40 */
#pragma option -x-
#endif					

/*----------------------------------------------------------------------*/
/*
* Includes
*/ 

#include <OpenGl_tgl_all.hxx>

#include <stdio.h>

#include <GL/gl.h>

#include <OpenGl_tsm.hxx>
#include <OpenGl_tsm_ws.hxx>
#include <OpenGl_telem_pick.hxx>
#include <OpenGl_telem_view.hxx>
#include <OpenGl_Memory.hxx>


/*----------------------------------------------------------------------*/
/*
* Constantes
*/ 

#define ARRAY_GROW_SIZE 10


/*----------------------------------------------------------------------*/
/*
* Variables statiques
*/ 

static  GLuint *tgl_pick_buffer;  /* buffer to be given to GL */
static  Tint   tgl_pick_bufsize;


/*----------------------------------------------------------------------*/

static TStatus
allocate( Tint *cur_size, Tint size_reqd, void **arr, Tint elem_size )
{
  size_reqd = ( ( size_reqd / ARRAY_GROW_SIZE ) + 1 ) * ARRAY_GROW_SIZE;

  if( !( *cur_size ) ) {
    *arr = malloc( size_reqd * elem_size );
    memset( *arr, 0, size_reqd * elem_size );
  }
  else
#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)    
    *arr = realloc( *arr, size_reqd * elem_size );
#else
    *arr = (void*)cmn_resizemem<char>( (char*)*arr, size_reqd * elem_size );
#endif

  if( !(*arr) )
    return TFailure;

  *cur_size = size_reqd;
  return TSuccess;
}

static TStatus
Initialize( Tint wsid, Tint *view_stid )
{
  Tint         depth;
  CMN_KEY_DATA key;
  Tint         buf_siz_reqd = 0;

  TsmGetWSAttri( wsid, WSViewStid, &key );
  if( key.ldata == -1 )
    return TFailure;

  *view_stid = key.ldata;
  TsmGetStructureDepth( *view_stid, &depth );

  buf_siz_reqd = depth*6 + 2;

  if( tgl_pick_bufsize < buf_siz_reqd )
  {
    if( allocate( &tgl_pick_bufsize, buf_siz_reqd, (void **)&tgl_pick_buffer,
      sizeof( int ) ) == TFailure )
      return TFailure;
  }

  return TSuccess;
}
/*----------------------------------------------------------------------*/

static void
fill_pick_report( tel_pick_report rep, TPickOrder order, Tint depth )
{
  Tint  i, j;
  TEL_PACKED_NAME pn;

  rep->depth = tgl_pick_buffer[0]/6;

  if( order == TTopFirst )
  {
    for( i = 0, j = 3; i < ( Tint )(tgl_pick_buffer[0]/6) && i < depth; i++ )
    {         /* skip hit numb. zmin, zmax -> j = 3 */
      pn.s[0] = tgl_pick_buffer[j++];
      pn.s[1] = tgl_pick_buffer[j++];
      rep->pick_path[i].struct_id = pn.i;
      pn.s[0] = tgl_pick_buffer[j++];
      pn.s[1] = tgl_pick_buffer[j++];
      rep->pick_path[i].pick_id = pn.i;
      pn.s[0] = tgl_pick_buffer[j++];
      pn.s[1] = tgl_pick_buffer[j++];
      rep->pick_path[i].el_num = pn.i;
    }
  }
  else
  {
    Tint is, js;

    is = rep->depth < depth ? rep->depth : depth;
    js = 3 + ( rep->depth - is )*6;  /* skip hit numb. zmin, zmax -> j = 3 */
    is--;

    for( i = is, j = js; i >= 0; i-- )
    {
      pn.s[0] = tgl_pick_buffer[j++];
      pn.s[1] = tgl_pick_buffer[j++];
      rep->pick_path[i].struct_id = pn.i;
      pn.s[0] = tgl_pick_buffer[j++];
      pn.s[1] = tgl_pick_buffer[j++];
      rep->pick_path[i].pick_id = pn.i;
      pn.s[0] = tgl_pick_buffer[j++];
      pn.s[1] = tgl_pick_buffer[j++];
      rep->pick_path[i].el_num = pn.i;
    }
  }

  return;
}
/*----------------------------------------------------------------------*/

Tint TglVpBeingPicked = -1;

TStatus
TPick( Tint Wsid, Tint x, Tint y, Tfloat apw, Tfloat aph,
      TPickOrder order, Tint depth, tel_pick_report rep )
{
  Tint           vid;
  TSM_ELEM_DATA  data;
  CMN_KEY        key;
  CMN_KEY_DATA   key1;
  TEL_VIEW_REP   ovrep, vrep;

  if( Initialize( Wsid, &(data.ldata) ) == TFailure )
    return TFailure;

  TglActiveWs = Wsid;
  key.id      = Wsid;

  {
    /* TStatus  stat; */
    Tfloat   xsf, ysf, x1, x2, y1, y2, W, H, xm, xp, ym, yp, cx, cy;
    Tint              err;
    TEL_VIEW_MAPPING  map;

    vid  = Wsid;
    TglVpBeingPicked = vid;
    TelGetViewRepresentation( Wsid, vid, &vrep );
    ovrep = vrep;

    TsmGetWSAttri( Wsid, WSWidth, &key1 );
    W = ( float )key1.ldata;
    TsmGetWSAttri( Wsid, WSHeight, &key1 );
    H = ( float )key1.ldata;

    xm = vrep.extra.map.window.xmax, xp = vrep.extra.map.window.xmin;
    ym = vrep.extra.map.window.ymax, yp = vrep.extra.map.window.ymin;
    cx = xm + xp, cx /= 2;
    cy = ym + yp, cy /= 2;
    x1 = x - apw/2, x2 = x1 + apw, y1 = y - aph/2, y2 = y1 + aph;
    xsf = xm - xp, xsf /= W;
    ysf = ym - yp, ysf /= H;
    x1 = xsf * x1 + xp;
    y1 = ysf * y1 + yp;
    x2 = xsf * x2 + xp;
    y2 = ysf * y2 + yp;

    map = vrep.extra.map;
    map.window.xmin = x1, map.window.xmax = x2;
    map.window.ymin = y1, map.window.ymax = y2;
    TelEvalViewMappingMatrixPick( &map, &err, vrep.mapping_matrix, cx, cy );
    if( err )
      printf( "Error in Mapping pick\n" );

    if( TelSetViewRepresentation( Wsid, vid, &vrep ) == TFailure )
      printf( "Error in Set vrep for pick\n" );
  }

  glMatrixMode(GL_MODELVIEW);
  tgl_pick_buffer[0] = 0;
  glSelectBuffer( tgl_pick_bufsize ,  tgl_pick_buffer);
  glRenderMode(GL_SELECT);
  glLoadName(55);
  TsmSendMessage( TelExecuteStructure, PickTraverse, data, 1, &key );
  glRenderMode(GL_RENDER);
  TglVpBeingPicked = -1;

  /* print( ); */
  fill_pick_report( rep, order, depth );
  TelSetViewRepresentation( Wsid, vid, &ovrep );

  return TSuccess;
}
/*----------------------------------------------------------------------*/