summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2DayLightFilter.cpp
blob: 434dfd1c938c6891d06752041541c7567cfb8bd7 (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
#include "StdAfx.h"
#include "T2DayLightFilter.h"
#include <MINMAX.H>

T2DayLightFilter::T2DayLightFilter() {
	mBaseCol.rgbRed = 0;
	mBaseCol.rgbGreen = 0;
	mBaseCol.rgbBlue = 0;
	mTime = 0;
}

T2DayLightFilter::~T2DayLightFilter() {
}

void T2DayLightFilter::SetTime(int t1, int t2) {
#pragma var_order(base, low, high, time, midpoint)
	double time;
	double base = (sin(2.0 * (3.141592 * (t1 / 1000.0))) * 8.0) + 500.0;
	double midpoint = 1000.0 - base;
	double low = midpoint / 2.0;
	double high = 1000.0 - (midpoint / 2.0);

	if (t2 < low) {
		time = -90.0 * cos((3.141592 * (t2 / (midpoint / 2.0))) / 2.0);
	} else if (t2 < high) {
		time = 90.0 * sin(3.141592 * ((t2 - low) / base));
	} else {
		time = -90.0 * sin((3.141592 * ((t2 - high) / (midpoint / 2.0))) / 2.0);
	}

	if (time < 0.0) {
		mTime = -int(-time + 0.5);
	} else {
		mTime = int(time + 0.5);
	}
	if (mTime > 90)
		mTime = 90;
	if (mTime < -90)
		mTime = -90;

	while (mTime < 0)
		mTime += 360;
	while (mTime >= 360)
		mTime -= 360;

	CalcDayLightColor(&mBaseCol);
}

void T2DayLightFilter::RefractionColor(RGBQUAD* rgb) {
	rgb->rgbRed = (mBaseCol.rgbRed * rgb->rgbRed) / 255;
	rgb->rgbGreen = (mBaseCol.rgbGreen * rgb->rgbGreen) / 255;
	rgb->rgbBlue = (mBaseCol.rgbBlue * rgb->rgbBlue) / 255;
}

void T2DayLightFilter::CalcDayLightColor(RGBQUAD* rgb) {
#pragma var_order(outB, outR, base, tint1, tint2, outG)
	double base = (0.5 * sin((3.141592 * mTime) / 180.0)) + 0.5;
	if (base < 0.0)
		base = 0.0;

	double outR = base;
	double outG = base;
	double outB = base;

	double tint1 = 1.1 * sin((3.141592 * mTime) / 180.0);
	if (tint1 > 1.0)
		tint1 = 1.0;
	else if (tint1 < 0.1)
		tint1 = 0.1;

	outG *= 1.0 - (0.8 * (1.0 - tint1));
	outB *= tint1;

	outR += 0.4;
	outG += 0.4;
	outB += 0.4;

	double tint2 = 0.2 + (outR * 0.35) + (outG * 0.45) + (outB * 0.2);
	if (tint2 > 1.0)
		tint2 = 1.0;

	outR *= tint2;
	outG *= 1.0 - (0.7 * (1.0 - tint2));

	outR = min(max(outR, 0.0), 1.0);
	outG = min(max(outG, 0.0), 1.0);
	outB = min(max(outB, 0.0), 1.0);

	rgb->rgbRed = 255.0 * outR;
	rgb->rgbGreen = 255.0 * outG;
	rgb->rgbBlue = 255.0 * outB;
}

void T2DayLightFilter::SkyColor(RGBQUAD* rgb) {
#pragma var_order(outB, outR, inG, outG, inB, inR)
	double inR = mBaseCol.rgbRed / 255.0;
	double inG = mBaseCol.rgbGreen / 255.0;
	double inB = mBaseCol.rgbBlue / 255.0;

	double outR = inR;
	double outG = inG;
	double outB = inB;

	outR *= (rgb->rgbRed / 255.0);
	outG *= (rgb->rgbGreen / 255.0);
	outB *= (rgb->rgbBlue / 255.0);

	outR = (0.45 * outR) + (0.55 * inR);
	outG = (0.6 * outG) + (0.4 * inG);
	outB = (0.6 * outB) + (0.4 * inB);

	rgb->rgbRed = 255.0 * outR;
	rgb->rgbGreen = 255.0 * outG;
	rgb->rgbBlue = 255.0 * outB;
}