Bonjour, j'ai cherché pendant un moment un code assez simple montrant comment charger une texture DXT (faite avec le plugin Nvidia pour photoshop ou le Compressonator d'Ati). L'extension de ces textures est .dds, cela correspond aux textures S3TC.

Je n'ai pas trouvé d'exemple qui fonctionne.
J'ai donc décidé d'ecrire mon propre code en me basant sur ce que j'ai lu dans les differents exemples.

Avec le code ci dessous, j'ai un Segmentation fault des que la fonction glCompressedTexImage2DARB est appelée.

Est ce que qqun saurait m'aider ?

Merci !


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
 
const unsigned long FOURCC_DXT1 = 0x31545844; //(MAKEFOURCC('D','X','T','1'))
const unsigned long FOURCC_DXT3 = 0x33545844; //(MAKEFOURCC('D','X','T','3'))
const unsigned long FOURCC_DXT5 = 0x35545844; //(MAKEFOURCC('D','X','T','5'))
 
typedef struct
{
  GLsizei  width;
  GLsizei  height;
  GLint    components;
  GLenum   format;
  int      numMipMaps;
  GLubyte *pixels;
}
DDS_IMAGE_DATA;
 
typedef struct
{
  unsigned long dwSize;
  unsigned long dwFlags;
  unsigned long dwFourCC;
  unsigned long dwRGBBitCount;
  unsigned long dwRBitMask;
  unsigned long dwGBitMask;
  unsigned long dwBBitMask;
  unsigned long dwABitMask;
}
DDS_PIXELFORMAT;
 
typedef struct
{
  unsigned long dwSize;
  unsigned long dwFlags;
  unsigned long dwHeight;
  unsigned long dwWidth;
  unsigned long dwLinearSize;
  unsigned long dwDepth;
  unsigned long dwMipMapCount;
  unsigned long dwReserved1[11];
  DDS_PIXELFORMAT ddpfPixelFormat;
  unsigned long dwCaps1;
  unsigned long dwCaps2;
  unsigned long dwReserved2[3];
}
DDSURFACEDESC2;
 
PFNGLCOMPRESSEDTEXIMAGE2DARBPROC glCompressedTexImage2DARB;
 
DDS_IMAGE_DATA* loadDDSTextureFile( const char *filename )
{
  DDS_IMAGE_DATA *pDDSImageData;
  DDSURFACEDESC2 ddsd;
  char filecode[4];
  FILE *pFile;
  int factor;
  int bufferSize;
 
  // Open the file
  pFile = fopen( filename, "rb" );
 
  if( pFile == NULL )
  {
    fclose( pFile );return 0;
  }
 
  // Verify the file is a true .dds file
  fread( filecode, 1, 4, pFile );
 
  if( strncmp( filecode, "DDS ", 4 ) != 0 )
  {
    fclose( pFile );
    return 0;
  }
 
  // Get the surface descriptor
  fread( &ddsd, sizeof(ddsd), 1, pFile );
 
  pDDSImageData = (DDS_IMAGE_DATA*) malloc(sizeof(DDS_IMAGE_DATA));
 
  memset( pDDSImageData, 0, sizeof(DDS_IMAGE_DATA) );
 
  if( ddsd.ddpfPixelFormat.dwFourCC ==FOURCC_DXT1)
  {
    // DXT1's compression ratio is 8:1
    pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
    factor = 2;
    pDDSImageData->components = 3;
  }
  else
  {
    if( ddsd.ddpfPixelFormat.dwFourCC ==FOURCC_DXT3)
    {
      // DXT3's compression ratio is 4:1
      pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
      factor = 4;
      pDDSImageData->components = 4;
    }
    else
    {
      if( ddsd.ddpfPixelFormat.dwFourCC ==FOURCC_DXT5)
      {
        // DXT5's compression ratio is 4:1
        pDDSImageData->format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
        factor = 4;
        pDDSImageData->components = 4;
      }
      else
      {
        return 0;
      }
    }
  }
 
  if( ddsd.dwLinearSize == 0 )
  {
    return 0;
  }
 
  if( ddsd.dwMipMapCount > 1 )
    bufferSize = ddsd.dwLinearSize * factor;
  else
    bufferSize = ddsd.dwLinearSize;
 
  pDDSImageData->pixels = (unsigned char*)malloc(bufferSize * sizeof(unsigned char));
 
  fread( pDDSImageData->pixels, 1, bufferSize, pFile );
 
  // Close the file
  fclose( pFile );
 
  pDDSImageData->width      = ddsd.dwWidth;
  pDDSImageData->height     = ddsd.dwHeight;
  pDDSImageData->numMipMaps = ddsd.dwMipMapCount;
  return pDDSImageData;
}
 
 
int loadImage(int *l, char *file,int blur)
{
  int f;
  DDS_IMAGE_DATA *pDDSImageData;
  if (blur) f=GL_LINEAR; else f=GL_NEAREST;
 
  pDDSImageData = loadDDSTextureFile(file);
 
  if( pDDSImageData != NULL )
  {
    int nHeight     = pDDSImageData->height;
    int nWidth      = pDDSImageData->width;
    int nNumMipMaps = pDDSImageData->numMipMaps;
    int nSize;
    int nOffset = 0;
    int i;
    GLuint g_compressedTextureID = -1;
    int nBlockSize;
 
    if( pDDSImageData->format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT )
      nBlockSize = 8;
    else
      nBlockSize = 16;
 
    glGenTextures( 1, &g_compressedTextureID );
    glBindTexture( GL_TEXTURE_2D, g_compressedTextureID );
    *l = g_compressedTextureID;
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, f );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, f );
 
    // Load the mip-map levels
 
    unsigned int size = (((nWidth >> 2) + 3) & ~3) * (((nHeight >> 2) + 3) & ~3) * nBlockSize;
    if (pDDSImageData->pixels != NULL)
    {
 
      glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, pDDSImageData->format,
                                nWidth, nHeight, 0,
                                size,
                                pDDSImageData->pixels);
 
    }
    for(i = 0; i < nNumMipMaps; ++i )
    {
      if( nWidth  == 0 ) nWidth  = 1;
      if( nHeight == 0 ) nHeight = 1;
 
      nSize = ((nWidth+3)/4) * ((nHeight+3)/4) * nBlockSize;
 
      glCompressedTexImage2DARB( GL_TEXTURE_2D,
                                 i,
                                 pDDSImageData->format,
                                 nWidth,
                                 nHeight,
                                 0,
                                 nSize,
                                 pDDSImageData->pixels + nOffset );
 
      nOffset += nSize;
 
      // Half the image size for the next mip-map level...
      nWidth  = (nWidth  / 2);
      nHeight = (nHeight / 2);
    }
  }
 
  if( pDDSImageData != NULL )
  {
    if( pDDSImageData->pixels != NULL )
      free( pDDSImageData->pixels );
 
    free( pDDSImageData );
  }
 
  return 1;
}