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_quad_list.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 "gl_globals.h"
22 
23 #include "gfxlib.h"
24 #include "vegastrike.h"
25 #include "vs_globals.h"
26 #include <stdio.h>
27 #include "vsfilesystem.h"
28 
29 GFXQuadList::GFXQuadList( GFXBOOL color ) : numVertices( 0 )
30  , numQuads( 0 )
31 {
32  data.vertices = NULL;
33  Dirty = GFXFALSE;
34  isColor = color;
35 }
36 
38 {
39  if (isColor && data.colors)
40  free( data.colors );
41  else if (!isColor && data.vertices)
42  free( data.vertices );
43 }
44 
46 {
47  if (!numQuads) return;
48  if (isColor)
49  glInterleavedArrays( GL_T2F_C4F_N3F_V3F, sizeof (GFXColorVertex), &data.colors[0] );
50  else
51  glInterleavedArrays( GL_T2F_N3F_V3F, sizeof (GFXVertex), &data.vertices[0] );
52  glDrawArrays( GL_QUADS, 0, numQuads*4 );
53  if (isColor)
54  GFXColor( 1, 1, 1, 1 );
55 }
56 
57 int GFXQuadList::AddQuad( const GFXVertex *vertices, const GFXColorVertex *color )
58 {
59  int cur = numQuads*4;
60  if (cur+3 >= numVertices) {
61  if (!numVertices) {
62  numVertices = 16;
63  if (!isColor)
64  data.vertices = (GFXVertex*) malloc( numVertices*sizeof (GFXVertex) );
65  else
66  data.colors = (GFXColorVertex*) malloc( numVertices*sizeof (GFXColorVertex) );
67  quadassignments = (int*) malloc( numVertices*sizeof (int)/4 );
68  for (int i = 0; i < numVertices/8; i++)
69  quadassignments[i] = -1;
70  } else {
71  numVertices *= 2;
72  if (!isColor)
73  data.vertices = (GFXVertex*) realloc( data.vertices, numVertices*sizeof (GFXVertex) );
74  else
75  data.colors = (GFXColorVertex*) realloc( data.colors, numVertices*sizeof (GFXColorVertex) );
76  quadassignments = (int*) realloc( quadassignments, numVertices*sizeof (int)/4 );
77  }
78  for (int i = numVertices/8; i < numVertices/4; i++)
79  quadassignments[i] = -1;
80  Dirty = numVertices/8;
81  quadassignments[numQuads] = numQuads;
82  numQuads++;
83  if (!isColor && vertices)
84  memcpy( data.vertices+cur, vertices, 4*sizeof (GFXVertex) );
85  if (isColor && color)
86  memcpy( data.colors+cur, color, 4*sizeof (GFXColorVertex) );
87  return numQuads-1;
88  }
89  for (int i = 0; i < numVertices/4; i++)
90  if (quadassignments[i] == -1) {
91  quadassignments[i] = numQuads;
92  if (!isColor && vertices)
93  memcpy( data.vertices+(quadassignments[i]*4), vertices, 4*sizeof (GFXVertex) );
94  if (isColor && color)
95  memcpy( data.colors+(quadassignments[i]*4), color, 4*sizeof (GFXColorVertex) );
96  numQuads++;
97  Dirty--;
98  return i;
99  }
100  VSFileSystem::vs_dprintf( 1, "Fatal Error adding quads" );
101  //should NOT get here!
102  return -1;
103 }
104 void GFXQuadList::DelQuad( int which )
105 {
106  if (quadassignments[which] >= numQuads) {
107  VSFileSystem::vs_dprintf( 1, "error del" );
108  return;
109  }
110  if (which < 0 || which >= numVertices/4 || quadassignments[which] == -1)
111  return;
112  Dirty++;
113  for (int i = 0; i < numVertices/4; i++)
114  if (quadassignments[i] == numQuads-1) {
115  if (isColor)
116  memcpy( data.colors+(quadassignments[which]*4), data.colors+( (numQuads-1)*4 ), 4*sizeof (GFXColorVertex) );
117  else
118  memcpy( data.vertices+(quadassignments[which]*4), data.vertices+( (numQuads-1)*4 ), 4*sizeof (GFXVertex) );
119  quadassignments[i] = quadassignments[which];
120  quadassignments[which] = -1;
121  numQuads--;
122  return;
123  }
124  VSFileSystem::vs_dprintf( 1, " error deleting engine flame\n" );
125 }
126 void GFXQuadList::ModQuad( int which, const GFXVertex *vertices, float alpha )
127 {
128  if (which < 0 || which >= numVertices/4 || quadassignments[which] == -1)
129  return;
130  if (isColor) {
131  int w = quadassignments[which]*4;
132 
133  data.colors[w+0].SetVtx( vertices[0] );
134  data.colors[w+1].SetVtx( vertices[1] );
135  data.colors[w+2].SetVtx( vertices[2] );
136  data.colors[w+3].SetVtx( vertices[3] );
137  if (alpha != -1) {
138  if (alpha == 0)
139  alpha = .01;
140  float alp = (data.colors[w].r > data.colors[w].b)
141  ? ( (data.colors[w].r > data.colors[w].g) ? data.colors[w].r : data.colors[w].g )
142  : ( (data.colors[w].b > data.colors[w].g) ? data.colors[w].b : data.colors[w].g );
143  if (alp > .0001) {
144  float tmp[4] =
145  {alpha *data.colors[w+0].r/alp, alpha*data.colors[w+0].g/alp, alpha*data.colors[w+0].b/alp, alpha};
146  memcpy( &data.colors[w+0].r, tmp, sizeof (float)*4 );
147  memcpy( &data.colors[w+1].r, tmp, sizeof (float)*4 );
148  memcpy( &data.colors[w+2].r, tmp, sizeof (float)*4 );
149  memcpy( &data.colors[w+3].r, tmp, sizeof (float)*4 );
150  }
151  }
152  } else {
153  memcpy( data.vertices+(quadassignments[which]*4), vertices, 4*sizeof (GFXVertex) );
154  }
155 }
156 
157 void GFXQuadList::ModQuad( int which, const GFXColorVertex *vertices )
158 {
159  if (which < 0 || which >= numVertices/4 || quadassignments[which] == -1)
160  return;
161  if (isColor) {
162  memcpy( data.vertices+(quadassignments[which]*4), vertices, 4*sizeof (GFXColorVertex) );
163  } else {
164  data.vertices[(quadassignments[which]*4)+0].SetTexCoord( vertices[0].s,
165  vertices[0].t ).SetNormal( Vector( vertices[0].i,
166  vertices[0].j,
167  vertices[0].k ) ).
168  SetVertex(
169  Vector( vertices[0].x,
170  vertices
171  [
172  0
173  ].y, vertices[0].z ) );
174  data.vertices[(quadassignments[which]*4)+1].SetTexCoord( vertices[1].s,
175  vertices[1].t ).SetNormal( Vector( vertices[1].i,
176  vertices[1].j,
177  vertices[1].k ) ).
178  SetVertex(
179  Vector( vertices[1].x,
180  vertices
181  [
182  1
183  ].y, vertices[1].z ) );
184  data.vertices[(quadassignments[which]*4)+2].SetTexCoord( vertices[2].s,
185  vertices[2].t ).SetNormal( Vector( vertices[2].i,
186  vertices[2].j,
187  vertices[2].k ) ).
188  SetVertex(
189  Vector( vertices[2].x,
190  vertices
191  [
192  2
193  ].y, vertices[2].z ) );
194  data.vertices[(quadassignments[which]*4)+3].SetTexCoord( vertices[3].s,
195  vertices[3].t ).SetNormal( Vector( vertices[3].i,
196  vertices[3].j,
197  vertices[3].k ) ).
198  SetVertex(
199  Vector( vertices[3].x,
200  vertices
201  [
202  3
203  ].y, vertices[3].z ) );
204  }
205 }
206