1 /*
2 Copyright (C) 2003-2006 Andrey Nazarov
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #include "gl_local.h"
22 
23 glState_t gls;
24 
GL_BindTexture(int texnum)25 void GL_BindTexture( int texnum ) {
26     if( gls.texnum[gls.tmu] == texnum ) {
27         return;
28     }
29 
30 	qglBindTexture( GL_TEXTURE_2D, texnum );
31     c.texSwitches++;
32     gls.texnum[gls.tmu] = texnum;
33 }
34 
GL_SelectTMU(int tmu)35 void GL_SelectTMU( int tmu ) {
36     if( gls.tmu == tmu ) {
37         return;
38     }
39 
40     if( tmu < 0 || tmu >= gl_static.numTextureUnits ) {
41         Com_Error( ERR_FATAL, "GL_SelectTMU: bad tmu %d", tmu );
42     }
43 
44     qglActiveTextureARB( GL_TEXTURE0_ARB + tmu );
45     qglClientActiveTextureARB( GL_TEXTURE0_ARB + tmu );
46 
47     gls.tmu = tmu;
48 }
49 
GL_TexEnv(GLenum texenv)50 void GL_TexEnv( GLenum texenv ) {
51     if( gls.texenv[gls.tmu] == texenv ) {
52         return;
53     }
54 
55     switch( texenv ) {
56     case GL_REPLACE:
57         qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
58         break;
59     case GL_MODULATE:
60         qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
61         break;
62     case GL_BLEND:
63         qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
64         break;
65     case GL_ADD:
66         qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
67         break;
68     default:
69         Com_Error( ERR_FATAL, "GL_TexEnv: bad texenv" );
70         break;
71     }
72 
73     gls.texenv[gls.tmu] = texenv;
74 }
75 
GL_CullFace(glCullFace_t cull)76 void GL_CullFace( glCullFace_t cull ) {
77     if( gls.cull == cull ) {
78         return;
79     }
80     switch( cull ) {
81     case GLS_CULL_DISABLE:
82         qglDisable( GL_CULL_FACE );
83         break;
84     case GLS_CULL_FRONT:
85         qglEnable( GL_CULL_FACE );
86         qglCullFace( GL_FRONT );
87         break;
88     case GLS_CULL_BACK:
89         qglEnable( GL_CULL_FACE );
90         qglCullFace( GL_BACK );
91         break;
92     default:
93         Com_Error( ERR_FATAL, "GL_CullFace: bad cull" );
94         break;
95     }
96 
97     gls.cull = cull;
98 }
99 
GL_Bits(glStateBits_t bits)100 void GL_Bits( glStateBits_t bits ) {
101     glStateBits_t diff = bits ^ gls.bits;
102 
103     if( !diff ) {
104         return;
105     }
106 
107     if( diff & GLS_BLEND_MASK ) {
108 		if( bits & GLS_BLEND_MASK ) {
109 			qglEnable( GL_BLEND );
110 			if( bits & GLS_BLEND_BLEND ) {
111         		qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
112 			} else if( bits & GLS_BLEND_ADD ) {
113 				qglBlendFunc( GL_SRC_ALPHA, GL_ONE );
114 			} else if( bits & GLS_BLEND_MODULATE ) {
115 				qglBlendFunc( GL_DST_COLOR, GL_ONE );
116 			}
117         } else {
118             qglDisable( GL_BLEND );
119         }
120     }
121 
122     if( diff & GLS_DEPTHMASK_FALSE ) {
123         if( bits & GLS_DEPTHMASK_FALSE ) {
124             qglDepthMask( GL_FALSE );
125         } else {
126             qglDepthMask( GL_TRUE );
127         }
128     }
129 
130     if( diff & GLS_DEPTHTEST_DISABLE ) {
131         if( bits & GLS_DEPTHTEST_DISABLE ) {
132             qglDisable( GL_DEPTH_TEST );
133         } else {
134             qglEnable( GL_DEPTH_TEST );
135         }
136     }
137 
138 	if( diff & GLS_ALPHATEST_ENABLE ) {
139         if( bits & GLS_ALPHATEST_ENABLE ) {
140             qglEnable( GL_ALPHA_TEST );
141         } else {
142             qglDisable( GL_ALPHA_TEST );
143         }
144     }
145 
146     gls.bits = bits;
147 }
148 
GL_Setup2D(void)149 void GL_Setup2D( void ) {
150 	qglViewport( 0, 0, vid.width, vid.height );
151 
152 	qglMatrixMode( GL_PROJECTION );
153 	qglLoadIdentity();
154 
155 	qglOrtho( 0, vid.width, vid.height, 0, -1, 1 );
156 	draw.scale = 1;
157 
158 	*( uint32 * )draw.color = *( uint32 * )colorWhite;
159 
160 	if( draw.flags & DRAW_CLIP_MASK ) {
161 		qglDisable( GL_SCISSOR_TEST );
162 	}
163 
164 	draw.flags = 0;
165 
166 	qglMatrixMode( GL_MODELVIEW );
167 	qglLoadIdentity();
168 
169     GL_Bits( GLS_DEPTHTEST_DISABLE );
170     GL_CullFace( GLS_CULL_DISABLE );
171 }
172 
MYgluPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear,GLdouble zFar)173 static void MYgluPerspective( GLdouble fovy, GLdouble aspect,
174 		     GLdouble zNear, GLdouble zFar )
175 {
176    GLdouble xmin, xmax, ymin, ymax;
177 
178    ymax = zNear * tan( fovy * M_PI / 360.0 );
179    ymin = -ymax;
180 
181    xmin = ymin * aspect;
182    xmax = ymax * aspect;
183 
184    qglFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
185 }
186 
GL_Setup3D(void)187 void GL_Setup3D( void ) {
188     float screenAspect;
189 	int yb;
190 
191 	yb = glr.fd.y + glr.fd.height;
192 	qglViewport( glr.fd.x, vid.height - yb, glr.fd.width, glr.fd.height );
193 
194     screenAspect = ( float )glr.fd.width / glr.fd.height;
195 	qglMatrixMode( GL_PROJECTION );
196 	qglLoadIdentity();
197 	MYgluPerspective( glr.fd.fov_y, screenAspect,
198         gl_znear->value, gl_zfar->value );
199 
200 	qglMatrixMode( GL_MODELVIEW );
201 	qglLoadIdentity();
202 
203 	qglRotatef( -90, 1, 0, 0 ); /* put z axis up */
204 	qglRotatef(  90, 0, 0, 1 ); /* put y axis west, x axis north */
205 	qglRotatef( -glr.fd.viewangles[ROLL],  1, 0, 0 );
206 	qglRotatef( -glr.fd.viewangles[PITCH], 0, 1, 0 );
207 	qglRotatef( -glr.fd.viewangles[YAW],   0, 0, 1 );
208 	qglTranslatef( -glr.fd.vieworg[0], -glr.fd.vieworg[1], -glr.fd.vieworg[2] );
209 
210     GL_Bits( GLS_DEFAULT );
211     GL_CullFace( GLS_CULL_FRONT );
212 
213 }
214 
215