1cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
2cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichCopyright (C) 1996-1997 Id Software, Inc.
3cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
4cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichThis program is free software; you can redistribute it and/or
5cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichmodify it under the terms of the GNU General Public License
6cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichas published by the Free Software Foundation; either version 2
7cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichof the License, or (at your option) any later version.
8cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
9cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichThis program is distributed in the hope that it will be useful,
10cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichbut WITHOUT ANY WARRANTY; without even the implied warranty of
11cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
13cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichSee the GNU General Public License for more details.
14cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
15cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichYou should have received a copy of the GNU General Public License
16cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichalong with this program; if not, write to the Free Software
17cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
19cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
20cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
21cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// draw.c -- this is the only file outside the refresh that touches the
22cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// vid buffer
23cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
24cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#include "quakedef.h"
25cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
26cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define GL_COLOR_INDEX8_EXT     0x80E5
27cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
28cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
29cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichcvar_t		gl_nobind = CVAR2("gl_nobind", "0");
30cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichcvar_t		gl_max_size = CVAR2("gl_max_size", "1024");
31cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichcvar_t		gl_picmip = CVAR2("gl_picmip", "0");
32cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
33cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichbyte		*draw_chars;				// 8*8 graphic characters
34cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichqpic_t		*draw_disc;
35cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichqpic_t		*draw_backtile;
36cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
37cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint			translate_texture;
38cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint			char_texture;
39cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
40cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichtypedef struct
41cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
42cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		texnum;
43cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  float	sl, tl, sh, th;
44cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich} glpic_t;
45cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
46cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichtypedef union
47cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
48cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    qpic_t qpic;
49cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    struct {
50cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // First part is from qpic
51cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        int width;
52cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        int height;
53cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
54cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        glpic_t glpic;
55cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    } g;
56cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich} packedGlpic_t;
57cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
58cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichtypedef union
59cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
60cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    byte buffer[sizeof(qpic_t) + sizeof(glpic_t)];
61cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    packedGlpic_t pics;
62cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich} conback_t;
63cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
64cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichconback_t conbackUnion;
65cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
66cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define		conback_buffer (conbackUnion.buffer)
67cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichpackedGlpic_t *conback = &conbackUnion.pics;
68cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
69cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		gl_lightmap_format = 4;
70cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		gl_solid_format = 3;
71cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		gl_alpha_format = 4;
72cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
73cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#if 1 // Standard defaults
74cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		gl_filter_min = GL_LINEAR_MIPMAP_NEAREST;
75cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		gl_filter_max = GL_LINEAR;
76cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
77cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		gl_filter_min = GL_NEAREST_MIPMAP_NEAREST;
78cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		gl_filter_max = GL_NEAREST;
79cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
80cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
81cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		texels;
82cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
83cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichtypedef struct
84cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
85cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		texnum;
86cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  char	identifier[64];
87cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		width, height;
88cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qboolean	mipmap;
89cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich} gltexture_t;
90cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
91cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define	MAX_GLTEXTURES	1024
92cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichgltexture_t	gltextures[MAX_GLTEXTURES];
93cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint			numgltextures;
94cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
95cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// GlQuake creates textures, but never deletes them. This approach works fine on
96cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// computers with lots of RAM and/or swap, but not so well on our swapless
97cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// RAM-constrained system.
98cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//
99cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// We work around this problem by adding a level of indirection. We
100cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// hook GL_LoadTexture to store enough information to recreate a texture.
101cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// Then we hook GL_BindTexture to consult a table to see whether a texture
102cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// is currently in memory or not. If it isn't, we throw out some other
103cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// texture and bring the required texture back into memory. In this way
104cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// we can limit the working set of textures.
105cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//
106cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// The texture data is stored in a memory-mapped file that is backed by
107cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// a file on the sd card. It is recreated each time the game is run. We
108cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// don't bother deleting it.
109cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
110cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define USE_TEXTURE_STORE
111cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
112cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_TEXTURE_STORE
113cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
114cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#include <unistd.h>
115cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#include <sys/types.h>
116cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#include <sys/mman.h>
117cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#include <fcntl.h>
118cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#include <errno.h>
119cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
120cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// Allow named textures to be evicted from memory.
121cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
122cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define TEXTURE_STORE_NAME "glquake/texture.store"
123cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
124cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichclass textureStore {
125cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
126cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichprivate:
127300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich    static const GLuint UNUSED = (GLuint) -2;
128300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich    static const GLuint PAGED_OUT = (GLuint) -1;
129cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
130cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    struct entry
131cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
132cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        entry* next;
133cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        entry* prev;
134300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        GLuint real_texnum;    // UNUSED, PAGED_OUT
135cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        byte* pData; // 0 ==> not created by us.
136cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        size_t size;
137cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        qboolean alpha;
138cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        int width;
139cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        int height;
140cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        qboolean mipmap;
141cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
142cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        entry() {
143cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            next = 0;
144cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            prev = 0;
145300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich            real_texnum = UNUSED;
146cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            pData = 0;
147cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
148cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
149cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
150cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        void unlink() {
151cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            if (next) {
152cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                next->prev = prev;
153cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            }
154cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            if (prev) {
155cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                prev->next = next;
156cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            }
157cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            next = 0;
158cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            prev = 0;
159cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
160cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
161cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        void insertBefore(entry* e){
162cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            if (e) {
163cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                prev = e->prev;
164cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                if ( prev ) {
165cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                    prev->next = this;
166cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                }
167cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                next = e;
168cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                e->prev = this;
169cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            }
170cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            else {
171cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                prev = 0;
172cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                next = 0;
173cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            }
174cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
175cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    };
176cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
177cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichpublic:
178cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
179cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    static textureStore* get() {
180cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (g_pTextureCache == 0) {
181cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            g_pTextureCache = new textureStore();
182cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
183cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        return g_pTextureCache;
184cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
185cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
186cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // Equivalent of glBindTexture, but uses the virtual texture table
187cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
188cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    void bind(int virtTexNum) {
189cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if ( (unsigned int) virtTexNum >= TEXTURE_STORE_NUM_TEXTURES) {
190cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            Sys_Error("not in the range we're managing");
191cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
192cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mBoundTextureID = virtTexNum;
193cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        entry* e = &mTextures[virtTexNum];
194cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
195300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        if ( e->real_texnum == UNUSED) {
196300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich            glGenTextures( 1, &e->real_texnum);
197cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
198cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
199cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if ( e->pData == 0) {
200cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            glBindTexture(GL_TEXTURE_2D, e->real_texnum);
201cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            return;
202cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
203cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
204cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        update(e);
205cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
206cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
207cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    void update(entry* e)
208cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
209cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // Update the "LRU" part of the cache
210cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        unlink(e);
211cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        e->insertBefore(mFirst);
212cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mFirst = e;
213cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (! mLast) {
214cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            mLast = e;
215cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
216cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
217300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        if (e->real_texnum == PAGED_OUT ) {
218cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            // Create a real texture
219cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            // Make sure there is enough room for this texture
220cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            ensure(e->size);
221cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
222300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich            glGenTextures( 1, &e->real_texnum);
223cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
224300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich            glBindTexture(GL_TEXTURE_2D, e->real_texnum);
225cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            GL_Upload8 (e->pData, e->width, e->height, e->mipmap,
226cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                    e->alpha);
227cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
228cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        else {
229cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            glBindTexture(GL_TEXTURE_2D, e->real_texnum);
230cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
231cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
232cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
233cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // Create a texture, and remember the data so we can create
234cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // it again later.
235cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
236cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    void create(int width, int height, byte* data, qboolean mipmap,
237cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            qboolean alpha) {
238cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        int size = width * height;
239cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (size + mLength > mCapacity) {
240cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            Sys_Error("Ran out of virtual texture space. %d", size);
241cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        };
242cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        entry* e = &mTextures[mBoundTextureID];
243cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
244cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // Call evict in case the currently bound texture id is already
245cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // in use. (Shouldn't happen in Quake.)
246cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // To Do: reclaim the old texture memory from the virtual memory.
247cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
248cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        evict(e);
249cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
250cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        e->alpha = alpha;
251cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        e->pData = mBase + mLength;
252cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        memcpy(e->pData, data, size);
253cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        e->size = size;
254cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        e->width = width;
255cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        e->height = height;
256cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        e->mipmap = mipmap;
257300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        e->real_texnum = PAGED_OUT;
258cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mLength += size;
259cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
260cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        update(e);
261cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
262cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
263cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // Re-upload the current textures because we've been reset.
264cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    void rebindAll() {
265300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        grabMagicTextureIds();
266300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        for (entry* e = mFirst; e; e = e->next ) {
267300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich            if (! (e->real_texnum == UNUSED || e->real_texnum == PAGED_OUT)) {
268cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                glBindTexture(GL_TEXTURE_2D, e->real_texnum);
269cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                if (e->pData) {
270300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich                    GL_Upload8 (e->pData, e->width, e->height, e->mipmap,
271300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich                        e->alpha);
272cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                }
273300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich            }
274cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
275cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
276cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
277cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichprivate:
278cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
279cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    textureStore() {
280300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        grabMagicTextureIds();
281cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mFirst = 0;
282cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mLast = 0;
283cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mTextureCount = 0;
284cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
285cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        char fullpath[MAX_OSPATH];
286cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        sprintf(fullpath, "%s/%s", com_gamedir, TEXTURE_STORE_NAME);
287cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
288cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mFileId = open(fullpath, O_RDWR | O_CREAT, 0666);
289cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if ( mFileId == -1 ) {
290cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            Sys_Error("Could not open texture store file %s: %d", fullpath,
291cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                    errno);
292cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
293cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
294cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (-1 == lseek(mFileId, TEXTURE_STORE_SIZE-1, SEEK_SET)) {
295cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            Sys_Error("Could not extend the texture store file size. %d",
296cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                    errno);
297cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
298cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        char end;
299cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        end = 0;
300cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (-1 == write(mFileId, &end, 1)) {
301cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            Sys_Error("Could not write last byte of the texture store file. %d",
302cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                    errno);
303cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
304cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
305cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mBase = (byte*) mmap((caddr_t)0, TEXTURE_STORE_SIZE,
306cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                PROT_READ | PROT_WRITE, MAP_PRIVATE, mFileId, 0);
307cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
308cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (mBase == (byte*) -1) {
309cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            Sys_Error("Could not mmap file %s: %d", fullpath, errno);
310cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
311cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mLength = 0;
312cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mCapacity = TEXTURE_STORE_SIZE;
313cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mRamUsed = 0;
314cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mRamSize = LIVE_TEXTURE_LIMIT;
315cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
316cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
317cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    ~textureStore() {
318cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        munmap(mBase, mCapacity);
319cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        COM_CloseFile(mFileId);
320cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
321cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
322300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich    void grabMagicTextureIds() {
323300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        // reserve these two texture ids.
324300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        glBindTexture(GL_TEXTURE_2D, UNUSED);
325300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich        glBindTexture(GL_TEXTURE_2D, PAGED_OUT);
326300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich    }
327300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich
328cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    void unlink(entry* e) {
329cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (e == mFirst) {
330cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            mFirst = e->next;
331cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
332cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (e == mLast) {
333cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            mLast = e->prev;
334cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
335cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        e->unlink();
336cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
337cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
338cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    void ensure(int size) {
339cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        while ( mRamSize - mRamUsed < (unsigned int) size) {
340cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            entry* e = mLast;
341cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            if(! e) {
342cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                Sys_Error("Ran out of entries");
343cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                return;
344cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            }
345cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            evict(e);
346cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
347cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        mRamUsed += size;
348cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
349cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
350cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    void evict(entry* e) {
351cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        unlink(e);
352cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if ( e->pData ) {
353300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich            glDeleteTextures(1, &e->real_texnum);
354300b387c7f83756db8f1d6bee4fb1ab99dca7276Jack Palevich            e->real_texnum = PAGED_OUT;
355cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            mRamUsed -= e->size;
356cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        }
357cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
358cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
359cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    static const size_t TEXTURE_STORE_SIZE = 16 * 1024 * 1024;
360cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    static const size_t LIVE_TEXTURE_LIMIT = 1 * 1024 * 1024;
361cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    static const size_t TEXTURE_STORE_NUM_TEXTURES = 512;
362cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
363cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    int mFileId;
364cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    byte* mBase;    // Base address of the memory mapped file
365cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    size_t mLength; // How much of the mm file we are currently using
366cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    size_t mCapacity; // Total size of the memory mapped file
367cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
368cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // Keep track of texture RAM.
369cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    size_t mRamUsed;
370cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    size_t mRamSize;
371cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
372cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // The virtual textures
373cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
374cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
375cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    entry mTextures[MAX_GLTEXTURES];
376cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    entry* mFirst; // LRU queue
377cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    entry* mLast;
378cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    size_t mTextureCount; // How many virtual textures have been allocated
379cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
380cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    static textureStore* g_pTextureCache;
381cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
382cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    int mBoundTextureID;
383cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich};
384cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
385cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichtextureStore* textureStore::g_pTextureCache;
386cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
387cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
388cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
389cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
390cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_Bind (int texnum)
391cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
392cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (gl_nobind.value)
393cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    texnum = char_texture;
394cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (currenttexture == texnum)
395cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return;
396cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  currenttexture = texnum;
397cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef _WIN32
398cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  bindTexFunc (GL_TEXTURE_2D, texnum);
399cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
400cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
401cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_TEXTURE_STORE
402cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     textureStore::get()->bind(texnum);
403cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
404cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glBindTexture(GL_TEXTURE_2D, texnum);
405cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
406cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
407cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
408cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
409cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
410cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
411cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
412cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============================================================================
413cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
414cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scrap allocation
415cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
416cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Allocate all the little status bar obejcts into a single texture
417cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  to crutch up stupid hardware / drivers
418cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
419cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============================================================================
420cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
421cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
422cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define	MAX_SCRAPS		2
423cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define	BLOCK_WIDTH		256
424cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define	BLOCK_HEIGHT	256
425cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
426cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint			scrap_allocated[MAX_SCRAPS][BLOCK_WIDTH];
427cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichbyte		scrap_texels[MAX_SCRAPS][BLOCK_WIDTH*BLOCK_HEIGHT*4];
428cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichqboolean	scrap_dirty;
429cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint			scrap_texnum;
430cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
431cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// returns a texture number and the position inside it
432cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint Scrap_AllocBlock (int w, int h, int *x, int *y)
433cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
434cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		i, j;
435cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		best, best2;
436cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		bestx;
437cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		texnum;
438cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
439cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++)
440cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
441cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    best = BLOCK_HEIGHT;
442cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
443cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0 ; i<BLOCK_WIDTH-w ; i++)
444cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
445cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      best2 = 0;
446cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
447cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      for (j=0 ; j<w ; j++)
448cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      {
449cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (scrap_allocated[texnum][i+j] >= best)
450cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          break;
451cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (scrap_allocated[texnum][i+j] > best2)
452cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          best2 = scrap_allocated[texnum][i+j];
453cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      }
454cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (j == w)
455cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      {	// this is a valid spot
456cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        *x = i;
457cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        *y = best = best2;
458cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      }
459cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
460cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
461cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (best + h > BLOCK_HEIGHT)
462cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      continue;
463cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
464cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0 ; i<w ; i++)
465cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      scrap_allocated[texnum][*x + i] = best + h;
466cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
467cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return texnum;
468cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
469cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
470cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Sys_Error ("Scrap_AllocBlock: full");
471cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  return 0;
472cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
473cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
474cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint	scrap_uploads;
475cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
476cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Scrap_Upload (void)
477cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
478cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		texnum;
479cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
480cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scrap_uploads++;
481cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
482cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++) {
483cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    GL_Bind(scrap_texnum + texnum);
484cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    GL_Upload8 (scrap_texels[texnum], BLOCK_WIDTH, BLOCK_HEIGHT, false, true);
485cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
486cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scrap_dirty = false;
487cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
488cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
489cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//=============================================================================
490cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/* Support Routines */
491cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
492cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichtypedef struct cachepic_s
493cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
494cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  char		name[MAX_QPATH];
495cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qpic_t		pic;
496cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte		padding[32];	// for appended glpic
497cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich} cachepic_t;
498cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
499cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define	MAX_CACHED_PICS		128
500cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichcachepic_t	menu_cachepics[MAX_CACHED_PICS];
501cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint			menu_numcachepics;
502cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
503cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichbyte		menuplyr_pixels[4096];
504cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
505cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		pic_texels;
506cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint		pic_count;
507cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
508cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
509cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
510cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
511cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_LoadPicTexture
512cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
513cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
514cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint GL_LoadPicTexture (qpic_t *pic)
515cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
516cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  return GL_LoadTexture ("", pic->width, pic->height, pic->data, false, true);
517cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
518cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
519cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
520cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichqpic_t *Draw_PicFromWad (const char *name)
521cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
522cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    packedGlpic_t	*pp;
523cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
524cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  pp = (packedGlpic_t*) W_GetLumpName (name);
525cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
526cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qpic_t* p = & pp->qpic;
527cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glpic_t* gl = & pp->g.glpic;
528cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
529cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // load little ones into the scrap
530cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (p->width < 64 && p->height < 64)
531cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
532cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    int		x, y;
533cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    int		i, j, k;
534cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    int		texnum;
535cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
536cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    texnum = Scrap_AllocBlock (p->width, p->height, &x, &y);
537cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    scrap_dirty = true;
538cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    k = 0;
539cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0 ; i<p->height ; i++)
540cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      for (j=0 ; j<p->width ; j++, k++)
541cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        scrap_texels[texnum][(y+i)*BLOCK_WIDTH + x + j] = p->data[k];
542cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    texnum += scrap_texnum;
543cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->texnum = texnum;
544cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->sl = (x+0.01)/(float)BLOCK_WIDTH;
545cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->sh = (x+p->width-0.01)/(float)BLOCK_WIDTH;
546cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->tl = (y+0.01)/(float)BLOCK_WIDTH;
547cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->th = (y+p->height-0.01)/(float)BLOCK_WIDTH;
548cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
549cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    pic_count++;
550cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    pic_texels += p->width*p->height;
551cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
552cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
553cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
554cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->texnum = GL_LoadPicTexture (p);
555cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->sl = 0;
556cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->sh = 1;
557cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->tl = 0;
558cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    gl->th = 1;
559cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
560cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  return p;
561cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
562cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
563cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
564cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
565cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
566cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_CachePic
567cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
568cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
569cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichqpic_t	*Draw_CachePic (const char *path)
570cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
571cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  cachepic_t	*pic;
572cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			i;
573cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qpic_t		*dat;
574cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glpic_t		*gl;
575cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
576cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (pic=menu_cachepics, i=0 ; i<menu_numcachepics ; pic++, i++)
577cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (!strcmp (path, pic->name))
578cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      return &pic->pic;
579cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
580cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (menu_numcachepics == MAX_CACHED_PICS)
581cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Sys_Error ("menu_numcachepics == MAX_CACHED_PICS");
582cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  menu_numcachepics++;
583cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  strcpy (pic->name, path);
584cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
585cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//
586cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// load the pic from disk
587cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//
588cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  dat = (qpic_t *)COM_LoadTempFile (path);
589cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (!dat)
590cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Sys_Error ("Draw_CachePic: failed to load %s", path);
591cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  SwapPic (dat);
592cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
593cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // HACK HACK HACK --- we need to keep the bytes for
594cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // the translatable player picture just for the menu
595cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // configuration dialog
596cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (!strcmp (path, "gfx/menuplyr.lmp"))
597cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    memcpy (menuplyr_pixels, dat->data, dat->width*dat->height);
598cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
599cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  pic->pic.width = dat->width;
600cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  pic->pic.height = dat->height;
601cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
602cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glpic_t temp;
603cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl = &temp;
604cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->texnum = GL_LoadPicTexture (dat);
605cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->sl = 0;
606cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->sh = 1;
607cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->tl = 0;
608cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->th = 1;
609cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
610cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  memcpy(pic->pic.data, &temp, sizeof(temp));
611cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
612cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  return &pic->pic;
613cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
614cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
615cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
616cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_CharToConback (int num, byte *dest)
617cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
618cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		row, col;
619cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte	*source;
620cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		drawline;
621cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		x;
622cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
623cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  row = num>>4;
624cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  col = num&15;
625cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  source = draw_chars + (row<<10) + (col<<3);
626cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
627cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  drawline = 8;
628cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
629cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  while (drawline--)
630cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
631cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (x=0 ; x<8 ; x++)
632cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (source[x] != 255)
633cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        dest[x] = 0x60 + source[x];
634cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    source += 128;
635cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    dest += 320;
636cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
637cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
638cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
639cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
640cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichtypedef struct
641cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
642cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  const char *name;
643cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int	minimize, maximize;
644cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich} glmode_t;
645cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
646cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichglmode_t modes[] = {
647cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
648cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
649cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
650cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
651cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
652cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
653cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich};
654cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
655cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
656cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich===============
657cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_TextureMode_f
658cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich===============
659cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
660cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_TextureMode_f (void)
661cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
662cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		i;
663cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gltexture_t	*glt;
664cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
665cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (Cmd_Argc() == 1)
666cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
667cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0 ; i< 6 ; i++)
668cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (gl_filter_min == modes[i].minimize)
669cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      {
670cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        Con_Printf ("%s\n", modes[i].name);
671cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        return;
672cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      }
673cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Con_Printf ("current filter is unknown???\n");
674cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return;
675cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
676cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
677cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (i=0 ; i< 6 ; i++)
678cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
679cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
680cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      break;
681cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
682cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (i == 6)
683cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
684cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Con_Printf ("bad filter name\n");
685cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return;
686cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
687cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
688cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl_filter_min = modes[i].minimize;
689cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl_filter_max = modes[i].maximize;
690cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
691cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // change all the existing mipmap texture objects
692cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
693cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
694cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (glt->mipmap)
695cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
696cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      GL_Bind (glt->texnum);
697cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
698cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
699cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
700cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
701cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
702cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
703cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
704cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich===============
705cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_Init
706cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich===============
707cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
708cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_Init (void)
709cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
710cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		i;
711cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qpic_t	*cb;
712cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte	*dest, *src;
713cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		x, y;
714cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  char	ver[40];
715cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glpic_t	*gl;
716cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		start;
717cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte	*ncdata;
718cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		f, fstep;
719cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
720cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
721cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Cvar_RegisterVariable (&gl_nobind);
722cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Cvar_RegisterVariable (&gl_max_size);
723cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Cvar_RegisterVariable (&gl_picmip);
724cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
725cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // 3dfx can only handle 256 wide textures
726cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (!Q_strncasecmp ((char *)gl_renderer, "3dfx",4) ||
727cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    strstr((char *)gl_renderer, "Glide"))
728cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Cvar_Set ("gl_max_size", "256");
729cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
730cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Cmd_AddCommand ("gl_texturemode", &Draw_TextureMode_f);
731cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
732cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // load the console background and the charset
733cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // by hand, because we need to write the version
734cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // string into the background before turning
735cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // it into a texture
736cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  draw_chars = (byte*) W_GetLumpName ("conchars");
737cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (i=0 ; i<256*64 ; i++)
738cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (draw_chars[i] == 0)
739cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      draw_chars[i] = 255;	// proper transparent color
740cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
741cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // now turn them into textures
742cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  char_texture = GL_LoadTexture ("charset", 128, 128, draw_chars, false, true);
743cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
744cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  start = Hunk_LowMark();
745cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
746cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  cb = (qpic_t *)COM_LoadTempFile ("gfx/conback.lmp");
747cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (!cb)
748cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Sys_Error ("Couldn't load gfx/conback.lmp");
749cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  SwapPic (cb);
750cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
751cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // hack the version number directly into the pic
752cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#if defined(__linux__)
753cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  sprintf (ver, "(Linux %2.2f, gl %4.2f) %4.2f", (float)LINUX_VERSION, (float)GLQUAKE_VERSION, (float)VERSION);
754cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
755cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  sprintf (ver, "(gl %4.2f) %4.2f", (float)GLQUAKE_VERSION, (float)VERSION);
756cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
757cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  dest = cb->data + 320*186 + 320 - 11 - 8*strlen(ver);
758cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  y = strlen(ver);
759cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (x=0 ; x<y ; x++)
760cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Draw_CharToConback (ver[x], dest+(x<<3));
761cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
762cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#if 0
763cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  conback->width = vid.conwidth;
764cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  conback->height = vid.conheight;
765cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
766cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   // scale console to vid size
767cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   dest = ncdata = Hunk_AllocName(vid.conwidth * vid.conheight, "conback");
768cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
769cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   for (y=0 ; y<vid.conheight ; y++, dest += vid.conwidth)
770cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   {
771cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     src = cb->data + cb->width * (y*cb->height/vid.conheight);
772cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     if (vid.conwidth == cb->width)
773cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       memcpy (dest, src, vid.conwidth);
774cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     else
775cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     {
776cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       f = 0;
777cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       fstep = cb->width*0x10000/vid.conwidth;
778cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       for (x=0 ; x<vid.conwidth ; x+=4)
779cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       {
780cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich         dest[x] = src[f>>16];
781cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich         f += fstep;
782cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich         dest[x+1] = src[f>>16];
783cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich         f += fstep;
784cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich         dest[x+2] = src[f>>16];
785cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich         f += fstep;
786cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich         dest[x+3] = src[f>>16];
787cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich         f += fstep;
788cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       }
789cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     }
790cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   }
791cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
792cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  conback->g.width = cb->width;
793cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  conback->g.height = cb->height;
794cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  ncdata = cb->data;
795cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
796cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
797cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
798cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
799cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
800cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl = &conback->g.glpic;
801cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->texnum = GL_LoadTexture ("conback", conback->g.width, conback->g.height, ncdata, false, false);
802cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->sl = 0;
803cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->sh = 1;
804cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->tl = 0;
805cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl->th = 1;
806cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  conback->g.width = vid.width;
807cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  conback->g.height = vid.height;
808cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
809cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // free loaded console
810cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Hunk_FreeToLowMark(start);
811cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
812cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // save a texture slot for translated picture
813cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  translate_texture = texture_extension_number++;
814cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
815cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // save slots for scraps
816cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scrap_texnum = texture_extension_number;
817cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  texture_extension_number += MAX_SCRAPS;
818cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
819cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  //
820cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // get the other pics we need
821cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  //
822cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  draw_disc = Draw_PicFromWad ("disc");
823cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  draw_backtile = Draw_PicFromWad ("backtile");
824cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
825cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
826cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
827cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
828cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
829cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
830cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_Character
831cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
832cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraws one 8*8 graphics character with 0 being transparent.
833cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichIt can be clipped to the top of the screen to allow the console to be
834cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichsmoothly scrolled off.
835cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
836cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
837cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_Character (int x, int y, int num)
838cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
839cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte			*dest;
840cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte			*source;
841cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned short	*pusdest;
842cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int				drawline;
843cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int				row, col;
844cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  float			frow, fcol, size;
845cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
846cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (num == 32)
847cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return;		// space
848cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
849cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  num &= 255;
850cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
851cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (y <= -8)
852cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return;			// totally off screen
853cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
854cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  row = num>>4;
855cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  col = num&15;
856cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
857cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  frow = row*0.0625;
858cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  fcol = col*0.0625;
859cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  size = 0.0625;
860cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
861cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GL_Bind (char_texture);
862cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
863cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
864cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  DrawQuad(x, y, 8, 8, fcol, frow, size, size);
865cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
866cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glBegin (GL_QUADS);
867cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (fcol, frow);
868cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y);
869cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (fcol + size, frow);
870cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+8, y);
871cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (fcol + size, frow + size);
872cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+8, y+8);
873cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (fcol, frow + size);
874cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y+8);
875cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnd ();
876cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
877cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
878cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
879cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
880cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
881cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_String
882cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
883cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
884cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_String (int x, int y, const char *str)
885cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
886cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  while (*str)
887cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
888cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Draw_Character (x, y, *str);
889cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    str++;
890cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    x += 8;
891cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
892cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
893cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
894cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
895cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
896cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_DebugChar
897cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
898cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraws a single character directly to the upper right corner of the screen.
899cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichThis is for debugging lockups by drawing different chars in different parts
900cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichof the code.
901cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
902cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
903cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_DebugChar (char num)
904cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
905cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
906cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
907cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
908cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
909cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_AlphaPic
910cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
911cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
912cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_AlphaPic (int x, int y, packedGlpic_t *ppic, float alpha)
913cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
914cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte			*dest, *source;
915cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned short	*pusdest;
916cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int				v, u;
917cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glpic_t			*gl;
918cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
919cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scrap_dirty)
920cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Scrap_Upload ();
921cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl = & ppic->g.glpic;
922cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisable(GL_ALPHA_TEST);
923cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnable (GL_BLEND);
924cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
925cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//	glCullFace(GL_FRONT);
926cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor4f (1,1,1,alpha);
927cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GL_Bind (gl->texnum);
928cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
929cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    DrawQuad(x, y, ppic->g.width, ppic->g.height, gl->sl, gl->tl, gl->sh - gl->sl, gl->th - gl->tl);
930cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
931cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glBegin (GL_QUADS);
932cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (gl->sl, gl->tl);
933cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y);
934cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (gl->sh, gl->tl);
935cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+pic->width, y);
936cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (gl->sh, gl->th);
937cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+pic->width, y+pic->height);
938cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (gl->sl, gl->th);
939cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y+pic->height);
940cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnd ();
941cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
942cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor4f (1,1,1,1);
943cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnable(GL_ALPHA_TEST);
944cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisable (GL_BLEND);
945cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
946cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
947cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
948cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
949cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
950cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_Pic
951cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
952cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
953cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_Pic (int x, int y, qpic_t *pic)
954cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
955cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte			*dest, *source;
956cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned short	*pusdest;
957cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int				v, u;
958cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glpic_t			*gl;
959cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
960cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scrap_dirty)
961cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Scrap_Upload ();
962cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glpic_t temp;
963cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  memcpy(&temp, pic->data, sizeof(temp));
964cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gl = & temp;
965cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor4f (1,1,1,1);
966cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GL_Bind (gl->texnum);
967cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
968cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    DrawQuad(x, y, pic->width, pic->height, gl->sl, gl->tl, gl->sh - gl->sl, gl->th - gl->tl);
969cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
970cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glBegin (GL_QUADS);
971cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (gl->sl, gl->tl);
972cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y);
973cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (gl->sh, gl->tl);
974cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+pic->width, y);
975cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (gl->sh, gl->th);
976cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+pic->width, y+pic->height);
977cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (gl->sl, gl->th);
978cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y+pic->height);
979cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnd ();
980cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
981cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
982cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
983cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
984cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
985cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
986cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_TransPic
987cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
988cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
989cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_TransPic (int x, int y, qpic_t *pic)
990cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
991cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte	*dest, *source, tbyte;
992cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned short	*pusdest;
993cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int				v, u;
994cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
995cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (x < 0 || (unsigned)(x + pic->width) > vid.width || y < 0 ||
996cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     (unsigned)(y + pic->height) > vid.height)
997cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
998cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Sys_Error ("Draw_TransPic: bad coordinates");
999cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1000cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1001cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Draw_Pic (x, y, pic);
1002cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1003cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1004cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1005cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1006cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
1007cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_TransPicTranslate
1008cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1009cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichOnly used for the player color selection menu
1010cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
1011cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1012cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_TransPicTranslate (int x, int y, qpic_t *pic, byte *translation)
1013cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1014cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int				v, u, c;
1015cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned		trans[64*64], *dest;
1016cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte			*src;
1017cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int				p;
1018cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1019cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GL_Bind (translate_texture);
1020cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1021cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  c = pic->width * pic->height;
1022cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1023cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  dest = trans;
1024cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (v=0 ; v<64 ; v++, dest += 64)
1025cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1026cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    src = &menuplyr_pixels[ ((v*pic->height)>>6) *pic->width];
1027cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (u=0 ; u<64 ; u++)
1028cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1029cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      p = src[(u*pic->width)>>6];
1030cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (p == 255)
1031cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        dest[u] = p;
1032cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      else
1033cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        dest[u] =  d_8to24table[translation[p]];
1034cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1035cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1036cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1037cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexImage2DHelper (GL_TEXTURE_2D, 0, gl_alpha_format, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, trans);
1038cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1039cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1040cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1041cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1042cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor3f (1,1,1);
1043cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
1044cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    DrawQuad(x, y, pic->width, pic->height, 0, 0, 1, 1);
1045cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1046cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glBegin (GL_QUADS);
1047cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (0, 0);
1048cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y);
1049cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (1, 0);
1050cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+pic->width, y);
1051cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (1, 1);
1052cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+pic->width, y+pic->height);
1053cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (0, 1);
1054cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y+pic->height);
1055cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnd ();
1056cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1057cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1058cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1059cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1060cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1061cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1062cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_ConsoleBackground
1063cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1064cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1065cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1066cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_ConsoleBackground (int lines)
1067cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1068cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int y = (vid.height * 3) >> 2;
1069cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1070cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (lines > y)
1071cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Draw_Pic(0, lines - vid.height, &conback->qpic);
1072cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
1073cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Draw_AlphaPic (0, lines - vid.height, conback, (float)(1.2 * lines)/y);
1074cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1075cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1076cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1077cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1078cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
1079cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_TileClear
1080cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1081cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichThis repeats a 64*64 tile graphic to fill the screen around a sized down
1082cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichrefresh window.
1083cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
1084cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1085cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1086cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichtypedef union ByteToInt_t {
1087cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    byte b[4];
1088cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    int i;
1089cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich} ByteToInt;
1090cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1091cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_TileClear (int x, int y, int w, int h)
1092cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1093cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor3f (1,1,1);
1094cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  ByteToInt b;
1095cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  memcpy(b.b, draw_backtile->data, sizeof(b.b));
1096cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GL_Bind (b.i);
1097cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
1098cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  DrawQuad(x, y, w, h, x/64.0, y/64.0, w/64.0, h/64.0);
1099cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1100cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glBegin (GL_QUADS);
1101cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f (x/64.0, y/64.0);
1102cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y);
1103cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f ( (x+w)/64.0, y/64.0);
1104cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+w, y);
1105cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f ( (x+w)/64.0, (y+h)/64.0);
1106cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+w, y+h);
1107cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoord2f ( x/64.0, (y+h)/64.0 );
1108cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y+h);
1109cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnd ();
1110cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1111cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1112cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1113cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1114cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1115cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
1116cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_Fill
1117cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1118cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichFills a box of pixels with a single color
1119cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich=============
1120cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1121cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_Fill (int x, int y, int w, int h, int c)
1122cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1123cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisable (GL_TEXTURE_2D);
1124cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor3f (host_basepal[c*3]/255.0,
1125cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    host_basepal[c*3+1]/255.0,
1126cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    host_basepal[c*3+2]/255.0);
1127cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1128cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
1129cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  DrawQuad_NoTex(x, y, w, h);
1130cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1131cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glBegin (GL_QUADS);
1132cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1133cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x,y);
1134cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+w, y);
1135cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x+w, y+h);
1136cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (x, y+h);
1137cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1138cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnd ();
1139cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1140cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor3f (1,1,1);
1141cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnable (GL_TEXTURE_2D);
1142cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1143cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//=============================================================================
1144cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1145cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1146cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1147cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_FadeScreen
1148cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1149cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1150cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1151cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_FadeScreen (void)
1152cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1153cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnable (GL_BLEND);
1154cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisable (GL_TEXTURE_2D);
1155cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor4f (0, 0, 0, 0.8);
1156cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
1157cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  DrawQuad_NoTex(0, 0, vid.width, vid.height);
1158cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1159cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glBegin (GL_QUADS);
1160cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1161cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (0,0);
1162cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (vid.width, 0);
1163cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (vid.width, vid.height);
1164cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertex2f (0, vid.height);
1165cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1166cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnd ();
1167cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1168cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor4f (1,1,1,1);
1169cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnable (GL_TEXTURE_2D);
1170cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisable (GL_BLEND);
1171cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1172cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Sbar_Changed();
1173cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1174cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1175cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//=============================================================================
1176cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1177cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1178cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1179cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_BeginDisc
1180cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1181cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraws the little blue disc in the corner of the screen.
1182cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichCall before beginning any disc IO.
1183cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1184cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1185cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_BeginDisc (void)
1186cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1187cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (!draw_disc)
1188cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return;
1189cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
1190cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // !!! Implement this
1191cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1192cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDrawBuffer  (GL_FRONT);
1193cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Draw_Pic (vid.width - 24, 0, draw_disc);
1194cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDrawBuffer  (GL_BACK);
1195cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1196cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1197cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1198cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1199cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1200cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1201cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichDraw_EndDisc
1202cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1203cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichErases the disc icon.
1204cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichCall after completing any disc IO
1205cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1206cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1207cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid Draw_EndDisc (void)
1208cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1209cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1210cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1211cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1212cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1213cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_Set2D
1214cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1215cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichSetup as if the screen was 320*200
1216cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1217cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1218cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_Set2D (void)
1219cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1220cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glViewport (glx, gly, glwidth, glheight);
1221cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1222cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glMatrixMode(GL_PROJECTION);
1223cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glLoadIdentity ();
1224cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
1225cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glOrthof (0, vid.width, vid.height, 0, -99999, 99999);
1226cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1227cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
1228cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1229cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1230cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glMatrixMode(GL_MODELVIEW);
1231cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glLoadIdentity ();
1232cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1233cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisable (GL_DEPTH_TEST);
1234cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisable (GL_CULL_FACE);
1235cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisable (GL_BLEND);
1236cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnable (GL_ALPHA_TEST);
1237cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//	glDisable (GL_ALPHA_TEST);
1238cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1239cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor4f (1,1,1,1);
1240cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1241cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1242cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//====================================================================
1243cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1244cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1245cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1246cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_FindTexture
1247cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1248cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1249cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint GL_FindTexture (const char *identifier)
1250cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1251cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		i;
1252cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gltexture_t	*glt;
1253cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1254cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
1255cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1256cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (!strcmp (identifier, glt->identifier))
1257cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      return gltextures[i].texnum;
1258cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1259cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1260cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  return -1;
1261cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1262cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1263cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1264cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1265cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_ResampleTexture
1266cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1267cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1268cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_ResampleTexture (unsigned *in, int inwidth, int inheight, unsigned *out,  int outwidth, int outheight)
1269cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1270cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		i, j;
1271cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned	*inrow;
1272cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned	frac, fracstep;
1273cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1274cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  fracstep = inwidth*0x10000/outwidth;
1275cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (i=0 ; i<outheight ; i++, out += outwidth)
1276cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1277cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    inrow = in + inwidth*(i*inheight/outheight);
1278cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    frac = fracstep >> 1;
1279cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (j=0 ; j<outwidth ; j+=4)
1280cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1281cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[j] = inrow[frac>>16];
1282cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      frac += fracstep;
1283cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[j+1] = inrow[frac>>16];
1284cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      frac += fracstep;
1285cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[j+2] = inrow[frac>>16];
1286cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      frac += fracstep;
1287cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[j+3] = inrow[frac>>16];
1288cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      frac += fracstep;
1289cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1290cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1291cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1292cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1293cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1294cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1295cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_Resample8BitTexture -- JACK
1296cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1297cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1298cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_Resample8BitTexture (unsigned char *in, int inwidth, int inheight, unsigned char *out,  int outwidth, int outheight)
1299cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1300cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		i, j;
1301cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned	char *inrow;
1302cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned	frac, fracstep;
1303cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1304cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  fracstep = inwidth*0x10000/outwidth;
1305cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (i=0 ; i<outheight ; i++, out += outwidth)
1306cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1307cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    inrow = in + inwidth*(i*inheight/outheight);
1308cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    frac = fracstep >> 1;
1309cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (j=0 ; j<outwidth ; j+=4)
1310cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1311cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[j] = inrow[frac>>16];
1312cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      frac += fracstep;
1313cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[j+1] = inrow[frac>>16];
1314cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      frac += fracstep;
1315cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[j+2] = inrow[frac>>16];
1316cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      frac += fracstep;
1317cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[j+3] = inrow[frac>>16];
1318cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      frac += fracstep;
1319cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1320cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1321cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1322cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1323cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1324cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1325cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1326cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_MipMap
1327cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1328cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichOperates in place, quartering the size of the texture
1329cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1330cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1331cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_MipMap (byte *in, int width, int height)
1332cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1333cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		i, j;
1334cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte	*out;
1335cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1336cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  width <<=2;
1337cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  height >>= 1;
1338cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  out = in;
1339cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (i=0 ; i<height ; i++, in+=width)
1340cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1341cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (j=0 ; j<width ; j+=8, out+=4, in+=8)
1342cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1343cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[0] = (in[0] + in[4] + in[width+0] + in[width+4])>>2;
1344cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[1] = (in[1] + in[5] + in[width+1] + in[width+5])>>2;
1345cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[2] = (in[2] + in[6] + in[width+2] + in[width+6])>>2;
1346cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[3] = (in[3] + in[7] + in[width+3] + in[width+7])>>2;
1347cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1348cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1349cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1350cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1351cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef SUPPORT_8BIT_MIPMAPGENERATION
1352cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1353cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1354cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_MipMap8Bit
1355cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1356cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichMipping for 8 bit textures
1357cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1358cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichThe "in" and "out" arguments can point to the same buffer if desired
1359cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1360cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1361cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_MipMap8Bit (byte *in, byte* out, int width, int height)
1362cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1363cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int		i, j;
1364cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned short     r,g,b;
1365cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  byte	*at1, *at2, *at3, *at4;
1366cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1367cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich//	width <<=2;
1368cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  height >>= 1;
1369cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (i=0 ; i<height ; i++, in+=width)
1370cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1371cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (j=0 ; j<width ; j+=2, out+=1, in+=2)
1372cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1373cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      at1 = (byte *) (d_8to24table + in[0]);
1374cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      at2 = (byte *) (d_8to24table + in[1]);
1375cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      at3 = (byte *) (d_8to24table + in[width+0]);
1376cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      at4 = (byte *) (d_8to24table + in[width+1]);
1377cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1378cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       r = (at1[0]+at2[0]+at3[0]+at4[0]); r>>=5;
1379cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       g = (at1[1]+at2[1]+at3[1]+at4[1]); g>>=5;
1380cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich       b = (at1[2]+at2[2]+at3[2]+at4[2]); b>>=5;
1381cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1382cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      out[0] = d_15to8table[(r<<0) + (g<<5) + (b<<10)];
1383cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1384cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1385cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1386cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1387cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif // SUPPORT_8BIT_MIPMAPGENERATION
1388cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1389cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid glTexImage2DHelper( GLenum target,
1390cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   GLint level,
1391cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   GLint internalformat,
1392cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   GLsizei width,
1393cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   GLsizei height,
1394cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   GLint border,
1395cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   GLenum format,
1396cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   GLenum type,
1397cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   const GLvoid *pixels )
1398cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1399cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // In full OpenGL The internalformat can be 1..4, to indicate how many components of the data are valid.
1400cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // OpenGL ES requires the internalformat argument match the format for glTexImage2D.
1401cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1402cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexImage2D(target, level, format, width, height, border, format, type, pixels);
1403cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1404cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1405cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1406cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// Uncomment to enable manual MipMap generation
1407cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define USE_MANUAL_MIPMAP_GEN
1408cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1409cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// Uncomment one of the following:
1410cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1411cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// #define USE_16BPP_WITH_8888_ALPHA
1412cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// #define USE_16BPP_WITH_5551_ALPHA // <--- This has bugs on the simulator and the device. (Device has all alpha images invisible.)
1413cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define USE_16BPP_WITH_4444_ALPHA // <--- This has bugs on the simulator, works in device
1414cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// #define USE_32BPP
1415cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// #define USE_32BPP_MANUAL_MIPMAP_GEN
1416cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1417cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_MANUAL_MIPMAP_GEN
1418cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1419cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichinline unsigned int average4(unsigned int a, unsigned int b,
1420cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        unsigned int c, unsigned int d,
1421cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        unsigned int shift, unsigned int mask) {
1422cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    unsigned int aElem = (a >> shift) & mask;
1423cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    unsigned int bElem = (b >> shift) & mask;
1424cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    unsigned int cElem = (c >> shift) & mask;
1425cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    unsigned int dElem = (d >> shift) & mask;
1426cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    unsigned int avgElem = ((aElem + bElem + cElem + dElem) >> 2) & mask;
1427cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return avgElem << shift;
1428cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1429cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1430cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichinline unsigned int average2(unsigned int a, unsigned int b,
1431cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        unsigned int shift, unsigned int mask) {
1432cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    unsigned int aElem = (a >> shift) & mask;
1433cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    unsigned int bElem = (b >> shift) & mask;
1434cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    unsigned int avgElem = ((aElem + bElem) >> 1) & mask;
1435cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return avgElem << shift;
1436cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1437cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1438cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichinline unsigned int average4444(unsigned int a, unsigned int b) {
1439cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return
1440cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,0,0xf) |
1441cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,4,0xf) |
1442cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,8,0xf) |
1443cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,12,0xf);
1444cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1445cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1446cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichinline unsigned int average565(unsigned int a, unsigned int b) {
1447cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return
1448cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,0,0x1f) |
1449cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,5,0x3f) |
1450cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,11,0x1f);
1451cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1452cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1453cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichinline unsigned int average2_8888(unsigned int a, unsigned int b) {
1454cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return
1455cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,0,0xff) |
1456cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,8,0xff) |
1457cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,16,0xff) |
1458cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average2(a,b,24,0xff);
1459cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1460cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1461cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichinline unsigned int average4_8888(unsigned int a, unsigned int b,
1462cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        unsigned int c, unsigned int d) {
1463cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return
1464cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average4(a,b,c,d,0,0xff) |
1465cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average4(a,b,c,d,8,0xff) |
1466cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average4(a,b,c,d,16,0xff) |
1467cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        average4(a,b,c,d,24,0xff);
1468cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1469cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1470cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1471cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1472cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// pData is 8 bpp 32-bit color
1473cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
147464250492b627791eebf658722854a4eedd150061Jack Palevich
1475cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid sendTexture(int mipLevel, int width, int height, unsigned int* pData, qboolean alpha) {
1476cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (alpha) {
1477cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#if defined(USE_16BPP_WITH_8888_ALPHA)
1478cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // 8888
1479cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        glTexImage2D(GL_TEXTURE_2D, mipLevel, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pData);
1480cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#elif defined(USE_16BPP_WITH_5551_ALPHA)
1481cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // 5551
1482cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        glTexImage2D(GL_TEXTURE_2D, mipLevel, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 0);
1483cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        glTexSubImage2D(GL_TEXTURE_2D, mipLevel, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pData);
1484cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1485cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // 4444
1486cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        glTexImage2D(GL_TEXTURE_2D, mipLevel, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 0);
1487cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        glTexSubImage2D(GL_TEXTURE_2D, mipLevel, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pData);
1488cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1489cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1490cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    else {
1491cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#if 0
1492cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // 8888
1493cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        glTexImage2D(GL_TEXTURE_2D, mipLevel, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pData);
1494cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1495cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        // 565
149664250492b627791eebf658722854a4eedd150061Jack Palevich        static	unsigned short scaled[1024*512];	// [512*256];
1497cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        glTexImage2D(GL_TEXTURE_2D, mipLevel, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
149864250492b627791eebf658722854a4eedd150061Jack Palevich        // Some OpenGL ES implementations do not have to be able to convert from GL_RGBA to GL_RGB format, so
149964250492b627791eebf658722854a4eedd150061Jack Palevich        // we must do it manually here:
150064250492b627791eebf658722854a4eedd150061Jack Palevich        unsigned char* pSrc = (unsigned char*) pData;
150164250492b627791eebf658722854a4eedd150061Jack Palevich        unsigned short* pDest = scaled;
150264250492b627791eebf658722854a4eedd150061Jack Palevich        for (int y = 0; y < height; y++) {
150364250492b627791eebf658722854a4eedd150061Jack Palevich            for (int x = 0; x < width; x++) {
150464250492b627791eebf658722854a4eedd150061Jack Palevich                *pDest++ = ((pSrc[0] >> 3) << 11) |
150564250492b627791eebf658722854a4eedd150061Jack Palevich                    ((pSrc[1] >> 2) << 5) |
150664250492b627791eebf658722854a4eedd150061Jack Palevich                    (pSrc[2] >> 3);
150764250492b627791eebf658722854a4eedd150061Jack Palevich                pSrc += 4;
150864250492b627791eebf658722854a4eedd150061Jack Palevich            }
150964250492b627791eebf658722854a4eedd150061Jack Palevich        }
151064250492b627791eebf658722854a4eedd150061Jack Palevich        glTexSubImage2D(GL_TEXTURE_2D, mipLevel, 0, 0, width, height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, scaled);
1511cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1512cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1513cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1514cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1515cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1516cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich===============
1517cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_Upload32
1518cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich===============
1519cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1520cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_Upload32 (unsigned *data, int width, int height,  qboolean mipmap, qboolean alpha)
1521cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1522cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			samples;
1523cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			scaled_width, scaled_height;
152464250492b627791eebf658722854a4eedd150061Jack Palevich  static	unsigned	scaled[1024*512];	// [512*256];
1525cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1526cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (scaled_width = 1 ; scaled_width < width ; scaled_width<<=1)
1527cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    ;
1528cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (scaled_height = 1 ; scaled_height < height ; scaled_height<<=1)
1529cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    ;
1530cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1531cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scaled_width >>= (int)gl_picmip.value;
1532cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scaled_height >>= (int)gl_picmip.value;
1533cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1534cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width > gl_max_size.value)
1535cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    scaled_width = (int) gl_max_size.value;
1536cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_height > gl_max_size.value)
1537cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    scaled_height = (int) gl_max_size.value;
1538cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1539cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width * scaled_height > (int) sizeof(scaled)/4)
1540cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Sys_Error ("GL_LoadTexture: too big");
1541cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1542cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  samples = alpha ? gl_alpha_format : gl_solid_format;
1543cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1544cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    texels += scaled_width * scaled_height;
1545cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1546cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width == width && scaled_height == height)
1547cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1548cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#if 0 // Disable this optimization, we want to be able to easily switch texture formats
1549cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (!mipmap)
1550cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1551cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      glTexImage2DHelper (GL_TEXTURE_2D, 0, samples, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
1552cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      goto done;
1553cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1554cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1555cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    memcpy (scaled, data, width*height*4);
1556cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1557cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
1558cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    GL_ResampleTexture (data, width, height, scaled, scaled_width, scaled_height);
1559cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1560cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#if defined(USE_16BPP_WITH_8888_ALPHA) || defined(USE_16BPP_WITH_5551_ALPHA) || defined(USE_16BPP_WITH_4444_ALPHA)
1561cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // Upload as 16 bpp
1562cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1563cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_MANUAL_MIPMAP_GEN
1564cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1565cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // Use automatic MIPMAP generation
1566cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1567cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1568cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1);
1569cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1570cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1571cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1572cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  sendTexture(0, scaled_width, scaled_height, scaled, alpha);
1573cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1574cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_MANUAL_MIPMAP_GEN
1575cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap) {
1576cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      // Compute mip levels
1577cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      int mipWidth = scaled_width;
1578cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      int mipHeight = scaled_height;
1579cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      int mipLevel = 1;
1580cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      while (mipWidth > 1 || mipHeight > 1) {
1581cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          if (mipWidth > 1 && mipHeight > 1) {
1582cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich              // Scale horizontally and vertically
1583cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich              int srcWidth = mipWidth;
1584cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                mipWidth >>= 1;
1585cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                mipHeight >>= 1;
1586cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                const unsigned int* pIn = (const unsigned int*) scaled;
1587cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                unsigned int* pOut = (unsigned int*) scaled;
1588cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                for(int y = 0; y < mipHeight; y++) {
1589cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                    for (int x = 0; x < mipWidth; x++) {
1590cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                        *pOut++ = average4_8888(pIn[0], pIn[1],
1591cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                                pIn[srcWidth], pIn[srcWidth+1]);
1592cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                        pIn += 2;
1593cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                    }
1594cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                    pIn += srcWidth;
1595cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                }
1596cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          }
1597cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          else {
1598cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich              // Scale horizontally:
1599cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich              if (mipWidth > 1) {
1600cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  mipWidth >>= 1;
1601cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  const unsigned int* pIn = (const unsigned int*) scaled;
1602cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  unsigned int* pOut = (unsigned int*) scaled;
1603cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  unsigned int numTexels = mipHeight * mipWidth;
1604cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  for(unsigned int i = 0; i < numTexels; i++) {
1605cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                      *pOut++ = average2_8888(pIn[0], pIn[1]);
1606cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                      pIn += 2;
1607cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  }
1608cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich              }
1609cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich              // Scale vertically:
1610cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich              if (mipHeight > 1) {
1611cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  mipHeight >>= 1;
1612cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  const unsigned int* pIn = (const unsigned int*) scaled;
1613cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  unsigned int* pOut = (unsigned int*) scaled;
1614cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  for(int y = 0; y < mipHeight; y++) {
1615cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                      for (int x = 0; x < mipWidth; x++) {
1616cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                          *pOut++ = average2_8888(pIn[0], pIn[mipWidth]);
1617cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                          pIn += 1;
1618cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                      }
1619cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                      pIn += mipWidth;
1620cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich                  }
1621cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich              }
1622cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          }
1623cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1624cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          sendTexture(mipLevel, mipWidth, mipHeight, scaled, alpha);
1625cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          mipLevel++;
1626cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      }
1627cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1628cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1629cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1630cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1631cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1632cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 0);
1633cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1634cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1635cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1636cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#elif defined(USE_32BPP)
1637cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // 8888
1638cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // Use automatic MIPMAP generation
1639cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1640cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1641cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1);
1642cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1643cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexImage2DHelper (GL_TEXTURE_2D, 0, samples, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaled);
1644cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1645cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1646cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 0);
1647cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1648cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1649cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexImage2DHelper (GL_TEXTURE_2D, 0, samples, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaled);
1650cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1651cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1652cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    int		miplevel;
1653cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1654cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    miplevel = 0;
1655cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    while (scaled_width > 1 || scaled_height > 1)
1656cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1657cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      GL_MipMap ((byte *)scaled, scaled_width, scaled_height);
1658cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      scaled_width >>= 1;
1659cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      scaled_height >>= 1;
1660cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (scaled_width < 1)
1661cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        scaled_width = 1;
1662cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (scaled_height < 1)
1663cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        scaled_height = 1;
1664cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      miplevel++;
1665cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      glTexImage2DHelper (GL_TEXTURE_2D, miplevel, samples, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaled);
1666cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1667cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1668cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1669cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichdone: ;
1670cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1671cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1672cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1673cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
1674cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
1675cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1676cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
1677cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1678cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_max);
1679cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
1680cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1681cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1682cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1683cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
1684cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1685cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_Upload8_EXT (byte *data, int width, int height,  qboolean mipmap, qboolean alpha)
1686cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1687cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			i, s, bytesUsed;
1688cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qboolean	noalpha;
1689cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			p;
1690cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  static unsigned j;
1691cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    static	unsigned char compressedTextureBuffer[1024*512];	// [512*256];
1692cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned char* pTex = compressedTextureBuffer;
1693cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			scaled_width, scaled_height;
1694cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int miplevel = 0;
1695cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1696cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int originalScaledWidth;
1697cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int originalScaledHeight;
1698cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1699cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  s = width*height;
1700cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // if there are no transparent pixels, make it a 3 component
1701cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // texture even if it was specified as otherwise
1702cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (alpha)
1703cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1704cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    noalpha = true;
1705cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0 ; i<s ; i++)
1706cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1707cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (data[i] == 255)
1708cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        noalpha = false;
1709cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1710cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1711cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (alpha && noalpha)
1712cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      alpha = false;
1713cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1714cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (scaled_width = 1 ; scaled_width < width ; scaled_width<<=1)
1715cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    ;
1716cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (scaled_height = 1 ; scaled_height < height ; scaled_height<<=1)
1717cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    ;
1718cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1719cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scaled_width >>= (int)gl_picmip.value;
1720cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scaled_height >>= (int)gl_picmip.value;
1721cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1722cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width > gl_max_size.value)
1723cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    scaled_width = (int) gl_max_size.value;
1724cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_height > gl_max_size.value)
1725cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    scaled_height = (int) gl_max_size.value;
1726cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1727cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width * scaled_height > ((int) (sizeof(compressedTextureBuffer) * 3 / 4)))
1728cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Sys_Error ("GL_LoadTexture: too big");
1729cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1730cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // Copy the palette
1731cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1732cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int entrySize = alpha ? 4 : 3;
1733cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int paletteSize = entrySize * 256;
1734cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1735cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    byte* pDest = compressedTextureBuffer;
1736cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    const byte* pSrc = host_basepal;
1737cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if(alpha)
1738cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1739cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      for(int i = 0; i< 255; i++)
1740cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      {
1741cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        *pDest++ = *pSrc++;
1742cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        *pDest++ = *pSrc++;
1743cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        *pDest++ = *pSrc++;
1744cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        *pDest++ = 0xff;
1745cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      }
1746cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      // Entry 255 is transparent
1747cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      *pDest++ = 0x00;
1748cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      *pDest++ = 0x00;
1749cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      *pDest++ = 0x00;
1750cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      *pDest++ = 0x00;
1751cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1752cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    else
1753cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1754cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      memcpy(pDest, pSrc, paletteSize);
1755cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1756cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1757cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1758cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  bytesUsed = paletteSize;
1759cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  pTex += paletteSize;
1760cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1761cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  texels += scaled_width * scaled_height;
1762cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1763cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width == width && scaled_height == height)
1764cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1765cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    memcpy (pTex, data, scaled_width*scaled_height);
1766cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1767cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
1768cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    GL_Resample8BitTexture (data, width, height, pTex, scaled_width, scaled_height);
1769cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1770cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  bytesUsed += scaled_width * scaled_height;
1771cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1772cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  miplevel = 0;
1773cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1774cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  originalScaledWidth = scaled_width;
1775cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  originalScaledHeight = scaled_height;
1776cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1777cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1778cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1779cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef SUPPORT_8BIT_MIPMAPGENERATION
1780cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        miplevel = 1;
1781cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    while (scaled_width > 1 || scaled_height > 1)
1782cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1783cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      byte* pDest = (byte*) pTex + scaled_width * scaled_height;
1784cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      GL_MipMap8Bit ((byte *)pTex, pDest, scaled_width, scaled_height);
1785cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      pTex = pDest;
1786cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      scaled_width >>= 1;
1787cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      scaled_height >>= 1;
1788cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (scaled_width < 1)
1789cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        scaled_width = 1;
1790cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (scaled_height < 1)
1791cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        scaled_height = 1;
1792cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      bytesUsed += scaled_width * scaled_height;
1793cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      miplevel++;
1794cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1795cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1796cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Sys_Error("Unsupported attempt to generate 8 bit mip mapped texture. #define SUPPORT_8BIT_MIPMAPGENERATION");
1797cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1798cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1799cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1800cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GLint internalFormat = alpha ? GL_PALETTE8_RGBA8_OES : GL_PALETTE8_RGB8_OES;
1801cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glCompressedTexImage2D (GL_TEXTURE_2D, -miplevel, internalFormat,
1802cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            originalScaledWidth, originalScaledHeight,
1803cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich            0, bytesUsed, compressedTextureBuffer);
1804cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1805cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1806cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1807cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
1808cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
1809cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1810cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
1811cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1812cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_max);
1813cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
1814cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1815cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1816cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1817cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1818cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1819cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_Upload8_EXT (byte *data, int width, int height,  qboolean mipmap, qboolean alpha)
1820cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1821cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			i, s;
1822cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qboolean	noalpha;
1823cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			p;
1824cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  static unsigned j;
1825cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			samples;
1826cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    static	unsigned char scaled[1024*512];	// [512*256];
1827cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			scaled_width, scaled_height;
1828cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1829cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  s = width*height;
1830cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // if there are no transparent pixels, make it a 3 component
1831cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // texture even if it was specified as otherwise
1832cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (alpha)
1833cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1834cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    noalpha = true;
1835cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0 ; i<s ; i++)
1836cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1837cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (data[i] == 255)
1838cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        noalpha = false;
1839cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1840cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1841cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (alpha && noalpha)
1842cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      alpha = false;
1843cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1844cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (scaled_width = 1 ; scaled_width < width ; scaled_width<<=1)
1845cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    ;
1846cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  for (scaled_height = 1 ; scaled_height < height ; scaled_height<<=1)
1847cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    ;
1848cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1849cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scaled_width >>= (int)gl_picmip.value;
1850cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scaled_height >>= (int)gl_picmip.value;
1851cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1852cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width > gl_max_size.value)
1853cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    scaled_width = gl_max_size.value;
1854cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_height > gl_max_size.value)
1855cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    scaled_height = gl_max_size.value;
1856cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1857cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width * scaled_height > (int) sizeof(scaled))
1858cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    Sys_Error ("GL_LoadTexture: too big");
1859cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1860cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  samples = 1; // alpha ? gl_alpha_format : gl_solid_format;
1861cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1862cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  texels += scaled_width * scaled_height;
1863cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1864cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (scaled_width == width && scaled_height == height)
1865cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1866cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (!mipmap)
1867cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1868cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      glTexImage2D (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, scaled_width, scaled_height, 0, GL_COLOR_INDEX , GL_UNSIGNED_BYTE, data);
1869cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      goto done;
1870cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1871cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    memcpy (scaled, data, width*height);
1872cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1873cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
1874cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    GL_Resample8BitTexture (data, width, height, scaled, scaled_width, scaled_height);
1875cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1876cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glCompressedTexImage2D (GL_TEXTURE_2D, 0, GL_PALETTE8_RGB8_OES, scaled_width, scaled_height, 0, s, scaled);
1877cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1878cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1879cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef SUPPORT_8BIT_MIPMAPGENERATION
1880cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    int		miplevel;
1881cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1882cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    miplevel = 0;
1883cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    while (scaled_width > 1 || scaled_height > 1)
1884cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1885cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      GL_MipMap8Bit ((byte *)scaled, (byte*) scaled, scaled_width, scaled_height);
1886cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      scaled_width >>= 1;
1887cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      scaled_height >>= 1;
1888cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (scaled_width < 1)
1889cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        scaled_width = 1;
1890cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (scaled_height < 1)
1891cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        scaled_height = 1;
1892cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      miplevel++;
1893cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      glTexImage2D (GL_TEXTURE_2D, miplevel, GL_COLOR_INDEX8_EXT, scaled_width, scaled_height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, scaled);
1894cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1895cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
1896cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  Sys_Error("Unsupported attept to generate 8 bit mip mapped texture.");
1897cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1898cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1899cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichdone: ;
1900cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1901cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1902cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (mipmap)
1903cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1904cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
1905cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
1906cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1907cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
1908cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1909cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_max);
1910cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
1911cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1912cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1913cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1914cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif // ! OPENGL_ES
1915cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1916cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1917cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich===============
1918cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_Upload8
1919cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich===============
1920cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1921cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_Upload8 (byte *data, int width, int height,  qboolean mipmap, qboolean alpha)
1922cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1923cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichstatic	unsigned	trans[640*480];		// FIXME, temporary
1924cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			i, s;
1925cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qboolean	noalpha;
1926cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			p;
1927cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1928cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  s = width*height;
1929cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // if there are no transparent pixels, make it a 3 component
1930cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // texture even if it was specified as otherwise
1931cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (alpha)
1932cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1933cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    noalpha = true;
1934cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0 ; i<s ; i++)
1935cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1936cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      p = data[i];
1937cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (p == 255)
1938cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        noalpha = false;
1939cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      trans[i] = d_8to24table[p];
1940cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1941cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1942cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (alpha && noalpha)
1943cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      alpha = false;
1944cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1945cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else
1946cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1947cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    if (s&3)
1948cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      Sys_Error ("GL_Upload8: s&3");
1949cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0 ; i<s ; i+=4)
1950cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1951cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      trans[i] = d_8to24table[data[i]];
1952cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      trans[i+1] = d_8to24table[data[i+1]];
1953cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      trans[i+2] = d_8to24table[data[i+2]];
1954cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      trans[i+3] = d_8to24table[data[i+3]];
1955cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1956cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1957cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1958cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich   if (VID_Is8bit() && (data!=scrap_texels[0])
1959cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#if !defined(USE_OPENGLES)
1960cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    && !alpha
1961cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
1962cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  ) {
1963cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     GL_Upload8_EXT (data, width, height, mipmap, alpha);
1964cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich     return;
1965cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
1966cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GL_Upload32 (trans, width, height, mipmap, alpha);
1967cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
1968cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1969cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/*
1970cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1971cabb5dd768714a7df34469a096b5e1aa815a2c22Jack PalevichGL_LoadTexture
1972cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich================
1973cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich*/
1974cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichint GL_LoadTexture (const char *identifier, int width, int height, byte *data, qboolean mipmap, qboolean alpha)
1975cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
1976cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qboolean	noalpha;
1977cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  int			i, p, s;
1978cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  gltexture_t	*glt;
1979cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
1980cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  // see if the texture is allready present
1981cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (identifier[0])
1982cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  {
1983cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
1984cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    {
1985cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      if (!strcmp (identifier, glt->identifier))
1986cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      {
1987cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        if (width != glt->width || height != glt->height)
1988cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich          Sys_Error ("GL_LoadTexture: cache mismatch");
1989cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich        return gltextures[i].texnum;
1990cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich      }
1991cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    }
1992cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
1993cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // Surely we want to remember this new texture.
1994cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // Doing this costs 1% fps per timedemo on a DX7 PC,
1995cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // probably because of the linear search through the
1996cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // texture cache, but it saves 10 MB of VM growth per
1997cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // level load. It also makes the GL_TEXTUREMODE
1998cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    // console command work correctly.
1999cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    numgltextures++;
2000cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
2001cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
2002cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  else {
2003cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    glt = &gltextures[numgltextures];
2004cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    numgltextures++;
2005cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  }
2006cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2007cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  strcpy (glt->identifier, identifier);
2008cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glt->texnum = texture_extension_number;
2009cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glt->width = width;
2010cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glt->height = height;
2011cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glt->mipmap = mipmap;
2012cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2013cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    GL_Bind(texture_extension_number);
2014cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2015cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_TEXTURE_STORE
2016cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2017cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  textureStore::get()->create(width, height, data, mipmap, alpha);
2018cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2019cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
2020cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2021cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    GL_Upload8 (data, width, height, mipmap, alpha);
2022cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2023cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
2024cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2025cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  texture_extension_number++;
2026cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  return texture_extension_number-1;
2027cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
2028cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2029cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2030cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich/****************************************/
2031cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2032cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichstatic GLenum oldtarget = TEXTURE0_SGIS;
2033cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2034cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_SelectTexture (GLenum target)
2035cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
2036cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (!gl_mtexable)
2037cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return;
2038cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
2039cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glActiveTexture(target);
2040cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#else
2041cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  qglSelectTextureSGIS(target);
2042cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
2043cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  if (target == oldtarget)
2044cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    return;
2045cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  cnttextures[oldtarget-TEXTURE0_SGIS] = currenttexture;
2046cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  currenttexture = cnttextures[target-TEXTURE0_SGIS];
2047cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  oldtarget = target;
2048cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
2049cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2050cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// OpenGL ES compatible DrawQuad utility
2051cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2052cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define BEGIN_QUAD glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
2053cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#define END_QUAD glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2054cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2055cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid DrawQuad_NoTex(float x, float y, float w, float h)
2056cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
2057cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  BEGIN_QUAD
2058cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2059cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  float vertex[2*4] = {x,y,x+w,y, x+w, y+h, x, y+h};
2060cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  short index[4] = {0, 1, 2, 3};
2061cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertexPointer( 2, GL_FLOAT, 0, vertex);
2062cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2063cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, index);
2064cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2065cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2066cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  END_QUAD
2067cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
2068cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2069cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid DrawQuad(float x, float y, float w, float h, float u, float v, float uw, float vh)
2070cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
2071cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  BEGIN_QUAD
2072cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2073cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich    float texcoord[2*4] = {u, v, u + uw, v, u + uw, v + vh, u, v + vh};
2074cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  float vertex[2*4] = {x,y,x+w,y, x+w, y+h, x, y+h};
2075cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  unsigned short index[4] = {0, 1, 2, 3};
2076cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glTexCoordPointer( 2, GL_FLOAT, 0, texcoord);
2077cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glVertexPointer( 2, GL_FLOAT, 0, vertex);
2078cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, index);
2079cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2080cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  END_QUAD
2081cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
2082cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2083cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#ifdef USE_OPENGLES
2084cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2085cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// Reimplementation of OpenGL functions that are missing in OpenGL ES
2086cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2087cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid glColor3f(GLfloat r, GLfloat g, GLfloat b)
2088cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
2089cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor4f(r, g, b, 1.0f);
2090cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
2091cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2092cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid glColor4fv(GLfloat* pColor)
2093cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich{
2094cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  glColor4f(pColor[0], pColor[1], pColor[2], pColor[3]);
2095cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
2096cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2097cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichfloat gVertexBuffer[VERTEXARRAYSIZE];
2098cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichfloat gColorBuffer[VERTEXARRAYSIZE];
2099cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichfloat gTexCoordBuffer[VERTEXARRAYSIZE];
2100cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2101cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich// Called when we've lost the OpenGL context and have to recreate it.
2102cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichextern void GL_Init();
2103cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichextern void R_InitParticleTexture2();
2104cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichextern void GL_UploadLightmaps();
2105cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichextern void R_ReloadSky();
2106cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2107cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevichvoid GL_ReInit() {
2108cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GL_Init();
2109cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  textureStore::get()->rebindAll();
2110cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  scrap_dirty = true;
2111cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  R_InitParticleTexture2();
2112cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  GL_UploadLightmaps();
2113cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich  R_ReloadSky();
2114cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich}
2115cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich
2116cabb5dd768714a7df34469a096b5e1aa815a2c22Jack Palevich#endif
211772cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich
211872cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich#ifdef DEBUG_OPENGL_CALLS
211972cead5e5712e5704ab702f521b53bce367ee6dbJack Palevichvoid checkGLImp(const char* state, const char* file, int line) {
212072cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich    GLenum error = glGetError();
212172cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich    if (error != GL_NO_ERROR) {
212272cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich    	Sys_Error("%s: error 0x%04X at %s:%d\n", state, error, file, line);
212372cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich    }
212472cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich}
212572cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich
212672cead5e5712e5704ab702f521b53bce367ee6dbJack Palevich#endif
2127