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