1/**
2 * Mandelbulber v2, a 3D fractal generator       ,=#MKNmMMKmmßMNWy,
3 *                                             ,B" ]L,,p%%%,,,§;, "K
4 * Copyright (C) 2021 Mandelbulber Team        §R-==%w["'~5]m%=L.=~5N
5 *                                        ,=mm=§M ]=4 yJKA"/-Nsaj  "Bw,==,,
6 * This file is part of Mandelbulber.    §R.r= jw",M  Km .mM  FW ",§=ß., ,TN
7 *                                     ,4R =%["w[N=7]J '"5=],""]]M,w,-; T=]M
8 * Mandelbulber is free software:     §R.ß~-Q/M=,=5"v"]=Qf,'§"M= =,M.§ Rz]M"Kw
9 * you can redistribute it and/or     §w "xDY.J ' -"m=====WeC=\ ""%""y=%"]"" §
10 * modify it under the terms of the    "§M=M =D=4"N #"%==A%p M§ M6  R' #"=~.4M
11 * GNU General Public License as        §W =, ][T"]C  §  § '§ e===~ U  !§[Z ]N
12 * published by the                    4M",,Jm=,"=e~  §  §  j]]""N  BmM"py=ßM
13 * Free Software Foundation,          ]§ T,M=& 'YmMMpM9MMM%=w=,,=MT]M m§;'§,
14 * either version 3 of the License,    TWw [.j"5=~N[=§%=%W,T ]R,"=="Y[LFT ]N
15 * or (at your option)                   TW=,-#"%=;[  =Q:["V""  ],,M.m == ]N
16 * any later version.                      J§"mr"] ,=,," =="""J]= M"M"]==ß"
17 *                                          §= "=C=4 §"eM "=B:m|4"]#F,§~
18 * Mandelbulber is distributed in            "9w=,,]w em%wJ '"~" ,=,,ß"
19 * the hope that it will be useful,                 . "K=  ,=RMMMßM"""
20 * but WITHOUT ANY WARRANTY;                            .'''
21 * without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 *
24 * See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with Mandelbulber. If not, see <http://www.gnu.org/licenses/>.
27 *
28 * ###########################################################################
29 *
30 * Authors: Krzysztof Marczak (buddhi1980@gmail.com)
31 *
32 * calculate environment mapping effecr
33 */
34
35#ifdef USE_ENV_MAPPING
36float3 EnvMappingShader(
37	__constant sClInConstants *consts, sRenderData *renderData, sShaderInputDataCl *input)
38{
39	float3 envReflect;
40
41	float dotp = -dot(input->viewVector, input->normal);
42	float3 reflect = input->normal * 2.0f * dotp + input->viewVector;
43
44	float alphaTexture = -GetAlpha(reflect) + M_PI_F;
45	float betaTexture = -GetBeta(reflect);
46	int texWidth = renderData->textureSizes[0].x;
47	int texHeight = renderData->textureSizes[0].y;
48
49	if (betaTexture > 0.5f * M_PI_F) betaTexture = 0.5f * M_PI_F - betaTexture;
50
51	if (betaTexture < -0.5f * M_PI_F) betaTexture = -0.5f * M_PI_F + betaTexture;
52
53	float dtx = alphaTexture / (2.0f * M_PI_F) * texWidth + texWidth * 8.25f;
54	float dty = (betaTexture / M_PI_F + 0.5f) * texHeight + texHeight * 8.0f;
55	dtx = fmod(dtx, texWidth);
56	dty = fmod(dty, texHeight);
57	if (dtx < 0.0f) dtx = 0.0f;
58	if (dty < 0.0f) dty = 0.0f;
59
60	float reflectance = 1.0f;
61	if (input->material->fresnelReflectance)
62	{
63		float n1 = 1.0f;
64		float n2 = input->material->transparencyIndexOfRefraction;
65		reflectance = Reflectance(input->normal, input->viewVector, n1, n2);
66		if (reflectance < 0.0f) reflectance = 0.0f;
67		if (reflectance > 1.0f) reflectance = 1.0f;
68	}
69
70	__global uchar4 *texture = renderData->textures[0];
71
72	envReflect = BicubicInterpolation(dtx / texWidth, dty / texHeight, texture, texWidth, texHeight)
73							 * reflectance;
74
75	return envReflect;
76}
77#endif