-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2lookup.cpp
202 lines (145 loc) · 3.74 KB
/
m2lookup.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Filename: m2lookup.cpp
//
// Developed by Oliver Belaifa in 1997
//
//#include<copro.h>
#include <math.h>
#include<demo.h>
extern "C"
{
unsigned char pShadeLookup[65536];
unsigned char pBumpShadeLookup[65536];
unsigned char pAngleJoinTab[65536];
unsigned int pLightBrightnessTab[256];
}
const int iNumSplitAngles = 256;
void CalcShadeLookup()
{
int iBrightness;
int iCol;
const int iHighlite = 256;
for (iBrightness = 0; iBrightness < 256; iBrightness++)
for (iCol = 0; iCol < 256; iCol++)
{
float fA = (iBrightness*iCol)/255.0;
if (iBrightness > iHighlite)
{
fA += 0.3*255.0/(255.0 - iHighlite)*float(iBrightness - iHighlite);
if (fA > 255.0) fA = 255.0;
}
unsigned char cA = (unsigned char)fA;
pShadeLookup[256*iBrightness + iCol] = cA;
}
}
void CalcBumpShadeLookup()
{
int iBrightness;
int iCol;
const int iHighlite = 160;
for (iBrightness = 0; iBrightness < 256; iBrightness++)
for (iCol = 0; iCol < 256; iCol++)
{
// float fBrightness = (float)iBrightness;
int iBrightness2 = iBrightness;
if (iBrightness2 < 100)
iBrightness2 = 100;
float fA = (iBrightness2*iCol)/255.0;
if (iBrightness2 > iHighlite)
{
fA += 0.3*255.0/(255.0 - iHighlite)*float(iBrightness2 - iHighlite);
if (fA > 255.0) fA = 255.0;
}
unsigned char cA = (unsigned char)fA;
pBumpShadeLookup[256*iBrightness + iCol] = cA;
}
}
void CalcAngleJoinTab()
{
int iY;
int iX;
for (iY = 0; iY < 256; iY++)
for (iX = 0; iX < 256; iX++)
{
int iAngleU = iX - 128;
int iAngleV = iY - 128;
float fAngleU = (float)iAngleU;
float fAngleV = (float)iAngleV;
float fAbs = 300.0 - 2.5*sqrtf(fAngleU*fAngleU + fAngleV*fAngleV);
if (fAbs > 255.0)
fAbs = 255.0;
if (fAbs < 0)
fAbs = 0;
unsigned char cAbs = (unsigned char)fAbs;
pAngleJoinTab[iY*256 + iX] = cAbs;
}
}
void CalcLightBrightnessTab(int iRGB, float fBFactor)
// BFactor Should be 0 <= fBFactor <= 1.0(Original)
{
int iR = (iRGB&0xff0000)>>16;
int iG = (iRGB&0x00ff00)>>8;
int iB = (iRGB&0x0000ff);
float fR = (float)iR;
float fG = (float)iG;
float fB = (float)iB;
float fDelta = fBFactor/255.0;
float fCurFactor = 0;
int iI;
for (iI = 0; iI < 256; iI++)
{
float fFactor;
if (fCurFactor < 0.1)
fFactor = 0.1;
else
fFactor = fCurFactor;
float fCurR = fFactor*fR;
float fCurG = fFactor*fG;
float fCurB = fFactor*fB;
int iCurR = (int)fCurR;
int iCurG = (int)fCurG;
int iCurB = (int)fCurB;
if (iCurR > 255) iCurR = 255;
if (iCurG > 255) iCurG = 255;
if (iCurB > 255) iCurB = 255;
int iCurRGB = iCurR*65536 + iCurG*256 + iCurB;
pLightBrightnessTab[iI] = iCurRGB;
fCurFactor += fDelta;
}
}
void FromMeseCalcLightBrightnessTab(float *fRGB, float fBFactor)
// BFactor Should be 0 <= fBFactor <= 1.0(Original)
{
float fR = fRGB[0]*256.0;
float fG = fRGB[1]*256.0;
float fB = fRGB[2]*256.0;
float fDelta = fBFactor/255.0;
float fCurFactor = 0;
int iI;
for (iI = 0; iI < 256; iI++)
{
float fFactor;
if (fCurFactor < 0.1)
fFactor = 0.1;
else
fFactor = fCurFactor;
float fCurR = fFactor*fR;
float fCurG = fFactor*fG;
float fCurB = fFactor*fB;
int iCurR = (int)fCurR;
int iCurG = (int)fCurG;
int iCurB = (int)fCurB;
if (iCurR > 255) iCurR = 255;
if (iCurG > 255) iCurG = 255;
if (iCurB > 255) iCurB = 255;
int iCurRGB = iCurR*65536 + iCurG*256 + iCurB;
pLightBrightnessTab[iI] = iCurRGB;
fCurFactor += fDelta;
}
}
void CalcLookUpTables()
{
CalcShadeLookup();
CalcBumpShadeLookup();
CalcAngleJoinTab();
CalcLightBrightnessTab(0xffffff, 1.0);
}