]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
git-svn-id: https://www.simantics.org/svn/simantics/3d/branches/dev@8613 ac1ea38d...
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 13 Jan 2009 13:34:59 +0000 (13:34 +0000)
committerluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 13 Jan 2009 13:34:59 +0000 (13:34 +0000)
org.simantics.proconf.g3d/plugin.xml
org.simantics.proconf.g3d/schema/appearance.exsd [new file with mode: 0644]
org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceProvider.java [new file with mode: 0644]
org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceProviderRegistry.java [new file with mode: 0644]
org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/AppearanceTools.java
org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/DefaultAppearanceProvider.java [new file with mode: 0644]
org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/ResourceTextureCache.java
org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/scenegraph/ShapeNode.java

index fc55861e501eb7572035af2e1bd8fbbbf40936fe..034ac76faf0fc29598918b00f4583662ddf57403 100644 (file)
@@ -2,6 +2,7 @@
 <?eclipse version="3.2"?>\r
 <plugin>\r
    <extension-point id="geometry" name="Geometry" schema="schema/geometry.exsd"/>\r
+   <extension-point id="appearance" name="Appearance" schema="schema/appearance.exsd"/>\r
    <extension\r
          point="org.eclipse.ui.preferencePages">\r
       <page\r
             type="org.eclipse.jface.viewers.IStructuredSelection">\r
       </propertyTester>\r
    </extension>\r
+   <extension\r
+         point="org.simantics.proconf.g3d.appearance">\r
+      <Appearance\r
+            class="org.simantics.proconf.g3d.base.DefaultAppearanceProvider"\r
+            id="org.simantics.proconf.g3d.Appearance1">\r
+      </Appearance>\r
+   </extension>\r
 </plugin>\r
diff --git a/org.simantics.proconf.g3d/schema/appearance.exsd b/org.simantics.proconf.g3d/schema/appearance.exsd
new file mode 100644 (file)
index 0000000..f5cd6a0
--- /dev/null
@@ -0,0 +1,109 @@
+<?xml version='1.0' encoding='UTF-8'?>\r
+<!-- Schema file written by PDE -->\r
+<schema targetNamespace="org.simantics.proconf.g3d" xmlns="http://www.w3.org/2001/XMLSchema">\r
+<annotation>\r
+      <appinfo>\r
+         <meta.schema plugin="org.simantics.proconf.g3d" id="appearance" name="appearance"/>\r
+      </appinfo>\r
+      <documentation>\r
+         [Enter description of this extension point.]\r
+      </documentation>\r
+   </annotation>\r
+\r
+   <element name="extension">\r
+      <annotation>\r
+         <appinfo>\r
+            <meta.element />\r
+         </appinfo>\r
+      </annotation>\r
+      <complexType>\r
+         <sequence>\r
+            <element ref="Appearance" minOccurs="0" maxOccurs="unbounded"/>\r
+         </sequence>\r
+         <attribute name="point" type="string" use="required">\r
+            <annotation>\r
+               <documentation>\r
+                  \r
+               </documentation>\r
+            </annotation>\r
+         </attribute>\r
+         <attribute name="id" type="string">\r
+            <annotation>\r
+               <documentation>\r
+                  \r
+               </documentation>\r
+            </annotation>\r
+         </attribute>\r
+         <attribute name="name" type="string">\r
+            <annotation>\r
+               <documentation>\r
+                  \r
+               </documentation>\r
+               <appinfo>\r
+                  <meta.attribute translatable="true"/>\r
+               </appinfo>\r
+            </annotation>\r
+         </attribute>\r
+      </complexType>\r
+   </element>\r
+\r
+   <element name="Appearance">\r
+      <complexType>\r
+         <attribute name="id" type="string" use="required">\r
+            <annotation>\r
+               <documentation>\r
+                  \r
+               </documentation>\r
+            </annotation>\r
+         </attribute>\r
+         <attribute name="class" type="string" use="required">\r
+            <annotation>\r
+               <documentation>\r
+                  \r
+               </documentation>\r
+               <appinfo>\r
+                  <meta.attribute kind="java" basedOn=":org.simantics.proconf.g3d.base.AppearanceProvider"/>\r
+               </appinfo>\r
+            </annotation>\r
+         </attribute>\r
+      </complexType>\r
+   </element>\r
+\r
+   <annotation>\r
+      <appinfo>\r
+         <meta.section type="since"/>\r
+      </appinfo>\r
+      <documentation>\r
+         [Enter the first release in which this extension point appears.]\r
+      </documentation>\r
+   </annotation>\r
+\r
+   <annotation>\r
+      <appinfo>\r
+         <meta.section type="examples"/>\r
+      </appinfo>\r
+      <documentation>\r
+         [Enter extension point usage example here.]\r
+      </documentation>\r
+   </annotation>\r
+\r
+   <annotation>\r
+      <appinfo>\r
+         <meta.section type="apiinfo"/>\r
+      </appinfo>\r
+      <documentation>\r
+         [Enter API information here.]\r
+      </documentation>\r
+   </annotation>\r
+\r
+   <annotation>\r
+      <appinfo>\r
+         <meta.section type="implementation"/>\r
+      </appinfo>\r
+      <documentation>\r
+         [Enter information about supplied implementation of this extension point.]\r
+      </documentation>\r
+   </annotation>\r
+\r
+\r
+</schema>\r
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 (file)
index 0000000..ff181c9
--- /dev/null
@@ -0,0 +1,46 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.base;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.layer0.utils.IEntity;\r
+\r
+import com.jme.renderer.Renderer;\r
+import com.jme.scene.state.RenderState;\r
+\r
+\r
+/**\r
+ * Interface for Appearance / Material\r
+ * \r
+ * \r
+ * @author Marko Luukkainen\r
+ *\r
+ */\r
+public interface AppearanceProvider {\r
+    \r
+       /**\r
+        * Used to check if GeometryProvider can generate mesh\r
+        * @param instance\r
+        * @return\r
+        */\r
+    public boolean canHandle(IEntity instance);\r
+       \r
+    /**\r
+     * Generates mesh\r
+     * @param instance\r
+     * @param transform\r
+     * @return\r
+     */\r
+    public Collection<RenderState> getAppearanceFromResource(IEntity instance, Renderer renderer);\r
+    \r
+\r
+}\r
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 (file)
index 0000000..6540cf5
--- /dev/null
@@ -0,0 +1,150 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.base;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.runtime.CoreException;\r
+import org.eclipse.core.runtime.IConfigurationElement;\r
+import org.eclipse.core.runtime.IExtension;\r
+import org.eclipse.core.runtime.IExtensionPoint;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;\r
+import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;\r
+import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;\r
+import org.eclipse.core.runtime.dynamichelpers.IFilter;\r
+import org.simantics.db.Resource;\r
+import org.simantics.layer0.utils.IEntity;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+import com.jme.renderer.Renderer;\r
+import com.jme.scene.state.RenderState;\r
+\r
+public class AppearanceProviderRegistry implements IExtensionChangeHandler {\r
+    private static AppearanceProviderRegistry instance;\r
+    //public final static String ELEMENT_NAME = "Appearance";\r
+    public final static String NAMESPACE = "org.simantics.proconf.g3d";\r
+    public final static String EP_NAME = "appearance";\r
+    \r
+    private ExtensionTracker tracker;\r
+    \r
+    private List<AppearanceProviderExtension> extensions = new ArrayList<AppearanceProviderExtension>();\r
+    private Map<Resource,AppearanceProvider> providers;\r
+    \r
+    private AppearanceProviderRegistry() {\r
+        providers = new HashMap<Resource,AppearanceProvider>();\r
+        \r
+        tracker = new ExtensionTracker();\r
+               \r
+               IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE,EP_NAME);\r
+               loadExtensions(ep.getConfigurationElements());\r
+               \r
+               IFilter filter = ExtensionTracker.createExtensionPointFilter(ep);\r
+               tracker.registerHandler(this, filter);\r
+    }\r
+    \r
+    private void close() {\r
+               tracker.close();\r
+               tracker = null;\r
+               extensions = new ArrayList<AppearanceProviderExtension>();\r
+       }\r
+    \r
+    public static synchronized AppearanceProviderRegistry getInstance() {\r
+        if (instance==null) instance = new AppearanceProviderRegistry();\r
+        return instance;\r
+    }\r
+    \r
+    public static synchronized void dispose() {\r
+               if (instance != null) {\r
+                       instance.close();\r
+                       instance = null;\r
+               }       \r
+       }\r
+    \r
+    public synchronized List<AppearanceProviderExtension> getExtensions() {\r
+               return Collections.unmodifiableList(extensions);\r
+       }\r
+    \r
+    private synchronized void loadExtensions(IConfigurationElement[] elements) {\r
+               for (IConfigurationElement el : elements) {\r
+                       String id = el.getAttribute("id");\r
+                       try {\r
+                               Object o = el.createExecutableExtension("class");\r
+                               AppearanceProviderExtension ext = new AppearanceProviderExtension(id,(AppearanceProvider)o);\r
+                               tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
+                               extensions.add(ext);\r
+                       } catch (CoreException e) {\r
+                               ErrorLogger.defaultLogError("Cannot create java interface for GeometryProvider " + id, e);\r
+                       }\r
+               }\r
+       }\r
+    \r
+    private Map<Resource,AppearanceProvider> getProviderMap() {\r
+        return providers;\r
+    }\r
+    \r
+    public static Collection<RenderState> getAppearance(IEntity thing, Renderer renderer) {\r
+       AppearanceProvider provider = getAppearanceProvider(thing);\r
+        return provider.getAppearanceFromResource(thing, renderer);\r
+    }\r
+    \r
+    public static AppearanceProvider getAppearanceProvider(IEntity thing) {\r
+       Collection<IEntity> types = thing.getRelatedObjects(thing.getGraph().getBuiltins().InstanceOf);\r
+        \r
+       //Resource types[] = OntologyUtils.getTypes(resource);\r
+       for (IEntity t : types) {\r
+               AppearanceProvider provider = getInstance().getProviderMap().get(t.getResource());\r
+               if (provider == null) {\r
+                //for (Extension<GeometryProvider> e : getInstance().getExtensions()) {\r
+                       for (AppearanceProviderExtension e : getInstance().getExtensions()) {\r
+                    if (e.provider.canHandle(thing)) {\r
+                        getInstance().getProviderMap().put(t.getResource(), e.provider);\r
+                        return e.provider;\r
+                    }\r
+                }\r
+                \r
+            } else {\r
+               return provider;\r
+            }\r
+       }\r
+       \r
+       return null;\r
+    }\r
+    \r
+    @Override\r
+    public void addExtension(IExtensionTracker tracker, IExtension extension) {\r
+        loadExtensions(extension.getConfigurationElements());\r
+    }\r
+    \r
+    @Override\r
+       public synchronized void removeExtension(IExtension extension, Object[] objects) {\r
+               for (Object o : objects) {\r
+                       AppearanceProviderExtension ext = (AppearanceProviderExtension) o;\r
+                       tracker.unregisterObject(extension, ext);\r
+                       extensions.remove(ext);\r
+               }\r
+       }\r
+    \r
+    public class AppearanceProviderExtension {\r
+               public String id;\r
+               public AppearanceProvider provider;\r
+               \r
+               public AppearanceProviderExtension(String id, AppearanceProvider provider) {\r
+                       this.id = id;\r
+                       this.provider = provider;\r
+               }\r
+       }\r
+}\r
index 9e50624624952ea735695a250d6bf87a96796b93..779e862bbee4284b9daaf30ca9c0f1a69d54db52 100644 (file)
@@ -21,7 +21,6 @@ import org.simantics.layer0.utils.StubLinkedList;
 import org.simantics.proconf.g3d.Resources;\r
 import org.simantics.proconf.g3d.stubs.Appearance;\r
 import org.simantics.proconf.g3d.stubs.Color;\r
-import org.simantics.proconf.g3d.stubs.G3DResource;\r
 import org.simantics.proconf.g3d.stubs.ImageTexture;\r
 import org.simantics.proconf.g3d.stubs.Material;\r
 import org.simantics.proconf.g3d.stubs.MultiTexture;\r
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 (file)
index 0000000..e6ea37f
--- /dev/null
@@ -0,0 +1,29 @@
+package org.simantics.proconf.g3d.base;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.layer0.utils.IEntity;\r
+import org.simantics.proconf.g3d.Resources;\r
+import org.simantics.proconf.g3d.stubs.Appearance;\r
+\r
+import com.jme.renderer.Renderer;\r
+import com.jme.scene.state.RenderState;\r
+\r
+public class DefaultAppearanceProvider implements AppearanceProvider {\r
+       \r
+       \r
+\r
+       @Override\r
+       public boolean canHandle(IEntity instance) {\r
+               return instance.hasStatement(Resources.g3dResource.HasAppearance);\r
+       }\r
+       \r
+       @Override\r
+       public Collection<RenderState> getAppearanceFromResource(IEntity instance, Renderer renderer) {\r
+               Collection<IEntity> appearanceResource;\r
+        if ((appearanceResource = instance.getRelatedObjects(Resources.g3dResource.HasAppearance)) != null && appearanceResource.size() > 0) {\r
+            return AppearanceTools.getAppearance(new Appearance(instance.getGraph(),appearanceResource.iterator().next().getResource()), renderer);\r
+        }\r
+        throw new RuntimeException("Given instance does not have Appearance definition!");\r
+       }\r
+}\r
index 33b189007ceb584c2deab511ffd1b3c08dae958b..f29ddb47da2cb5921dd4470b917b50b52bdbf740 100644 (file)
@@ -18,7 +18,6 @@ import org.simantics.utils.ui.gfx.PixelDimension;
 \r
 import com.jme.image.Image;\r
 import com.jme.image.Texture;\r
-import com.jme.util.TextureManager;\r
 \r
 /**\r
  * Caches resource based textures.\r
@@ -26,6 +25,7 @@ import com.jme.util.TextureManager;
  * TODO : either use shared context or use separate cache for each editor\r
  * TODO : ShapeNode implementation won't use release texture yet\r
  * 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.\r
+ * TODO : Support for other types of textures (not just pattern texture, preferably extensible interface)\r
  * \r
  * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>\r
  *\r
@@ -53,19 +53,13 @@ public class ResourceTextureCache {
                        ImageTexture it = new ImageTexture(g,res);\r
                        org.simantics.image.stubs.Image pattern = it.getImage();\r
                        Image image = loadImage(g, pattern.getResource());\r
-//                     image.getData().rewind();\r
-//                     while (image.getData().hasRemaining())\r
-//                             image.getData().put((byte)(Math.random()*256.0 - 127.0));\r
+\r
                        if (image == null) {\r
                                return null;\r
                        }\r
                        t = new Texture();\r
                        t.setImage(image);\r
                        \r
-                       //t = TextureManager.loadTexture(image, Texture.MM_LINEAR, Texture.FM_LINEAR);\r
-//                     t.setImageLocation(res.toString());\r
-//                     URL url = FileLocator.find(com.jme.eclipse.Activator.getDefault().getBundle(),new Path("data/texture/clouds.png"),null);\r
-//             t = TextureManager.loadTexture(url, Texture.MM_LINEAR, Texture.FM_LINEAR);\r
                \r
                        t.setFilter(com.jme.image.Texture.FM_LINEAR);\r
                t.setMipmapState(com.jme.image.Texture.MM_LINEAR);\r
@@ -167,7 +161,7 @@ public class ResourceTextureCache {
                return instance;\r
        }\r
        \r
-       private static Image getImage(ImageData imageData) {\r
+       public static Image getImage(ImageData imageData) {\r
        int width = imageData.width;\r
        int height = imageData.height;\r
        int type = 0;\r
index 3369b5ead95e7afacde03555ca0ac9b7e646e181..d345b8b416be11b73b7039b6c4bf3d0aa5e7ea1c 100644 (file)
@@ -29,6 +29,8 @@ import org.simantics.proconf.g3d.animation.ConstantInterpolator;
 import org.simantics.proconf.g3d.animation.ScalarInterpolator;\r
 import org.simantics.proconf.g3d.animation.SlerpInterpolator;\r
 import org.simantics.proconf.g3d.animation.TCBInterpolator;\r
+import org.simantics.proconf.g3d.base.AppearanceProvider;\r
+import org.simantics.proconf.g3d.base.AppearanceProviderRegistry;\r
 import org.simantics.proconf.g3d.base.AppearanceTools;\r
 import org.simantics.proconf.g3d.base.G3DTools;\r
 import org.simantics.proconf.g3d.base.GeometryProvider;\r
@@ -226,7 +228,7 @@ public class ShapeNode extends AbstractGraphicsNode implements Animatable, IGeom
 \r
             \r
             G3DNode shape = getG3DNode(graph);\r
-            if (renderStates == null)\r
+            //if (renderStates == null)\r
                updateAppearance(shape);\r
 \r
             if (isVisible()) {\r
@@ -258,9 +260,9 @@ public class ShapeNode extends AbstractGraphicsNode implements Animatable, IGeom
     }\r
     \r
     protected void updateAppearance(IEntity shape) {\r
-       Collection<IEntity> appearanceResource;\r
-        if ((appearanceResource = shape.getRelatedObjects(Resources.g3dResource.HasAppearance)) != null && appearanceResource.size() > 0) {\r
-            renderStates = AppearanceTools.getAppearance(new Appearance(shape.getGraph(),appearanceResource.iterator().next().getResource()), editor.getRenderingComponent().getDisplaySystem().getRenderer());\r
+       AppearanceProvider provider = AppearanceProviderRegistry.getAppearanceProvider(shape);\r
+        if (provider != null) {\r
+            renderStates = provider.getAppearanceFromResource(shape, editor.getRenderingComponent().getDisplaySystem().getRenderer());\r
         } else {\r
                renderStates = getMaterial();\r
         }\r