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.prism.es2; 27 28 import com.sun.prism.Graphics; 29 import com.sun.prism.Material; 30 import com.sun.prism.impl.BaseMeshView; 31 import com.sun.prism.impl.Disposer; 32 33 /** 34 * TODO: 3D - Need documentation 35 */ 36 class ES2MeshView extends BaseMeshView { 37 38 static int count = 0; 39 private final ES2Context context; 40 private final long nativeHandle; 41 private float ambientLightRed = 0; 42 private float ambientLightBlue = 0; 43 private float ambientLightGreen = 0; 44 45 // NOTE: We only support up to 3 point lights at the present 46 private ES2Light[] lights = new ES2Light[3]; 47 48 // TODO: 3D - Need a mechanism to "decRefCount" Mesh and Material 49 // if we need to do eager clean up 50 final private ES2Mesh mesh; 51 private ES2PhongMaterial material; 52 53 private ES2MeshView(ES2Context context, long nativeHandle, ES2Mesh mesh, 54 Disposer.Record disposerRecord) { 55 super(disposerRecord); 56 this.context = context; 57 this.mesh = mesh; 58 this.nativeHandle = nativeHandle; 59 count++; 60 } 61 62 static ES2MeshView create(ES2Context context, ES2Mesh mesh) { 63 long nativeHandle = context.createES2MeshView(mesh); 64 return new ES2MeshView(context, nativeHandle, mesh, new ES2MeshViewDisposerRecord(context, nativeHandle)); 65 } 66 67 @Override 68 public void setCullingMode(int cullingMode) { 69 context.setCullingMode(nativeHandle, cullingMode); 70 } 71 72 @Override 73 public void setMaterial(Material material) { 74 context.setMaterial(nativeHandle, material); 75 this.material = (ES2PhongMaterial) material; 76 } 77 78 @Override 79 public void setWireframe(boolean wireframe) { 80 context.setWireframe(nativeHandle, wireframe); 81 } 82 83 @Override 84 public void setAmbientLight(float r, float g, float b) { 85 ambientLightRed = r; 86 ambientLightGreen = g; 87 ambientLightBlue = b; 88 context.setAmbientLight(nativeHandle, r, g, b); 89 } 90 91 float getAmbientLightRed() { 92 return ambientLightRed; 93 } 94 95 float getAmbientLightGreen() { 96 return ambientLightGreen; 97 } 98 99 float getAmbientLightBlue() { 100 return ambientLightBlue; 101 } 102 103 @Override 104 public void setPointLight(int index, float x, float y, float z, float r, float g, float b, float w) { 105 // NOTE: We only support up to 3 point lights at the present 106 if (index >= 0 && index <= 2) { 107 lights[index] = new ES2Light(x, y, z, r, g, b, w); 108 context.setPointLight(nativeHandle, index, x, y, z, r, g, b, w); 109 } 110 } 111 112 ES2Light[] getPointLights() { 113 return lights; 114 } 115 116 @Override 117 public void render(Graphics g) { 118 material.lockTextureMaps(); 119 context.renderMeshView(nativeHandle, g.getTransformNoClone(), this); 120 material.unlockTextureMaps(); 121 } 122 123 ES2PhongMaterial getMaterial() { 124 return material; 125 } 126 127 @Override 128 public void dispose() { 129 // TODO: 3D - Need a mechanism to "decRefCount" Mesh and Material 130 material = null; 131 lights = null; 132 disposerRecord.dispose(); 133 count--; 134 } 135 136 public int getCount() { 137 return count; 138 } 139 140 static class ES2MeshViewDisposerRecord implements Disposer.Record { 141 142 private final ES2Context context; 143 private long nativeHandle; 144 145 ES2MeshViewDisposerRecord(ES2Context context, long nativeHandle) { 146 this.context = context; 147 this.nativeHandle = nativeHandle; 148 } 149 150 void traceDispose() { 151 } 152 153 public void dispose() { 154 if (nativeHandle != 0L) { 155 traceDispose(); 156 context.releaseES2MeshView(nativeHandle); 157 nativeHandle = 0L; 158 } 159 } 160 } 161 }