Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 05266e8bea145a43b033e8025fa438d460c9e945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
   render/dust.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "render/dust.h"
#include "render/gl.h"
#include "core/cvar.h"
#include "core/core.h"
#include "math/functions.h"
#include "sys/sys.h"

namespace render
{

core::Cvar *r_dust;
core::Cvar *r_dustsize;

const float LOWSPEEDLIMIT = 5.0f;
const float TRAILLENGHT = 0.25f;
const float DUSTMAXALPHA = 0.8f;
const float DUSTDISTANCE = 8.0f;

float	*dust;
size_t dustsize;

void Dust::init()
{
	r_dust = core::Cvar::get("r_dust", "1", core::Cvar::Archive);
	r_dust->set_info("[bool] render dust");

	r_dustsize = core::Cvar::get("r_dustsize", "128", core::Cvar::Archive);
	r_dustsize->set_info("[int] number of dust particles");

	dust = 0;
	dustsize = (size_t) r_dustsize->value();
}

void Dust::shutdown()
{
	if (dust) {
		delete dust;
		dust = 0;
		dustsize = 0;
	}
}

void Dust::reset()
{
	delete dust;
	dust = 0;
}

void Dust::draw()
{
	if (!r_dust->value()) {
		if (dust) {
			delete dust;
			dust = 0;
		}
		return;
	}
	
	if (!core::localcontrol()) {
		return;
	}

	if ((size_t) r_dustsize->value() != dustsize) {
		con_debug << "  changing dust size..." << std::endl;
		if (dust) {
			delete dust;
			dust = 0;
		}
	}

	dustsize = (size_t) r_dustsize->value();

	if (!dustsize)	{
		if (dust) {
			delete dust;
			dust = 0;
		}
		return;
	}

	if (!dust) {
		con_debug << "  generating dust..." << std::endl;
		dust = new float[dustsize*3];

		for (size_t i = 0; i < dustsize; i++) {
			dust[i*3] = core::localcontrol()->location().x +  (math::randomf(2) - 1) * (DUSTDISTANCE + core::localcontrol()->radius());
			dust[i*3+1] = core::localcontrol()->location().y + (math::randomf(2) - 1) * (DUSTDISTANCE + core::localcontrol()->radius());
			dust[i*3+2] = core::localcontrol()->location().z + (math::randomf(2) - 1) * (DUSTDISTANCE + core::localcontrol()->radius());
		}
	}

	if (!core::localcontrol()->speed()) 
		return;

	math::Color color;
	if (core::localcontrol()->speed() < LOWSPEEDLIMIT) {
		color.a = core::localcontrol()->speed() / LOWSPEEDLIMIT;
	}
	color.a	*= DUSTMAXALPHA;

	gl::color(color);
	gl::begin(gl::Lines);
	math::Vector3f v;

	for (size_t i = 0; i < dustsize; i++) {
		v.assign(core::localcontrol()->location());
		for (size_t j = 0; j < 3; j++)
			v[j] -=  dust[i*3+j];

		if (v.lengthsquared() > (core::localcontrol()->radius() + DUSTDISTANCE)*(core::localcontrol()->radius() + DUSTDISTANCE)) {

			dust[i*3] = core::localcontrol()->location().x +  (math::randomf(2) - 1) * (DUSTDISTANCE + core::localcontrol()->radius());
			dust[i*3+1] = core::localcontrol()->location().y + (math::randomf(2) - 1) * (DUSTDISTANCE + core::localcontrol()->radius());
			dust[i*3+2] = core::localcontrol()->location().z + (math::randomf(2) - 1) * (DUSTDISTANCE + core::localcontrol()->radius());
		}

		glVertex3fv(dust+3*i);

		for (size_t j = 0; j < 3; j++)
			v[j] = dust[i*3+j];
		v -= core::localcontrol()->state()->axis().forward() * color.a * TRAILLENGHT;
		gl::vertex(v);
	}

	gl::end();
}

}