-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCLensEffect.cpp
166 lines (120 loc) · 4.12 KB
/
CLensEffect.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
#include "CLensEffect.h"
CLensEffect::CLensEffect()
{
}
CLensEffect::CLensEffect(float radius,float focalDistance,float objectDistance)
{
this->radius = radius;
this->focal_distance = focalDistance;
this->object_distance = objectDistance;
}
void CLensEffect::Init(SDL_Surface *bufferIn)
{
this->bufferIn = bufferIn;
this->LensPrecal();
}
void CLensEffect::Update(long deltaTime, SDL_Surface *bufferOut)
{
this->drawLensPrecal(bufferOut);
}
void CLensEffect::LensPrecal()
{
unsigned int px;
unsigned int py;
this->pxPrecal = new int[this->radius * 2];
this->pyPrecal = new int[this->radius * 2];
int *pxPrecal = this->pxPrecal;
int *pyPrecal = this->pyPrecal;
memset(pxPrecal, 0, sizeof(int) *this->radius * 2);
memset(pyPrecal, 0, sizeof(int) *this->radius * 2);
unsigned int diameter = this->radius * 2;
unsigned int radiusPow2 = this->radius*this->radius;
for (int y = 0; y < diameter; y++)
for (int x = 0; x < diameter; x++)
{
int y_temp = (y - this->radius);
int x_temp = (x - this->radius);
if ((x_temp*x_temp + y_temp * y_temp) < (radiusPow2))
{
int py = tan(asin(y_temp / this->focal_distance))*this->object_distance;
int px = tan(asin(x_temp / this->focal_distance))*this->object_distance;
pyPrecal[y] = py;
pxPrecal[x] = px;
}
}
}
void CLensEffect::drawLensPrecal( SDL_Surface *bufferOut)
{
int px;
int py;
int *pxPrecal =this->pxPrecal;
int *pyPrecal =this->pyPrecal;
if (bufferIn == NULL || bufferOut == NULL)
return;
unsigned int *ptrOrig = (unsigned int*)this->bufferIn->pixels;
unsigned int *ptrDest = (unsigned int*)bufferOut->pixels;
unsigned int H = this->bufferIn->h;
unsigned int W = this->bufferIn->w;
unsigned int maxSize = this->bufferIn->h*this->bufferIn->w;
unsigned int diameter = this->radius * 2;
unsigned int radiusPow2 = this->radius*this->radius;
for (int y = 0; y < diameter; y++)
for (int x = 0; x < diameter; x++)
{
int y_temp = (y - this->radius);
int x_temp = (x - this->radius);
if ((x_temp*x_temp + y_temp * y_temp) < (radiusPow2))
{
//get px and py pixel generate of the refraction of the glass
py = pyPrecal[y];//tan(asin(y_temp / lens->focal_distance))*lens->object_distance;
px = pxPrecal[x];//tan(asin(x_temp / lens->focal_distance))*lens->object_distance;
int origPixel = px + this->radius + this->x + W * (-py + this->radius + this->y);
int destPixel = x_temp + this->radius + this->x + W * (-y_temp + this->radius + this->y);
//this is a validation for prevent memory range out
if (origPixel > maxSize || destPixel > maxSize)
continue;
//the refracted pixel copy into the dest buffer
ptrDest[destPixel] = ptrOrig[origPixel];
}
}
}
void CLensEffect::drawLens(SDL_Surface *bufferOut)
{
int px;
int py;
int *pxPrecal = this->pxPrecal;
int *pyPrecal = this->pyPrecal;
if (bufferIn == NULL || bufferOut == NULL)
return;
unsigned int *ptrOrig = (unsigned int*)this->bufferIn->pixels;
unsigned int *ptrDest = (unsigned int*)bufferOut->pixels;
unsigned int H = this->bufferIn->h;
unsigned int W = this->bufferIn->w;
unsigned int maxSize = this->bufferIn->h*this->bufferIn->w;
unsigned int diameter = this->radius * 2;
unsigned int radiusPow2 = this->radius*this->radius;
for (int y = 0; y < diameter; y++)
for (int x = 0; x < diameter; x++)
{
int y_temp = (y - this->radius);
int x_temp = (x - this->radius);
if ((x_temp*x_temp + y_temp * y_temp) < (radiusPow2))
{
//get px and py pixel generate of the refraction of the glass
py = tan(asin(y_temp / this->focal_distance))*this->object_distance;
px = tan(asin(x_temp / this->focal_distance))*this->object_distance;
int origPixel = px + this->radius + this->x + W * (-py + this->radius + this->y);
int destPixel = x_temp + this->radius + this->x + W * (-y_temp + this->radius + this->y);
//this is a validation for prevent memory range out
if (origPixel > maxSize || destPixel > maxSize)
continue;
//the refracted pixel copy into the dest buffer
ptrDest[destPixel] = ptrOrig[origPixel];
}
}
}
CLensEffect::~CLensEffect()
{
delete []this->pxPrecal;
delete []this->pyPrecal;
}