Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gl_misc.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn & Alan Shieh
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 #include <string.h>
22 #define GL_MISC_CPP
23 #include "gl_globals.h"
24 #undef GL_MISC_CPP
25 #include "vegastrike.h"
26 #include "gfxlib.h"
27 #include "vs_globals.h"
28 #include "gl_light.h"
29 #include "config_xml.h"
30 #include "winsys.h"
31 
33 {
34  return gl_options.Multitexture != 0;
35 }
36 
37 void GFXCircle( float x, float y, float wid, float hei )
38 {
39  static float aaccuracy = XMLSupport::parse_float( vs_config->getVariable( "graphics", "circle_accuracy", "0.1" ) );
40  float segmag =
41  ( Vector( wid*g_game.x_resolution, 0,
42  0 )
43  -Vector( wid*g_game.x_resolution*cos( 2.*M_PI/360.0 ), hei*g_game.y_resolution*sin( 2.*M_PI/360.0 ), 0 ) ).Magnitude();
44  int accuracy = (int) ( 360.0f*aaccuracy*(1.0f < segmag ? 1.0 : segmag) );
45  if (accuracy < 4) accuracy = 4;
46  //const int accuracy=((wid*g_game.x_resolution)+(hei*g_game.y_resolution))*M_PI;
48  float iaccuracy = 1.0f/accuracy;
49  for (int i = 0; i <= accuracy; i++)
50  GFXVertex3f( x+wid*cos( i*2.*M_PI*iaccuracy ), y+hei*sin( i*2.*M_PI*iaccuracy ), 0 );
51  GFXEnd();
52 }
53 
54 void /*GFXDRVAPI*/ GFXBeginScene()
55 {
56  GFXLoadIdentity( MODEL ); //bad this should instead load the cached view matrix
58 }
59 
60 void /*GFXDRVAPI*/ GFXEndScene()
61 {
62  glFlush();
63  winsys_swap_buffers(); //swap the buffers
64 }
65 
66 void /*GFXDRVAPI*/ GFXClear( const GFXBOOL colorbuffer, const GFXBOOL depthbuffer, const GFXBOOL stencilbuffer )
67 {
68  glClear( (colorbuffer ? GL_COLOR_BUFFER_BIT : 0)
69  |(depthbuffer ? GL_DEPTH_BUFFER_BIT : 0)
70  |(stencilbuffer ? GL_STENCIL_BUFFER_BIT : 0) );
71 }
72 
73 GFXBOOL /*GFXDRVAPI*/ GFXCapture( char *filename )
74 {
75  return GFXFALSE;
76 }
77 
78 static float last_factor = 0;
79 static float last_units = 0;
80 
81 void GFXGetPolygonOffset( float *factor, float *units )
82 {
83  *factor = last_factor;
84  *units = last_units;
85 }
86 
87 void /*GFXDRVAPI*/ GFXPolygonOffset( float factor, float units )
88 {
89  last_factor = factor;
90  last_units = units;
91  if (!factor && !units) {
92  glDisable( GL_POLYGON_OFFSET_FILL );
93  glDisable( GL_POLYGON_OFFSET_POINT );
94  glDisable( GL_POLYGON_OFFSET_LINE );
95  glPolygonOffset( 0, 0 );
96  } else {
97  glPolygonOffset( factor, units );
98  glEnable( GL_POLYGON_OFFSET_FILL );
99  glEnable( GL_POLYGON_OFFSET_POINT );
100  glEnable( GL_POLYGON_OFFSET_LINE );
101  }
102 }
103 
104 void /*GFXDRVAPI*/ GFXPolygonMode( const enum POLYMODE polymode )
105 {
106  GLenum mode;
107  switch (polymode)
108  {
109  default:
110  case GFXFILLMODE:
111  mode = GL_FILL;
112  break;
113  case GFXLINEMODE:
114  mode = GL_LINE;
115  break;
116  case GFXPOINTMODE:
117  mode = GL_POINT;
118  break;
119  }
120  glPolygonMode( GL_FRONT_AND_BACK, mode );
121 }
122 
123 void /*GFXDRVAPI*/ GFXCullFace( const enum POLYFACE polyface )
124 {
125  GLenum face;
126  switch (polyface)
127  {
128  case GFXFRONT:
129  face = GL_FRONT;
130  break;
131  default:
132  case GFXBACK:
133  face = GL_BACK;
134  break;
135  case GFXFRONTANDBACK:
136  face = GL_FRONT_AND_BACK;
137  break;
138  }
139  glCullFace( face );
140 }
141 
142 void GFXPointSize( const float size )
143 {
144  glPointSize( size );
145 }
146 
147 void GFXLineWidth( const float size )
148 {
149  glLineWidth( size );
150 }
151 
152 void /*GFXDRVAPI*/ GFXBegin( const enum POLYTYPE ptype )
153 {
154  GLenum mode;
155  switch (ptype)
156  {
157  case GFXTRI:
158  mode = GL_TRIANGLES;
159  break;
160  case GFXLINE:
161  mode = GL_LINES;
162  break;
163  case GFXTRISTRIP:
164  mode = GL_TRIANGLE_STRIP;
165  break;
166  case GFXTRIFAN:
167  mode = GL_TRIANGLE_FAN;
168  break;
169  case GFXQUAD:
170  mode = GL_QUADS;
171  break;
172  case GFXQUADSTRIP:
173  mode = GL_QUAD_STRIP;
174  break;
175  case GFXLINESTRIP:
176  mode = GL_LINE_STRIP;
177  break;
178  case GFXPOLY:
179  mode = GL_POLYGON;
180  break;
181  case GFXPOINT:
182  mode = GL_POINTS;
183  break;
184  default:
185  mode = GL_POINTS;
186  assert( 0 ||! "Bad case in gl_misc.cpp." );
187  break;
188  }
189  glBegin( mode );
190 }
191 
192 void /*GFXDRVAPI*/ GFXColorf( const GFXColor &col )
193 {
194  glColor4fv( &col.r );
195 }
196 
198 {
199  float col[4];
200  glGetFloatv( GL_CURRENT_COLOR, col ); //It's best this way, we don't use it much, anyway.
201  return GFXColor( col[0], col[1], col[2], col[3] );
202 }
203 
204 #if 0
205 //HELL slow on the TNT...we can't have it
206 void /*GFXDRVAPI*/ GFXBlendColor( const GFXColor &col )
207 {
208 #ifndef WIN32
209  glBlendColor( col.r, col.g, col.b, col.a );
210 #endif
211 }
212 #endif
213 
214 void /*GFXDRVAPI*/ GFXColor4f( const float r, const float g, const float b, const float a )
215 {
216  glColor4f( r, g, b, a );
217 }
218 
219 void /*GFXDRVAPI*/ GFXTexCoord2f( const float s, const float t )
220 {
221  glTexCoord2f( s, t );
222 }
223 
224 void /*GFXDRVAPI*/ GFXTexCoord4f( const float s, const float t, const float u, const float v )
225 {
226 #if !defined (IRIX)
227  if (gl_options.Multitexture) {
228  glMultiTexCoord2fARB_p( GL_TEXTURE0_ARB, s, t );
229  glMultiTexCoord2fARB_p( GL_TEXTURE1_ARB, u, v );
230  } else
231 #endif
232  {
233  glTexCoord2f( s, t );
234  }
235 }
236 
237 void /*GFXDRVAPI*/ GFXTexCoord224f( const float s,
238  const float t,
239  const float s2,
240  const float t2,
241  const float s3,
242  const float t3,
243  const float u3,
244  const float v3 )
245 {
246 #if !defined (IRIX)
247  if (gl_options.Multitexture) {
248  glMultiTexCoord2fARB_p( GL_TEXTURE0_ARB, s, t );
249  glMultiTexCoord2fARB_p( GL_TEXTURE1_ARB, s2, t2 );
250  glMultiTexCoord4fARB_p( GL_TEXTURE2_ARB, s3, t3, u3, v3 );
251  } else
252 #endif
253  {
254  glTexCoord2f( s, t );
255  }
256 }
257 
258 void /*GFXDRVAPI*/ GFXNormal3f( const float i, const float j, const float k )
259 {
260  glNormal3f( i, j, k );
261 }
262 
263 void /*GFXDRVAPI*/ GFXNormal( const Vector &n )
264 {
265  glNormal3fv( &n.i );
266 }
267 
268 void /*GFXDRVAPI*/ GFXVertex3f( const float x, const float y, const float z )
269 {
270  glVertex3f( x, y, z );
271 }
272 
273 void /*GFXDRVAPI*/ GFXVertex3f( const double x, const double y, const double z )
274 {
275  glVertex3d( x, y, z );
276 }
277 
278 void /*GFXDRVAPI*/ GFXVertex3d( const double x, const double y, const double z )
279 {
280  glVertex3d( x, y, z );
281 }
282 
283 void GFXVertexf( const Vector &v )
284 {
285  glVertex3f( v.i, v.j, v.k );
286 }
287 
288 void GFXVertexf( const QVector &v )
289 {
290  glVertex3d( v.i, v.j, v.k );
291 }
292 
293 void /*GFXDRVAPI*/ GFXEnd()
294 {
295  glEnd();
296 }
297 
299 {
300  glGetError();
301  int list = glGenLists( 1 );
302  glNewList( list, GL_COMPILE );
303  return list;
304 }
305 
307 {
308  glEndList();
309  return glGetError() != GL_OUT_OF_MEMORY;
310 }
311 
312 void GFXCallList( int list )
313 {
314 //VSFileSystem::Fprintf (stderr,"CallListStart");///causes crash with GF2 privaledge instruction on Win2k in certain instances
315 //fflush (stderr);
316  glCallList( list );
317 //VSFileSystem::Fprintf (stderr,"CallListEnd");
318 //fflush (stderr);
319 }
320 
321 void GFXDeleteList( int list )
322 {
323  if ( glIsList( list ) )
324  glDeleteLists( list, 1 );
325 }
326 
327 void GFXSubwindow( int x, int y, int xsize, int ysize )
328 {
329  glViewport( x, y, xsize, ysize );
330  glScissor( x, y, xsize, ysize );
331  if (x == 0 && y == 0 && xsize == g_game.x_resolution && ysize == g_game.y_resolution)
332  glDisable( GL_SCISSOR_TEST );
333  else
334  glEnable( GL_SCISSOR_TEST );
335 }
336 
337 void GFXSubwindow( float x, float y, float xsize, float ysize )
338 {
340  int(ysize*g_game.y_resolution) );
341 }
342 
344 {
345  float l, r, b, t, n, f;
346  GFXGetFrustumVars( true, &l, &r, &b, &t, &n, &f );
347  return Vector( (l+(r-l)*float(x)/g_game.x_resolution),
348  (t+(b-t)*float(y)/g_game.y_resolution),
349  n );
350 }
351