Oolite
Loading...
Searching...
No Matches
OOConvertCubeMapToLatLong.m File Reference
Include dependency graph for OOConvertCubeMapToLatLong.m:

Go to the source code of this file.

Macros

#define kPiF   (3.14159265358979323846264338327950288f)

Functions

OOPixMap OOConvertCubeMapToLatLong (OOPixMap sourcePixMap, OOPixMapDimension height, BOOL leaveSpaceForMipMaps)

Macro Definition Documentation

◆ kPiF

#define kPiF   (3.14159265358979323846264338327950288f)

Definition at line 33 of file OOConvertCubeMapToLatLong.m.

Referenced by OOConvertCubeMapToLatLong().

Function Documentation

◆ OOConvertCubeMapToLatLong()

OOPixMap OOConvertCubeMapToLatLong ( OOPixMap sourcePixMap,
OOPixMapDimension height,
BOOL leaveSpaceForMipMaps )

Definition at line 35 of file OOConvertCubeMapToLatLong.m.

36{
37 if (!OOIsValidPixMap(sourcePixMap) || sourcePixMap.format != kOOPixMapRGBA || sourcePixMap.height != sourcePixMap.width * 6) {
38 return kOONullPixMap;
39 }
40
41 NSCParameterAssert(height > 0);
42
43 height *= 2;
44 OOPixMapDimension width = height * 2;
45 OOPixMap outPixMap = OOAllocatePixMap(width, height, 4, 0, 0);
46 if (!OOIsValidPixMap(outPixMap))
47 return kOONullPixMap;
48
50 uint32_t* pixel = outPixMap.pixels;
51 float fheight = height;
52 float rheight = 1.0f / fheight;
53
54 float halfSize = (sourcePixMap.width - 1) * 0.5f;
55 uint8_t* srcBytes = sourcePixMap.pixels;
56
57 // Build tables of sin/cos of longitude.
58 float sinTable[width];
59 float cosTable[width];
60 for (x = 0; x < width; x++) {
61 float lon = ((float)x * rheight) * kPiF;
62 sinTable[x] = sin(lon);
63 cosTable[x] = cos(lon);
64 }
65
66 for (y = 0; y < height; y++) {
67 // Calcuate sin/cos of latitude.
68 /*
69 Clang static analyzer (Xcode 3.2.5 version, through to Xcode 4.4
70 and freestanding checker-268 at least) says:
71 "Assigned value is garbage or undefined."
72 Memsetting sinTable to all-zeros moves this to the cosTable line.
73 Since every value in each of those tables is in fact defined, this
74 is an error in the analyzer.
75 -- Ahruman 2011-01-25/2012-09-14
76 */
77 float cy = -sinTable[width * 3 / 4 - y];
78 float lac = -cosTable[width * 3 / 4 - y];
79 float ay = fabs(cy);
80
81 for (x = 0; x < width; x++) {
82 float cx = sinTable[x] * lac;
83 float cz = cosTable[x] * lac;
84
85 float ax = fabs(cx);
86 float az = fabs(cz);
87
88 // Y offset of start of this face in image.
89 OOPixMapDimension yOffset;
90
91 // Coordinates within selected face.
92 float x, y, r;
93
94 // Select source face.
95 if (ax >= ay && ax >= az) {
96 x = cz;
97 y = -cy;
98 r = ax;
99 if (0.0f < cx) {
100 yOffset = 0;
101 } else {
102 x = -x;
103 yOffset = 1;
104 }
105 } else if (ay >= ax && ay >= az) {
106 x = cx;
107 y = cz;
108 r = ay;
109 if (0.0f < cy) {
110 y = -y;
111 yOffset = 2;
112 } else {
113 yOffset = 3;
114 }
115 } else {
116 x = cx;
117 y = -cy;
118 r = az;
119 if (0.0f < cz) {
120 x = -x;
121 yOffset = 5;
122 } else {
123 yOffset = 4;
124 }
125 }
126
127 // Scale coordinates.
128 r = 1.0f / r;
129 OOPixMapDimension ix = (x * r + 1.0f) * halfSize;
130 OOPixMapDimension iy = (y * r + 1.0f) * halfSize;
131
132#ifndef NDEBUG
133 assert(ix < sourcePixMap.width && iy < sourcePixMap.width);
134#endif
135
136 // Look up pixel.
137 iy += sourcePixMap.width * yOffset;
138
139 uint32_t* row = (uint32_t*)(srcBytes + iy * sourcePixMap.rowBytes);
140 *pixel++ = row[ix];
141 }
142 }
143
144 // Scale to half size for supersamplingness.
145 return OOScalePixMap(outPixMap, width / 2, height / 2, leaveSpaceForMipMaps);
146}
#define kPiF
uint32_t OOPixMapDimension
Definition OOPixMap.h:32
@ kOOPixMapRGBA
Definition OOPixMap.h:40
const OOPixMap kOONullPixMap
Definition OOPixMap.m:30
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:66
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
float y
float x
OOPixMap OOScalePixMap(OOPixMap srcPixMap, OOPixMapDimension dstWidth, OOPixMapDimension dstHeight, BOOL leaveSpaceForMipMaps)
OOPixMapDimension height
Definition OOPixMap.h:45
size_t rowBytes
Definition OOPixMap.h:47
void * pixels
Definition OOPixMap.h:44
OOPixMapDimension width
Definition OOPixMap.h:45
OOPixMapFormat format
Definition OOPixMap.h:46

References OOPixMap::format, OOPixMap::height, kOONullPixMap, kOOPixMapRGBA, kPiF, OOAllocatePixMap(), OOIsValidPixMap(), OOScalePixMap(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by OOTextureLoader(OOPrivate)::applySettings.

Here is the call graph for this function:
Here is the caller graph for this function: