diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c
index 03af059adda2fe2baba8a2a4ca197a562d343c16..97c88791016821834c19179feb80eb394fb6f9bb 100644
--- a/src/hardware/r_opengl/r_opengl.c
+++ b/src/hardware/r_opengl/r_opengl.c
@@ -63,7 +63,6 @@ static float NEAR_CLIPPING_PLANE =   NZCLIP_PLANE;
 // **************************************************************************
 
 
-static  GLuint      NextTexAvail    = FIRST_TEX_AVAIL;
 static  GLuint      tex_downloaded  = 0;
 static  GLfloat     fov             = 90.0f;
 #if 0
@@ -72,8 +71,8 @@ static  FRGBAFloat  const_pal_col;
 #endif
 static  FBITFIELD   CurrentPolyFlags;
 
-static  FTextureInfo*  gr_cachetail = NULL;
-static  FTextureInfo*  gr_cachehead = NULL;
+static  FTextureInfo *gr_cachetail = NULL;
+static  FTextureInfo *gr_cachehead = NULL;
 
 RGBA_t  myPaletteData[256];
 GLint   screen_width    = 0;               // used by Draw2DLine()
@@ -243,6 +242,7 @@ FUNCPRINTF void DBG_Printf(const char *lpFmt, ...)
 
 /* 1.1 functions */
 /* texture objects */ //GL_EXT_texture_object
+#define pglGenTextures glGenTextures
 #define pglDeleteTextures glDeleteTextures
 #define pglBindTexture glBindTexture
 /* texture mapping */ //GL_EXT_copy_texture
@@ -359,6 +359,8 @@ static PFNglFogfv pglFogfv;
 
 /* 1.1 functions */
 /* texture objects */ //GL_EXT_texture_object
+typedef void (APIENTRY * PFNglGenTextures) (GLsizei n, const GLuint *textures);
+static PFNglGenTextures pglGenTextures;
 typedef void (APIENTRY * PFNglDeleteTextures) (GLsizei n, const GLuint *textures);
 static PFNglDeleteTextures pglDeleteTextures;
 typedef void (APIENTRY * PFNglBindTexture) (GLenum target, GLuint texture);
@@ -487,6 +489,7 @@ boolean SetupGLfunc(void)
 	GETOPENGLFUNC(pglFogf , glFogf)
 	GETOPENGLFUNC(pglFogfv , glFogfv)
 
+	GETOPENGLFUNC(pglGenTextures , glGenTextures)
 	GETOPENGLFUNC(pglDeleteTextures , glDeleteTextures)
 	GETOPENGLFUNC(pglBindTexture , glBindTexture)
 
@@ -723,33 +726,12 @@ void Flush(void)
 
 	while (gr_cachehead)
 	{
-		// this is not necessary at all, because you have loaded them normally,
-		// and so they already are in your list!
-#if 0
-		//Hurdler: 25/04/2000: now support colormap in hardware mode
-		FTextureInfo    *tmp = gr_cachehead->nextskin;
-
-		// The memory should be freed in the main code
-		while (tmp)
-		{
-			pglDeleteTextures(1, &tmp->downloaded);
-			tmp->downloaded = 0;
-			tmp = tmp->nextcolormap;
-		}
-#endif
-		pglDeleteTextures(1, (GLuint *)&gr_cachehead->downloaded);
+		if (gr_cachehead->downloaded)
+			pglDeleteTextures(1, (GLuint *)&gr_cachehead->downloaded);
 		gr_cachehead->downloaded = 0;
 		gr_cachehead = gr_cachehead->nextmipmap;
 	}
 	gr_cachetail = gr_cachehead = NULL; //Hurdler: well, gr_cachehead is already NULL
-	NextTexAvail = FIRST_TEX_AVAIL;
-#if 0
-	if (screentexture != FIRST_TEX_AVAIL)
-	{
-		pglDeleteTextures(1, &screentexture);
-		screentexture = FIRST_TEX_AVAIL;
-	}
-#endif
 
 	tex_downloaded = 0;
 }
@@ -1128,8 +1110,10 @@ EXPORT void HWRAPI(SetTexture) (FTextureInfo *pTexInfo)
 		static RGBA_t   tex[2048*2048];
 		const GLvoid   *ptex = tex;
 		INT32             w, h;
+		GLuint texnum = 0;
 
-		//DBG_Printf ("DownloadMipmap %d %x\n",NextTexAvail,pTexInfo->grInfo.data);
+		pglGenTextures(1, &texnum);
+		//DBG_Printf ("DownloadMipmap %d %x\n",(INT32)texnum,pTexInfo->grInfo.data);
 
 		w = pTexInfo->width;
 		h = pTexInfo->height;
@@ -1217,9 +1201,10 @@ EXPORT void HWRAPI(SetTexture) (FTextureInfo *pTexInfo)
 		else
 			DBG_Printf ("SetTexture(bad format) %ld\n", pTexInfo->grInfo.format);
 
-		pTexInfo->downloaded = NextTexAvail++;
-		tex_downloaded = pTexInfo->downloaded;
-		pglBindTexture(GL_TEXTURE_2D, pTexInfo->downloaded);
+		// the texture number was already generated by pglGenTextures
+		pglBindTexture(GL_TEXTURE_2D, texnum);
+		pTexInfo->downloaded = texnum;
+		tex_downloaded = texnum;
 
 		// disable texture filtering on any texture that has holes so there's no dumb borders or blending issues
 		if (pTexInfo->flags & TF_TRANSPARENT)