git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / tankgraph / RenderTracer.cpp
blob7d9de12a4e697ef6d6df2b750df69be7b069f2a7
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <tankgraph/RenderTracer.h>
22 #include <tank/TankContainer.h>
23 #include <GLEXT/GLTexture.h>
24 #include <image/ImageBitmap.h>
25 #include <client/ScorchedClient.h>
27 RenderTracer *RenderTracer::instance_ = 0;
29 RenderTracer *RenderTracer::instance()
31 if (!instance_) instance_ = new RenderTracer;
32 return instance_;
35 RenderTracer::RenderTracer() :
36 GameStateI("RenderTracer"),
37 current_(0), listNo_(0)
39 obj_ = gluNewQuadric();
42 RenderTracer::~RenderTracer()
44 gluDeleteQuadric(obj_);
47 void RenderTracer::clearTracers()
49 if (current_)
51 current_->lines.clear();
52 current_->points.clear();
56 void RenderTracer::clearTracerLines()
58 if (current_)
60 current_->lines.clear();
64 void RenderTracer::draw(const unsigned state)
66 Tank *current = ScorchedClient::instance()->getTankContainer().getCurrentTank();
67 if (!current) return;
69 if (!current_ ||
70 current_->tank != current->getPlayerId())
72 std::map<unsigned int, TraceEntry>::iterator itor =
73 traceEntries_.find(current->getPlayerId());
74 if (itor == traceEntries_.end())
76 TraceEntry entry(current->getPlayerId());
77 traceEntries_[current->getPlayerId()] = entry;
78 current_ = &traceEntries_[current->getPlayerId()];
80 else
82 current_ = &(*itor).second;
85 if (current_->points.empty()) return;
87 glColor3fv(current->getColor());
89 std::list<Vector>::iterator itor = current_->points.begin();
90 std::list<Vector>::iterator itorend = current_->points.end();
91 for (;itor != itorend; itor++)
93 drawTracerEnd(*itor);
96 std::list<std::list<TracerLinePoint> >::iterator itor2 = current_->lines.begin();
97 std::list<std::list<TracerLinePoint> >::iterator itorend2 = current_->lines.end();
98 for (;itor2 != itorend2; itor2++)
100 drawSmokeTracer(*itor2);
104 void RenderTracer::drawTracerEnd(Vector &position)
106 GLState currentState(GLState::TEXTURE_OFF | GLState::BLEND_OFF);
108 glPushMatrix();
109 glTranslatef(position[0], position[1], position[2]);
110 if (!listNo_)
112 glNewList(listNo_ = glGenLists(1), GL_COMPILE_AND_EXECUTE);
113 gluSphere(obj_, 0.5f, 4, 2);
114 glEndList();
116 else
118 glCallList(listNo_);
120 glPopMatrix();
123 void RenderTracer::drawSmokeTracer(std::list<TracerLinePoint> &positions)
125 GLState currentState(GLState::TEXTURE_OFF | GLState::BLEND_OFF);
126 /*glEnable(GL_ALPHA_TEST);
128 static GLTexture arrowTexture;
129 if (!arrowTexture.textureValid())
131 std::string file1 = S3D::getDataFile("data/windows/arrow.bmp");
132 std::string file2 = S3D::getDataFile("data/windows/arrowi.bmp");
133 ImageBitmap bitmap(file1.c_str(), file2.c_str(), true);
134 arrowTexture.create(bitmap);
136 arrowTexture.draw();*/
138 // Draw twice (for front and back facing)
139 for (int i=0; i<2; i++)
141 glBegin(GL_QUAD_STRIP);
142 Vector lastPos;
143 float totalDist = 0.0f;
144 std::list<TracerLinePoint>::iterator itor = positions.begin();
145 std::list<TracerLinePoint>::iterator itorend = positions.end();
146 for (;itor != itorend; itor++)
148 Vector &currentPos = (*itor).position;
149 Vector &cross = (*itor).cross;
150 if (itor == positions.begin()) lastPos = currentPos;
152 float dist = (lastPos - currentPos).Magnitude();
153 if (dist < 100.0f)
155 if (i==0)
157 glTexCoord2f(0.0f, totalDist);
158 glVertex3fv(currentPos - cross / 2.0f);
159 glTexCoord2f(1.0f, totalDist);
160 glVertex3fv(currentPos + cross / 2.0f);
162 else
164 glTexCoord2f(0.0f, totalDist);
165 glVertex3fv(currentPos + cross / 2.0f);
166 glTexCoord2f(1.0f, totalDist);
167 glVertex3fv(currentPos - cross / 2.0f);
170 else
172 glEnd();
173 glBegin(GL_QUAD_STRIP);
176 totalDist += dist / 10.0f;
177 lastPos = currentPos;
179 glEnd();
181 //glDisable(GL_ALPHA_TEST);
184 void RenderTracer::newGame()
186 traceEntries_.clear();
187 current_ = 0;
190 void RenderTracer::addTracer(unsigned int tank, Vector &position)
192 std::map<unsigned int, TraceEntry>::iterator itor =
193 traceEntries_.find(tank);
194 if (itor == traceEntries_.end())
196 TraceEntry entry(tank);
197 entry.points.push_back(position);
198 traceEntries_[tank] = entry;
200 else
202 (*itor).second.points.push_back(position);
206 void RenderTracer::addSmokeTracer(unsigned int tank,
207 Vector &position, std::list<TracerLinePoint> &positions)
209 std::map<unsigned int, TraceEntry>::iterator itor =
210 traceEntries_.find(tank);
211 if (itor == traceEntries_.end())
213 TraceEntry entry(tank);
214 entry.lines.push_back(positions);
215 entry.points.push_back(position);
216 traceEntries_[tank] = entry;
218 else
220 (*itor).second.lines.push_back(positions);
221 (*itor).second.points.push_back(position);