forked from bridgecommand/bc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavLight.cpp
More file actions
158 lines (132 loc) · 6.48 KB
/
NavLight.cpp
File metadata and controls
158 lines (132 loc) · 6.48 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Bridge Command 5.0 Ship Simulator
Copyright (C) 2014 James Packer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE. See the
GNU General Public License For more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "NavLight.hpp"
#include "Angles.hpp"
#include <iostream>
#include <cmath> //For fmod()
#include <cstdlib> //For rand()
using namespace irr;
NavLight::NavLight(irr::scene::ISceneNode* parent, irr::scene::ISceneManager* smgr, irr::core::dimension2d<f32> lightSize, irr::core::vector3df position, irr::video::SColor colour, irr::f32 lightStartAngle, irr::f32 lightEndAngle, irr::f32 lightRange, std::string lightSequence) {
lightNode = smgr->addBillboardSceneNode(parent, lightSize, position);
lightNode->setColor(colour);
lightNode->setMaterialTexture(0, smgr->getVideoDriver()->getTexture("media/particlewhite.png"));
lightNode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
lightNode->setMaterialFlag(video::EMF_LIGHTING, false);
//Fix angles if start is negative
while (lightStartAngle < 0) {
lightStartAngle +=360;
lightEndAngle +=360;
}
//store extra information
startAngle = lightStartAngle;
endAngle = lightEndAngle;
range = lightRange;
//initialise light sequence information
sequence = lightSequence;
timeOffset=60.0*((irr::f32)std::rand()/RAND_MAX); //Random, 0-60s
//set initial alpha to implausible value
currentAlpha = -1;
}
NavLight::~NavLight() {
}
void NavLight::update(irr::f32 scenarioTime, irr::core::vector3df viewPosition, irr::u32 lightLevel) {
//find light position
lightNode->updateAbsolutePosition(); //ToDo: This is needed, but seems odd that it's required
core::vector3df lightPosition = lightNode->getAbsolutePosition();
//std::cout << "Light pos: " << lightPosition.X << "," << lightPosition.Y << "," << lightPosition.Z << std::endl;
//std::cout << "View pos: " << viewPosition.X << "," << viewPosition.Z << std::endl;
//scale so lights appear same size independent of range
f32 lightDistance=lightPosition.getDistanceFrom(viewPosition);
lightNode->setSize(core::dimension2df(lightDistance*0.01,lightDistance*0.01));
//set light visibility depending on range
if (lightDistance > range) {
lightNode->setVisible(false);
} else {
lightNode->setVisible(true);
}
//set light visibility depending on angle: Set to false if not visible
f32 relativeAngleDeg = (viewPosition-lightPosition).getHorizontalAngle().Y; //Degrees: Angle from the light to viewpoint.
f32 parentAngleDeg = lightNode->getParent()->getRotation().Y;
f32 localRelativeAngleDeg = relativeAngleDeg-parentAngleDeg; //Angle from light to viewpoint, relative to light's parent coordinate system.
//std::cout << relativeAngleDeg << " " << parentAngleDeg << " " << localRelativeAngleDeg <<std::endl;
if (!Angles::isAngleBetween(localRelativeAngleDeg,startAngle,endAngle)) {
lightNode->setVisible(false);
}
//set light visibility depending on light sequence
//find length of sequence
std::string::size_type sequenceLength = sequence.length();
if (sequenceLength > 0) {
f32 charTime = 0.25; //where each character represents 0.25s of time
f32 timeInSequence = std::fmod(((scenarioTime+timeOffset) / charTime),sequenceLength);
u32 positionInSequence = timeInSequence;
if (positionInSequence>=sequenceLength) {positionInSequence = sequenceLength-1;} //Should not be required, but double check we're not off the end of the sequence
if (sequence[positionInSequence] == 'D') {
lightNode->setVisible(false);
}
}
//set transparency dependent on light level, only changing if required, as this is a slow operation
//std::cout << lightLevel << std::endl;
u16 requiredAlpha = 255-lightLevel;
if (requiredAlpha != currentAlpha) {
setAlpha((u8)requiredAlpha, lightNode->getMaterial(0).getTexture(0));
currentAlpha = requiredAlpha;
}
}
bool NavLight::setAlpha(irr::u8 alpha, irr::video::ITexture* tex)
//Modified from http://irrlicht.sourceforge.net/forum/viewtopic.php?t=31400
//FIXME: Check how the texture color format is set
{
if(!tex)
{
return false;
};
u32 size = tex->getSize().Width*tex->getSize().Height; // get Texture Size
switch(tex->getColorFormat()) //getTexture Format, (nly 2 support alpha)
{
case video::ECF_A1R5G5B5: //see video::ECOLOR_FORMAT for more information on the texture formats.
{
// printf("16BIT\n");
u16* Data = (u16*)tex->lock(); //get Data for 16-bit Texture
for(u32 i = 0; i < size ; i++)
{
u8 alphaToUse = (u8)video::getAlpha(Data[i])==0 ? 0 : alpha; //If already transparent, leave as-is
Data[i] = video::RGBA16(video::getRed(Data[i]), video::getGreen(Data[i]), video::getBlue(Data[i]), alphaToUse);
}
tex->unlock();
break;
};
case video::ECF_A8R8G8B8:
{
u32* Data = (u32*)tex->lock();
for( u32 i = 0 ; i < size ; i++)
{
//u8 minAlpha = std::min(((u8*)&Data[i])[3],alpha);
u8 alphaToUse = ((u8*)&Data[i])[3] == 0 ? 0 : alpha; //If already transparent, leave as-is
((u8*)&Data[i])[3] = alphaToUse;//get Data for 32-bit Texture
}
tex->unlock();
break;
};
default:
return false;
};
return true;
}
void NavLight::moveNode(irr::f32 deltaX, irr::f32 deltaY, irr::f32 deltaZ)
{
core::vector3df currentPos = lightNode->getPosition();
irr::f32 newPosX = currentPos.X + deltaX;
irr::f32 newPosY = currentPos.Y + deltaY;
irr::f32 newPosZ = currentPos.Z + deltaZ;
lightNode->setPosition(core::vector3df(newPosX,newPosY,newPosZ));
}