diff options
author | milo24x7@gmail.com <milo24x7@gmail.com@076cb2c4-205e-83fd-5cf3-1be9aa105544> | 2013-07-07 22:08:49 +0000 |
---|---|---|
committer | milo24x7@gmail.com <milo24x7@gmail.com@076cb2c4-205e-83fd-5cf3-1be9aa105544> | 2013-07-07 22:08:49 +0000 |
commit | d17521c8b9085a91d08fecfd0b51bbbf7b1dccac (patch) | |
tree | 4673104b47dc68a079cac9f94deefd48dfcb66fa /Stars45/Trail.cpp | |
parent | 1de4b2bdbb019be6f1b7262c3eba5568d7682edd (diff) | |
download | starshatter-d17521c8b9085a91d08fecfd0b51bbbf7b1dccac.zip starshatter-d17521c8b9085a91d08fecfd0b51bbbf7b1dccac.tar.gz starshatter-d17521c8b9085a91d08fecfd0b51bbbf7b1dccac.tar.bz2 |
Updated open source license declaration and fixed some formatting issues.
Diffstat (limited to 'Stars45/Trail.cpp')
-rw-r--r-- | Stars45/Trail.cpp | 424 |
1 files changed, 224 insertions, 200 deletions
diff --git a/Stars45/Trail.cpp b/Stars45/Trail.cpp index 0eba06f..e826c55 100644 --- a/Stars45/Trail.cpp +++ b/Stars45/Trail.cpp @@ -1,200 +1,224 @@ -/* Project Starshatter 4.5
- Destroyer Studios LLC
- Copyright © 1997-2004. All Rights Reserved.
-
- SUBSYSTEM: Stars.exe
- FILE: Trail.cpp
- AUTHOR: John DiCamillo
-
-
- OVERVIEW
- ========
- Missile Trail representation class
-*/
-
-#include "MemDebug.h"
-#include "Trail.h"
-#include "Weapon.h"
-#include "Sim.h"
-
-#include "Game.h"
-#include "Light.h"
-#include "Bitmap.h"
-#include "DataLoader.h"
-#include "Sound.h"
-
-// +--------------------------------------------------------------------+
-
-Trail::Trail(Bitmap* tex, int n)
-: ntrail(0), length0(0), length1(0), texture(tex), maxtrail(n), dim(1)
-{
- trans = true;
- luminous = true;
-
- mtl.Kd = Color::White;
- mtl.tex_diffuse = texture;
- mtl.tex_emissive = texture;
- mtl.blend = Video::BLEND_ADDITIVE;
- mtl.luminous = true;
-
- if (maxtrail < 4) maxtrail = 512;
- trail = new(__FILE__,__LINE__) Point[maxtrail];
- verts = new(__FILE__,__LINE__) VertexSet(maxtrail*2);
- verts->Clear();
- verts->nverts = 0;
-
- for (int i = 0; i < maxtrail*2; i++) {
- verts->diffuse[i] = D3DCOLOR_RGBA(255,255,255,255);
- verts->specular[i] = D3DCOLOR_RGBA( 0, 0, 0,255);
- verts->tu[i] = 0.0f;
- verts->tv[i] = (i & 1) ? 1.0f : 0.0f;
- }
-
- polys = new(__FILE__,__LINE__) Poly[maxtrail];
-
- for (int i = 0; i < maxtrail; i++) {
- polys[i].vertex_set = verts;
- polys[i].nverts = 4;
- polys[i].material = &mtl;
- }
-
- npolys = 0;
- nverts = 0;
- width = 6;
- length = 0;
- dim = 3;
-
- last_point_time = 0;
-}
-
-Trail::~Trail()
-{
- delete [] trail;
- delete [] polys;
- delete verts;
-}
-
-void
-Trail::UpdateVerts(const Point& cam_pos)
-{
- if (ntrail < 2) return;
-
- int bright = 255 - dim*ntrail;
-
- Point head = trail[1] + loc;
- Point tail = trail[0] + loc;
- Point vcam = cam_pos - head;
- Point vtmp = vcam.cross(head-tail);
- vtmp.Normalize();
- Point vlat = vtmp * (width + (0.1 * width * ntrail));
-
- verts->loc[0] = tail - vlat;
- verts->loc[1] = tail + vlat;
- verts->diffuse[0] = 0;
- verts->diffuse[1] = 0;
-
- for (int i = 0; i < ntrail-1; i++) {
- bright+=dim;
- Point head = trail[i+1] + loc;
- Point tail = trail[i] + loc;
- Point vcam = cam_pos - head;
- Point vtmp = vcam.cross(head-tail);
- vtmp.Normalize();
-
- float trail_width = (float) (width + (ntrail-i) * width * 0.1);
- if (i == ntrail-2) trail_width = (float) (width * 0.7);
- Point vlat = vtmp * trail_width;
-
- verts->loc[2*i+2] = head - vlat;
- verts->loc[2*i+3] = head + vlat;
-
- if (bright <= 0) {
- verts->diffuse[2*i+2] = 0;
- verts->diffuse[2*i+3] = 0;
- }
- else {
- verts->diffuse[2*i+2] = D3DCOLOR_RGBA(bright,bright,bright,bright);
- verts->diffuse[2*i+3] = D3DCOLOR_RGBA(bright,bright,bright,bright);
- }
- }
-}
-
-void
-Trail::Render(Video* video, DWORD flags)
-{
- if (!npolys) return;
-
- if ((flags & RENDER_ADDITIVE) == 0)
- return;
-
- if (video && life) {
- const Camera* cam = video->GetCamera();
-
- if (cam)
- UpdateVerts(cam->Pos());
-
- video->DrawPolys(npolys, polys);
- }
-}
-
-void
-Trail::AddPoint(const Point& v)
-{
- if (ntrail >= maxtrail-1) return;
-
- double real_time = Game::RealTime() / 1000.0;
-
- if (ntrail == 0) {
- radius = 1000;
- }
- else {
- radius = (float) Point(v-loc).length();
- }
-
- // just adjust the last point:
- if (ntrail > 1 && real_time - last_point_time < 0.05) {
- trail[ntrail-1] = v;
- }
-
- // add a new point:
- else {
- last_point_time = real_time;
- trail[ntrail++] = v;
-
- nverts += 2;
- verts->nverts = nverts;
-
- if (ntrail > 1) {
- length0 = length1;
- length1 = length0 + (trail[ntrail-1]-trail[ntrail-2]).length();
-
- polys[npolys].vertex_set = verts;
- polys[npolys].nverts = 4;
-
- polys[npolys].verts[0] = nverts-4;
- polys[npolys].verts[1] = nverts-2;
- polys[npolys].verts[2] = nverts-1;
- polys[npolys].verts[3] = nverts-3;
-
- float tu1 = (float) length1 / 250.0f;
-
- verts->tu[2*ntrail-1] = tu1;
- verts->tu[2*ntrail-2] = tu1;
-
- npolys++;
- }
- }
-}
-
-double
-Trail::AverageLength()
-{
- double avg = 0;
-
- for (int i = 0; i < ntrail-1; i++)
- avg += (trail[i+1]-trail[i]).length();
- avg /= ntrail;
-
- return avg;
-}
-
+/* Starshatter OpenSource Distribution + Copyright (c) 1997-2004, Destroyer Studios LLC. + All Rights Reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name "Destroyer Studios" nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + SUBSYSTEM: Stars.exe + FILE: Trail.cpp + AUTHOR: John DiCamillo + + + OVERVIEW + ======== + Missile Trail representation class +*/ + +#include "MemDebug.h" +#include "Trail.h" +#include "Weapon.h" +#include "Sim.h" + +#include "Game.h" +#include "Light.h" +#include "Bitmap.h" +#include "DataLoader.h" +#include "Sound.h" + +// +--------------------------------------------------------------------+ + +Trail::Trail(Bitmap* tex, int n) +: ntrail(0), length0(0), length1(0), texture(tex), maxtrail(n), dim(1) +{ + trans = true; + luminous = true; + + mtl.Kd = Color::White; + mtl.tex_diffuse = texture; + mtl.tex_emissive = texture; + mtl.blend = Video::BLEND_ADDITIVE; + mtl.luminous = true; + + if (maxtrail < 4) maxtrail = 512; + trail = new(__FILE__,__LINE__) Point[maxtrail]; + verts = new(__FILE__,__LINE__) VertexSet(maxtrail*2); + verts->Clear(); + verts->nverts = 0; + + for (int i = 0; i < maxtrail*2; i++) { + verts->diffuse[i] = D3DCOLOR_RGBA(255,255,255,255); + verts->specular[i] = D3DCOLOR_RGBA( 0, 0, 0,255); + verts->tu[i] = 0.0f; + verts->tv[i] = (i & 1) ? 1.0f : 0.0f; + } + + polys = new(__FILE__,__LINE__) Poly[maxtrail]; + + for (int i = 0; i < maxtrail; i++) { + polys[i].vertex_set = verts; + polys[i].nverts = 4; + polys[i].material = &mtl; + } + + npolys = 0; + nverts = 0; + width = 6; + length = 0; + dim = 3; + + last_point_time = 0; +} + +Trail::~Trail() +{ + delete [] trail; + delete [] polys; + delete verts; +} + +void +Trail::UpdateVerts(const Point& cam_pos) +{ + if (ntrail < 2) return; + + int bright = 255 - dim*ntrail; + + Point head = trail[1] + loc; + Point tail = trail[0] + loc; + Point vcam = cam_pos - head; + Point vtmp = vcam.cross(head-tail); + vtmp.Normalize(); + Point vlat = vtmp * (width + (0.1 * width * ntrail)); + + verts->loc[0] = tail - vlat; + verts->loc[1] = tail + vlat; + verts->diffuse[0] = 0; + verts->diffuse[1] = 0; + + for (int i = 0; i < ntrail-1; i++) { + bright+=dim; + Point head = trail[i+1] + loc; + Point tail = trail[i] + loc; + Point vcam = cam_pos - head; + Point vtmp = vcam.cross(head-tail); + vtmp.Normalize(); + + float trail_width = (float) (width + (ntrail-i) * width * 0.1); + if (i == ntrail-2) trail_width = (float) (width * 0.7); + Point vlat = vtmp * trail_width; + + verts->loc[2*i+2] = head - vlat; + verts->loc[2*i+3] = head + vlat; + + if (bright <= 0) { + verts->diffuse[2*i+2] = 0; + verts->diffuse[2*i+3] = 0; + } + else { + verts->diffuse[2*i+2] = D3DCOLOR_RGBA(bright,bright,bright,bright); + verts->diffuse[2*i+3] = D3DCOLOR_RGBA(bright,bright,bright,bright); + } + } +} + +void +Trail::Render(Video* video, DWORD flags) +{ + if (!npolys) return; + + if ((flags & RENDER_ADDITIVE) == 0) + return; + + if (video && life) { + const Camera* cam = video->GetCamera(); + + if (cam) + UpdateVerts(cam->Pos()); + + video->DrawPolys(npolys, polys); + } +} + +void +Trail::AddPoint(const Point& v) +{ + if (ntrail >= maxtrail-1) return; + + double real_time = Game::RealTime() / 1000.0; + + if (ntrail == 0) { + radius = 1000; + } + else { + radius = (float) Point(v-loc).length(); + } + + // just adjust the last point: + if (ntrail > 1 && real_time - last_point_time < 0.05) { + trail[ntrail-1] = v; + } + + // add a new point: + else { + last_point_time = real_time; + trail[ntrail++] = v; + + nverts += 2; + verts->nverts = nverts; + + if (ntrail > 1) { + length0 = length1; + length1 = length0 + (trail[ntrail-1]-trail[ntrail-2]).length(); + + polys[npolys].vertex_set = verts; + polys[npolys].nverts = 4; + + polys[npolys].verts[0] = nverts-4; + polys[npolys].verts[1] = nverts-2; + polys[npolys].verts[2] = nverts-1; + polys[npolys].verts[3] = nverts-3; + + float tu1 = (float) length1 / 250.0f; + + verts->tu[2*ntrail-1] = tu1; + verts->tu[2*ntrail-2] = tu1; + + npolys++; + } + } +} + +double +Trail::AverageLength() +{ + double avg = 0; + + for (int i = 0; i < ntrail-1; i++) + avg += (trail[i+1]-trail[i]).length(); + avg /= ntrail; + + return avg; +} + |