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