source: src/materials/_normalisationCubemap.pym @ 1242:6dfb34cad6e3

Revision 1242:6dfb34cad6e3, 8.2 KB checked in by Arc Riley <arcriley@…>, 7 years ago (diff)

Ticket #955 :

  • added some comments and required New-BSD license header
Line 
1# PySoy's materials._normalisationCubemap function
2#
3# Copyright (C) 2006,2007,2008 PySoy Group
4#
5#  This program is free software; you can redistribute it and/or modify
6#  it under the terms of the GNU Affero General Public License as published
7#  by the Free Software Foundation, either version 3 of the License, or
8#  (at your option) any later version.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU Affero General Public License for more details.
14#
15#  You should have received a copy of the GNU Affero General Public License
16#  along with this program; if not, see http://www.gnu.org/licenses
17#
18# $Id: _normalisationCubemap.pym 1288 2008-05-20 05:39:26Z ArcRiley $
19
20cimport stdlib
21cimport soy.textures
22
23cdef void _normalisationCubemap(soy.textures.Texture _cubemap) :
24  cdef int _i, _j
25  cdef float _length
26  cdef float _vector[3]
27  cdef unsigned char* _ptr
28  #
29  ########################################
30  #
31  # call the texture's _resize function
32  #
33  # This sets up many of the textures parameters and mallocs _texels
34  # In the future the first array (bytes) should be 4 for float, which should
35  # not only speed this up a bit but provide more accurate normalisation
36  #
37  _cubemap._resize(1, 3, 32, 32, 6)
38  #
39  ########################################
40  #
41  # initialize _ptr to the texture's _texels
42  #
43  # we use _ptr to scan over the _texels data as it's generated, which is
44  # slightly faster than using _ptr as an int and adding it many times, ie:
45  #   _ptr[0] = <unsigned char> (_vector[0] * 255)
46  #   _ptr[1] = <unsigned char> (_vector[1] * 255)
47  #   _ptr[2] = <unsigned char> (_vector[2] * 255)
48  #   _ptr = _ptr + 3
49  #
50  #  vs the slower
51  #
52  #   _cubemap._texels[_ptr  ] = <unsigned char> (_vector[0] * 255)
53  #   _cubemap._texels[_ptr+1] = <unsigned char> (_vector[0] * 255)
54  #   _cubemap._texels[_ptr+2] = <unsigned char> (_vector[0] * 255)
55  #   _ptr = _ptr + 3
56  #
57  _ptr = _cubemap._texels
58  #
59  ############################################################################
60  #
61  # This following code was derived from "Normalisation Cube Map.cpp"
62  # from http://paulsprojects.net/tutorials/simplebump/simplebump.html
63  #
64  # Copyright (c) 2006, Paul Baker
65  # All rights reserved.
66  #
67  # Redistribution and use in source and binary forms, with or without
68  # modification, are permitted provided that the following conditions are met:
69  #
70  #  * Redistributions of source code must retain the above copyright notice,
71  #    this list of conditions and the following disclaimer.
72  #  * Redistributions in binary form must reproduce the above copyright
73  #    notice, this list of conditions and the following disclaimer in the
74  #    documentation and/or other materials provided with the distribution.
75  #  * Neither the name of paulsprojects.net nor the names of its contributors
76  #    may be used to endorse or promote products derived from this software
77  #    without specific prior written permission.
78  #
79  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
80  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82  # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
83  # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
84  # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
85  # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
86  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
87  # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
88  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
89  # POSSIBILITY OF SUCH DAMAGE.
90  #
91  ########################################
92  #
93  # +X
94  #
95  for _j from 0 < _j <= 32:
96    for _i from 0 < _i <= 32:
97      _vector[0] =  16.0
98      _vector[1] = -(_j + 0.5 - 16.0)
99      _vector[2] = -(_i + 0.5 - 16.0)
100
101      _length = stdlib.sqrtf( _vector[0] * _vector[0] +
102                              _vector[1] * _vector[1] +
103                              _vector[2] * _vector[2] )
104
105      _vector[0] = 0.5 * (_vector[0] / _length) + 0.5
106      _vector[1] = 0.5 * (_vector[1] / _length) + 0.5
107      _vector[2] = 0.5 * (_vector[2] / _length) + 0.5
108
109      _ptr[0] = <unsigned char> (_vector[0] * 255)
110      _ptr[1] = <unsigned char> (_vector[1] * 255)
111      _ptr[2] = <unsigned char> (_vector[2] * 255)
112      _ptr = _ptr + 3
113  #
114  ########################################
115  #
116  # -X
117  #
118  for _j from 0 < _j <= 32:
119    for _i from 0 < _i <= 32:
120      _vector[0] = -16.0
121      _vector[1] = -(_j + 0.5 - 16.0)
122      _vector[2] =  (_i + 0.5 - 16.0)
123
124      _length = stdlib.sqrtf( _vector[0] * _vector[0] +
125                              _vector[1] * _vector[1] +
126                              _vector[2] * _vector[2] )
127
128      _vector[0] = 0.5 * (_vector[0] / _length) + 0.5
129      _vector[1] = 0.5 * (_vector[1] / _length) + 0.5
130      _vector[2] = 0.5 * (_vector[2] / _length) + 0.5
131
132      _ptr[0] = <unsigned char> (_vector[0] * 255)
133      _ptr[1] = <unsigned char> (_vector[1] * 255)
134      _ptr[2] = <unsigned char> (_vector[2] * 255)
135      _ptr = _ptr + 3
136  #
137  ########################################
138  #
139  # +Y
140  #
141  for _j from 0 < _j <= 32:
142    for _i from 0 < _i <= 32:
143      _vector[0] =  (_i + 0.5 - 16.0)
144      _vector[1] =  16.0
145      _vector[2] =  (_j + 0.5 - 16.0)
146
147      _length = stdlib.sqrtf( _vector[0] * _vector[0] +
148                              _vector[1] * _vector[1] +
149                              _vector[2] * _vector[2] )
150
151      _vector[0] = 0.5 * (_vector[0] / _length) + 0.5
152      _vector[1] = 0.5 * (_vector[1] / _length) + 0.5
153      _vector[2] = 0.5 * (_vector[2] / _length) + 0.5
154
155      _ptr[0] = <unsigned char> (_vector[0] * 255)
156      _ptr[1] = <unsigned char> (_vector[1] * 255)
157      _ptr[2] = <unsigned char> (_vector[2] * 255)
158      _ptr = _ptr + 3
159  #
160  ########################################
161  #
162  # -Y
163  #
164  for _j from 0 < _j <= 32:
165    for _i from 0 < _i <= 32:
166      _vector[0] =  (_i + 0.5 - 16.0)
167      _vector[1] = -16.0
168      _vector[2] = -(_j + 0.5 - 16.0)
169
170      _length = stdlib.sqrtf( _vector[0] * _vector[0] +
171                              _vector[1] * _vector[1] +
172                              _vector[2] * _vector[2] )
173
174      _vector[0] = 0.5 * (_vector[0] / _length) + 0.5
175      _vector[1] = 0.5 * (_vector[1] / _length) + 0.5
176      _vector[2] = 0.5 * (_vector[2] / _length) + 0.5
177
178      _ptr[0] = <unsigned char> (_vector[0] * 255)
179      _ptr[1] = <unsigned char> (_vector[1] * 255)
180      _ptr[2] = <unsigned char> (_vector[2] * 255)
181      _ptr = _ptr + 3
182  #
183  ########################################
184  #
185  # +Z
186  #
187  for _j from 0 < _j <= 32:
188    for _i from 0 < _i <= 32:
189      _vector[0] =  (_i + 0.5 - 16.0)
190      _vector[1] = -(_j + 0.5 - 16.0)
191      _vector[2] =  16.0
192
193      _length = stdlib.sqrtf( _vector[0] * _vector[0] +
194                              _vector[1] * _vector[1] +
195                              _vector[2] * _vector[2] )
196
197      _vector[0] = 0.5 * (_vector[0] / _length) + 0.5
198      _vector[1] = 0.5 * (_vector[1] / _length) + 0.5
199      _vector[2] = 0.5 * (_vector[2] / _length) + 0.5
200
201      _ptr[0] = <unsigned char> (_vector[0] * 255)
202      _ptr[1] = <unsigned char> (_vector[1] * 255)
203      _ptr[2] = <unsigned char> (_vector[2] * 255)
204      _ptr = _ptr + 3
205  #
206  ########################################
207  #
208  # -Z
209  #
210  for _j from 0 < _j <= 32:
211    for _i from 0 < _i <= 32:
212      _vector[0] = -(_i + 0.5 - 16.0)
213      _vector[1] = -(_j + 0.5 - 16.0)
214      _vector[2] = -16.0
215
216      _length = stdlib.sqrtf( _vector[0] * _vector[0] +
217                              _vector[1] * _vector[1] +
218                              _vector[2] * _vector[2] )
219
220      _vector[0] = 0.5 * (_vector[0] / _length) + 0.5
221      _vector[1] = 0.5 * (_vector[1] / _length) + 0.5
222      _vector[2] = 0.5 * (_vector[2] / _length) + 0.5
223
224      _ptr[0] = <unsigned char> (_vector[0] * 255)
225      _ptr[1] = <unsigned char> (_vector[1] * 255)
226      _ptr[2] = <unsigned char> (_vector[2] * 255)
227      _ptr = _ptr + 3
228  #
229  ############################################################################
Note: See TracBrowser for help on using the repository browser.