summaryrefslogtreecommitdiff
path: root/src/OpenGl/OpenGl_togl_texture.cxx
blob: ed810b7c8b39407334367189d66d5120a724eb6d (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/***********************************************************************

FONCTION :
----------
File OpenGl_togl_texture.c :


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


HISTORIQUE DES MODIFICATIONS   :
--------------------------------
05-08-97 : PCT ; Support texture mapping
20-11-97 : FMN ; Ajout call_togl_inquiretexture
Ajout coupure du texture mapping

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

#define OCC1188  /*SAV 23/12/02 - added methods to set background image */

/*----------------------------------------------------------------------*/
/*
* Includes
*/
#include <stdlib.h>
#include <string.h>
#include <OpenGl_tgl_all.hxx>
#include <InterfaceGraphic_Labels.hxx>
#include <InterfaceGraphic_Graphic3d.hxx>
#include <InterfaceGraphic_Visual3d.hxx>
#include <OpenGl_tgl_funcs.hxx>
#include <OpenGl_TextureBox.hxx>

#include <OpenGl_cmn_varargs.hxx>
#include <OpenGl_tsm.hxx>
#include <OpenGl_tsm_ws.hxx>

#ifdef OCC1188
#include <GL/glu.h>
#endif

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

int EXPORT
call_togl_create_texture
(
 int Type,
 unsigned int Width,
 unsigned int Height, 
 unsigned char *Data,
 char *FileName
 )
{
  if (call_togl_inquiretexture ())
  {
    switch (Type)
    {
    case 0:
      return GetTextureData1D(FileName, Width, Height, Data);

    case 1:
      return GetTextureData2D(FileName, Width, Height, Data);

    case 2:
      return GetTextureData2DMipMap(FileName, Width, Height, Data);

    default:
      return -1;
    }
  }
  return -1 ;
}

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

void EXPORT
call_togl_destroy_texture
(
 int TexId
 )
{
  if (call_togl_inquiretexture ())
    FreeTexture(TexId);
}

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

void EXPORT
call_togl_modify_texture
(
 int TexId,
 CALL_DEF_INIT_TEXTURE *init_tex
 )
{
  if (call_togl_inquiretexture ())
  {
    if (init_tex->doModulate)
      SetTextureModulate(TexId);
    else
      SetTextureDecal(TexId);

    if (init_tex->doRepeat)
      SetTextureRepeat(TexId);
    else
      SetTextureClamp(TexId);

    switch (init_tex->Mode)
    {
    case 0:
      SetModeObject(TexId,
        &init_tex->sparams[0], &init_tex->tparams[0]);
      break;

    case 1:
      SetModeSphere(TexId);
      break;

    case 2:
      SetModeEye(TexId,
        &init_tex->sparams[0], &init_tex->tparams[0]);
      break;

    case 3:
      SetModeManual(TexId);
      break;        
    }

    if (init_tex->doLinear)
      SetRenderLinear(TexId);
    else
      SetRenderNearest(TexId);

    SetTexturePosition(TexId,
      init_tex->sx, init_tex->sy,
      init_tex->tx, init_tex->ty,
      init_tex->angle);
  }
}

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

void EXPORT 
call_togl_environment(CALL_DEF_VIEW *aview)
{
  CMN_KEY_DATA data;

  if (call_togl_inquiretexture ())
  {
    data.ldata = aview->Context.TexEnvId;
    TsmSetWSAttri(aview->WsId, WSTextureEnv, &data);

    data.ldata = aview->Context.SurfaceDetail;
    TsmSetWSAttri(aview->WsId, WSSurfaceDetail, &data);
  }
}

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

int EXPORT
call_togl_inquiretexture ()
{
#if defined(__sun)
  return 1;
#else
  return 1;
#endif /* SUN */
}

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

#ifdef OCC1188
void EXPORT call_togl_create_bg_texture( CALL_DEF_VIEW* view, int width, int height, 
                                        unsigned char* data, int style )
{
  TSM_BG_TEXTURE tex;
  tsm_bg_texture createdTex;
  CMN_KEY_DATA  cmnData;
  CMN_KEY_DATA  cmnKey;
  GLuint texture = 0;
  GLubyte *image = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
  memcpy( image, data, ( width * height * 3 ) );

  /* check if some bg texture is already created */
  TsmGetWSAttri( view->WsId, WSBgTexture, &cmnData );
  createdTex = (tsm_bg_texture)cmnData.pdata;
  if ( createdTex->texId != 0 )
    glDeleteTextures( 1, (GLuint*)&(createdTex->texId) );
  glGenTextures( 1, &texture );
  glBindTexture( GL_TEXTURE_2D, texture );
  /* Create MipMapped Texture */
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  gluBuild2DMipmaps( GL_TEXTURE_2D, 3/*4*/, width, height, GL_RGB, GL_UNSIGNED_BYTE, image );

  tex.texId = texture;
  tex.width = width;
  tex.height = height;
  switch ( style ) {
    case 0 :
    case 1 :
      tex.style = TSM_FS_CENTER;
      break;
    case 2 :
      tex.style = TSM_FS_TILE;
      break;
    case 3 :
      tex.style = TSM_FS_STRETCH;
      break;
    default :
      tex.style = TSM_FS_CENTER;
      break;
  }

  /* setting flag to update changes */
  cmnKey.ldata = TNotDone;
  TsmSetWSAttri( view->WsId, WSUpdateState, &cmnKey );

  /* storing background texture */
  cmnData.pdata = &tex;
  TsmSetWSAttri( view->WsId, WSBgTexture, &cmnData );
  free( image );
}

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

void EXPORT call_togl_set_bg_texture_style( CALL_DEF_VIEW* view, int style )
{
  tsm_bg_texture texture;
  CMN_KEY_DATA  cmnData;
  CMN_KEY_DATA  cmnKey;

  /* check if background texture is already created */
  TsmGetWSAttri( view->WsId, WSBgTexture, &cmnData );
  texture = (tsm_bg_texture)cmnData.pdata;
  if ( texture->texId != 0 ) {
    switch ( style ) {
        case 0 :
        case 1 :
          texture->style = TSM_FS_CENTER;
          break;
        case 2 :
          texture->style = TSM_FS_TILE;
          break;
        case 3 :
          texture->style = TSM_FS_STRETCH;
          break;
        default :
          texture->style = TSM_FS_CENTER;
          break;
    }

    /* setting flag to update changes */
    cmnKey.ldata = TNotDone;
    TsmSetWSAttri( view->WsId, WSUpdateState, &cmnKey );

    /* storing background texture */
    cmnData.pdata = texture;
    TsmSetWSAttri( view->WsId, WSBgTexture, &cmnData );
  }
}
#endif /* OCC1188 */
/*----------------------------------------------------------------------*/