OCC22108 Cutting plane unpredictable behaviour in V3d_View
[occt.git] / src / OpenGl / OpenGl_togl_texture.cxx
1 /***********************************************************************
2
3 FONCTION :
4 ----------
5 File OpenGl_togl_texture.c :
6
7
8 REMARQUES:
9 ---------- 
10
11
12 HISTORIQUE DES MODIFICATIONS   :
13 --------------------------------
14 05-08-97 : PCT ; Support texture mapping
15 20-11-97 : FMN ; Ajout call_togl_inquiretexture
16 Ajout coupure du texture mapping
17
18 ************************************************************************/
19
20 #define OCC1188  /*SAV 23/12/02 - added methods to set background image */
21
22 /*----------------------------------------------------------------------*/
23 /*
24 * Includes
25 */
26 #include <stdlib.h>
27 #include <string.h>
28 #include <OpenGl_tgl_all.hxx>
29 #include <InterfaceGraphic_Labels.hxx>
30 #include <InterfaceGraphic_Graphic3d.hxx>
31 #include <InterfaceGraphic_Visual3d.hxx>
32 #include <OpenGl_tgl_funcs.hxx>
33 #include <OpenGl_TextureBox.hxx>
34
35 #include <OpenGl_cmn_varargs.hxx>
36 #include <OpenGl_tsm.hxx>
37 #include <OpenGl_tsm_ws.hxx>
38
39 #ifdef OCC1188
40 #include <GL/glu.h>
41 #endif
42
43 /*----------------------------------------------------------------------*/
44
45 int EXPORT
46 call_togl_create_texture
47 (
48  int Type,
49  unsigned int Width,
50  unsigned int Height, 
51  unsigned char *Data,
52  char *FileName
53  )
54 {
55   if (call_togl_inquiretexture ())
56   {
57     switch (Type)
58     {
59     case 0:
60       return GetTextureData1D(FileName, Width, Height, Data);
61
62     case 1:
63       return GetTextureData2D(FileName, Width, Height, Data);
64
65     case 2:
66       return GetTextureData2DMipMap(FileName, Width, Height, Data);
67
68     default:
69       return -1;
70     }
71   }
72   return -1 ;
73 }
74
75 /*----------------------------------------------------------------------*/
76
77 void EXPORT
78 call_togl_destroy_texture
79 (
80  int TexId
81  )
82 {
83   if (call_togl_inquiretexture ())
84     FreeTexture(TexId);
85 }
86
87 /*----------------------------------------------------------------------*/
88
89 void EXPORT
90 call_togl_modify_texture
91 (
92  int TexId,
93  CALL_DEF_INIT_TEXTURE *init_tex
94  )
95 {
96   if (call_togl_inquiretexture ())
97   {
98     if (init_tex->doModulate)
99       SetTextureModulate(TexId);
100     else
101       SetTextureDecal(TexId);
102
103     if (init_tex->doRepeat)
104       SetTextureRepeat(TexId);
105     else
106       SetTextureClamp(TexId);
107
108     switch (init_tex->Mode)
109     {
110     case 0:
111       SetModeObject(TexId,
112         &init_tex->sparams[0], &init_tex->tparams[0]);
113       break;
114
115     case 1:
116       SetModeSphere(TexId);
117       break;
118
119     case 2:
120       SetModeEye(TexId,
121         &init_tex->sparams[0], &init_tex->tparams[0]);
122       break;
123
124     case 3:
125       SetModeManual(TexId);
126       break;        
127     }
128
129     if (init_tex->doLinear)
130       SetRenderLinear(TexId);
131     else
132       SetRenderNearest(TexId);
133
134     SetTexturePosition(TexId,
135       init_tex->sx, init_tex->sy,
136       init_tex->tx, init_tex->ty,
137       init_tex->angle);
138   }
139 }
140
141 /*----------------------------------------------------------------------*/
142
143 void EXPORT 
144 call_togl_environment(CALL_DEF_VIEW *aview)
145 {
146   CMN_KEY_DATA data;
147
148   if (call_togl_inquiretexture ())
149   {
150     data.ldata = aview->Context.TexEnvId;
151     TsmSetWSAttri(aview->WsId, WSTextureEnv, &data);
152
153     data.ldata = aview->Context.SurfaceDetail;
154     TsmSetWSAttri(aview->WsId, WSSurfaceDetail, &data);
155   }
156 }
157
158 /*----------------------------------------------------------------------*/
159
160 int EXPORT
161 call_togl_inquiretexture ()
162 {
163 #if defined(__sun)
164   return 1;
165 #else
166   return 1;
167 #endif /* SUN */
168 }
169
170 /*----------------------------------------------------------------------*/
171
172 #ifdef OCC1188
173 void EXPORT call_togl_create_bg_texture( CALL_DEF_VIEW* view, int width, int height, 
174                                         unsigned char* data, int style )
175 {
176   TSM_BG_TEXTURE tex;
177   tsm_bg_texture createdTex;
178   CMN_KEY_DATA  cmnData;
179   CMN_KEY_DATA  cmnKey;
180   GLuint texture = 0;
181   GLubyte *image = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
182   memcpy( image, data, ( width * height * 3 ) );
183
184   /* check if some bg texture is already created */
185   TsmGetWSAttri( view->WsId, WSBgTexture, &cmnData );
186   createdTex = (tsm_bg_texture)cmnData.pdata;
187   if ( createdTex->texId != 0 )
188     glDeleteTextures( 1, (GLuint*)&(createdTex->texId) );
189   glGenTextures( 1, &texture );
190   glBindTexture( GL_TEXTURE_2D, texture );
191   /* Create MipMapped Texture */
192   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
193   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
194   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
195   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
196   gluBuild2DMipmaps( GL_TEXTURE_2D, 3/*4*/, width, height, GL_RGB, GL_UNSIGNED_BYTE, image );
197
198   tex.texId = texture;
199   tex.width = width;
200   tex.height = height;
201   switch ( style ) {
202     case 0 :
203     case 1 :
204       tex.style = TSM_FS_CENTER;
205       break;
206     case 2 :
207       tex.style = TSM_FS_TILE;
208       break;
209     case 3 :
210       tex.style = TSM_FS_STRETCH;
211       break;
212     default :
213       tex.style = TSM_FS_CENTER;
214       break;
215   }
216
217   /* setting flag to update changes */
218   cmnKey.ldata = TNotDone;
219   TsmSetWSAttri( view->WsId, WSUpdateState, &cmnKey );
220
221   /* storing background texture */
222   cmnData.pdata = &tex;
223   TsmSetWSAttri( view->WsId, WSBgTexture, &cmnData );
224   free( image );
225 }
226
227 /*----------------------------------------------------------------------*/
228
229 void EXPORT call_togl_set_bg_texture_style( CALL_DEF_VIEW* view, int style )
230 {
231   tsm_bg_texture texture;
232   CMN_KEY_DATA  cmnData;
233   CMN_KEY_DATA  cmnKey;
234
235   /* check if background texture is already created */
236   TsmGetWSAttri( view->WsId, WSBgTexture, &cmnData );
237   texture = (tsm_bg_texture)cmnData.pdata;
238   if ( texture->texId != 0 ) {
239     switch ( style ) {
240         case 0 :
241         case 1 :
242           texture->style = TSM_FS_CENTER;
243           break;
244         case 2 :
245           texture->style = TSM_FS_TILE;
246           break;
247         case 3 :
248           texture->style = TSM_FS_STRETCH;
249           break;
250         default :
251           texture->style = TSM_FS_CENTER;
252           break;
253     }
254
255     /* setting flag to update changes */
256     cmnKey.ldata = TNotDone;
257     TsmSetWSAttri( view->WsId, WSUpdateState, &cmnKey );
258
259     /* storing background texture */
260     cmnData.pdata = texture;
261     TsmSetWSAttri( view->WsId, WSBgTexture, &cmnData );
262   }
263 }
264 #endif /* OCC1188 */
265 /*----------------------------------------------------------------------*/