Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / util / u_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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_texture.h"
41
42 void util_map_texcoords2d_onto_cubemap(unsigned face,
43 const float *in_st, unsigned in_stride,
44 float *out_str, unsigned out_stride)
45 {
46 int i;
47 float rx, ry, rz;
48
49 /* loop over quad verts */
50 for (i = 0; i < 4; i++) {
51 /* Compute sc = +/-scale and tc = +/-scale.
52 * Not +/-1 to avoid cube face selection ambiguity near the edges,
53 * though that can still sometimes happen with this scale factor...
54 */
55 const float scale = 0.9999f;
56 const float sc = (2 * in_st[0] - 1) * scale;
57 const float tc = (2 * in_st[1] - 1) * scale;
58
59 switch (face) {
60 case PIPE_TEX_FACE_POS_X:
61 rx = 1;
62 ry = -tc;
63 rz = -sc;
64 break;
65 case PIPE_TEX_FACE_NEG_X:
66 rx = -1;
67 ry = -tc;
68 rz = sc;
69 break;
70 case PIPE_TEX_FACE_POS_Y:
71 rx = sc;
72 ry = 1;
73 rz = tc;
74 break;
75 case PIPE_TEX_FACE_NEG_Y:
76 rx = sc;
77 ry = -1;
78 rz = -tc;
79 break;
80 case PIPE_TEX_FACE_POS_Z:
81 rx = sc;
82 ry = -tc;
83 rz = 1;
84 break;
85 case PIPE_TEX_FACE_NEG_Z:
86 rx = -sc;
87 ry = -tc;
88 rz = -1;
89 break;
90 default:
91 rx = ry = rz = 0;
92 assert(0);
93 }
94
95 out_str[0] = rx; /*s*/
96 out_str[1] = ry; /*t*/
97 out_str[2] = rz; /*r*/
98
99 in_st += in_stride;
100 out_str += out_stride;
101 }
102 }