Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Trail.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: Trail.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Missile Trail representation class
13 */
14 
15 #include "MemDebug.h"
16 #include "Trail.h"
17 #include "Weapon.h"
18 #include "Sim.h"
19 
20 #include "Game.h"
21 #include "Light.h"
22 #include "Bitmap.h"
23 #include "DataLoader.h"
24 #include "Sound.h"
25 
26 // +--------------------------------------------------------------------+
27 
28 Trail::Trail(Bitmap* tex, int n)
29 : ntrail(0), length0(0), length1(0), texture(tex), maxtrail(n), dim(1)
30 {
31  trans = true;
32  luminous = true;
33 
38  mtl.luminous = true;
39 
40  if (maxtrail < 4) maxtrail = 512;
41  trail = new(__FILE__,__LINE__) Point[maxtrail];
42  verts = new(__FILE__,__LINE__) VertexSet(maxtrail*2);
43  verts->Clear();
44  verts->nverts = 0;
45 
46  for (int i = 0; i < maxtrail*2; i++) {
47  verts->diffuse[i] = D3DCOLOR_RGBA(255,255,255,255);
48  verts->specular[i] = D3DCOLOR_RGBA( 0, 0, 0,255);
49  verts->tu[i] = 0.0f;
50  verts->tv[i] = (i & 1) ? 1.0f : 0.0f;
51  }
52 
53  polys = new(__FILE__,__LINE__) Poly[maxtrail];
54 
55  for (int i = 0; i < maxtrail; i++) {
56  polys[i].vertex_set = verts;
57  polys[i].nverts = 4;
58  polys[i].material = &mtl;
59  }
60 
61  npolys = 0;
62  nverts = 0;
63  width = 6;
64  length = 0;
65  dim = 3;
66 
67  last_point_time = 0;
68 }
69 
71 {
72  delete [] trail;
73  delete [] polys;
74  delete verts;
75 }
76 
77 void
78 Trail::UpdateVerts(const Point& cam_pos)
79 {
80  if (ntrail < 2) return;
81 
82  int bright = 255 - dim*ntrail;
83 
84  Point head = trail[1] + loc;
85  Point tail = trail[0] + loc;
86  Point vcam = cam_pos - head;
87  Point vtmp = vcam.cross(head-tail);
88  vtmp.Normalize();
89  Point vlat = vtmp * (width + (0.1 * width * ntrail));
90 
91  verts->loc[0] = tail - vlat;
92  verts->loc[1] = tail + vlat;
93  verts->diffuse[0] = 0;
94  verts->diffuse[1] = 0;
95 
96  for (int i = 0; i < ntrail-1; i++) {
97  bright+=dim;
98  Point head = trail[i+1] + loc;
99  Point tail = trail[i] + loc;
100  Point vcam = cam_pos - head;
101  Point vtmp = vcam.cross(head-tail);
102  vtmp.Normalize();
103 
104  float trail_width = (float) (width + (ntrail-i) * width * 0.1);
105  if (i == ntrail-2) trail_width = (float) (width * 0.7);
106  Point vlat = vtmp * trail_width;
107 
108  verts->loc[2*i+2] = head - vlat;
109  verts->loc[2*i+3] = head + vlat;
110 
111  if (bright <= 0) {
112  verts->diffuse[2*i+2] = 0;
113  verts->diffuse[2*i+3] = 0;
114  }
115  else {
116  verts->diffuse[2*i+2] = D3DCOLOR_RGBA(bright,bright,bright,bright);
117  verts->diffuse[2*i+3] = D3DCOLOR_RGBA(bright,bright,bright,bright);
118  }
119  }
120 }
121 
122 void
123 Trail::Render(Video* video, DWORD flags)
124 {
125  if (!npolys) return;
126 
127  if ((flags & RENDER_ADDITIVE) == 0)
128  return;
129 
130  if (video && life) {
131  const Camera* cam = video->GetCamera();
132 
133  if (cam)
134  UpdateVerts(cam->Pos());
135 
136  video->DrawPolys(npolys, polys);
137  }
138 }
139 
140 void
142 {
143  if (ntrail >= maxtrail-1) return;
144 
145  double real_time = Game::RealTime() / 1000.0;
146 
147  if (ntrail == 0) {
148  radius = 1000;
149  }
150  else {
151  radius = (float) Point(v-loc).length();
152  }
153 
154  // just adjust the last point:
155  if (ntrail > 1 && real_time - last_point_time < 0.05) {
156  trail[ntrail-1] = v;
157  }
158 
159  // add a new point:
160  else {
161  last_point_time = real_time;
162  trail[ntrail++] = v;
163 
164  nverts += 2;
165  verts->nverts = nverts;
166 
167  if (ntrail > 1) {
168  length0 = length1;
169  length1 = length0 + (trail[ntrail-1]-trail[ntrail-2]).length();
170 
172  polys[npolys].nverts = 4;
173 
174  polys[npolys].verts[0] = nverts-4;
175  polys[npolys].verts[1] = nverts-2;
176  polys[npolys].verts[2] = nverts-1;
177  polys[npolys].verts[3] = nverts-3;
178 
179  float tu1 = (float) length1 / 250.0f;
180 
181  verts->tu[2*ntrail-1] = tu1;
182  verts->tu[2*ntrail-2] = tu1;
183 
184  npolys++;
185  }
186  }
187 }
188 
189 double
191 {
192  double avg = 0;
193 
194  for (int i = 0; i < ntrail-1; i++)
195  avg += (trail[i+1]-trail[i]).length();
196  avg /= ntrail;
197 
198  return avg;
199 }
200