1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc.  All rights reserved.
6bf215546Sopenharmony_ci * Copyright 2009 Marek Olšák <maraeo@gmail.com>
7bf215546Sopenharmony_ci *
8bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
9bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
10bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
11bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
12bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
13bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
14bf215546Sopenharmony_ci * the following conditions:
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
17bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
18bf215546Sopenharmony_ci * of the Software.
19bf215546Sopenharmony_ci *
20bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27bf215546Sopenharmony_ci *
28bf215546Sopenharmony_ci **************************************************************************/
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/**
31bf215546Sopenharmony_ci * @file
32bf215546Sopenharmony_ci * Texture mapping utility functions.
33bf215546Sopenharmony_ci *
34bf215546Sopenharmony_ci * @author Brian Paul
35bf215546Sopenharmony_ci *         Marek Olšák
36bf215546Sopenharmony_ci */
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include "pipe/p_defines.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#include "util/u_debug.h"
41bf215546Sopenharmony_ci#include "util/u_texture.h"
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_civoid util_map_texcoords2d_onto_cubemap(unsigned face,
44bf215546Sopenharmony_ci                                       const float *in_st, unsigned in_stride,
45bf215546Sopenharmony_ci                                       float *out_str, unsigned out_stride,
46bf215546Sopenharmony_ci                                       boolean allow_scale)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci   int i;
49bf215546Sopenharmony_ci   float rx, ry, rz;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /* loop over quad verts */
52bf215546Sopenharmony_ci   for (i = 0; i < 4; i++) {
53bf215546Sopenharmony_ci      /* Compute sc = +/-scale and tc = +/-scale.
54bf215546Sopenharmony_ci       * Not +/-1 to avoid cube face selection ambiguity near the edges,
55bf215546Sopenharmony_ci       * though that can still sometimes happen with this scale factor...
56bf215546Sopenharmony_ci       *
57bf215546Sopenharmony_ci       * XXX: Yep, there is no safe scale factor that will prevent sampling
58bf215546Sopenharmony_ci       * the neighbouring face when stretching out.  A more reliable solution
59bf215546Sopenharmony_ci       * would be to clamp (sc, tc) against +/- 1.0-1.0/mipsize, in the shader.
60bf215546Sopenharmony_ci       *
61bf215546Sopenharmony_ci       * Also, this is not necessary when minifying, or 1:1 blits.
62bf215546Sopenharmony_ci       */
63bf215546Sopenharmony_ci      const float scale = allow_scale ? 0.9999f : 1.0f;
64bf215546Sopenharmony_ci      const float sc = (2 * in_st[0] - 1) * scale;
65bf215546Sopenharmony_ci      const float tc = (2 * in_st[1] - 1) * scale;
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci      switch (face) {
68bf215546Sopenharmony_ci         case PIPE_TEX_FACE_POS_X:
69bf215546Sopenharmony_ci            rx = 1;
70bf215546Sopenharmony_ci            ry = -tc;
71bf215546Sopenharmony_ci            rz = -sc;
72bf215546Sopenharmony_ci            break;
73bf215546Sopenharmony_ci         case PIPE_TEX_FACE_NEG_X:
74bf215546Sopenharmony_ci            rx = -1;
75bf215546Sopenharmony_ci            ry = -tc;
76bf215546Sopenharmony_ci            rz = sc;
77bf215546Sopenharmony_ci            break;
78bf215546Sopenharmony_ci         case PIPE_TEX_FACE_POS_Y:
79bf215546Sopenharmony_ci            rx = sc;
80bf215546Sopenharmony_ci            ry = 1;
81bf215546Sopenharmony_ci            rz = tc;
82bf215546Sopenharmony_ci            break;
83bf215546Sopenharmony_ci         case PIPE_TEX_FACE_NEG_Y:
84bf215546Sopenharmony_ci            rx = sc;
85bf215546Sopenharmony_ci            ry = -1;
86bf215546Sopenharmony_ci            rz = -tc;
87bf215546Sopenharmony_ci            break;
88bf215546Sopenharmony_ci         case PIPE_TEX_FACE_POS_Z:
89bf215546Sopenharmony_ci            rx = sc;
90bf215546Sopenharmony_ci            ry = -tc;
91bf215546Sopenharmony_ci            rz = 1;
92bf215546Sopenharmony_ci            break;
93bf215546Sopenharmony_ci         case PIPE_TEX_FACE_NEG_Z:
94bf215546Sopenharmony_ci            rx = -sc;
95bf215546Sopenharmony_ci            ry = -tc;
96bf215546Sopenharmony_ci            rz = -1;
97bf215546Sopenharmony_ci            break;
98bf215546Sopenharmony_ci         default:
99bf215546Sopenharmony_ci            rx = ry = rz = 0;
100bf215546Sopenharmony_ci            assert(0);
101bf215546Sopenharmony_ci      }
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci      out_str[0] = rx; /*s*/
104bf215546Sopenharmony_ci      out_str[1] = ry; /*t*/
105bf215546Sopenharmony_ci      out_str[2] = rz; /*r*/
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci      in_st += in_stride;
108bf215546Sopenharmony_ci      out_str += out_stride;
109bf215546Sopenharmony_ci   }
110bf215546Sopenharmony_ci}
111