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
aux_logo.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
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 "gfx/vec.h"
22 #include "cmd/unit_generic.h"
23 #include "vertex.h"
24 #include "aux_logo.h"
25 #include "aux_texture.h"
26 #include <assert.h>
27 #include "gfxlib.h"
28 #include "vs_globals.h"
29 #include "config_xml.h"
30 #include "xml_support.h"
31 vector< Logo* >undrawn_logos;
32 Hashtable< int, Logo, 257 >Logo::decalHash;
33 
34 Logo::Logo( int numberlogos,
35  Vector *center,
36  Vector *normal,
37  float *size,
38  float *rotation,
39  float offset,
40  Texture *Dec,
41  Vector *Ref )
42 {
43  offset = 0;
44  refcount = -1;
45  draw_queue = NULL;
46 
47  numlogos = numberlogos;
48  GFXVertex *vertices = new GFXVertex[numlogos*4];
49  GFXVertex *LogoCorner = vertices;
50  //LogoCorner = new glVertex* [numlogos]; //hope to hell we have enough mem
51  Vector p, q, r, v1, v2, v3, v4; //temps
52  will_be_drawn = false;
53  for (int i = 0; i < numlogos; i++, LogoCorner += 4) {
54  r = normal[i];
55  Normalize( r );
56  Vector translation = r*offset;
57  if (Ref[i].i || Ref[i].j || Ref[i].k) {
58  p = Ref[i];
59  Normalize( p );
60  ScaledCrossProduct( r, p, q );
61  ::Roll( rotation[i], p, q, r );
62  float tsize = size[i]*0.50F;
63  Vector tcenter = center[i]+translation;
64  v4 = p* -tsize-q*tsize+tcenter;
65  v3 = p* -tsize+q*tsize+tcenter;
66  v2 = p*tsize+q*tsize+tcenter;
67  v1 = p*tsize-q*tsize+tcenter;
68  } else {
70  Vector y;
71  if ( (r.i == 1 || r.i == -1) && !r.j && !r.k )
72  y = Vector( 0, 1, 0 );
73  else
74  y = Vector( 1, 0, 0 );
75  ScaledCrossProduct( r, y, p );
76  ScaledCrossProduct( r, p, q );
77  ::Roll( rotation[i], p, q, r );
78  float tsize = size[i]*0.50F;
79  Vector tcenter = center[i]+translation;
80  v1 = q*tsize+tcenter;
81  v2 = p* -tsize+tcenter;
82  v3 = q* -tsize+tcenter;
83  v4 = p*tsize+tcenter;
84  }
85  LogoCorner[0].SetVertex( v1 ).SetNormal( r ).SetTexCoord( 0, 0 );
86  LogoCorner[1].SetVertex( v2 ).SetNormal( r ).SetTexCoord( 0, 1 );
87  LogoCorner[2].SetVertex( v3 ).SetNormal( r ).SetTexCoord( 1, 1 );
88  LogoCorner[3].SetVertex( v4 ).SetNormal( r ).SetTexCoord( 1, 0 );
89  //LogoCorner[4] = LogoCorner[2];
90  //LogoCorner[5] = LogoCorner[1];
91  }
92  vlist = new GFXVertexList( GFXQUAD, 4*numlogos, vertices, 4*numlogos );
93  delete[] vertices;
94  SetDecal( Dec );
95 }
96 
97 void Logo::SetDecal( Texture *decal )
98 {
99  Decal = decal;
100  //Check which draw_queue to use:
101  Logo *l;
102  if ( ( l = decalHash.Get( decal->name ) ) != NULL ) {
103  draw_queue = l->draw_queue;
105  l->refcount++;
106  } else {
107  l = new Logo( *this ); //(Logo*)malloc (sizeof (Logo));
108  //memcpy (l,this,sizeof(Logo));
109  decalHash.Put( decal->name, l );
110  draw_queue = l->draw_queue = new vector< DrawContext > ();
112  l->refcount = 1;
113  }
114 }
115 
116 /*Logo::Logo(int numberlogos, Vector* center,Vector* normal, float* size, float* rotation, float* offset,char *tex, char *alp)
117  * {
118  * Decal = NULL;
119  * Decal = new Texture (tex,alp);
120  * if (Decal)
121  * {
122  * if (!Decal->Data)
123  * {
124  * delete Decal;
125  * Decal = new Texture (tex,NULL);
126  * }
127  * }
128  *
129  *
130  *
131  * }*/
132 
133 void Logo::Draw( const Matrix &m )
134 {
135  if (!numlogos)
136  return;
137  //Matrix m;
138  //GFXGetMatrix(MODEL, m);
139  draw_queue->push_back( DrawContext( m, vlist ) );
141  undrawn_logos.push_back( owner_of_draw_queue );
143  }
144 }
145 
147 {
148  if ( !g_game.use_logos || draw_queue->empty() )
149  return;
150  static float offs = XMLSupport::parse_float( vs_config->getVariable( "graphics", "LogoOffset", "-1" ) );
151  static float scl = XMLSupport::parse_float( vs_config->getVariable( "graphics", "LogoOffsetScale", "-4.0" ) );
152 
153  GFXEnable( TEXTURE0 );
154  GFXEnable( TEXTURE1 );
155  Decal->MakeActive();
157  GFXDisable( LIGHTING );
158  GFXColor4f( 1, 1, 1, 1 );
159  GFXPolygonOffset( offs, scl );
161  //GFXBlendMode(ONE,ZERO);
162  DrawContext c = draw_queue->back();
163  c.vlist->LoadDrawState();
164  while ( draw_queue->size() ) {
165  c = draw_queue->back();
166  draw_queue->pop_back();
167  GFXLoadMatrixModel( c.m );
169  c.vlist->Draw();
171  }
173  GFXPolygonOffset( 0, 0 );
174 }
175 
177 {
178  if (owner_of_draw_queue != this)
179  delete vlist;
180  //if(LogoCorner!=NULL)
181  //delete [] LogoCorner;
182  if (owner_of_draw_queue != NULL) {
183  if (owner_of_draw_queue != this)
185  if (owner_of_draw_queue->refcount == 0 && owner_of_draw_queue != this)
186  delete owner_of_draw_queue;
187  }
188  if (owner_of_draw_queue == this) {
189  assert( refcount == 0 );
190  decalHash.Delete( Decal->name );
191  delete draw_queue;
192  }
193 }
194