1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.graphics.g3d.environment; 18 19 import com.badlogic.gdx.graphics.Color; 20 import com.badlogic.gdx.math.Vector3; 21 import com.badlogic.gdx.utils.GdxRuntimeException; 22 23 public class AmbientCubemap { 24 private final static float clamp (final float v) { 25 return v < 0f ? 0f : (v > 1f ? 1f : v); 26 } 27 28 public final float data[]; 29 30 public AmbientCubemap () { 31 data = new float[6 * 3]; 32 } 33 34 public AmbientCubemap (final float copyFrom[]) { 35 if (copyFrom.length != (6 * 3)) throw new GdxRuntimeException("Incorrect array size"); 36 data = new float[copyFrom.length]; 37 System.arraycopy(copyFrom, 0, data, 0, data.length); 38 } 39 40 public AmbientCubemap (final AmbientCubemap copyFrom) { 41 this(copyFrom.data); 42 } 43 44 public AmbientCubemap set (final float values[]) { 45 for (int i = 0; i < data.length; i++) 46 data[i] = values[i]; 47 return this; 48 } 49 50 public AmbientCubemap set (final AmbientCubemap other) { 51 return set(other.data); 52 } 53 54 public AmbientCubemap set (final Color color) { 55 return set(color.r, color.g, color.b); 56 } 57 58 public AmbientCubemap set (float r, float g, float b) { 59 for (int idx = 0; idx < data.length;) { 60 data[idx++] = r; 61 data[idx++] = g; 62 data[idx++] = b; 63 } 64 return this; 65 } 66 67 public Color getColor (final Color out, int side) { 68 side *= 3; 69 return out.set(data[side], data[side + 1], data[side + 2], 1f); 70 } 71 72 public AmbientCubemap clear () { 73 for (int i = 0; i < data.length; i++) 74 data[i] = 0f; 75 return this; 76 } 77 78 public AmbientCubemap clamp () { 79 for (int i = 0; i < data.length; i++) 80 data[i] = clamp(data[i]); 81 return this; 82 } 83 84 public AmbientCubemap add (float r, float g, float b) { 85 for (int idx = 0; idx < data.length;) { 86 data[idx++] += r; 87 data[idx++] += g; 88 data[idx++] += b; 89 } 90 return this; 91 } 92 93 public AmbientCubemap add (final Color color) { 94 return add(color.r, color.g, color.b); 95 } 96 97 public AmbientCubemap add (final float r, final float g, final float b, final float x, final float y, final float z) { 98 final float x2 = x * x, y2 = y * y, z2 = z * z; 99 float d = x2 + y2 + z2; 100 if (d == 0f) return this; 101 d = 1f / d * (d + 1f); 102 final float rd = r * d, gd = g * d, bd = b * d; 103 int idx = x > 0 ? 0 : 3; 104 data[idx] += x2 * rd; 105 data[idx + 1] += x2 * gd; 106 data[idx + 2] += x2 * bd; 107 idx = y > 0 ? 6 : 9; 108 data[idx] += y2 * rd; 109 data[idx + 1] += y2 * gd; 110 data[idx + 2] += y2 * bd; 111 idx = z > 0 ? 12 : 15; 112 data[idx] += z2 * rd; 113 data[idx + 1] += z2 * gd; 114 data[idx + 2] += z2 * bd; 115 return this; 116 } 117 118 public AmbientCubemap add (final Color color, final Vector3 direction) { 119 return add(color.r, color.g, color.b, direction.x, direction.y, direction.z); 120 } 121 122 public AmbientCubemap add (final float r, final float g, final float b, final Vector3 direction) { 123 return add(r, g, b, direction.x, direction.y, direction.z); 124 } 125 126 public AmbientCubemap add (final Color color, final float x, final float y, final float z) { 127 return add(color.r, color.g, color.b, x, y, z); 128 } 129 130 public AmbientCubemap add (final Color color, final Vector3 point, final Vector3 target) { 131 return add(color.r, color.g, color.b, target.x - point.x, target.y - point.y, target.z - point.z); 132 } 133 134 public AmbientCubemap add (final Color color, final Vector3 point, final Vector3 target, final float intensity) { 135 final float t = intensity / (1f + target.dst(point)); 136 return add(color.r * t, color.g * t, color.b * t, target.x - point.x, target.y - point.y, target.z - point.z); 137 } 138 139 @Override 140 public String toString () { 141 String result = ""; 142 for (int i = 0; i < data.length; i += 3) { 143 result += Float.toString(data[i]) + ", " + Float.toString(data[i + 1]) + ", " + Float.toString(data[i + 2]) + "\n"; 144 } 145 return result; 146 } 147 } 148