Oolite
Loading...
Searching...
No Matches
OOConvertCubeMapToLatLong.m
Go to the documentation of this file.
1/*
2
3OOConvertCubeMapToLatLong.m
4
5Convert a cube map texture to a lat/long texture.
6
7
8Copyright (C) 2010-2013 Jens Ayton
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27
28*/
29
31#import "OOTextureScaling.h"
32
33#define kPiF (3.14159265358979323846264338327950288f)
34
35OOPixMap OOConvertCubeMapToLatLong(OOPixMap sourcePixMap, OOPixMapDimension height, BOOL leaveSpaceForMipMaps)
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}
OOPixMap OOConvertCubeMapToLatLong(OOPixMap sourcePixMap, OOPixMapDimension height, BOOL leaveSpaceForMipMaps)
#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