1 #ifdef GL_HERETIC
2 
3 //#define __DOOMTYPE__
4 
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include "gl_syms.h"
9 
10 #include "gl_struct.h"
11 #include "gl_mem.h"
12 #ifndef GL_HEXEN
13 #include "doomdef.h"
14 #else
15 #include "h2def.h"
16 #endif
17 #include "p_spec.h"
18 
19 extern GLboolean g_b3Dfx;
20 
21 // Coronas...
22 extern unsigned char *g_p_bCoronaTexture;
23 extern int g_iCorona;
24 extern unsigned char *g_p_bLightTexture;
25 
26 // Flares
27 /*extern unsigned char *flareTex[6];
28 extern unsigned char *shineTex[10];
29 extern int g_iFlare[6];*/
30 extern int g_iShine[10];
31 
32 // Wall impacts
33 extern unsigned char *g_p_bImpactTexture[7];
34 extern int g_iImpact;
35 
36 extern int		firstflat, lastflat, numflats;
37 extern int		firstpatch, lastpatch, numpatches;
38 extern int		firstspritelump, lastspritelump, numspritelumps;
39 
40 extern int			numtextures;
41 
42 extern int		flatmemory, texturememory, spritememory;
43 extern texture_t	**textures;
44 extern byte *R_GetColumn (int tex, int col);
45 // sprites
46 extern GLTexture *p_stGLSprites;
47 extern int	iNbGLSprites;
48 extern GLTexArray *p_stGLSprArray;
49 
50 // Fonts
51 extern GLTexture *p_stGLFontA;
52 extern int	iNbGLFontA;
53 extern GLTexture *p_stGLFontB;
54 extern int	iNbGLFontB;
55 GLuint *FontAobjs=NULL,*FontBobjs;
56 
57 // textures animations
58 #ifndef GL_HEXEN
59 extern anim_t anims[MAXANIMS];
60 extern anim_t *lastanim;
61 extern animdef_t animdefs[];
62 #else	// GL_HEXEN
63 extern animDef_t AnimDefs[MAX_ANIM_DEFS];
64 extern frameDef_t FrameDefs[MAX_FRAME_DEFS];
65 extern int AnimDefCount;
66 
67 int GL_Sky1Texture,GL_Sky2Texture;
68 #endif	// GL_HEXEN
69 extern switchlist_t alphSwitchList[];
70 int *a_iGLTexTranslation;
71 int	glSwitchList[MAXSWITCHES * 2];
72 
73 GLTexture *p_stGLTextures;
74 int			iNbTexturesInMap,iNbGLTextures;
75 GLuint *texobjs;
76 GLuint *sprobjs;
77 GLTexArray *p_stGLTexArray;
78 
79 byte *p_bPalette;
80 #ifdef PALETTED_TEXTURE
81 GLubyte p_bGLPalette[256][4];
82 GLfloat p_fAlpha[256];
83 GLfloat p_fRed[256],p_fGreen[256],p_fBlue[256];
84 #endif
85 
86 int xS[512],yS[512];
87 int xD[512],yD[512];
88 
89 GLboolean g_bPaletteTexture=FALSE;
90 
91 GLboolean g_bOnTheFly;
92 GLint GL_vRegisterFlatTextureOnTheFly(int iTexNum);
93 
fn_vStretchTexture(long lWidthS,long lHeightS,long lWidthD,long lHeightD)94 void fn_vStretchTexture(long lWidthS,long lHeightS,long lWidthD,long lHeightD)
95 { int x,y;
96   float fXRatio,fYRatio;
97 
98 	fXRatio=(float)lWidthS/(float)lWidthD;
99 	fYRatio=(float)lHeightS/(float)lHeightD;
100 	for (y=0;y<lHeightD;y++)
101 	{
102 		yS[y]=(int)(fYRatio*(float)y);
103 	}
104 	for (x=0;x<lWidthD;x++)
105 	{
106 		xS[x]=(int)(fXRatio*(float)x);
107 	}
108 	// MR2506
109 	for (y=0;y<lHeightS;y++)
110 	{
111 		yD[y]=(int)((float)y/fYRatio);
112 	}
113 	for (x=0;x<lWidthS;x++)
114 	{
115 		xD[x]=(int)((float)x/fXRatio);
116 	}
117 }
118 
fn_vInitGLTexFonts()119 void fn_vInitGLTexFonts()
120 { int i,iTexMemory;
121 #ifdef PALETTED_TEXTURE
122 
123 	if (g_bPaletteTexture)
124 	{	for (i=0;i<256;i++)
125 		{	p_bGLPalette[i][0]=p_bPalette[3*i];
126 			p_bGLPalette[i][1]=p_bPalette[3*i+1];
127 			p_bGLPalette[i][2]=p_bPalette[3*i+2];
128 			p_fRed[i]=(float)p_bPalette[3*i]/255;
129 			p_fGreen[i]=(float)p_bPalette[3*i+1]/255;
130 			p_fBlue[i]=(float)p_bPalette[3*i+2]/255;
131 			if (i==205)
132 			{	p_bGLPalette[i][3]=0;
133 				p_fAlpha[i]=0.0f;
134 			}
135 			else
136 			{	p_bGLPalette[i][3]=255;
137 				p_fAlpha[i]=1.0f;
138 			}
139 		}
140 		/*for (i=0;i<256;i++)
141 		{	p_bGLPalette[4*i]=(p_bPalette[3*i]>>3);
142 			p_bGLPalette[4*i+1]=(p_bPalette[3*i+1]>>3);
143 			p_bGLPalette[4*i+2]=(p_bPalette[3*i+2]>>3);
144 			if (i==205)
145 				p_bGLPalette[4*i+3]=0;
146 			else
147 				p_bGLPalette[4*i+3]=1;
148 		}*/
149 
150 		/*glPixelMapfv(GL_PIXEL_MAP_I_TO_R,256,p_fRed);
151 		glPixelMapfv(GL_PIXEL_MAP_I_TO_G,256,p_fGreen);
152 		glPixelMapfv(GL_PIXEL_MAP_I_TO_B,256,p_fBlue);
153 		glPixelMapfv(GL_PIXEL_MAP_I_TO_A,256,p_fAlpha);*/
154 
155 	}
156 #endif
157 
158 	// FontA
159 	FontAobjs=(GLuint *)Malloc((iNbGLFontA)*sizeof(GLuint));
160 	iTexMemory=0;
161 
162 	for (i = 0; i < iNbGLFontA; i++)
163 	{
164 #ifndef GL_HEXEN
165 		FontAobjs[i] = numspritelumps+numflats+numtextures+numpatches+i;
166 #else
167 		FontAobjs[i] = 10000+i;
168 #endif
169 		(*glBindTexture_s) (GL_TEXTURE_2D, FontAobjs[i]);
170 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
171 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
172 //#ifdef BILINEAR
173 //		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
174 //		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
175 //#else
176 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
177 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
178 //#endif
179 		(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
180 
181 #ifdef PALETTED_SPRITE
182 		if (g_bPaletteTexture)
183 		{
184 			(*glColorTableEXT_s) (GL_TEXTURE_2D,GL_RGB8,256,GL_RGB,GL_UNSIGNED_BYTE,p_bPalette);
185 			(*glTexImage2D_s) (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, p_stGLFontA[i].iWidth, p_stGLFontA[i].iHeight, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, p_stGLFontA[i].p_bRGBBuffer);
186 		}
187 		else
188 #endif
189 		(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, p_stGLFontA[i].iWidth, p_stGLFontA[i].iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_stGLFontA[i].p_bRGBBuffer);
190 
191 		iTexMemory+=p_stGLFontA[i].iWidth*p_stGLFontA[i].iHeight*4;
192 	}
193 //	fn_vSaveRAW("FontA.raw",p_stGLFontA[0].p_bRGBBuffer,p_stGLFontA[0].iWidth,p_stGLFontA[0].iHeight);
194 	// SCREENS
195 	GLRegisterScreens();
196 }
197 
198 extern void GL_LoadCoronaTexture(void);
199 extern void GL_LoadImpactTextures(void);
200 
fn_vInitGLTextures()201 void fn_vInitGLTextures()
202 { int i,j;
203   int iTexMemory=0,iRealSprMemory=0;
204 #ifdef FLARES
205   unsigned char *buf;
206   char filename[256];
207   int width, height, components;
208   GLenum minFilter, maxFilter;
209 
210   minFilter = GL_LINEAR_MIPMAP_LINEAR;
211   maxFilter = GL_LINEAR;
212 #endif
213 
214 	texobjs=(GLuint *)Malloc(iNbGLTextures*sizeof(GLuint));
215 	for (i = 0; i < iNbGLTextures; i++)
216 		texobjs[i] = i+1;
217 
218 	for (i = 0; i < iNbGLTextures; i++)
219 	{
220 		(*glBindTexture_s) (GL_TEXTURE_2D, texobjs[i]);
221 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
222 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
223 #ifdef BILINEAR
224 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
226 #else
227 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
228 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
229 #endif
230 		(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE /*GL_DECAL*/);
231 
232 #ifdef PALETTED_TEXTURE
233 		if ((g_bPaletteTexture)&&(!p_stGLTextures[i].a_stSubTex[0].bIsNZ))
234 		{	(*glColorTableEXT_s) (GL_TEXTURE_2D,GL_RGB8,256,GL_RGB,GL_UNSIGNED_BYTE,p_bPalette);
235 			(*glTexImage2D_s) (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, p_stGLTextures[i].iWidth, p_stGLTextures[i].iHeight, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, p_stGLTextures[i].p_bRGBBuffer);
236 			iTexMemory+=p_stGLTextures[i].iWidth*p_stGLTextures[i].iHeight;
237 		}
238 		else
239 #endif
240 		{	(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, p_stGLTextures[i].iWidth, p_stGLTextures[i].iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_stGLTextures[i].p_bRGBBuffer);
241 
242 			iTexMemory+=p_stGLTextures[i].iWidth*p_stGLTextures[i].iHeight*4;
243 		}
244     }
245 	printf("[SDLGLdrv/textures] GL Map Texture memory: %d bytes (%d kb)\n", iTexMemory, iTexMemory / 1024);
246 
247 	// Sprites
248 	iTexMemory=0;
249 	sprobjs=(GLuint *)Malloc(iNbGLSprites*sizeof(GLuint));
250 	for (i = 0; i < iNbGLSprites; i++)
251 		//sprobjs[i+iNbGLTextures] = i+iNbGLTextures+1;
252 		sprobjs[i] = i+numtextures+numpatches+1;
253 
254 	for (i = 0; i < iNbGLSprites; i++)
255 	{
256 		(*glBindTexture_s) (GL_TEXTURE_2D, sprobjs[i]);
257 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
258 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
259 #ifdef BILINEAR
260 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
261 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
262 #else
263 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
264 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
265 #endif
266 		(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
267 
268 #ifdef PALETTED_SPRITE
269 		if (g_bPaletteTexture)
270 		{	//(*glColorTableEXT_s) (GL_TEXTURE_2D,GL_RGBA8,256,GL_RGBA,GL_UNSIGNED_BYTE,p_bGLPalette);
271 			(*glColorTableEXT_s) (GL_TEXTURE_2D,GL_RGB8,256,GL_RGB,GL_UNSIGNED_BYTE,p_bPalette);
272 			//glGetColorTableParameterivEXT(GL_PROXY_TEXTURE_2D,GL_COLOR_TABLE_WIDTH_EXT,&width);
273 			(*glTexImage2D_s) (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, p_stGLSprites[i].iWidth, p_stGLSprites[i].iHeight, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, p_stGLSprites[i].p_bRGBBuffer);
274 			//(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, p_stGLSprites[i].iWidth, p_stGLSprites[i].iHeight, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, p_stGLSprites[i].p_bRGBBuffer);
275 		}
276 		else
277 #endif
278 		(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, p_stGLSprites[i].iWidth, p_stGLSprites[i].iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_stGLSprites[i].p_bRGBBuffer);
279 
280 		iTexMemory+=p_stGLSprites[i].iWidth*p_stGLSprites[i].iHeight*4;
281 		for (j=0;j<p_stGLSprites[i].iNbSubTextures;j++)
282 			iRealSprMemory+=p_stGLSprites[i].a_stSubTex[j].iWidth*p_stGLSprites[i].a_stSubTex[j].iHeight*4;
283 
284 //#define SHOW_GL_SPRITES
285 #ifdef SHOW_GL_SPRITES
286 		for (j=0;j<p_stGLSprites[i].iNbFreeSpace;j++)
287 		{ int x,y,iU,iV;
288 			iU=p_stGLSprites[i].a_stFreeSpace[j].iU;
289 			iV=p_stGLSprites[i].a_stFreeSpace[j].iV;
290 			for (x=0;x<p_stGLSprites[i].a_stFreeSpace[j].iWidth;x++)
291 			for (y=0;y<p_stGLSprites[i].a_stFreeSpace[j].iHeight;y++)
292 			{	p_stGLSprites[i].p_bRGBBuffer[4*((x+iU)+(y+iV)*SPRITE_WIDTH)]=255;
293 				p_stGLSprites[i].p_bRGBBuffer[4*((x+iU)+(y+iV)*SPRITE_WIDTH)+1]=0;
294 				p_stGLSprites[i].p_bRGBBuffer[4*((x+iU)+(y+iV)*SPRITE_WIDTH)+2]=0;
295 				p_stGLSprites[i].p_bRGBBuffer[4*((x+iU)+(y+iV)*SPRITE_WIDTH)+3]=255;
296 			}
297 		}
298 #endif
299     }
300 	printf("[SDLGLdrv/textures] GL Sprite Texture memory: %d bytes (%d kb)\n", iTexMemory, iTexMemory / 1024);
301 	printf("[SDLGLdrv/textures] Sprite Texture memory (original): %d bytes (%d kb)\n", iRealSprMemory, iRealSprMemory / 1024);
302 //	fn_vSaveSprites();	// Save all the GLSprites
303 
304 // Coronas...
305 	GL_LoadCoronaTexture();
306 	//g_iCorona=iNbGLTextures+iNbGLSprites+50;
307 	g_iCorona=sprobjs[iNbGLSprites-1]+50;
308 
309 	//(*glBindTexture_s) (GL_TEXTURE_2D, texobjs[iNbGLTextures+iNbGLSprites]);
310 	(*glBindTexture_s) (GL_TEXTURE_2D, g_iCorona);
311 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
312 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
313 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
314 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
315 	(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
316 
317 	(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_p_bCoronaTexture);
318 	//(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_p_bCoronaTexture);
319 // Light
320 	(*glBindTexture_s) (GL_TEXTURE_2D, g_iCorona+1);
321 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
322 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
323 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
324 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
325 	(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
326 	(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_p_bLightTexture);
327 
328 #ifdef FLARES
329 // Flares
330 	/*for (i = 0; i < 6; i++)
331 	{
332 	iNbGLSprites++;
333 	g_iFlare[i] = sprobjs[iNbGLSprites-1]+50+i+2;
334 
335     (*glBindTexture_s) (GL_TEXTURE_2D, g_iFlare[i]);
336     (*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
337     (*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
338     (*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter);
339 
340 	sprintf(filename, "Flare%d.BW", i+1);
341 
342 	buf = load_luminance(filename, &width, &height, &components);
343     gluBuild2DMipmaps(GL_TEXTURE_2D, 1, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, buf);
344 
345 	free(buf);
346 
347 	printf("DEBG - SET: g_iFlare %d\n", i);
348 	}
349 	*/
350 
351 for (i = 0; i < 10; i++)
352   {
353     iNbGLSprites++;
354 	 g_iShine[i] = sprobjs[iNbGLSprites-1]+50+i+2;
355 
356 	 //(*glBindTexture_s) (GL_TEXTURE_2D, shineTex[i]);
357 	 (*glBindTexture_s) (GL_TEXTURE_2D, g_iShine[i]);
358     (*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
359     (*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
360     (*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter);
361 
362 	sprintf(filename, "Shine%d.BW", i);
363 
364 	buf = load_luminance(filename, &width, &height, &components);
365     gluBuild2DMipmaps(GL_TEXTURE_2D, 1, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, buf);
366 
367 	free(buf);
368 
369 	printf("[SDLGLdrv/textures] DEBG: g_iShine %d ; width: %d ; height: %d\n", i, width, height);
370   }
371 #endif
372 // Bullet impact
373 	GL_LoadImpactTextures();
374 	//g_iCorona=iNbGLTextures+iNbGLSprites+50;
375 	g_iImpact=sprobjs[iNbGLSprites-1]+70;
376 
377 	for (i=0;i<7;i++)
378 	{	(*glBindTexture_s) (GL_TEXTURE_2D, g_iImpact+i);
379 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
380 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
381 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
382 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
383 		(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
384 
385 		if (i==0)
386 			(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_p_bImpactTexture[i]);
387 		else
388 		if (i<3)
389 			(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_p_bImpactTexture[i]);
390 		else //if (i<6)
391 			(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_p_bImpactTexture[i]);
392 		/*else
393 			(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, 64, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_p_bImpactTexture[i]);*/
394 
395 		//Free(g_p_bImpactTexture[i]);
396 	}
397 
398 	g_bOnTheFly=TRUE;
399 }
400 
fn_vAllocTextureMemory()401 void fn_vAllocTextureMemory()
402 {
403 //	p_stGLTextures=(GLTexture *)Malloc((numtextures+numflats)*sizeof(GLTexture));
404 //	a_iGLTexTranslation=(int *)Malloc((numtextures+numflats)*sizeof(int));
405 	p_stGLTextures=(GLTexture *)Malloc(sizeof(GLTexture));
406 	a_iGLTexTranslation=(int *)Malloc(sizeof(int));
407 	p_bPalette=W_CacheLumpName("PLAYPAL", PU_STATIC);
408 	iNbGLTextures=0;
409 	iNbTexturesInMap=0;
410 #ifndef GL_HEXEN
411 	lastanim = anims;	// animated textures
412 #endif
413 	g_bOnTheFly=FALSE;
414 }
415 
416 //void fn_vRegisterFlatTexture(char *szName)
fn_vRegisterFlatTexture(int flattex)417 void fn_vRegisterFlatTexture(int flattex)
418 { unsigned char *p_bTexture;
419   int i,lump,x,y;
420 
421 	//i=R_FlatNumForName(szName);
422 	i=flattex;
423 	/*if (!strncmp(szName,"F_SKY1",8))
424 		return;*/		// a voir
425 	//flatmemory = 0;
426 	lump = firstflat + i;
427 	flatmemory += lumpinfo[lump].size;
428 	p_bTexture=(unsigned char *)W_CacheLumpNum(lump, PU_CACHE);
429 
430 	p_stGLTextures=(GLTexture *)Realloc(p_stGLTextures,sizeof(GLTexture)*(iNbGLTextures+1));
431 	a_iGLTexTranslation=(int *)Realloc(a_iGLTexTranslation,sizeof(int)*(iNbGLTextures+1));
432 
433 	p_stGLTextures[iNbGLTextures].iWidth=FLAT_TEX_SIZE;
434 	p_stGLTextures[iNbGLTextures].iHeight=FLAT_TEX_SIZE;
435 
436 #ifdef PALETTED_TEXTURE
437 	if (g_bPaletteTexture)
438 		p_stGLTextures[iNbGLTextures].p_bRGBBuffer=(unsigned char *)Malloc(FLAT_TEX_SIZE*FLAT_TEX_SIZE);
439 	else
440 #endif
441 	p_stGLTextures[iNbGLTextures].p_bRGBBuffer=(unsigned char *)Malloc(FLAT_TEX_SIZE*FLAT_TEX_SIZE*4);
442 
443 	p_stGLTextures[iNbGLTextures].a_stSubTex=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
444 	p_stGLTextures[iNbGLTextures].a_stFreeSpace=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
445 	p_stGLTextures[iNbGLTextures].iNbSubTextures=1;
446 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].iU=0;
447 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].iV=0;
448 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].iWidth=FLAT_TEX_SIZE;
449 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].iHeight=FLAT_TEX_SIZE;
450 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].bIsNZ=0;
451 	p_stGLTextures[iNbGLTextures].iNbFreeSpace=0;
452 	p_stGLTextures[iNbGLTextures].a_stFreeSpace[0].iU=0;
453 	p_stGLTextures[iNbGLTextures].a_stFreeSpace[0].iV=0;
454 	p_stGLTextures[iNbGLTextures].a_stFreeSpace[0].iWidth=0;
455 	p_stGLTextures[iNbGLTextures].a_stFreeSpace[0].iHeight=0;
456 
457 	for (x=0;x<FLAT_TEX_SIZE;x++)
458 	{
459 #define Y y
460 		for (y=0;y<FLAT_TEX_SIZE;y++)
461 		{
462 #ifdef PALETTED_TEXTURE
463 			if (g_bPaletteTexture)
464 				p_stGLTextures[iNbGLTextures].p_bRGBBuffer[x*FLAT_TEX_SIZE+Y]=p_bTexture[y+x*FLAT_TEX_SIZE];
465 			else
466 #endif
467 			{	p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(x*FLAT_TEX_SIZE+Y)]=p_bPalette[p_bTexture[y+x*FLAT_TEX_SIZE]*3];
468 				p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(x*FLAT_TEX_SIZE+Y)+1]=p_bPalette[p_bTexture[y+x*FLAT_TEX_SIZE]*3+1];
469 				p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(x*FLAT_TEX_SIZE+Y)+2]=p_bPalette[p_bTexture[y+x*FLAT_TEX_SIZE]*3+2];
470 				p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(x*FLAT_TEX_SIZE+Y)+3]=255;
471 			}
472 		}
473 	}
474 
475 	a_iGLTexTranslation[iNbGLTextures]=iNbGLTextures;
476 
477 	p_stGLTexArray=(GLTexArray *)Realloc(p_stGLTexArray,++iNbTexturesInMap*sizeof(GLTexArray));
478 	/*for (k=0;k<8;k++)
479 		p_stGLTexArray[iNbTexturesInMap-1].szTexName[k]=szName[k];*/
480 	p_stGLTexArray[iNbTexturesInMap-1].iFlatID=flattex;
481 	p_stGLTexArray[iNbTexturesInMap-1].iTexID=-1;
482 	p_stGLTexArray[iNbTexturesInMap-1].iGLTexIndex=iNbGLTextures++;
483 	p_stGLTexArray[iNbTexturesInMap-1].iUVIndex=0;
484 }
485 
fn_iGetTexHeightGL(int iHeight)486 int fn_iGetTexHeightGL(int iHeight)
487 {
488 	if (g_b3Dfx&&(iHeight>256))	// MR0709
489 		return 256;
490 	if ((iHeight==2)||(iHeight==4)||(iHeight==8)||(iHeight==16)||(iHeight==32)||(iHeight==64)||(iHeight==128)||(iHeight==256)||(iHeight==512)||(iHeight==1024))
491 		return iHeight;
492 	if (iHeight<2)		// DOOM_GL: Bug in 3Dfx beta 2.1 drivers. Tex Width<16 => White!!
493 		return 2;
494 	if (iHeight<4)
495 		return 4;
496 	if (iHeight<8)
497 		return 8;
498 	if (iHeight<16)
499 		return 16;
500 	if (iHeight<32)
501 		return 32;
502 	if (iHeight<64)
503 		return 64;
504 	if (iHeight<128)
505 		return 128;
506 	if (iHeight<256)
507 		return 256;
508 	if (iHeight<512)
509 		return 512;
510 	if (iHeight<1024)
511 		return 1024;
512 
513 	fprintf(stderr, "[SDLGLdrv/textures] iHeight: %d\n", iHeight);
514 	return 1024;		/* shoot up compiler warning ! */
515 }
516 
517 #ifdef DOOM_GL
518 #define NB_NON_TRANSP_TEX 14
519 char szNonTranspTex[NB_NON_TRANSP_TEX][8]= { "COMPTILE","LITEBLU4","COMPSTA1","DOORBLU","DOORBLU2","SW2SKIN","COMPBLUE","SW1BLUE",
520 	"SW2BLUE","TEKGREN3","PLAT1", "SKINEDGE","PANBLUE","COMPUTE1" };
521 // MR2506
522 #define NB_TRANSP_TEX 17
523 char szTranspTex[NB_TRANSP_TEX][8]= { "BRNBIGC","BRNBIGL","BRNBIGR","BRNSMAL1","BRNSMAL2","BRNSMALC","BRNSMALL","BRNSMALR","MIDBRN1",
524 	"MIDGRATE","MIDVINE1","MIDVINE2","MIDBARS1","MIDBARS3","MIDBRONZ","MIDSPACE","IIEBLOD1"  };
525 
526 #elif GL_HEXEN
527 #define NB_NON_TRANSP_TEX 7
528 char szNonTranspTex[NB_NON_TRANSP_TEX][8]= { "FOREST01","GLASS01","GLASS02","GLASS03","GLASS04","GLASS05","GLASS06" };
529 
530 #define NB_TRANSP_TEX 31
531 char szTranspTex[NB_TRANSP_TEX][8]= { "GATE01","GATE02","GATE03","GATE04","GATE51","GATE52","GATE53","VILL06","VILL07","VILL08",
532 	"WEB1_L","WEB1_R","WEB2_L","WEB2_R","WEB3","GLASS07","TOMB18","BLANK","CASTLE09","CAVE11","FIRE16","FOREST12","MONK24","PRTL07",
533 	"SEWER14","SWAMP07","TOMB13","TPORTX","SEWER03","SEWER04","FOREST06"  };
534 
535 #else
536 #define NB_NON_TRANSP_TEX 8
537 char szNonTranspTex[NB_NON_TRANSP_TEX][8]= { "MOSAIC1","MOSAIC2","MOSAIC3","MOSAIC4","MOSAIC5","STNGLS1","STNGLS2","STNGLS3"};
538 
539 #define NB_TRANSP_TEX 11
540 char szTranspTex[NB_TRANSP_TEX][8]= { "GATMETL","GATMETL2","GATMETL3","GATMETL4","GATMETL5","WDGAT64","WEB1_B","WEB1_F","WEB2_B","WEB2_F",
541 	"WEB3_M"  };
542 
543 #endif
fn_bTexIsNotTransp(char * szName)544 GLboolean fn_bTexIsNotTransp(char *szName)
545 { int i;
546 
547 	for (i=0;i<NB_NON_TRANSP_TEX;i++)
548 		if (!strncmp(szName,szNonTranspTex[i],8))
549 			return TRUE;
550 	return FALSE;
551 }
552 
fn_bTexIsTransp(char * szName)553 GLboolean fn_bTexIsTransp(char *szName)
554 { int i;
555 
556 	for (i=0;i<NB_TRANSP_TEX;i++)
557 		if (!strncmp(szName,szTranspTex[i],8))
558 			return TRUE;
559 	return FALSE;
560 }
561 
562 // MR0709
fn_bTextureIsTransp(texture_t * texture)563 GLboolean fn_bTextureIsTransp(texture_t *texture)
564 { int x,i;
565   patch_t *patch;
566   GLboolean bExtern,bTransp;
567 
568 	/*if (texture->width<=256)	// original doom texture are < 256 and original transp texture are in the szTranspTex array.
569 		return FALSE;*/
570 
571 	bExtern=FALSE;
572 	bTransp=FALSE;
573 	for (i=0;i<texture->patchcount;i++)
574 	{	patch = W_CacheLumpNum (texture->patches[i].patch, PU_CACHE);
575 		if (lumpinfo[texture->patches[i].patch].handle!=lumpinfo[0].handle)
576 			bExtern=TRUE;
577 		for (x=0;x<SHORT(patch->width);x++)
578 		{ column_t *p_bColumn_t;
579 		  byte *p_bColumn;
580 
581 			p_bColumn_t=(column_t *)((byte *)patch+LONG(patch->columnofs[x]));
582 			if (p_bColumn_t->length!=SHORT(patch->height))
583 				//return TRUE;
584 				bTransp=TRUE;
585 			for ( ; p_bColumn_t->topdelta != 0xff ; )
586 			{
587 				p_bColumn=(byte *)p_bColumn_t + 3;
588 				p_bColumn_t = (column_t *)(  (byte *)p_bColumn_t + p_bColumn_t->length + 4);
589 			}
590 		}
591 	}
592 	//return FALSE;
593 	return bTransp & bExtern;
594 }
595 
fn_bWTAlreadyRegistered(int iTexNum)596 GLboolean fn_bWTAlreadyRegistered(int iTexNum)	// MR2606
597 { int i;
598 
599 	for (i=0;i<iNbTexturesInMap;i++)
600 		if (p_stGLTexArray[i].iTexID==iTexNum)
601 		return true;
602 	return false;
603 }
604 
fn_vRegisterWallTextures(int iTexNum,texture_t * texture)605 void fn_vRegisterWallTextures(int iTexNum,texture_t	*texture)
606 { int iGLIndex,iU,iV,iFreeSpace,iSubIndex;
607   int iGLTexHeight;
608   int iGLTexWidth;	// DOOM_GL
609   //int i;
610 
611 	if (fn_bWTAlreadyRegistered(iTexNum))
612 		return;
613 
614 /*	if (!strncmp(texture->name,"WFALL1",8))
615 		printf("prout");*/
616 
617 	//for (i=0 ; i<numtextures ; i++)
618 	{
619 		iGLIndex=-1;
620 
621 		if (iGLIndex==-1)
622 		{	iU=0;
623 			iV=0;
624 			iGLTexWidth=fn_iGetTexHeightGL(texture->width);
625 			iGLTexHeight=fn_iGetTexHeightGL(texture->height);
626 
627 			p_stGLTextures=(GLTexture *)Realloc(p_stGLTextures,sizeof(GLTexture)*(iNbGLTextures+1));
628 			a_iGLTexTranslation=(int *)Realloc(a_iGLTexTranslation,sizeof(int)*(iNbGLTextures+1));
629 
630 			//p_stGLTextures[iNbGLTextures].iWidth=texture->width;
631 			p_stGLTextures[iNbGLTextures].iWidth=iGLTexWidth;
632 			p_stGLTextures[iNbGLTextures].iHeight=iGLTexHeight;
633 			p_stGLTextures[iNbGLTextures].p_bRGBBuffer=(unsigned char *)Malloc(iGLTexHeight*iGLTexWidth*4);
634 			memset(p_stGLTextures[iNbGLTextures].p_bRGBBuffer,0,iGLTexHeight*iGLTexWidth*4);
635 
636 			p_stGLTextures[iNbGLTextures].a_stSubTex=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
637 			p_stGLTextures[iNbGLTextures].a_stFreeSpace=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
638 			p_stGLTextures[iNbGLTextures].iNbSubTextures=0;
639 			p_stGLTextures[iNbGLTextures].a_stSubTex[0].iU=0;
640 			p_stGLTextures[iNbGLTextures].a_stSubTex[0].iV=0;
641 			p_stGLTextures[iNbGLTextures].a_stSubTex[0].iWidth=texture->width;
642 			p_stGLTextures[iNbGLTextures].a_stSubTex[0].iHeight=texture->height;
643 
644 			p_stGLTextures[iNbGLTextures].iNbFreeSpace=0;
645 			iGLIndex=iNbGLTextures++;
646 			iFreeSpace=0;
647 		}
648 
649 		{ int x,y;
650 
651 			iSubIndex=0;
652 			p_stGLTextures[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=0;
653 
654 			//fn_vStretchTexture(texture->width,texture->height,texture->width,iGLTexHeight);
655 			fn_vStretchTexture(texture->width,texture->height,iGLTexWidth,iGLTexHeight);
656 
657 			if (!fn_bTexIsTransp(texture->name)&&!fn_bTextureIsTransp(texture))// MR0709
658 			{
659 				for (y=0;y<iGLTexHeight;y++)
660 				{ byte *p_bColumn;
661 
662 #define Y y
663 					//for (x=0;x<texture->width;x++)
664 					for (x=0;x<iGLTexWidth;x++)
665 					{
666 						p_bColumn=R_GetColumn(iTexNum,xS[x]);
667 // MR2506
668 //						if ((p_bColumn[yS[y]]!=205)||fn_bTexIsNotTransp(texture->name))
669 						{	p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)]=p_bPalette[p_bColumn[yS[y]]*3];
670 							p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+1]=p_bPalette[p_bColumn[yS[y]]*3+1];
671 							p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+2]=p_bPalette[p_bColumn[yS[y]]*3+2];
672 							p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+3]=255;
673 						}
674 // MR2506
675 /*						else
676 						{	p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)]=255;
677 							p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+1]=0;
678 							p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+2]=0;
679 							p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+3]=0;
680 							p_stGLTextures[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=1;
681 						}*/
682 					}
683 				}
684 #ifdef PALETTED_TEXTURE
685 				if ((g_bPaletteTexture)&&(!p_stGLTextures[iGLIndex].a_stSubTex[iSubIndex].bIsNZ))
686 				{
687 					p_stGLTextures[iGLIndex].p_bRGBBuffer=(unsigned char *)Realloc(p_stGLTextures[iGLIndex].p_bRGBBuffer,iGLTexHeight*iGLTexWidth);
688 					for (y=0;y<iGLTexHeight;y++)
689 					{ byte *p_bColumn;
690 
691 						for (x=0;x<iGLTexWidth;x++)
692 						{
693 							p_bColumn=R_GetColumn(iTexNum,xS[x]);
694 							p_stGLTextures[iGLIndex].p_bRGBBuffer[(x+iU)+(Y+iV)*iGLTexWidth]=p_bColumn[yS[y]];
695 						}
696 					}
697 				}
698 #endif
699 			}
700 			else	// tex is transp
701 			// MR0709
702 			{ patch_t *patch;
703 			  int i,j;
704 			  unsigned char *p_bRGBBuffer1;
705 
706 				p_stGLTextures[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=1;
707 				p_bRGBBuffer1=(unsigned char *)Malloc(4*texture->width*texture->height);
708 				memset(p_bRGBBuffer1,0,4*texture->width*texture->height);
709 				for (i=0;i<texture->patchcount;i++)
710 				{	patch = W_CacheLumpNum (texture->patches[i].patch, PU_CACHE);
711 					for (x=0;x<SHORT(patch->width);x++)
712 					{ column_t *p_bColumn_t;
713 					  byte *p_bColumn;
714 
715 						p_bColumn_t=(column_t *)((byte *)patch+LONG(patch->columnofs[x]));
716 						for ( ; p_bColumn_t->topdelta != 0xff ; )
717 						{
718 							p_bColumn=(byte *)p_bColumn_t + 3;
719 
720 	#undef Y
721 	#define Y (j+p_bColumn_t->topdelta)
722 
723 							for (j=0;j<p_bColumn_t->length;j++)
724 								if ((texture->patches[i].originx+x<texture->width)&&(Y<texture->height))
725 								{	p_bRGBBuffer1[4*(texture->patches[i].originx+x+Y*texture->width)]=p_bPalette[p_bColumn[j]*3];
726 									p_bRGBBuffer1[4*(texture->patches[i].originx+x+Y*texture->width)+1]=p_bPalette[p_bColumn[j]*3+1];
727 									p_bRGBBuffer1[4*(texture->patches[i].originx+x+Y*texture->width)+2]=p_bPalette[p_bColumn[j]*3+2];
728 									p_bRGBBuffer1[4*(texture->patches[i].originx+x+Y*texture->width)+3]=255;
729 								}
730 							p_bColumn_t = (column_t *)(  (byte *)p_bColumn_t + p_bColumn_t->length + 4);
731 						}
732 					}
733 				 }
734 
735 #undef Y
736 #define Y y
737 				for (x=0;x<iGLTexWidth;x++)
738 				for (y=0;y<iGLTexHeight;y++)
739 				if ((xS[x]+yS[y]*texture->width)<texture->width*texture->height)
740 				{	p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)]=p_bRGBBuffer1[4*(xS[x]+yS[y]*texture->width)];
741 					p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+1]=p_bRGBBuffer1[4*(xS[x]+yS[y]*texture->width)+1];
742 					p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+2]=p_bRGBBuffer1[4*(xS[x]+yS[y]*texture->width)+2];
743 					p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+3]=p_bRGBBuffer1[4*(xS[x]+yS[y]*texture->width)+3];
744 				}
745 
746 				Free(p_bRGBBuffer1);
747 			}	// end tex transp
748 			/*{ patch_t *patch;
749 			  int j;
750 			  unsigned char *p_bRGBBuffer1;
751 
752 				p_stGLTextures[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=1;
753 				patch = W_CacheLumpNum (texture->patches->patch, PU_CACHE);
754 				p_bRGBBuffer1=(unsigned char *)Malloc(4*SHORT(patch->width)*SHORT(patch->height));
755 				memset(p_bRGBBuffer1,0,4*SHORT(patch->width)*SHORT(patch->height));
756 				for (x=0;x<SHORT(patch->width);x++)
757 				{ column_t *p_bColumn_t;
758 				  byte *p_bColumn;
759 
760 					p_bColumn_t=(column_t *)((byte *)patch+patch->columnofs[x]);
761 					for ( ; p_bColumn_t->topdelta != 0xff ; )
762 					{
763 						p_bColumn=(byte *)p_bColumn_t + 3;
764 
765 #undef Y
766 #define Y (j+p_bColumn_t->topdelta)
767 
768 						for (j=0;j<p_bColumn_t->length;j++)
769 						{	p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))]=p_bPalette[p_bColumn[j]*3];
770 							p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+1]=p_bPalette[p_bColumn[j]*3+1];
771 							p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+2]=p_bPalette[p_bColumn[j]*3+2];
772 							p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+3]=255;
773 						}
774 						p_bColumn_t = (column_t *)(  (byte *)p_bColumn_t + p_bColumn_t->length + 4);
775 					}
776 				}
777 
778 #undef Y
779 #define Y y
780 				for (x=0;x<iGLTexWidth;x++)
781 				for (y=0;y<iGLTexHeight;y++)
782 				if ((xS[x]+yS[y]*SHORT(patch->width))<SHORT(patch->width)*SHORT(patch->height))
783 				{	p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)]=p_bRGBBuffer1[4*(xS[x]+yS[y]*SHORT(patch->width))];
784 					p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+1]=p_bRGBBuffer1[4*(xS[x]+yS[y]*SHORT(patch->width))+1];
785 					p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+2]=p_bRGBBuffer1[4*(xS[x]+yS[y]*SHORT(patch->width))+2];
786 					p_stGLTextures[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+3]=p_bRGBBuffer1[4*(xS[x]+yS[y]*SHORT(patch->width)y)+3];
787 				}
788 
789 				Free(p_bRGBBuffer1);
790 			}	// end tex transp*/
791 
792 			p_stGLTextures[iGLIndex].iNbSubTextures++;
793 
794 			a_iGLTexTranslation[iGLIndex]=iGLIndex;
795 
796 			if (!iNbTexturesInMap)
797 				p_stGLTexArray=(GLTexArray *)Malloc(++iNbTexturesInMap*sizeof(GLTexArray));
798 			else
799 				p_stGLTexArray=(GLTexArray *)Realloc(p_stGLTexArray,++iNbTexturesInMap*sizeof(GLTexArray));
800 			//for (k=0;k<8;k++)
801 			//	p_stGLTexArray[iNbTexturesInMap-1].szTexName[k]=texture->name[k];
802 			p_stGLTexArray[iNbTexturesInMap-1].iTexID=R_TextureNumForName(texture->name);
803 			p_stGLTexArray[iNbTexturesInMap-1].iFlatID=-1;
804 			p_stGLTexArray[iNbTexturesInMap-1].iGLTexIndex=iGLIndex;
805 			p_stGLTexArray[iNbTexturesInMap-1].iUVIndex=iSubIndex;
806 		}
807 
808 	}
809 /*	if (!strncmp(texture->name,"WFALL1",8))
810 		fn_vSaveRAW("Debug.raw",p_stGLTextures[iGLIndex].p_bRGBBuffer,iGLTexWidth,iGLTexHeight);*/
811 }
812 
813 //#pragma optimize( "", on )
814 
815 //int fn_iGetGLTexturef(char *szTexName,short xOffset,short yOffset,float *fU1,float *fU2,float *fV1,float *fV2,float *fU1Off,float *fV2Off,int *iSubTextIndex)
fn_iGetGLTexturef(short sTexture,short xOffset,short yOffset,float * fU1,float * fU2,float * fV1,float * fV2,float * fU1Off,float * fV2Off,int * iSubTextIndex)816 int fn_iGetGLTexturef(short sTexture,short xOffset,short yOffset,float *fU1,float *fU2,float *fV1,float *fV2,float *fU1Off,float *fV2Off,int *iSubTextIndex)
817 { int i,iUVIndex,iGLTexIndex=-1;
818 
819 	for (i=0;i<iNbTexturesInMap;i++)
820 	{
821 		//if (!strncmp(szTexName,p_stGLTexArray[i].szTexName,8))
822 		if ((p_stGLTexArray[i].iTexID==sTexture)||
823 			((p_stGLTexArray[i].iTexID==-1)&&(p_stGLTexArray[i].iFlatID==-1)&&(sTexture==-2)))	// Sky
824 		{
825 			iGLTexIndex=p_stGLTexArray[i].iGLTexIndex;
826 			iUVIndex=p_stGLTexArray[i].iUVIndex;
827 			*iSubTextIndex=iUVIndex;
828 
829 			if (xOffset>=p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iWidth)
830 				xOffset=0;
831 			if (yOffset>=p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iHeight)
832 				yOffset=0;
833 
834 // MR0709
835 /*			*fU1Off=(float)(p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iU+xOffset)/(float)p_stGLTextures[iGLTexIndex].iWidth;
836 			*fU1=(float)(p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iU)/(float)p_stGLTextures[iGLTexIndex].iWidth;
837 			*fU2=(float)(p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iU+p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iWidth)/(float)p_stGLTextures[iGLTexIndex].iWidth;
838 
839 			*fV1=0.0f;
840 			*fV2=1.0f;
841 			*fV2Off=(float)(p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iV+yOffset)/(float)p_stGLTextures[iGLTexIndex].iHeight;*/
842 			*fU1Off=(float)(p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iU+xOffset)/(float)p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iWidth;
843 			*fU1=(float)(p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iU)/(float)p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iWidth;
844 			*fU2=(float)(p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iU+p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iWidth)/(float)p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iWidth;
845 
846 			*fV1=0.0f;
847 			*fV2=1.0f;
848 			*fV2Off=(float)(p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iV+yOffset)/(float)p_stGLTextures[iGLTexIndex].a_stSubTex[iUVIndex].iHeight;
849 
850 			return iGLTexIndex;
851 		}
852 	}
853 	return iGLTexIndex;
854 }
855 
856 
857 //int fn_iGetGLFlatTexturef(char *szTexName)
fn_iGetGLFlatTexturef(int flattex)858 int fn_iGetGLFlatTexturef(int flattex)
859 { int i,iGLTexIndex=-1;
860 
861 	for (i=0;i<iNbTexturesInMap;i++)
862 	{
863 		//if (!strncmp(szTexName,p_stGLTexArray[i].szTexName,8))
864 		if (p_stGLTexArray[i].iFlatID==flattex)
865 		{
866 			iGLTexIndex=p_stGLTexArray[i].iGLTexIndex;
867 
868 			return iGLTexIndex;
869 		}
870 	}
871 	return iGLTexIndex;
872 }
873 
874 #ifdef DOOM_GL
875 char szSkyName[5][8]={ "SKY1","SKY2", "SKY3", "SKY4", "SKY3" };
876 #elif GL_HEXEN
877 
878 extern fixed_t Sky1ScrollDelta;
879 extern fixed_t Sky2ScrollDelta;
880 extern int Sky1Texture;
881 extern int Sky2Texture;
882 extern fixed_t Sky1ColumnOffset;
883 extern fixed_t Sky2ColumnOffset;
884 extern int skyflatnum;
885 extern int skytexturemid;
886 extern fixed_t skyiscale;
887 extern H_boolean DoubleSky;
888 
889 #else
890 char szSkyName[6][8]={ "SKY1","SKY2", "SKY3", "SKY1", "SKY3", "SKY1" };
891 #endif
892 
fn_vLoadSkyTexture(int episode)893 void fn_vLoadSkyTexture(int episode)
894 { texture_t		*texture;
895   int skytexture,j,lump;
896   int x,y;
897   patch_t		*realpatch = NULL;
898 
899 #ifdef DOOM_GL
900     if ( (gamemode == commercial)
901 	 /*|| ( gamemode == pack_tnt )
902 	 || ( gamemode == pack_plut ) */)
903     {
904 	skytexture = R_TextureNumForName ("SKY3");
905 	if (gamemap < 12)
906 	    skytexture = R_TextureNumForName ("SKY1");
907 	else
908 	    if (gamemap < 21)
909 		skytexture = R_TextureNumForName ("SKY2");
910     }
911 	else
912 #endif
913 #ifndef GL_HEXEN
914 	skytexture = R_TextureNumForName(szSkyName[episode-1]);
915 #else
916 	if (episode==1)
917 	{	skytexture = Sky1Texture;
918 		GL_Sky1Texture=iNbGLTextures+1;
919 	}
920 	else
921 	{	skytexture = Sky2Texture;
922 		GL_Sky2Texture=iNbGLTextures+1;
923 	}
924 #endif
925 	texture = textures[skytexture];
926 	for (j=0 ; j<texture->patchcount ; j++)
927 	{
928 
929 		lump = texture->patches[j].patch;
930 		realpatch = W_CacheLumpNum (lump, PU_CACHE);
931 	}
932 
933 	p_stGLTextures=(GLTexture *)Realloc(p_stGLTextures,sizeof(GLTexture)*(iNbGLTextures+1));
934 	a_iGLTexTranslation=(int *)Realloc(a_iGLTexTranslation,sizeof(int)*(iNbGLTextures+1));
935 
936 	p_stGLTextures[iNbGLTextures].iWidth=TEX_SKY_W;
937 	p_stGLTextures[iNbGLTextures].iHeight=TEX_SKY_H;
938 
939 #ifndef GL_HEXEN
940 	if (g_bPaletteTexture)	// MR1405
941 #else
942 	if (g_bPaletteTexture&&
943 		(!DoubleSky||
944 		(DoubleSky&&(episode==2))))
945 #endif
946 		p_stGLTextures[iNbGLTextures].p_bRGBBuffer=(unsigned char *)Malloc(TEX_SKY_W*TEX_SKY_H);
947 	else
948 		p_stGLTextures[iNbGLTextures].p_bRGBBuffer=(unsigned char *)Malloc(TEX_SKY_W*TEX_SKY_H*4);
949 
950 	p_stGLTextures[iNbGLTextures].a_stSubTex=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
951 	p_stGLTextures[iNbGLTextures].a_stFreeSpace=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
952 	p_stGLTextures[iNbGLTextures].iNbSubTextures=1;
953 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].iU=0;
954 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].iV=0;
955 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].iWidth=TEX_SKY_W;
956 	p_stGLTextures[iNbGLTextures].a_stSubTex[0].iHeight=TEX_SKY_H;
957 #ifdef GL_HEXEN
958 	if (DoubleSky&&(episode==1))
959 		p_stGLTextures[iNbGLTextures].a_stSubTex[0].bIsNZ=1;
960 	else
961 #endif
962 		p_stGLTextures[iNbGLTextures].a_stSubTex[0].bIsNZ=0;
963 
964 	p_stGLTextures[iNbGLTextures].iNbFreeSpace=0;
965 	p_stGLTextures[iNbGLTextures].a_stFreeSpace[0].iU=0;
966 	p_stGLTextures[iNbGLTextures].a_stFreeSpace[0].iV=0;
967 	p_stGLTextures[iNbGLTextures].a_stFreeSpace[0].iWidth=0;
968 	p_stGLTextures[iNbGLTextures].a_stFreeSpace[0].iHeight=0;
969 
970 	for (x=0;x<TEX_SKY_W;x++)
971 	{ byte *p_bColumn;
972 
973 		p_bColumn=(byte *)realpatch+LONG(realpatch->columnofs[x])+3;
974 #undef Y
975 #define Y y
976 		for (y=0;y<SHORT(realpatch->height);y++)
977 #ifndef GL_HEXEN
978 			if (g_bPaletteTexture)	// MR1405
979 #else
980 			if (g_bPaletteTexture&&
981 				(!DoubleSky||
982 				(DoubleSky&&(episode==2))))
983 #endif
984 				p_stGLTextures[iNbGLTextures].p_bRGBBuffer[Y*TEX_SKY_W+x]=p_bColumn[y];
985 			else
986 			{	p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(Y*TEX_SKY_W+x)]=p_bPalette[p_bColumn[y]*3];
987 				p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(Y*TEX_SKY_W+x)+1]=p_bPalette[p_bColumn[y]*3+1];
988 				p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(Y*TEX_SKY_W+x)+2]=p_bPalette[p_bColumn[y]*3+2];
989 #ifdef GL_HEXEN
990 				if (DoubleSky&&(episode==1)&&!p_bColumn[y])
991 					p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(Y*TEX_SKY_W+x)+3]=0;
992 				else
993 #endif
994 					p_stGLTextures[iNbGLTextures].p_bRGBBuffer[4*(Y*TEX_SKY_W+x)+3]=255;
995 			}
996 	}
997 
998 	p_stGLTexArray=(GLTexArray *)Realloc(p_stGLTexArray,++iNbTexturesInMap*sizeof(GLTexArray));
999 	/*for (k=0;k<8;k++)
1000 		p_stGLTexArray[iNbTexturesInMap-1].szTexName[k]=szSkyName[episode-1][k];*/
1001 
1002 	p_stGLTexArray[iNbTexturesInMap-1].iFlatID=-1;
1003 	p_stGLTexArray[iNbTexturesInMap-1].iTexID=-1;
1004 	p_stGLTexArray[iNbTexturesInMap-1].iGLTexIndex=iNbGLTextures++;
1005 	p_stGLTexArray[iNbTexturesInMap-1].iUVIndex=0;
1006 }
1007 
1008 #ifdef GL_HEXEN
fn_vLoadSkyTextures()1009 void fn_vLoadSkyTextures()
1010 {
1011 	fn_vLoadSkyTexture(1);
1012 	//if (DoubleSky)
1013 	if (Sky1Texture!=Sky2Texture)
1014 		fn_vLoadSkyTexture(2);
1015 }
1016 #endif
1017 
fn_vCheckIfTextureIsAnimated(int tex)1018 void fn_vCheckIfTextureIsAnimated(int tex)
1019 {
1020   int i,j,basepic;
1021   texture_t	*texture;
1022   char bAnim=0;
1023 
1024 #ifndef GL_HEXEN
1025 	if (fn_bWTAlreadyRegistered(tex))
1026 //		return;
1027 		goto switches;
1028 
1029 	for(i = 0; animdefs[i].istexture != -1; i++)		// MR (USave: char)
1030 	{
1031 		// DOOM_GL
1032 /*		if((animdefs[i].istexture)&&(R_CheckTextureNumForName(animdefs[i].startname)==tex))
1033 			bAnim=1;
1034 		if((animdefs[i].istexture)&&(R_CheckTextureNumForName(animdefs[i].endname)==tex))
1035 			bAnim=2;
1036 		// MR0906
1037 		if((animdefs[i].istexture)&&(R_CheckTextureNumForName(animdefs[i].endname)>tex)
1038 			&&(R_CheckTextureNumForName(animdefs[i].startname)<tex))
1039 			bAnim=3;*/
1040 		if((animdefs[i].istexture)&&(R_CheckTextureNumForName(animdefs[i].endname)>=tex)
1041 			&&(R_CheckTextureNumForName(animdefs[i].startname)<=tex))
1042 			bAnim=1;
1043 		if (bAnim)
1044 		{
1045 			lastanim->picnum = R_TextureNumForName(animdefs[i].endname);
1046 			lastanim->basepic = R_TextureNumForName(animdefs[i].startname);
1047 
1048 			lastanim->istexture = animdefs[i].istexture;
1049 			lastanim->numpics = lastanim->picnum-lastanim->basepic+1;
1050 			if(lastanim->numpics < 2)
1051 			{
1052 				I_Error("P_InitPicAnims: bad cycle from %s to %s",
1053 					animdefs[i].startname, animdefs[i].endname);
1054 			}
1055 			lastanim->speed = animdefs[i].speed;
1056 
1057 			/*basepic=iNbGLTextures-1;
1058 			if (bAnim==1)
1059 				for (j=lastanim->basepic+1;j<lastanim->basepic+lastanim->numpics;j++)
1060 				{	texture = textures[j];
1061 					fn_vRegisterWallTextures(j,texture);
1062 				}
1063 			if (bAnim==2)
1064 				for (j=lastanim->basepic;j<lastanim->basepic+lastanim->numpics-1;j++)
1065 				{	texture = textures[j];
1066 					fn_vRegisterWallTextures(j,texture);
1067 				}
1068 			if (bAnim==3)	// MR0906
1069 				for (j=lastanim->basepic;j<lastanim->basepic+lastanim->numpics;j++)
1070 				{	if (j!=tex)
1071 					{	texture = textures[j];
1072 						fn_vRegisterWallTextures(j,texture);
1073 					}
1074 				}*/
1075 			basepic=iNbGLTextures;
1076 			for (j=lastanim->basepic;j<lastanim->basepic+lastanim->numpics;j++)
1077 			{	texture = textures[j];
1078 				fn_vRegisterWallTextures(j,texture);
1079 			}
1080 
1081 			lastanim->basepic = basepic;
1082 			lastanim->picnum = iNbGLTextures-1;
1083 
1084 			lastanim++;
1085 			return;
1086 		}
1087 	}
1088 #else	// GL_HEXEN
1089 	animDef_t *ad;
1090 
1091 	for (i=0;i<AnimDefCount;i++)
1092 	{	ad = &AnimDefs[i];
1093 
1094 		if ((ad->type==1)&&(ad->indexInit==tex))
1095 			bAnim=1;
1096 		/*else
1097 		for (j=ad->startFrameDef;j<ad->endFrameDef;j++)
1098 		{	if (FrameDefs[j].index==tex)
1099 			{	bAnim=1;
1100 				break;
1101 			}
1102 		}*/
1103 		if ((bAnim)&&(ad->indexInit==ad->index))
1104 		{	ad->index=iNbGLTextures-1;
1105 			ad->iAnimPresentInMap=1;
1106 
1107 			for (j=ad->startFrameDef;j<=ad->endFrameDef;j++)
1108 				if (FrameDefs[j].index!=tex)
1109 				{
1110 					texture = textures[FrameDefs[j].index];
1111 					fn_vRegisterWallTextures(FrameDefs[j].index,texture);
1112 					FrameDefs[j].index=iNbGLTextures-1;
1113 				}
1114 				else
1115 					FrameDefs[j].index=ad->index;
1116 			return;
1117 		}
1118 	}
1119 #endif	// GL_HEXEN
1120 
1121 switches:
1122 	// If switch load other switch position
1123 	for (i = 0;i < MAXSWITCHES;i++)
1124 	{
1125 #ifndef GL_HEXEN
1126 		if (!alphSwitchList[i].episode)
1127 			break;
1128 #endif
1129 		// DOOM_GL
1130 		//if (tex==R_TextureNumForName(alphSwitchList[i].name1))
1131 		if (tex==R_CheckTextureNumForName(alphSwitchList[i].name1))
1132 		{	glSwitchList[i*2]=iNbGLTextures-1;
1133 			texture = textures[R_TextureNumForName(alphSwitchList[i].name2)];
1134 			tex=R_TextureNumForName(alphSwitchList[i].name2);
1135 			fn_vRegisterWallTextures(tex,texture);
1136 			glSwitchList[i*2+1]=iNbGLTextures-1;
1137 		}
1138 		// MR2606
1139 		if (tex==R_CheckTextureNumForName(alphSwitchList[i].name2))
1140 		{	texture = textures[R_TextureNumForName(alphSwitchList[i].name1)];
1141 			if (fn_bWTAlreadyRegistered(R_CheckTextureNumForName(alphSwitchList[i].name1)))
1142 				return;
1143 			glSwitchList[i*2+1]=iNbGLTextures-1;
1144 			tex=R_TextureNumForName(alphSwitchList[i].name1);
1145 			fn_vRegisterWallTextures(tex,texture);
1146 			glSwitchList[i*2]=iNbGLTextures-1;
1147 		}
1148 	}
1149 }
1150 
fn_vCheckIfFlatIsAnimated(int tex)1151 void fn_vCheckIfFlatIsAnimated(int tex)
1152 {
1153   int i,j,basepic;
1154   texture_t	*texture;
1155   char bAnim=0;
1156 #ifndef GL_HEXEN
1157 
1158 	for(i = 0; animdefs[i].istexture != -1; i++)		// MR (USave: char)
1159 	{
1160 		// DOOM_GL
1161 		if((!animdefs[i].istexture)&&(W_CheckNumForName(animdefs[i].startname) != -1)&&
1162 			(R_FlatNumForName(animdefs[i].startname)==tex))
1163 			bAnim=1;
1164 		if((!animdefs[i].istexture)&&(W_CheckNumForName(animdefs[i].endname) != -1)&&
1165 			(R_FlatNumForName(animdefs[i].endname)==tex))
1166 			bAnim=2;
1167 		// MR2606
1168 		if((!animdefs[i].istexture)&&
1169 			(W_CheckNumForName(animdefs[i].endname) != -1)&&
1170 			(W_CheckNumForName(animdefs[i].startname) != -1)
1171 			&&(R_FlatNumForName(animdefs[i].endname)>tex)
1172 			&&(R_FlatNumForName(animdefs[i].startname)<tex))
1173 			bAnim=3;
1174 		if (bAnim)
1175 		{
1176 			lastanim->picnum = R_FlatNumForName(animdefs[i].endname);
1177 			lastanim->basepic = R_FlatNumForName(animdefs[i].startname);
1178 
1179 			lastanim->istexture = animdefs[i].istexture;
1180 			lastanim->numpics = lastanim->picnum-lastanim->basepic+1;
1181 			if(lastanim->numpics < 2)
1182 			{
1183 				I_Error("P_InitPicAnims: bad cycle from %s to %s",
1184 					animdefs[i].startname, animdefs[i].endname);
1185 			}
1186 			lastanim->speed = animdefs[i].speed;
1187 
1188 			basepic=iNbGLTextures-1;
1189 			// DOOM_GL
1190 			if (bAnim==1)
1191 				for (j=lastanim->basepic+1;j<lastanim->basepic+lastanim->numpics;j++)
1192 				{	texture = textures[j];
1193 					fn_vRegisterFlatTexture(j);
1194 				}
1195 			if (bAnim==2)
1196 				for (j=lastanim->basepic;j<lastanim->basepic+lastanim->numpics-1;j++)
1197 				{	texture = textures[j];
1198 					fn_vRegisterFlatTexture(j);
1199 				}
1200 			if (bAnim==3)	// MR2606
1201 				for (j=lastanim->basepic;j<lastanim->basepic+lastanim->numpics;j++)
1202 				{	if (j!=tex)
1203 					{	texture = textures[j];
1204 						fn_vRegisterFlatTexture(j);
1205 					}
1206 				}
1207 			lastanim->basepic = basepic;
1208 			lastanim->picnum = iNbGLTextures-1;
1209 
1210 			lastanim++;
1211 		}
1212 		bAnim=0;
1213 	}
1214 #else	// GL_HEXEN
1215 	animDef_t *ad;
1216 
1217 	for (i=0;i<AnimDefCount;i++)
1218 	{	ad = &AnimDefs[i];
1219 
1220 		if ((ad->type==0)&&(ad->indexInit==tex))
1221 			bAnim=1;
1222 		/*else
1223 		for (j=ad->startFrameDef;j<ad->endFrameDef;j++)
1224 		{	if (FrameDefs[j].index==tex)
1225 			{	bAnim=1;
1226 				break;
1227 			}
1228 		}*/
1229 		if ((bAnim)&&(ad->indexInit==ad->index))
1230 		{	ad->index=iNbGLTextures-1;
1231 			ad->iAnimPresentInMap=1;
1232 
1233 			for (j=ad->startFrameDef;j<=ad->endFrameDef;j++)
1234 				if (FrameDefs[j].index!=tex)
1235 				{
1236 					//texture = textures[FrameDefs[j].index];
1237 					if (!g_bOnTheFly)
1238 						fn_vRegisterFlatTexture(FrameDefs[j].index);
1239 					else
1240 						GL_vRegisterFlatTextureOnTheFly(FrameDefs[j].index);
1241 					FrameDefs[j].index=iNbGLTextures-1;
1242 				}
1243 				else
1244 					FrameDefs[j].index=ad->index;
1245 			return;
1246 		}
1247 	}
1248 #endif	// GL_HEXEN
1249 }
1250 
GL_PurgeTextures()1251 void GL_PurgeTextures()
1252 {
1253 	//(*glDeleteTextures_s) (iNbGLTextures+iNbGLSprites,texobjs);
1254 	(*glDeleteTextures_s) (iNbGLTextures,texobjs);
1255 	(*glDeleteTextures_s) (iNbGLSprites,sprobjs);
1256 }
1257 
1258 #ifdef GL_HEXEN
fn_iIsTextureRegistered(int iTexNum)1259 GLint fn_iIsTextureRegistered(int iTexNum)
1260 { int i;
1261 
1262 	for (i=0;i<iNbTexturesInMap;i++)
1263 	{
1264 		if (p_stGLTexArray[i].iTexID==iTexNum)
1265 			return i;
1266 	}
1267 	return -1;
1268 }
1269 
GL_vRegisterWallTextureOnTheFly(int iTexNum)1270 GLint GL_vRegisterWallTextureOnTheFly(int iTexNum)
1271 { texture_t	*texture;
1272   GLint iTex;
1273 
1274 	if ((iTex=fn_iIsTextureRegistered(iTexNum))!=-1)
1275 		return iTex;
1276 	texture=textures[iTexNum];
1277 	fn_vRegisterWallTextures(iTexNum,texture);
1278 	iTex=iNbGLTextures-1;
1279 
1280 	texobjs=(GLuint *)Realloc(texobjs,iNbGLTextures*sizeof(GLuint));
1281 	texobjs[iNbGLTextures-1] = iNbGLTextures;
1282 
1283 	(*glBindTexture_s) (GL_TEXTURE_2D, texobjs[iNbGLTextures-1]);
1284 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1285 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1286 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1287 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1288 
1289 	(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE /*GL_DECAL*/);
1290 
1291 #ifdef PALETTED_TEXTURE
1292 	if ((g_bPaletteTexture)&&(!p_stGLTextures[iNbGLTextures-1].a_stSubTex[0].bIsNZ))
1293 	{	(*glColorTableEXT_s) (GL_TEXTURE_2D,GL_RGB8,256,GL_RGB,GL_UNSIGNED_BYTE,p_bPalette);
1294 		(*glTexImage2D_s) (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, p_stGLTextures[iNbGLTextures-1].iWidth, p_stGLTextures[iNbGLTextures-1].iHeight, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, p_stGLTextures[iNbGLTextures-1].p_bRGBBuffer);
1295 	}
1296 	else
1297 #endif
1298 	{	(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, p_stGLTextures[iNbGLTextures-1].iWidth, p_stGLTextures[iNbGLTextures-1].iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_stGLTextures[iNbGLTextures-1].p_bRGBBuffer);
1299 	}
1300 	return iTex;
1301 }
1302 
1303 // MR0309
fn_iIsFlatTextureRegistered(int iTexNum)1304 GLint fn_iIsFlatTextureRegistered(int iTexNum)
1305 { int i;
1306 
1307 	for (i=0;i<iNbTexturesInMap;i++)
1308 	{
1309 		if (p_stGLTexArray[i].iFlatID==iTexNum)
1310 			return i;
1311 	}
1312 	return -1;
1313 }
1314 
GL_vRegisterFlatTextureOnTheFly(int iTexNum)1315 GLint GL_vRegisterFlatTextureOnTheFly(int iTexNum)
1316 { texture_t	*texture;
1317   GLint iTex;
1318 
1319 	if ((iTex=fn_iIsFlatTextureRegistered(iTexNum))!=-1)
1320 		return iTex;
1321 	texture=textures[iTexNum];
1322 	fn_vRegisterFlatTexture(iTexNum);
1323 	iTex=iNbGLTextures-1;
1324 
1325 	texobjs=(GLuint *)Realloc(texobjs,iNbGLTextures*sizeof(GLuint));
1326 	texobjs[iNbGLTextures-1] = iNbGLTextures;
1327 
1328 	(*glBindTexture_s) (GL_TEXTURE_2D, texobjs[iNbGLTextures-1]);
1329 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1330 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1331 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1332 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1333 
1334 	(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE /*GL_DECAL*/);
1335 
1336 #ifdef PALETTED_TEXTURE
1337 	if ((g_bPaletteTexture)&&(!p_stGLTextures[iNbGLTextures-1].a_stSubTex[0].bIsNZ))
1338 	{	(*glColorTableEXT_s) (GL_TEXTURE_2D,GL_RGB8,256,GL_RGB,GL_UNSIGNED_BYTE,p_bPalette);
1339 		(*glTexImage2D_s) (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, p_stGLTextures[iNbGLTextures-1].iWidth, p_stGLTextures[iNbGLTextures-1].iHeight, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, p_stGLTextures[iNbGLTextures-1].p_bRGBBuffer);
1340 	}
1341 	else
1342 #endif
1343 	{	(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, p_stGLTextures[iNbGLTextures-1].iWidth, p_stGLTextures[iNbGLTextures-1].iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_stGLTextures[iNbGLTextures-1].p_bRGBBuffer);
1344 	}
1345 	return iTex;
1346 }
1347 #endif
1348 
1349 #endif // GL_HERETIC
1350