-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDrawClouds.cpp
172 lines (121 loc) · 4.64 KB
/
DrawClouds.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
// CGFractal.cpp
//
// CGDraw Class
// Copyright (c) 2015 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
void CGFractal::CreateSphericalCloudAnimation (int cxWidth, int cyHeight, const SGCloudDesc &Desc, int iFrames, bool bHemisphere, TArray<CG8bitImage> *retFrames)
// CreateSphericalCloudAnimation
//
// Creates a set of spherical maps
{
int i;
ASSERT(iFrames >= 0);
retFrames->DeleteAll();
if (iFrames <= 0)
return;
// Create our own cloud descriptor
SGCloudDesc CloudDesc(Desc);
// Always create our own generator
int iRange = 2;
CGCloudGenerator3D Generator(CloudDesc.iScale, iRange);
Metric rFactor = (Metric)(CloudDesc.iScale * iRange) / iFrames;
// Build all frames
retFrames->InsertEmpty(iFrames);
for (i = 0; i < iFrames; i++)
{
CloudDesc.rOffset = (Metric)i * rFactor;
CreateSphericalCloudMap(cxWidth, cyHeight, CloudDesc, Generator, bHemisphere, &retFrames->GetAt(i));
}
}
void CGFractal::CreateSphericalCloudMap (int cxWidth, int cyHeight, const SGCloudDesc &Desc, CGCloudGenerator3D &Generator, bool bHemisphere, CG8bitImage *retImage)
// CreateSphericalCloudMap
//
// Creates a image such that when wrapped on a sphere, it will display fractal
// clouds.
//
// NOTE: We only return one half of the sphere texture (one pole) since we are
// using this to paint to a 2D circle.
//
// See: Texture & Modeling: A Procedural Approach pp.119-120.
{
if (cxWidth <= 0 || cyHeight <= 0)
return;
// Create the image
retImage->Create(cxWidth, cyHeight);
// NOTE: Since we only ever paint half a sphere, we don't need the entire
// bitmap, we only fill in the north half of the spehere (which is why we
// multiply the height * 2 below.
Metric rUInc = 1.0 / (Metric)cxWidth;
Metric rVInc = 0.5 / (Metric)((bHemisphere ? 2 : 1) * cyHeight);
// Compute the factor to adjust the pixel value
Metric rRange = (Metric)(Desc.byMax - Desc.byMin) + 0.99999;
Metric rFactor = rRange / (2.0 * Generator.GetMaxValue());
// The generator origin needs to be larger than the level of detail
// so that we can always end up with positive numbers in GetAtPeriodic.
// But we want it to be constant, if possible, so that different levels
// of detail are centered.
int iOrigin = Max(10000, (int)Desc.rDetail);
// Loop
BYTE *pDestRow = retImage->GetPixelPos(0, 0);
BYTE *pDestRowEnd = retImage->GetPixelPos(0, 0 + cyHeight);
Metric rV = 0.25;
while (pDestRow < pDestRowEnd)
{
BYTE *pDest = pDestRow;
BYTE *pDestEnd = pDestRow + cxWidth;
Metric rU = 0.5;
while (pDest < pDestEnd)
{
// Convert this to a 3D point on a unit sphere with center at 0,0,0
Metric rX = cos((rV - 0.5) * 2.0 * PI) * sin(rU * 2.0 * PI);
Metric rY = cos((rV - 0.5) * 2.0 * PI) * cos(rU * 2.0 * PI);
Metric rZ = sin((rV - 0.5) * 2.0 * PI);
// Get a value from the generator. We use the periodic fuction,
// which uses frames (passed in to the generator) as the period.
// The period is only on the Z-axis, so we don't multiply the Z
// by detail (because we want smaller changes).
Metric rValue = Desc.rContrast * Generator.GetAtPeriodic(iOrigin + (int)(rX * Desc.rDetail), iOrigin + (int)(rY * Desc.rDetail), iOrigin + (int)(rZ + Desc.rOffset));
// Map to our range
*pDest++ = (BYTE)Clamp((DWORD)Desc.byMin + (DWORD)(rFactor * (Generator.GetMaxValue() + rValue)), (DWORD)Desc.byMin, (DWORD)Desc.byMax);
// Next
rU += rUInc;
}
pDestRow = retImage->NextRow(pDestRow);
rV += rVInc;
}
}
void CGFractal::FillClouds (CG8bitImage &Dest, int xDest, int yDest, int cxWidth, int cyHeight, const SGCloudDesc &Desc)
// FillClouds
//
// Fills an 8-bit image with fractal clouds.
{
// Make sure we're in bounds
if (!Dest.AdjustCoords(NULL, NULL, 0, 0,
&xDest, &yDest,
&cxWidth, &cyHeight))
return;
// Create a cloud generator, if necessary
CCloudGenerator Generator(Desc.iScale);
// Compute the factor to adjust the pixel value
Metric rRange = (Metric)(Desc.byMax - Desc.byMin) + 0.99999;
Metric rFactor = rRange / (2.0 * Generator.GetMaxValue());
// Fill
BYTE *pDestRow = Dest.GetPixelPos(xDest, yDest);
BYTE *pDestRowEnd = Dest.GetPixelPos(xDest, yDest + cyHeight);
Generator.ResetY(yDest);
while (pDestRow < pDestRowEnd)
{
BYTE *pDest = pDestRow;
BYTE *pDestEnd = pDestRow + cxWidth;
Generator.ResetX(xDest);
while (pDest < pDestEnd)
{
// The generator will return a value between -maxValue and +maxValue
Metric rValue = Generator.GetNext();
// Map to our range
*pDest++ = (BYTE)Clamp((DWORD)Desc.byMin + (DWORD)(rFactor * (Generator.GetMaxValue() + rValue)), (DWORD)Desc.byMin, (DWORD)Desc.byMax);
}
pDestRow = Dest.NextRow(pDestRow);
Generator.NextY();
}
}