From: luukkainen Date: Tue, 13 Jan 2009 13:34:59 +0000 (+0000) Subject: git-svn-id: https://www.simantics.org/svn/simantics/3d/branches/dev@8613 ac1ea38d... X-Git-Tag: simantics-1.19.0~20^2~15 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=15af86b4ca7eb7e85cfdad040c8438c6910a7272;p=simantics%2F3d.git git-svn-id: https://www.simantics.org/svn/simantics/3d/branches/dev@8613 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.proconf.g3d/plugin.xml b/org.simantics.proconf.g3d/plugin.xml index fc55861e..034ac76f 100644 --- a/org.simantics.proconf.g3d/plugin.xml +++ b/org.simantics.proconf.g3d/plugin.xml @@ -2,6 +2,7 @@ + + + + + diff --git a/org.simantics.proconf.g3d/schema/appearance.exsd b/org.simantics.proconf.g3d/schema/appearance.exsd new file mode 100644 index 00000000..f5cd6a03 --- /dev/null +++ b/org.simantics.proconf.g3d/schema/appearance.exsd @@ -0,0 +1,109 @@ + + + + + + + + + [Enter description of this extension point.] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + [Enter the first release in which this extension point appears.] + + + + + + + + + [Enter extension point usage example here.] + + + + + + + + + [Enter API information here.] + + + + + + + + + [Enter information about supplied implementation of this extension point.] + + + + + diff --git a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceProvider.java b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceProvider.java new file mode 100644 index 00000000..ff181c9c --- /dev/null +++ b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceProvider.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.proconf.g3d.base; + +import java.util.Collection; + +import org.simantics.layer0.utils.IEntity; + +import com.jme.renderer.Renderer; +import com.jme.scene.state.RenderState; + + +/** + * Interface for Appearance / Material + * + * + * @author Marko Luukkainen + * + */ +public interface AppearanceProvider { + + /** + * Used to check if GeometryProvider can generate mesh + * @param instance + * @return + */ + public boolean canHandle(IEntity instance); + + /** + * Generates mesh + * @param instance + * @param transform + * @return + */ + public Collection getAppearanceFromResource(IEntity instance, Renderer renderer); + + +} diff --git a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceProviderRegistry.java b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceProviderRegistry.java new file mode 100644 index 00000000..6540cf52 --- /dev/null +++ b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceProviderRegistry.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.proconf.g3d.base; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; +import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; +import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; +import org.eclipse.core.runtime.dynamichelpers.IFilter; +import org.simantics.db.Resource; +import org.simantics.layer0.utils.IEntity; +import org.simantics.utils.ui.ErrorLogger; + +import com.jme.renderer.Renderer; +import com.jme.scene.state.RenderState; + +public class AppearanceProviderRegistry implements IExtensionChangeHandler { + private static AppearanceProviderRegistry instance; + //public final static String ELEMENT_NAME = "Appearance"; + public final static String NAMESPACE = "org.simantics.proconf.g3d"; + public final static String EP_NAME = "appearance"; + + private ExtensionTracker tracker; + + private List extensions = new ArrayList(); + private Map providers; + + private AppearanceProviderRegistry() { + providers = new HashMap(); + + tracker = new ExtensionTracker(); + + IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE,EP_NAME); + loadExtensions(ep.getConfigurationElements()); + + IFilter filter = ExtensionTracker.createExtensionPointFilter(ep); + tracker.registerHandler(this, filter); + } + + private void close() { + tracker.close(); + tracker = null; + extensions = new ArrayList(); + } + + public static synchronized AppearanceProviderRegistry getInstance() { + if (instance==null) instance = new AppearanceProviderRegistry(); + return instance; + } + + public static synchronized void dispose() { + if (instance != null) { + instance.close(); + instance = null; + } + } + + public synchronized List getExtensions() { + return Collections.unmodifiableList(extensions); + } + + private synchronized void loadExtensions(IConfigurationElement[] elements) { + for (IConfigurationElement el : elements) { + String id = el.getAttribute("id"); + try { + Object o = el.createExecutableExtension("class"); + AppearanceProviderExtension ext = new AppearanceProviderExtension(id,(AppearanceProvider)o); + tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG); + extensions.add(ext); + } catch (CoreException e) { + ErrorLogger.defaultLogError("Cannot create java interface for GeometryProvider " + id, e); + } + } + } + + private Map getProviderMap() { + return providers; + } + + public static Collection getAppearance(IEntity thing, Renderer renderer) { + AppearanceProvider provider = getAppearanceProvider(thing); + return provider.getAppearanceFromResource(thing, renderer); + } + + public static AppearanceProvider getAppearanceProvider(IEntity thing) { + Collection types = thing.getRelatedObjects(thing.getGraph().getBuiltins().InstanceOf); + + //Resource types[] = OntologyUtils.getTypes(resource); + for (IEntity t : types) { + AppearanceProvider provider = getInstance().getProviderMap().get(t.getResource()); + if (provider == null) { + //for (Extension e : getInstance().getExtensions()) { + for (AppearanceProviderExtension e : getInstance().getExtensions()) { + if (e.provider.canHandle(thing)) { + getInstance().getProviderMap().put(t.getResource(), e.provider); + return e.provider; + } + } + + } else { + return provider; + } + } + + return null; + } + + @Override + public void addExtension(IExtensionTracker tracker, IExtension extension) { + loadExtensions(extension.getConfigurationElements()); + } + + @Override + public synchronized void removeExtension(IExtension extension, Object[] objects) { + for (Object o : objects) { + AppearanceProviderExtension ext = (AppearanceProviderExtension) o; + tracker.unregisterObject(extension, ext); + extensions.remove(ext); + } + } + + public class AppearanceProviderExtension { + public String id; + public AppearanceProvider provider; + + public AppearanceProviderExtension(String id, AppearanceProvider provider) { + this.id = id; + this.provider = provider; + } + } +} diff --git a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceTools.java b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceTools.java index 9e506246..779e862b 100644 --- a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceTools.java +++ b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceTools.java @@ -21,7 +21,6 @@ import org.simantics.layer0.utils.StubLinkedList; import org.simantics.proconf.g3d.Resources; import org.simantics.proconf.g3d.stubs.Appearance; import org.simantics.proconf.g3d.stubs.Color; -import org.simantics.proconf.g3d.stubs.G3DResource; import org.simantics.proconf.g3d.stubs.ImageTexture; import org.simantics.proconf.g3d.stubs.Material; import org.simantics.proconf.g3d.stubs.MultiTexture; diff --git a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/DefaultAppearanceProvider.java b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/DefaultAppearanceProvider.java new file mode 100644 index 00000000..e6ea37fd --- /dev/null +++ b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/DefaultAppearanceProvider.java @@ -0,0 +1,29 @@ +package org.simantics.proconf.g3d.base; + +import java.util.Collection; + +import org.simantics.layer0.utils.IEntity; +import org.simantics.proconf.g3d.Resources; +import org.simantics.proconf.g3d.stubs.Appearance; + +import com.jme.renderer.Renderer; +import com.jme.scene.state.RenderState; + +public class DefaultAppearanceProvider implements AppearanceProvider { + + + + @Override + public boolean canHandle(IEntity instance) { + return instance.hasStatement(Resources.g3dResource.HasAppearance); + } + + @Override + public Collection getAppearanceFromResource(IEntity instance, Renderer renderer) { + Collection appearanceResource; + if ((appearanceResource = instance.getRelatedObjects(Resources.g3dResource.HasAppearance)) != null && appearanceResource.size() > 0) { + return AppearanceTools.getAppearance(new Appearance(instance.getGraph(),appearanceResource.iterator().next().getResource()), renderer); + } + throw new RuntimeException("Given instance does not have Appearance definition!"); + } +} diff --git a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/ResourceTextureCache.java b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/ResourceTextureCache.java index 33b18900..f29ddb47 100644 --- a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/ResourceTextureCache.java +++ b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/ResourceTextureCache.java @@ -18,7 +18,6 @@ import org.simantics.utils.ui.gfx.PixelDimension; import com.jme.image.Image; import com.jme.image.Texture; -import com.jme.util.TextureManager; /** * Caches resource based textures. @@ -26,6 +25,7 @@ import com.jme.util.TextureManager; * TODO : either use shared context or use separate cache for each editor * TODO : ShapeNode implementation won't use release texture yet * TODO : Texture is released when reference count goes to zero; we probably want to wait for a while before texture is released because it might be used again. + * TODO : Support for other types of textures (not just pattern texture, preferably extensible interface) * * @author Marko Luukkainen * @@ -53,19 +53,13 @@ public class ResourceTextureCache { ImageTexture it = new ImageTexture(g,res); org.simantics.image.stubs.Image pattern = it.getImage(); Image image = loadImage(g, pattern.getResource()); -// image.getData().rewind(); -// while (image.getData().hasRemaining()) -// image.getData().put((byte)(Math.random()*256.0 - 127.0)); + if (image == null) { return null; } t = new Texture(); t.setImage(image); - //t = TextureManager.loadTexture(image, Texture.MM_LINEAR, Texture.FM_LINEAR); -// t.setImageLocation(res.toString()); -// URL url = FileLocator.find(com.jme.eclipse.Activator.getDefault().getBundle(),new Path("data/texture/clouds.png"),null); -// t = TextureManager.loadTexture(url, Texture.MM_LINEAR, Texture.FM_LINEAR); t.setFilter(com.jme.image.Texture.FM_LINEAR); t.setMipmapState(com.jme.image.Texture.MM_LINEAR); @@ -167,7 +161,7 @@ public class ResourceTextureCache { return instance; } - private static Image getImage(ImageData imageData) { + public static Image getImage(ImageData imageData) { int width = imageData.width; int height = imageData.height; int type = 0; diff --git a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/scenegraph/ShapeNode.java b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/scenegraph/ShapeNode.java index 3369b5ea..d345b8b4 100644 --- a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/scenegraph/ShapeNode.java +++ b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/scenegraph/ShapeNode.java @@ -29,6 +29,8 @@ import org.simantics.proconf.g3d.animation.ConstantInterpolator; import org.simantics.proconf.g3d.animation.ScalarInterpolator; import org.simantics.proconf.g3d.animation.SlerpInterpolator; import org.simantics.proconf.g3d.animation.TCBInterpolator; +import org.simantics.proconf.g3d.base.AppearanceProvider; +import org.simantics.proconf.g3d.base.AppearanceProviderRegistry; import org.simantics.proconf.g3d.base.AppearanceTools; import org.simantics.proconf.g3d.base.G3DTools; import org.simantics.proconf.g3d.base.GeometryProvider; @@ -226,7 +228,7 @@ public class ShapeNode extends AbstractGraphicsNode implements Animatable, IGeom G3DNode shape = getG3DNode(graph); - if (renderStates == null) + //if (renderStates == null) updateAppearance(shape); if (isVisible()) { @@ -258,9 +260,9 @@ public class ShapeNode extends AbstractGraphicsNode implements Animatable, IGeom } protected void updateAppearance(IEntity shape) { - Collection appearanceResource; - if ((appearanceResource = shape.getRelatedObjects(Resources.g3dResource.HasAppearance)) != null && appearanceResource.size() > 0) { - renderStates = AppearanceTools.getAppearance(new Appearance(shape.getGraph(),appearanceResource.iterator().next().getResource()), editor.getRenderingComponent().getDisplaySystem().getRenderer()); + AppearanceProvider provider = AppearanceProviderRegistry.getAppearanceProvider(shape); + if (provider != null) { + renderStates = provider.getAppearanceFromResource(shape, editor.getRenderingComponent().getDisplaySystem().getRenderer()); } else { renderStates = getMaterial(); }