summaryrefslogtreecommitdiff
path: root/src/Graphic3d/Graphic3d_TextureRoot.cxx
blob: 8b5d382e37af350c367a72f10d973accfec2ef4b (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
// File:	Graphic3d_TextureRoot.cxx
// Created:	Mon Jul 28 17:47:49 1997
// Author:	Pierre CHALAMET
//		<pct@sgi93>
//
// modified:
//    8/09/97 : mise en commentaire des tentatives pour charger
//              autre chose que du RGB. AlienImage buggee ?
//    5/01/99 : Ajout test sur les objets Path et FileName dans LoadTexture().
//		Si le path est null on trappe.   
//   11/06/99 : GG Enable to use GIF and BMP image format
//   10/01/00 : GG IMP Add Path() and Type() methods.
//   10/11/00 : GG Add Image() & LoadTexture() methods.

#define IMP140601	//GG Avoid to change the Trek of the current defined path
//			when the directory string is NULL or empty.

#define xTRACE 1

#include <Graphic3d_TextureRoot.ixx>
#include <Graphic3d_GraphicDevice.hxx>
#include <Graphic3d_GraphicDriver.hxx>

#include <AlienImage_EuclidAlienImage.hxx>
#include <AlienImage_SGIRGBAlienImage.hxx>
#include <AlienImage_XAlienImage.hxx>
#include <AlienImage_GIFAlienImage.hxx>
#include <AlienImage_BMPAlienImage.hxx>
#include <AlienImage_AidaAlienImage.hxx>
#include <AlienImage_SunRFAlienImage.hxx>
#include <AlienImage_AlienImage.hxx>

#include <OSD_Protection.hxx>
#include <OSD_File.hxx>
#include <stdio.h>

Graphic3d_TextureRoot::Graphic3d_TextureRoot(const Handle(Graphic3d_StructureManager)& SM,const Standard_CString Path,const Standard_CString FileName,const Graphic3d_TypeOfTexture Type) : MyPath(FileName),MyType(Type)
{
  // It will be necessary to improve the code below as soon as 3D or 4D textures
  // are implemented. Currently, always allocating an array for two texture bounds...
  MyTexUpperBounds = new TColStd_HArray1OfReal(1,2);

#ifdef IMP140601
  if( Path && (strlen(Path) > 0) )
#endif
    MyPath.SetTrek(TCollection_AsciiString( Path ));

  MyGraphicDriver = Handle(Graphic3d_GraphicDriver)::DownCast(SM->GraphicDevice()->GraphicDriver());

  if (MyGraphicDriver->InquireTextureAvailable())
  {
      // chargement de l'image
//-GG      Handle(AlienImage_AlienImage) MyImage = LoadTexture();
      if (MyImage.IsNull() && FileName && (strlen(FileName) > 0))
	MyImage = LoadTexture();
    
      if (MyImage.IsNull()) {
	MyTexId = -1;
	return;
      }
    
      MyTexId = MyGraphicDriver->CreateTexture(Type, MyImage, FileName, MyTexUpperBounds);
  }
#ifdef TRACE
  printf(" *** Graphic3d_TextureRoot::Create() textId %d\n",MyTexId);
#endif
}

void Graphic3d_TextureRoot::Destroy() const
{
  if (MyTexId >= 0 )  
	MyGraphicDriver->DestroyTexture(MyTexId);
#ifdef TRACE
  printf(" *** Graphic3d_TextureRoot::Destroy() textId %d\n",MyTexId);
#endif
}


Standard_Integer Graphic3d_TextureRoot::TextureId() const
{
  return MyTexId;
}


void Graphic3d_TextureRoot::Update() const
{
  if( MyTexId >= 0 ) 
	MyGraphicDriver->ModifyTexture(MyTexId, MyCInitTexture);
}


Standard_Boolean Graphic3d_TextureRoot::IsDone() const
{
  return (MyTexId >= 0) ? Standard_True : Standard_False;
}

void Graphic3d_TextureRoot::LoadTexture(const Handle(AlienImage_AlienImage)& anImage) {

  if (MyGraphicDriver->InquireTextureAvailable()) {
    if( MyTexId >= 0 )  
	MyGraphicDriver->DestroyTexture(MyTexId);
    MyImage = anImage;
    MyTexId = MyGraphicDriver->CreateTexture(MyType, MyImage, "", MyTexUpperBounds);
    Update(); 
  }
}
 
Handle(AlienImage_AlienImage) Graphic3d_TextureRoot::LoadTexture() const
{
  OSD_Protection Protection( OSD_R, OSD_R, OSD_R, OSD_R ) ; /* Read Only */
  Handle(AlienImage_AlienImage) TheAlienImage = NULL ;

  OSD_File File(MyPath);
  File.Open(OSD_ReadOnly, Protection);

  // open file ok ?
  if ( File.IsOpen() == Standard_False ) {
    TCollection_AsciiString sysname;
    MyPath.SystemName(sysname);
    cout << " *** Can't open texture file '" << sysname << "'" << endl;
    return TheAlienImage;
  }


  ////////////////////////
  // file reading       //
  ////////////////////////


  // image X ?
  ////////////
  Handle(AlienImage_XAlienImage) XAlienImage = new AlienImage_XAlienImage() ;
  
  if (XAlienImage->Read( File )) {
    File.Close();
    return XAlienImage;
  }

  // image GIF ?
  ////////////
  Handle(AlienImage_GIFAlienImage) GIFAlienImage = new AlienImage_GIFAlienImage() ;
  
  if (GIFAlienImage->Read( File )) {
    File.Close();
    return GIFAlienImage;
  }

  // image BMP ?
  ////////////
  Handle(AlienImage_BMPAlienImage) BMPAlienImage = new AlienImage_BMPAlienImage() ;
  
  if (BMPAlienImage->Read( File )) {
    File.Close();
    return BMPAlienImage;
  }
  
  // SunRaster ?
  //////////////
  Handle(AlienImage_SunRFAlienImage) SunRFAlienImage = new AlienImage_SunRFAlienImage() ;

  if (SunRFAlienImage->Read( File )) {
    File.Close();
    return SunRFAlienImage;
  }

  // Aida ?
  /////////
  Handle(AlienImage_AidaAlienImage) AidaAlienImage = new AlienImage_AidaAlienImage() ;
  
  if (AidaAlienImage->Read( File )) {
    File.Close();
    return AidaAlienImage;
  }

  // Euclid ?
  ///////////
  Handle(AlienImage_EuclidAlienImage) EuclidAlienImage = new AlienImage_EuclidAlienImage() ;

  if (EuclidAlienImage->Read( File )) {
    File.Close();
    return EuclidAlienImage;
  }


  // SGIRGB ?
  ///////////
  Handle(AlienImage_SGIRGBAlienImage) SGIRGBAlienImage = new AlienImage_SGIRGBAlienImage() ;

  if (SGIRGBAlienImage->Read( File )) {
    File.Close();
    return SGIRGBAlienImage;
  }


  // raise exception: file type unknown 
  return TheAlienImage;
}

const OSD_Path& Graphic3d_TextureRoot::Path() const {

  return MyPath;
}

Graphic3d_TypeOfTexture Graphic3d_TextureRoot::Type() const {

  return MyType;
}

Handle(AlienImage_AlienImage) Graphic3d_TextureRoot::Image() const {

  return MyImage;
}

Handle(TColStd_HArray1OfReal) Graphic3d_TextureRoot::GetTexUpperBounds() const
{
  return MyTexUpperBounds;
}