1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.sg.prism; 27 28 import javafx.application.ConditionalFeature; 29 import javafx.application.Platform; 30 import javafx.scene.shape.CullFace; 31 import javafx.scene.shape.DrawMode; 32 import com.sun.javafx.geom.Vec3d; 33 import com.sun.javafx.geom.transform.Affine3D; 34 import com.sun.prism.Graphics; 35 import com.sun.prism.Material; 36 import com.sun.prism.MeshView; 37 import com.sun.prism.ResourceFactory; 38 39 /** 40 * TODO: 3D - Need documentation 41 */ 42 public abstract class NGShape3D extends NGNode { 43 private NGPhongMaterial material; 44 private DrawMode drawMode; 45 private CullFace cullFace; 46 private boolean materialDirty = false; 47 private boolean drawModeDirty = false; 48 NGTriangleMesh mesh; 49 private MeshView meshView; 50 51 public void setMaterial(NGPhongMaterial material) { 52 this.material = material; 53 materialDirty = true; 54 visualsChanged(); 55 } 56 public void setDrawMode(Object drawMode) { 57 this.drawMode = (DrawMode) drawMode; 58 drawModeDirty = true; 59 visualsChanged(); 60 } 61 62 public void setCullFace(Object cullFace) { 63 this.cullFace = (CullFace) cullFace; 64 visualsChanged(); 65 } 66 67 void invalidate() { 68 meshView = null; 69 visualsChanged(); 70 } 71 72 private void renderMeshView(Graphics g) { 73 74 //validate state 75 g.setup3DRendering(); 76 77 ResourceFactory rf = g.getResourceFactory(); 78 79 if (meshView == null && mesh != null) { 80 meshView = rf.createMeshView(mesh.createMesh(rf)); 81 setMaterial(material); 82 setDrawMode(drawMode); 83 setCullFace(cullFace); 84 } 85 86 if (meshView == null || !mesh.validate()) { 87 return; 88 } 89 90 Material mtl = material.createMaterial(rf); 91 if (materialDirty) { 92 meshView.setMaterial(mtl); 93 materialDirty = false; 94 } 95 96 // NOTE: Always check determinant in case of mirror transform. 97 int cullingMode = cullFace.ordinal(); 98 if (cullFace.ordinal() != MeshView.CULL_NONE 99 && g.getTransformNoClone().getDeterminant() < 0) { 100 cullingMode = cullingMode == MeshView.CULL_BACK 101 ? MeshView.CULL_FRONT : MeshView.CULL_BACK; 102 } 103 meshView.setCullingMode(cullingMode); 104 105 if (drawModeDirty) { 106 meshView.setWireframe(drawMode == DrawMode.LINE); 107 drawModeDirty = false; 108 } 109 110 // Setup lights 111 int pointLightIdx = 0; 112 if (g.getLights() == null || g.getLights()[0] == null) { 113 // If no lights are in scene apply default light. Default light 114 // is a single point white point light at camera eye position. 115 meshView.setAmbientLight(0.0f, 0.0f, 0.0f); 116 Vec3d cameraPos = g.getCameraNoClone().getPositionInWorld(null); 117 meshView.setPointLight(pointLightIdx++, 118 (float)cameraPos.x, 119 (float)cameraPos.y, 120 (float)cameraPos.z, 121 1.0f, 1.0f, 1.0f, 1.0f); 122 } else { 123 float ambientRed = 0.0f; 124 float ambientBlue = 0.0f; 125 float ambientGreen = 0.0f; 126 127 for (int i = 0; i < g.getLights().length; i++) { 128 NGLightBase lightBase = g.getLights()[i]; 129 if (lightBase == null) { 130 // The array of lights can have nulls 131 break; 132 } else if (lightBase.affects(this)) { 133 float rL = lightBase.getColor().getRed(); 134 float gL = lightBase.getColor().getGreen(); 135 float bL = lightBase.getColor().getBlue(); 136 /* TODO: 3D 137 * There is a limit on the number of lights that can affect 138 * a 3D shape. (Currently we simply select the first 3) 139 * Thus it is important to select the most relevant lights. 140 * 141 * One such way would be to sort lights according to 142 * intensity, which becomes especially relevant when lights 143 * are attenuated. Only the most intense set of lights 144 * would be set. 145 * The approximate intesity a light will have on a given 146 * shape, could be defined by: 147 */ 148 // // Where d is distance from point light 149 // float attenuationFactor = 1/(c + cL * d + cQ * d * d); 150 // float intensity = rL * 0.299f + gL * 0.587f + bL * 0.114f; 151 // intensity *= attenuationFactor; 152 if (lightBase instanceof NGPointLight) { 153 NGPointLight light = (NGPointLight)lightBase; 154 if (rL != 0.0f || gL != 0.0f || bL != 0.0f) { 155 Affine3D lightWT = light.getWorldTransform(); 156 meshView.setPointLight(pointLightIdx++, 157 (float)lightWT.getMxt(), 158 (float)lightWT.getMyt(), 159 (float)lightWT.getMzt(), 160 rL, gL, bL, 1.0f); 161 } 162 } else if (lightBase instanceof NGAmbientLight) { 163 // Accumulate ambient lights 164 ambientRed += rL; 165 ambientGreen += gL; 166 ambientBlue += bL; 167 } 168 } 169 } 170 ambientRed = saturate(ambientRed); 171 ambientGreen = saturate(ambientGreen); 172 ambientBlue = saturate(ambientBlue); 173 meshView.setAmbientLight(ambientRed, ambientGreen, ambientBlue); 174 } 175 // TODO: 3D Required for D3D implementation of lights, which is limited to 3 176 while (pointLightIdx < 3) { 177 // Reset any previously set lights 178 meshView.setPointLight(pointLightIdx++, 0, 0, 0, 0, 0, 0, 0); 179 } 180 181 meshView.render(g); 182 } 183 184 // Clamp between [0, 1] 185 private static float saturate(float value) { 186 return value < 1.0f ? ((value < 0.0f) ? 0.0f : value) : 1.0f; 187 } 188 189 public void setMesh(NGTriangleMesh triangleMesh) { 190 this.mesh = triangleMesh; 191 meshView = null; 192 visualsChanged(); 193 } 194 195 @Override 196 protected void renderContent(Graphics g) { 197 if (!Platform.isSupported(ConditionalFeature.SCENE3D) || material == null) { 198 return; 199 } 200 renderMeshView(g); 201 } 202 203 // This node requires 3D graphics state for rendering 204 @Override 205 boolean isShape3D() { 206 return true; 207 } 208 209 @Override 210 protected boolean hasOverlappingContents() { 211 return false; 212 } 213 214 @Override 215 public void release() { 216 // TODO: 3D - Need to release native resources 217 // material, mesh and meshview have native backing that need clean up. 218 } 219 }