1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     Util.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 /*
17  *------------------------------------------------------------
18  * Copyright(c) 2009-2010 by Digital Media Professionals Inc.
19  * All rights reserved.
20  *------------------------------------------------------------
21  * This source code is the confidential and proprietary
22  * of Digital Media Professionals Inc.
23  *------------------------------------------------------------
24  */
25 
26 #include <math.h>
27 
28 #include <GLES2/gl2.h>
29 #include <GLES2/gl2ext.h>
30 #include "Util.h"
31 #include "Tga.h"
32 #include "Memory.h"
33 
loadTexture(const char * _name,unsigned _target,int _level,bool & _useAlpha,bool _is2D,int * _width,unsigned _forceformat)34 void loadTexture(const char * _name, unsigned _target, int _level, bool& _useAlpha, bool _is2D, int *_width, unsigned _forceformat)
35 {
36     unsigned char* pixels;
37     unsigned width, height, format, type, orientation;
38 
39     (void)_useAlpha;
40     pixels = (unsigned char*)dmpLoadTGA(_name, &width, &height, &format, &type, &orientation);
41     if (!pixels)
42         return;
43 
44     switch (format)
45     {
46         case IMAGE_RGB:     format = GL_RGB;    break;
47         case IMAGE_RGBA:    format = GL_RGBA;   break;
48         default:
49             if (pixels)
50                 free(pixels);
51             return;
52     }
53 
54     /* force to convert format */
55     if (_forceformat != 0 && format != _forceformat)
56     {
57         unsigned i;
58         unsigned char* newpixels = 0;
59 
60         switch (format)
61         {
62             case GL_RGB:
63                 switch (_forceformat)
64                 {
65                     case GL_RGBA:
66                         newpixels = (unsigned char*)malloc(width * height * 4);
67                         if (!newpixels)
68                         {
69                             free(pixels);
70                             return;
71                         }
72                         for (i = 0; i < width * height; i++)
73                         {
74                             newpixels[i * 4 + 0] = pixels[i * 3 + 0];
75                             newpixels[i * 4 + 1] = pixels[i * 3 + 1];
76                             newpixels[i * 4 + 2] = pixels[i * 3 + 2];
77                             newpixels[i * 4 + 3] = 0xff;
78                         }
79                         break;
80                 }
81                 break;
82             case GL_RGBA:
83                 switch (_forceformat)
84                 {
85                     case GL_RGB:
86                         newpixels = (unsigned char*)malloc(width * height * 3);
87                         if (!newpixels)
88                         {
89                             free(pixels);
90                             return;
91                         }
92                         for (i = 0; i < width * height; i++)
93                         {
94                             newpixels[i * 3 + 0] = pixels[i * 4 + 0];
95                             newpixels[i * 3 + 1] = pixels[i * 4 + 1];
96                             newpixels[i * 3 + 2] = pixels[i * 4 + 2];
97                         }
98                         break;
99                 }
100                 break;
101         }
102 
103         if (newpixels)
104         {
105             format = _forceformat;
106             free(pixels);
107             pixels = newpixels;
108         }
109     }
110 
111     switch (type)
112     {
113         case IMAGE_UNSIGNED_BYTE:   type = GL_UNSIGNED_BYTE;    break;
114         default:
115             if (pixels)
116                 free(pixels);
117             return;
118     }
119 
120     if (_is2D)
121         glTexImage2D(_target, _level, format, width, height, 0, format, type, pixels);
122     else
123         glTexImage1D(_target, _level, format, width, 0, format, type, pixels);
124 
125     if (pixels)
126         free(pixels);
127 
128     if (_width)
129         (*_width) = width;
130 }
131 
z_schlick(float r,float t,bool normalize)132 float z_schlick(float r, float t, bool normalize)
133 {
134     float ret ;
135     ret = 1.f + (r - 1.f) * t * t ;
136     ret *= ret ;
137     if (!ret)
138         return 1000.f ;
139     if (normalize)
140         ret = r * r / ret ;
141     else
142         ret = r / ret ;
143     return ret ;
144 }
145 
a_schlick(float p,float w,bool normalize)146 float a_schlick(float p, float w, bool normalize)
147 {
148     float ret ;
149     w *= w ;
150     ret = p * p * (1.f - w) + w ;
151     if (!ret)
152         return 1000.f ;
153     if (normalize)
154         ret = p * sqrt(1.f / ret) ;
155     else
156         ret = sqrt(p / ret) ;
157     return ret ;
158 }
159 
gaussian(float _c,float m)160 float gaussian(float _c, float m)
161 {
162     GLfloat a = acos(_c) ;
163     return exp( - (a * a) / (m * m)) ;
164 }
165 
beckmann(float _c,float m)166 float beckmann(float _c, float m)
167 {
168     float tan2 ;
169     if (!_c)
170         return 0.f ;
171     tan2 = (_c * _c - 1.f) / (_c * _c) ;
172     return exp(tan2 / (m * m)) ;
173 }
174 
beckmann2(float _c,float m)175 float beckmann2(float _c, float m)
176 {
177     if (!_c)
178         return 0.f ;
179     GLfloat tan2 ;
180     GLfloat cos2 = _c * _c;
181     tan2 = (cos2 - 1.f) / cos2 ;
182     return exp(tan2 / (m * m)) / (cos2 * cos2) ;
183 }
184 
r_fresnel(float _c,float _m,float _r0,float _r1)185 float r_fresnel(float _c, float _m, float _r0, float _r1 )
186 {
187     // we get Schlick's approximation of the Fresnel formula
188     return _r1 + (_r0 - _r1) * pow(1.f - _c, _m) ;
189 }
190 
nk_fresnel(float _c,float _n,float _k)191 float nk_fresnel(float _c, float _n, float _k)
192 {
193     // we get Schlick's approximation of the Fresnel formula
194     GLfloat r1, r0, m ;
195     r1 = ((_n-1) * (_n-1) + _k * _k) / ((_n+1)*(_n+1) + _k*_k) ;
196     r0 = 1.f ;
197     m  = 5.0f ;
198     return r_fresnel(_c, m, r0, r1) ;
199 }
200 
201 
202 
203