├── .gitignore ├── LICENSE ├── README.md ├── osgExport ├── BundleAnimation.cs ├── BundleBoxCollider.cs ├── BundleCamera.cs ├── BundleComponent.cs ├── BundleGameObject.cs ├── BundleLight.cs ├── BundleLightmap.cs ├── BundleMaterial.cs ├── BundleMesh.cs ├── BundleMeshCollider.cs ├── BundleMeshRenderer.cs ├── BundleObject.cs ├── BundleParticleSystem.cs ├── BundleResource.cs ├── BundleRigidBody.cs ├── BundleScene.cs ├── BundleShader.cs ├── BundleSkinnedMeshRenderer.cs ├── BundleTerrain.cs ├── BundleTexture.cs ├── BundleTimeOfDay.cs ├── BundleTransform.cs ├── ExportMesh.cs ├── ExportMeterial.cs ├── ExportParticle.cs ├── ExportParticle.cs.meta ├── ExportTerrain.cs ├── SceneDataClasses.cs ├── SceneError.cs └── SceneExporter.cs ├── osgExportHelpers ├── AnimationHelper.cs └── TimeOfDay.cs └── viewer ├── CMakeLists.txt ├── Default.frag ├── Default.vert ├── Legacy_Shaders └── Bumped_Diffuse.frag ├── Particle.cpp ├── Particle.h ├── Terrain.frag ├── Terrain.vert ├── builtin_shaders-5.4.0f3.zip ├── particle_data.cpp ├── shader_data.cpp ├── spark ├── Core │ ├── SPK_ArrayBuffer.h │ ├── SPK_Buffer.h │ ├── SPK_BufferHandler.cpp │ ├── SPK_BufferHandler.h │ ├── SPK_DEF.cpp │ ├── SPK_DEF.h │ ├── SPK_Emitter.cpp │ ├── SPK_Emitter.h │ ├── SPK_Factory.cpp │ ├── SPK_Factory.h │ ├── SPK_Group.cpp │ ├── SPK_Group.h │ ├── SPK_Interpolator.cpp │ ├── SPK_Interpolator.h │ ├── SPK_Model.cpp │ ├── SPK_Model.h │ ├── SPK_Modifier.cpp │ ├── SPK_Modifier.h │ ├── SPK_Particle.cpp │ ├── SPK_Particle.h │ ├── SPK_Pool.h │ ├── SPK_RegWrapper.h │ ├── SPK_Registerable.cpp │ ├── SPK_Registerable.h │ ├── SPK_Renderer.cpp │ ├── SPK_Renderer.h │ ├── SPK_System.cpp │ ├── SPK_System.h │ ├── SPK_Transformable.cpp │ ├── SPK_Transformable.h │ ├── SPK_Vector3D.cpp │ ├── SPK_Vector3D.h │ ├── SPK_Zone.cpp │ └── SPK_Zone.h ├── Extensions │ ├── Emitters │ │ ├── SPK_NormalEmitter.cpp │ │ ├── SPK_NormalEmitter.h │ │ ├── SPK_RandomEmitter.cpp │ │ ├── SPK_RandomEmitter.h │ │ ├── SPK_SphericEmitter.cpp │ │ ├── SPK_SphericEmitter.h │ │ ├── SPK_StaticEmitter.h │ │ ├── SPK_StraightEmitter.cpp │ │ └── SPK_StraightEmitter.h │ ├── Modifiers │ │ ├── SPK_Collision.cpp │ │ ├── SPK_Collision.h │ │ ├── SPK_Destroyer.cpp │ │ ├── SPK_Destroyer.h │ │ ├── SPK_LinearForce.cpp │ │ ├── SPK_LinearForce.h │ │ ├── SPK_ModifierGroup.cpp │ │ ├── SPK_ModifierGroup.h │ │ ├── SPK_Obstacle.cpp │ │ ├── SPK_Obstacle.h │ │ ├── SPK_PointMass.cpp │ │ ├── SPK_PointMass.h │ │ ├── SPK_Rotator.h │ │ ├── SPK_Vortex.cpp │ │ └── SPK_Vortex.h │ ├── Renderers │ │ ├── SPK_LineRendererInterface.h │ │ ├── SPK_Oriented2DRendererInterface.h │ │ ├── SPK_Oriented3DRendererInterface.cpp │ │ ├── SPK_Oriented3DRendererInterface.h │ │ ├── SPK_PointRendererInterface.h │ │ ├── SPK_QuadRendererInterface.cpp │ │ └── SPK_QuadRendererInterface.h │ └── Zones │ │ ├── SPK_AABox.cpp │ │ ├── SPK_AABox.h │ │ ├── SPK_Cylinder.cpp │ │ ├── SPK_Cylinder.h │ │ ├── SPK_Line.cpp │ │ ├── SPK_Line.h │ │ ├── SPK_Plane.cpp │ │ ├── SPK_Plane.h │ │ ├── SPK_Point.cpp │ │ ├── SPK_Point.h │ │ ├── SPK_Ring.cpp │ │ ├── SPK_Ring.h │ │ ├── SPK_Sphere.cpp │ │ ├── SPK_Sphere.h │ │ ├── SPK_ZoneIntersection.cpp │ │ ├── SPK_ZoneIntersection.h │ │ ├── SPK_ZoneUnion.cpp │ │ └── SPK_ZoneUnion.h ├── RenderingAPIs │ └── OpenGL │ │ ├── SPK_GLExtHandler.cpp │ │ ├── SPK_GLExtHandler.h │ │ ├── SPK_GLLineRenderer.cpp │ │ ├── SPK_GLLineRenderer.h │ │ ├── SPK_GLLineTrailRenderer.cpp │ │ ├── SPK_GLLineTrailRenderer.h │ │ ├── SPK_GLPointRenderer.cpp │ │ ├── SPK_GLPointRenderer.h │ │ ├── SPK_GLQuadRenderer.cpp │ │ ├── SPK_GLQuadRenderer.h │ │ ├── SPK_GLRenderer.cpp │ │ ├── SPK_GLRenderer.h │ │ └── SPK_GL_DEF.h ├── SPK.h ├── SPK_All.cpp ├── SPK_GL.h └── SPK_GL_All.cpp ├── stb_image.h ├── terrain_data.cpp ├── user_data_classes.h ├── utilities.cpp └── viewer.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Rui Wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityToOSG 2 | Convert Unity scene to OpenSceneGraph format 3 | 4 | ## Usage 5 | Copy osgExport and osgExportHelpers to your Unity project folder\Assets 6 | You will find a new "nwTools" menu at the top and use menu items to export .osg files! 7 | Note that the texture paths are always relative to Unity project folder, so you may have to redirect them 8 | 9 | ## TODO List 10 | 1. Support multiple texture maps (done) 11 | 2. Support lightmaps and lightprobes (partly) 12 | 3. Use specified viewer (with a default shader) to show scene (done) 13 | 4. Support light and camera data outputs (partly) 14 | 5. Make use of dynamic lights from Forward+ pass 15 | 6. Support game object and camera animations 16 | 7. Support character data and animation outputs 17 | 8. Play kinds of animations in viewer 18 | 9. Support mesh collider data outputs (partly) 19 | 10. Load and use colliders in physics engine integrations 20 | 11. Support particle animations 21 | 12. Support Unity legacy shaders and reimplement them in OSG (partly) 22 | 13. Support terrains and plants (partly) 23 | 14. Support Unity 5.x standard PBR shader 24 | -------------------------------------------------------------------------------- /osgExport/BundleBoxCollider.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | namespace nwTools 6 | { 7 | 8 | public class BundleBoxCollider : BundleComponent 9 | { 10 | override public void Preprocess() 11 | { 12 | unityBoxCollider = unityComponent as BoxCollider; 13 | } 14 | 15 | override public void QueryResources() 16 | { 17 | } 18 | 19 | new public static void Reset() 20 | { 21 | } 22 | 23 | public override SceneComponent GetObjectData() 24 | { 25 | var sceneData = new SceneBoxCollider(); 26 | sceneData.type = "BoxCollider"; 27 | sceneData.size = unityBoxCollider.size; 28 | sceneData.center = unityBoxCollider.center; 29 | return sceneData; 30 | } 31 | 32 | BoxCollider unityBoxCollider; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /osgExport/BundleCamera.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleCamera : BundleComponent 10 | { 11 | override public void Preprocess() 12 | { 13 | unityCamera = unityComponent as Camera; 14 | } 15 | 16 | new public static void Reset() 17 | { 18 | } 19 | 20 | public override SceneComponent GetObjectData() 21 | { 22 | var sceneData = new SceneCamera(); 23 | sceneData.type = "Camera"; 24 | return sceneData; 25 | } 26 | 27 | Camera unityCamera; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /osgExport/BundleComponent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleComponent : BundleObject 10 | { 11 | virtual public void QueryResources() 12 | { 13 | } 14 | 15 | virtual public void Preprocess() 16 | { 17 | } 18 | 19 | virtual public void Process() 20 | { 21 | } 22 | 23 | virtual public void PostProcess() 24 | { 25 | } 26 | 27 | public virtual new SceneComponent GetObjectData() 28 | { 29 | throw new NotImplementedException("Attempting to call GetObjectData"); 30 | } 31 | 32 | public static void Reset() 33 | { 34 | BundleTransform.Reset(); 35 | BundleMeshRenderer.Reset(); 36 | conversions = new Dictionary(); 37 | } 38 | 39 | public static void QueryComponents( BundleGameObject jgo ) 40 | { 41 | // for every registered conversion get that component 42 | bool isQueried = false; 43 | foreach( KeyValuePair pair in conversions ) 44 | { 45 | Component[] components = jgo.unityGameObject.GetComponents(pair.Key); 46 | if ( components.Length>0 ) isQueried = true; 47 | 48 | foreach ( Component component in components ) 49 | { 50 | MeshRenderer meshRenderer = component as MeshRenderer; 51 | if ( meshRenderer!=null && !meshRenderer.enabled ) continue; 52 | 53 | var jcomponent = Activator.CreateInstance(pair.Value) as BundleComponent; 54 | if ( jcomponent==null ) 55 | ExportError.FatalError("Export component creation failed"); 56 | 57 | jcomponent.unityComponent = component; 58 | jcomponent.jeGameObject = jgo; 59 | jgo.AddComponent( jcomponent ); 60 | } 61 | } 62 | 63 | if ( !isQueried ) 64 | Debug.LogWarning( "Unregistered game object " + jgo.unityGameObject.name ); 65 | } 66 | 67 | public static void RegisterConversion( Type componentType, Type exportType ) 68 | { 69 | conversions[componentType] = exportType; 70 | } 71 | 72 | public static void RegisterStandardComponents() 73 | { 74 | RegisterConversion(typeof(Transform), typeof(BundleTransform)); 75 | RegisterConversion(typeof(Camera), typeof(BundleCamera)); 76 | RegisterConversion(typeof(MeshRenderer), typeof(BundleMeshRenderer)); 77 | RegisterConversion(typeof(SkinnedMeshRenderer), typeof(BundleSkinnedMeshRenderer)); 78 | RegisterConversion(typeof(Terrain), typeof(BundleTerrain)); 79 | RegisterConversion(typeof(AnimationHelper), typeof(BundleAnimation)); 80 | RegisterConversion(typeof(BoxCollider), typeof(BundleBoxCollider)); 81 | RegisterConversion(typeof(MeshCollider), typeof(BundleMeshCollider)); 82 | RegisterConversion(typeof(Rigidbody), typeof(BundleRigidBody)); 83 | RegisterConversion(typeof(Light), typeof(BundleLight)); 84 | RegisterConversion(typeof(ParticleSystem), typeof(BundleParticleSystem)); 85 | RegisterConversion(typeof(TimeOfDay), typeof(BundleTimeOfDay)); 86 | } 87 | 88 | static Dictionary conversions; 89 | public Component unityComponent; 90 | public BundleGameObject jeGameObject; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /osgExport/BundleGameObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleGameObject : BundleObject 10 | { 11 | 12 | public void AddComponent( BundleComponent component ) 13 | { 14 | components.Add(component); 15 | } 16 | 17 | public BundleGameObject( GameObject go, BundleGameObject parent ) 18 | { 19 | allGameObjectLookup[go] = this; 20 | this.unityGameObject = go; 21 | this.parent = parent; 22 | this.name = go.name; 23 | 24 | if ( parent!=null ) 25 | parent.children.Add( this ); 26 | BundleComponent.QueryComponents(this); 27 | } 28 | 29 | // first pass is preprocess 30 | public void Preprocess() 31 | { 32 | foreach ( var component in components ) 33 | component.Preprocess(); 34 | foreach ( var child in children ) 35 | child.Preprocess(); 36 | } 37 | 38 | // next we query resources 39 | public void QueryResources() 40 | { 41 | foreach ( var component in components ) 42 | component.QueryResources(); 43 | foreach ( var child in children ) 44 | child.QueryResources(); 45 | } 46 | 47 | public void Process() 48 | { 49 | foreach ( var component in components ) 50 | component.Process(); 51 | foreach ( var child in children ) 52 | child.Process(); 53 | } 54 | 55 | public void PostProcess() 56 | { 57 | foreach ( var component in components ) 58 | component.PostProcess(); 59 | foreach ( var child in children ) 60 | child.PostProcess(); 61 | } 62 | 63 | public static void Reset() 64 | { 65 | allGameObjectLookup = new Dictionary(); 66 | } 67 | 68 | public new SceneGameObject GetObjectData() 69 | { 70 | SceneGameObject sceneData = new SceneGameObject(); 71 | sceneData.name = name; 72 | sceneData.children = new List(); 73 | foreach ( var child in children ) 74 | sceneData.children.Add( child.GetObjectData() ); 75 | 76 | sceneData.components = new List(); 77 | foreach ( var component in components ) 78 | sceneData.components.Add( component.GetObjectData() ); 79 | return sceneData; 80 | } 81 | 82 | public BundleTransform transform; 83 | public BundleGameObject parent; 84 | public List children = new List(); 85 | public List components = new List(); 86 | public GameObject unityGameObject; 87 | public static Dictionary allGameObjectLookup; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /osgExport/BundleLight.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | namespace nwTools 6 | { 7 | 8 | public class BundleLight : BundleComponent 9 | { 10 | override public void Preprocess() 11 | { 12 | unityLight = unityComponent as Light; 13 | } 14 | 15 | override public void QueryResources() 16 | { 17 | } 18 | 19 | new public static void Reset() 20 | { 21 | } 22 | 23 | public override SceneComponent GetObjectData() 24 | { 25 | var sceneData = new SceneLight(); 26 | sceneData.type = "Light"; 27 | sceneData.lightType = "Point"; 28 | sceneData.color = unityLight.color; 29 | sceneData.range = unityLight.range; 30 | sceneData.castsShadows = (unityLight.shadows != LightShadows.None); 31 | sceneData.realtime = true; 32 | 33 | SerializedObject serial = new SerializedObject(unityLight); 34 | SerializedProperty lightmapProp = serial.FindProperty("m_Lightmapping"); 35 | if ( lightmapProp.intValue!=0 ) 36 | { 37 | // not a realtime light 38 | sceneData.realtime = false; 39 | } 40 | return sceneData; 41 | } 42 | 43 | Light unityLight; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /osgExport/BundleLightmap.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleLightmap : BundleResource 10 | { 11 | void preprocess() 12 | { 13 | } 14 | 15 | void process() 16 | { 17 | } 18 | 19 | void postprocess() 20 | { 21 | } 22 | 23 | // TODO: we also want to copy the exr data in case user wants it 24 | new public static void Preprocess() 25 | { 26 | LightmapData[] lightmaps = LightmapSettings.lightmaps; 27 | for ( int i=0; i(); 89 | } 90 | 91 | public new SceneTexture GetObjectData() 92 | { 93 | return null; 94 | } 95 | 96 | public static List GenerateObjectList() 97 | { 98 | List lightmaps = new List(); 99 | for ( int i=0; i allLightmaps = new List(); 114 | public string filename; 115 | public string base64PNG; 116 | public int base64PNGLength; 117 | } 118 | 119 | } 120 | 121 | // 122 | -------------------------------------------------------------------------------- /osgExport/BundleMesh.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | using UnityEditor; 6 | 7 | namespace nwTools 8 | { 9 | 10 | public class BundleMesh : BundleResource 11 | { 12 | private BundleMesh( Mesh mesh ) 13 | { 14 | unityMesh = mesh; 15 | index = allMeshes.Count; 16 | allMeshes[mesh] = this; 17 | 18 | // we can have duplicates with same name, referenced by index 19 | //#name = mesh.name; 20 | string path = AssetDatabase.GetAssetPath(mesh); 21 | path = Path.GetFileNameWithoutExtension(path); 22 | name = path + "_" + mesh.name; 23 | 24 | // TODO: handle bones 25 | } 26 | 27 | void preprocess() 28 | { 29 | } 30 | 31 | void process() 32 | { 33 | } 34 | 35 | void postprocess() 36 | { 37 | } 38 | 39 | new public static void Preprocess() 40 | { 41 | foreach ( var mesh in allMeshes.Values ) 42 | { 43 | mesh.preprocess(); 44 | } 45 | } 46 | 47 | new public static void Process() 48 | { 49 | foreach ( var mesh in allMeshes.Values ) 50 | { 51 | mesh.process(); 52 | } 53 | } 54 | 55 | new public static void PostProcess() 56 | { 57 | foreach ( var mesh in allMeshes.Values ) 58 | { 59 | mesh.postprocess(); 60 | } 61 | } 62 | 63 | public static BundleMesh RegisterMesh( Mesh mesh ) 64 | { 65 | if ( allMeshes.ContainsKey(mesh) ) 66 | return allMeshes[mesh]; 67 | return new BundleMesh(mesh); 68 | } 69 | 70 | new public static void Reset() 71 | { 72 | } 73 | 74 | public new SceneMesh GetObjectData() 75 | { 76 | var sceneData = new SceneMesh(); 77 | sceneData.name = name; 78 | 79 | // submeshes 80 | sceneData.subMeshCount = unityMesh.subMeshCount; 81 | sceneData.triangles = new int[unityMesh.subMeshCount][]; 82 | for ( int i=0; i GenerateObjectList() 134 | { 135 | List meshes = new List(); 136 | foreach ( var mesh in allMeshes.Values ) 137 | meshes.Add( mesh.GetObjectData() ); 138 | return meshes; 139 | } 140 | 141 | public static Dictionary allMeshes = new Dictionary(); 142 | public Mesh unityMesh; 143 | public int index; 144 | public BoneWeight[] boneWeights; 145 | 146 | // set by skinned renderer if any 147 | public Transform[] bones; 148 | public String rootBone = ""; 149 | } 150 | 151 | } 152 | -------------------------------------------------------------------------------- /osgExport/BundleMeshCollider.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | namespace nwTools 6 | { 7 | 8 | public class BundleMeshCollider : BundleComponent 9 | { 10 | override public void Preprocess() 11 | { 12 | unityMeshCollider = unityComponent as MeshCollider; 13 | } 14 | 15 | override public void QueryResources() 16 | { 17 | } 18 | 19 | new public static void Reset() 20 | { 21 | } 22 | 23 | public override SceneComponent GetObjectData() 24 | { 25 | var sceneData = new SceneMeshCollider(); 26 | sceneData.type = "MeshCollider"; 27 | if ( unityMeshCollider!=null ) 28 | { 29 | Mesh unityMesh = unityMeshCollider.sharedMesh; 30 | if ( unityMesh!=null ) 31 | { 32 | sceneData.mesh = new SceneMesh(); 33 | sceneData.mesh.name = unityMesh.name; 34 | 35 | // submeshes 36 | sceneData.mesh.subMeshCount = unityMesh.subMeshCount; 37 | sceneData.mesh.triangles = new int[unityMesh.subMeshCount][]; 38 | for ( int i=0; i(); 14 | if ( unityMeshFilter==null ) 15 | { 16 | ExportError.FatalError( "MeshRenderer with no MeshFilter" ); 17 | } 18 | } 19 | 20 | override public void QueryResources() 21 | { 22 | mesh = BundleMesh.RegisterMesh( unityMeshFilter.sharedMesh ); 23 | for ( int i=0; i materials = new List(); 54 | MeshRenderer unityMeshRenderer; 55 | MeshFilter unityMeshFilter; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /osgExport/BundleObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleObject 10 | { 11 | public object GetObjectData() 12 | { 13 | throw new NotImplementedException("Attempting to call GetObjectData"); 14 | } 15 | 16 | public string name; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /osgExport/BundleResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleResource : BundleObject 10 | { 11 | public static void Reset() 12 | { 13 | BundleMesh.Reset(); 14 | BundleMaterial.Reset(); 15 | BundleTexture.Reset(); 16 | BundleShader.Reset(); 17 | BundleLightmap.Reset(); 18 | } 19 | 20 | public static void Preprocess() 21 | { 22 | BundleTexture.Preprocess(); 23 | BundleShader.Preprocess(); 24 | BundleMaterial.Preprocess(); 25 | BundleLightmap.Preprocess(); 26 | BundleMesh.Preprocess(); 27 | } 28 | 29 | public static void Process() 30 | { 31 | BundleTexture.Process(); 32 | BundleShader.Process(); 33 | BundleMaterial.Process(); 34 | BundleLightmap.Process(); 35 | BundleMesh.Process(); 36 | } 37 | 38 | public static void PostProcess() 39 | { 40 | BundleTexture.PostProcess(); 41 | BundleShader.PostProcess(); 42 | BundleMaterial.PostProcess(); 43 | BundleLightmap.PostProcess(); 44 | BundleMesh.PostProcess(); 45 | } 46 | 47 | public static new SceneResources GetObjectData() 48 | { 49 | var sceneData = new SceneResources(); 50 | sceneData.textures = BundleTexture.GenerateObjectList(); 51 | sceneData.lightmaps = BundleLightmap.GenerateObjectList(); 52 | sceneData.shaders = BundleShader.GenerateObjectList(); 53 | sceneData.materials = BundleMaterial.GenerateObjectList(); 54 | sceneData.meshes = BundleMesh.GenerateObjectList(); 55 | return sceneData; 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /osgExport/BundleRigidBody.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | namespace nwTools 6 | { 7 | 8 | public class BundleRigidBody : BundleComponent 9 | { 10 | override public void Preprocess() 11 | { 12 | unityRigidBody = unityComponent as Rigidbody; 13 | } 14 | 15 | override public void QueryResources() 16 | { 17 | } 18 | 19 | new public static void Reset() 20 | { 21 | } 22 | 23 | public override SceneComponent GetObjectData() 24 | { 25 | var sceneData = new SceneRigidBody(); 26 | sceneData.type = "RigidBody"; 27 | sceneData.mass = unityRigidBody.mass; 28 | return sceneData; 29 | } 30 | 31 | Rigidbody unityRigidBody; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /osgExport/BundleScene.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleScene : BundleObject 10 | { 11 | public static BundleScene TraverseScene( bool onlySelected ) 12 | { 13 | var scene = new BundleScene(); 14 | List root = new List(); 15 | 16 | if ( onlySelected ) 17 | { 18 | object[] objects = Selection.objects; 19 | foreach ( object o in objects ) 20 | { 21 | GameObject go = o as GameObject; 22 | if ( go!=null ) root.Add( go ); 23 | } 24 | } 25 | else 26 | { 27 | // Unity has no root object, so collect root game objects this way 28 | object[] objects = GameObject.FindObjectsOfType( typeof(GameObject) ); 29 | foreach ( object o in objects ) 30 | { 31 | GameObject go = (GameObject)o; 32 | if ( go.transform.parent==null ) root.Add( go ); 33 | } 34 | } 35 | 36 | if ( root.Count==0 ) 37 | { 38 | ExportError.FatalError("Cannot Export Empty Scene"); 39 | } 40 | 41 | // traverse the "root" game objects, collecting child game objects and components 42 | Debug.Log( root.Count + " root game objects to export" ); 43 | foreach ( var go in root ) 44 | { 45 | scene.rootGameObjects.Add( Traverse(go) ); 46 | } 47 | return scene; 48 | } 49 | 50 | public void Preprocess() 51 | { 52 | foreach( var jgo in rootGameObjects ) 53 | jgo.Preprocess(); 54 | foreach( var jgo in rootGameObjects ) 55 | jgo.QueryResources(); // discover resources 56 | BundleResource.Preprocess(); 57 | } 58 | 59 | public void Process() 60 | { 61 | BundleResource.Process(); 62 | foreach(var jgo in rootGameObjects) 63 | jgo.Process(); 64 | } 65 | 66 | public void PostProcess() 67 | { 68 | BundleResource.PostProcess(); 69 | foreach(var jgo in rootGameObjects) 70 | jgo.PostProcess(); 71 | } 72 | 73 | public static void Reset() 74 | { 75 | } 76 | 77 | static BundleGameObject Traverse( GameObject obj, BundleGameObject jparent=null ) 78 | { 79 | BundleGameObject jgo = new BundleGameObject(obj, jparent); 80 | foreach ( Transform child in obj.transform ) 81 | { 82 | Traverse( child.gameObject, jgo ); 83 | } 84 | return jgo; 85 | } 86 | 87 | public SceneData GetSceneData() 88 | { 89 | var sceneData = new SceneData(); 90 | sceneData.name = sceneName; 91 | sceneData.resources = BundleResource.GetObjectData(); 92 | sceneData.hierarchy = new List(); 93 | 94 | foreach ( var go in rootGameObjects ) 95 | sceneData.hierarchy.Add( go.GetObjectData() ); 96 | return sceneData; 97 | } 98 | 99 | public static string sceneName; 100 | private List rootGameObjects = new List(); 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /osgExport/BundleShader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleShader : BundleResource 10 | { 11 | private BundleShader( Shader shader ) 12 | { 13 | this.unityShader = shader; 14 | this.name = shader.name; 15 | allShaders[shader] = this; 16 | } 17 | 18 | public static BundleShader RegisterShader( Shader shader ) 19 | { 20 | if ( allShaders.ContainsKey(shader) ) 21 | return allShaders[shader]; 22 | return new BundleShader(shader); 23 | } 24 | 25 | void preprocess() 26 | { 27 | //Debug.Log("preprocess - " + unityShader); 28 | } 29 | 30 | void process() 31 | { 32 | //Debug.Log("process - " + unityShader); 33 | } 34 | 35 | void postprocess() 36 | { 37 | //Debug.Log("postprocess - " + unityShader); 38 | } 39 | 40 | new public static void Preprocess() 41 | { 42 | foreach ( var shader in allShaders.Values ) 43 | { 44 | shader.preprocess(); 45 | } 46 | } 47 | 48 | new public static void Process() 49 | { 50 | foreach ( var shader in allShaders.Values ) 51 | { 52 | shader.process(); 53 | } 54 | } 55 | 56 | new public static void PostProcess() 57 | { 58 | foreach ( var shader in allShaders.Values ) 59 | { 60 | shader.postprocess(); 61 | } 62 | } 63 | 64 | new public static void Reset() 65 | { 66 | allShaders = new Dictionary(); 67 | } 68 | 69 | public new SceneShader GetObjectData() 70 | { 71 | var sceneData = new SceneShader(); 72 | sceneData.name = name; 73 | sceneData.renderQueue = unityShader.renderQueue; 74 | return sceneData; 75 | } 76 | 77 | public static List GenerateObjectList() 78 | { 79 | List shaders = new List(); 80 | foreach ( var shader in allShaders.Values ) 81 | shaders.Add( shader.GetObjectData() ); 82 | return shaders; 83 | } 84 | 85 | public static Dictionary allShaders; 86 | Shader unityShader; 87 | } 88 | 89 | 90 | } 91 | -------------------------------------------------------------------------------- /osgExport/BundleSkinnedMeshRenderer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | namespace nwTools 6 | { 7 | 8 | public class BundleSkinnedMeshRenderer : BundleComponent 9 | { 10 | override public void Preprocess() 11 | { 12 | unityMeshRenderer = unityComponent as SkinnedMeshRenderer; 13 | } 14 | 15 | override public void QueryResources() 16 | { 17 | mesh = BundleMesh.RegisterMesh( unityMeshRenderer.sharedMesh ); 18 | mesh.bones = unityMeshRenderer.bones; 19 | mesh.rootBone = (unityMeshRenderer.rootBone==null) ? "" : unityMeshRenderer.rootBone.gameObject.name; 20 | for ( int i=0; i materials = new List(); 51 | SkinnedMeshRenderer unityMeshRenderer; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /osgExport/BundleTexture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.IO.Compression; 4 | using System.Collections.Generic; 5 | using UnityEngine; 6 | using UnityEditor; 7 | 8 | namespace nwTools 9 | { 10 | 11 | public class BundleTexture : BundleResource 12 | { 13 | public static int GetSuggestedUnit( string propName ) 14 | { 15 | int suggestedUnit = -1; 16 | if ( propName=="_MainTex" ) suggestedUnit = 0; 17 | //else if ( propName=="_LightMap" ) suggestedUnit = 1; 18 | else if ( propName=="_BumpMap" ) suggestedUnit = 2; 19 | else if ( propName=="_Illum" ) suggestedUnit = 3; 20 | else if ( propName=="_Specular" ) suggestedUnit = 4; 21 | //else if ( propName=="_Cube" ) suggestedUnit = 5; 22 | else Debug.LogWarning("Unassiagned texture property: " + propName); 23 | return suggestedUnit; 24 | } 25 | 26 | private BundleTexture( Texture texture, string propName ) 27 | { 28 | this.unityTexture = texture as Texture2D; 29 | allTextures[texture] = this; 30 | name = propName + ": " + texture.name; 31 | } 32 | 33 | void preprocess() 34 | { 35 | //Debug.Log("preprocess - " + unityTexture); 36 | } 37 | 38 | void process() 39 | { 40 | //Debug.Log("process - " + unityTexture); 41 | if ( unityTexture!=null ) 42 | { 43 | path = AssetDatabase.GetAssetPath(unityTexture); 44 | uniqueID = unityTexture.GetInstanceID(); 45 | } 46 | 47 | /* 48 | TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; 49 | if ( textureImporter.isReadable==false ) 50 | { 51 | textureImporter.isReadable = true; 52 | AssetDatabase.ImportAsset( path ); 53 | } 54 | 55 | Texture2D ntexture = new Texture2D(unityTexture.width, unityTexture.height, TextureFormat.ARGB32, false); 56 | ntexture.SetPixels32( unityTexture.GetPixels32() ); 57 | ntexture.Apply(); 58 | 59 | var bytes = ntexture.EncodeToPNG(); 60 | base64PNGLength = bytes.Length; 61 | base64PNG = System.Convert.ToBase64String(bytes, 0, bytes.Length); 62 | UnityEngine.Object.DestroyImmediate(ntexture); 63 | */ 64 | } 65 | 66 | void postprocess() 67 | { 68 | //Debug.Log("postprocess - " + unityTexture); 69 | } 70 | 71 | public static BundleTexture RegisterTexture( Texture texture, string propName ) 72 | { 73 | if ( allTextures.ContainsKey(texture) ) 74 | return allTextures[texture]; 75 | return new BundleTexture(texture, propName); 76 | } 77 | 78 | new public static void Preprocess() 79 | { 80 | foreach ( var texture in allTextures.Values ) 81 | { 82 | texture.preprocess(); 83 | } 84 | } 85 | 86 | new public static void Process() 87 | { 88 | foreach ( var texture in allTextures.Values ) 89 | { 90 | texture.process(); 91 | } 92 | } 93 | 94 | new public static void PostProcess() 95 | { 96 | foreach ( var texture in allTextures.Values ) 97 | { 98 | texture.postprocess(); 99 | } 100 | } 101 | 102 | new public static void Reset() 103 | { 104 | allTextures = new Dictionary(); 105 | } 106 | 107 | public new SceneTexture GetObjectData() 108 | { 109 | var sceneData = new SceneTexture(); 110 | sceneData.uniqueID = uniqueID; 111 | sceneData.name = name; 112 | sceneData.path = path; 113 | //sceneData.base64PNG = base64PNG; 114 | //sceneData.base64PNGLength = base64PNGLength; 115 | return sceneData; 116 | } 117 | 118 | public static List GenerateObjectList() 119 | { 120 | List textures = new List(); 121 | foreach ( var texture in allTextures.Values ) 122 | textures.Add( texture.GetObjectData() ); 123 | return textures; 124 | } 125 | 126 | Texture2D unityTexture; 127 | public new string name; 128 | public string path; 129 | public string base64PNG; 130 | public int uniqueID; 131 | public int base64PNGLength; 132 | public static Dictionary allTextures; 133 | } 134 | 135 | 136 | } 137 | -------------------------------------------------------------------------------- /osgExport/BundleTimeOfDay.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | namespace nwTools 6 | { 7 | 8 | public class BundleTimeOfDay : BundleComponent 9 | { 10 | override public void Preprocess() 11 | { 12 | unityTimeOfDay = unityComponent as TimeOfDay; 13 | } 14 | 15 | override public void QueryResources() 16 | { 17 | } 18 | 19 | new public static void Reset() 20 | { 21 | } 22 | 23 | public override SceneComponent GetObjectData() 24 | { 25 | var sceneData = new SceneTimeOfDay(); 26 | sceneData.type = "TimeOfDay"; 27 | sceneData.timeOn = unityTimeOfDay.TimeOn; 28 | sceneData.timeOff = unityTimeOfDay.TimeOff; 29 | return sceneData; 30 | } 31 | 32 | TimeOfDay unityTimeOfDay; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /osgExport/BundleTransform.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | public class BundleTransform : BundleComponent 10 | { 11 | override public void Preprocess() 12 | { 13 | unityTransform = unityComponent as Transform; 14 | } 15 | 16 | new public static void Reset() 17 | { 18 | } 19 | 20 | public override SceneComponent GetObjectData() 21 | { 22 | var sceneData = new SceneTransform(); 23 | sceneData.type = "Transform"; 24 | if ( unityTransform!=null ) 25 | { 26 | sceneData.localPosition = unityTransform.localPosition; 27 | sceneData.localRotation = unityTransform.localRotation; 28 | sceneData.localScale = unityTransform.localScale; 29 | } 30 | return sceneData; 31 | } 32 | 33 | Transform unityTransform; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /osgExport/ExportMesh.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | using UnityEditor; 6 | using UnityEditor.SceneManagement; 7 | 8 | namespace nwTools 9 | { 10 | 11 | public class MeshExporter 12 | { 13 | public static string ExportGeometry( ref SceneData sceneData, ref SceneMeshRenderer smr, 14 | ref SceneMesh mesh, string spaces ) 15 | { 16 | string osgData = spaces + "useDisplayList TRUE\n" 17 | + spaces + "useVertexBufferObjects FALSE\n"; 18 | if ( smr!=null ) 19 | osgData += MaterialExporter.ExportStateSet( ref sceneData, ref smr, spaces ); 20 | osgData += ExportGeometryData( ref sceneData, ref mesh, spaces ); 21 | return osgData; 22 | } 23 | 24 | public static string ExportSkinnedGeometry( ref SceneData sceneData, ref SceneSkinnedMeshRenderer smr, 25 | ref SceneMesh mesh, string spaces ) 26 | { 27 | string osgData = spaces + "useDisplayList TRUE\n" 28 | + spaces + "useVertexBufferObjects FALSE\n"; 29 | if ( smr!=null ) 30 | { 31 | SceneMeshRenderer smr0 = (SceneMeshRenderer)smr; 32 | osgData += MaterialExporter.ExportStateSet( ref sceneData, ref smr0, spaces ); 33 | } 34 | osgData += ExportGeometryData( ref sceneData, ref mesh, spaces ); 35 | return osgData; 36 | } 37 | 38 | private static string ExportGeometryData( ref SceneData sceneData, ref SceneMesh mesh, string spaces ) 39 | { 40 | // Add all primitive sets 41 | string osgData = spaces + "PrimitiveSets " + mesh.subMeshCount + " {\n"; 42 | for ( int i=0; i0 ) 67 | { 68 | osgData += spaces + "NormalBinding PER_VERTEX\n" 69 | + spaces + "NormalArray Vec3Array " + mesh.vertexCount + " {\n"; 70 | for ( int i=0; i0 ) 80 | { 81 | osgData += spaces + "TexCoordArray 0 Vec2Array " + mesh.vertexCount + " {\n"; 82 | for ( int i=0; i0 ) 91 | { 92 | osgData += spaces + "TexCoordArray 1 Vec2Array " + mesh.vertexCount + " {\n"; 93 | for ( int i=0; i=0 ) 71 | { 72 | SceneTexture texture = sceneData.resources.lightmaps[st.lightmapIndex]; 73 | if ( texture!=null ) 74 | { 75 | osgData += spaces + "Lightmap \"" + texture.name + "\"" 76 | + " \"" + texture.path + "\"\n"; 77 | osgData += spaces + "LightmapTilingOffset " + st.lightmapTilingOffset.x + " " 78 | + st.lightmapTilingOffset.y + " " + st.lightmapTilingOffset.z + " " 79 | + st.lightmapTilingOffset.w + "\n"; 80 | } 81 | } 82 | return osgData; 83 | } 84 | 85 | public static void Reset() 86 | { 87 | } 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /osgExport/SceneError.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEditor; 5 | 6 | namespace nwTools 7 | { 8 | 9 | static class ExportError 10 | { 11 | public static void FatalError( string message ) 12 | { 13 | EditorUtility.ClearProgressBar(); 14 | EditorUtility.DisplayDialog( "Error", message, "Ok" ); 15 | throw new Exception(message); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /osgExportHelpers/AnimationHelper.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class AnimationHelper : MonoBehaviour 5 | { 6 | public AnimationClip[] animationClips; 7 | } 8 | -------------------------------------------------------------------------------- /osgExportHelpers/TimeOfDay.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class TimeOfDay : MonoBehaviour 5 | { 6 | public float TimeOn; 7 | public float TimeOff; 8 | 9 | // Use this for initialization 10 | void Start() 11 | { 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /viewer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(UnityToOSG) 2 | 3 | CMAKE_MINIMUM_REQUIRED(VERSION 2.4.7) 4 | SET(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix for Debug mode, usually d on windows") 5 | 6 | IF(COMMAND CMAKE_POLICY) 7 | CMAKE_POLICY(SET CMP0003 NEW) 8 | ENDIF(COMMAND CMAKE_POLICY) 9 | 10 | SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR};${CMAKE_MODULE_PATH}") 11 | SET(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually d on windows") 12 | INCLUDE(CheckIncludeFiles) 13 | 14 | IF(WIN32) 15 | IF(MSVC) 16 | ADD_DEFINITIONS(-D_SCL_SECURE_NO_WARNINGS) 17 | ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE) 18 | ENDIF(MSVC) 19 | 20 | IF(NOT CMAKE_CL_64) 21 | # /LARGEADDRESSAWARE enables 32-bit applications to use more than 2 GB RAM 22 | SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LARGEADDRESSAWARE") 23 | ENDIF(NOT CMAKE_CL_64) 24 | ELSE(WIN32) 25 | SET(CMAKE_CXX_FLAGS "-W -Wall -Wno-unused") 26 | ENDIF(WIN32) 27 | 28 | FIND_PATH(OPENSCENEGRAPH_INCLUDE_DIR osg/Referenced 29 | PATHS 30 | $ENV{OSG_ROOT}/include 31 | $ENV{OSG_DIR}/include 32 | /usr/include 33 | /usr/local/include 34 | ) 35 | 36 | FIND_PATH(OPENSCENEGRAPH_LIB_DIR libosg.so osg.lib 37 | PATHS 38 | $ENV{OSG_ROOT}/lib 39 | $ENV{OSG_DIR}/lib 40 | /usr/lib 41 | /usr/local/lib 42 | ) 43 | 44 | FIND_PACKAGE(OpenGL) 45 | INCLUDE_DIRECTORIES(${OPENSCENEGRAPH_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR}) 46 | LINK_DIRECTORIES(${OPENSCENEGRAPH_LIB_DIR}) 47 | 48 | SET(SOURCE_FILES 49 | viewer.cpp 50 | shader_data.cpp 51 | terrain_data.cpp 52 | particle_data.cpp 53 | utilities.cpp 54 | user_data_classes.h 55 | 56 | Particle.h 57 | Particle.cpp 58 | spark/SPK_All.cpp 59 | spark/SPK_GL_All.cpp 60 | ) 61 | 62 | SET(COMMON_SHADER_FILES 63 | Default.vert 64 | Default.frag 65 | Terrain.vert 66 | Terrain.frag 67 | ) 68 | 69 | SET(LEGACY_SHADER_FILES 70 | Legacy_Shaders/Bumped_Diffuse.frag 71 | ) 72 | 73 | ADD_EXECUTABLE(osgUnitySceneViewer ${SOURCE_FILES}) 74 | SET_TARGET_PROPERTIES(osgUnitySceneViewer PROPERTIES DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}") 75 | 76 | TARGET_LINK_LIBRARIES(osgUnitySceneViewer 77 | debug osg${CMAKE_DEBUG_POSTFIX} optimized osg 78 | debug osgAnimation${CMAKE_DEBUG_POSTFIX} optimized osgAnimation 79 | debug osgParticle${CMAKE_DEBUG_POSTFIX} optimized osgParticle 80 | debug osgShadow${CMAKE_DEBUG_POSTFIX} optimized osgShadow 81 | debug osgDB${CMAKE_DEBUG_POSTFIX} optimized osgDB 82 | debug osgGA${CMAKE_DEBUG_POSTFIX} optimized osgGA 83 | debug osgText${CMAKE_DEBUG_POSTFIX} optimized osgText 84 | debug osgUtil${CMAKE_DEBUG_POSTFIX} optimized osgUtil 85 | debug osgViewer${CMAKE_DEBUG_POSTFIX} optimized osgViewer 86 | debug OpenThreads${CMAKE_DEBUG_POSTFIX} optimized OpenThreads 87 | ${OPENGL_LIBRARIES} 88 | ) 89 | INSTALL(TARGETS osgUnitySceneViewer RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) 90 | INSTALL(FILES ${COMMON_SHADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/shaders) 91 | INSTALL(FILES ${LEGACY_SHADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/shaders/Legacy_Shaders) 92 | -------------------------------------------------------------------------------- /viewer/Default.frag: -------------------------------------------------------------------------------- 1 | #version 130 2 | varying vec4 gl_TexCoord[gl_MaxTextureCoords]; 3 | uniform sampler2D mainTexture; 4 | uniform sampler2D lightTexture; 5 | uniform sampler2D normalTexture; 6 | uniform sampler2D specularTexture; 7 | uniform sampler2D emissionTexture; 8 | uniform samplerCube reflectTexture; 9 | 10 | uniform vec3 lightColor, lightDirection; 11 | uniform mat4 osg_ViewMatrix; 12 | 13 | varying vec4 eyeVec; 14 | varying vec3 normalVec, tangentVec, binormalVec; 15 | 16 | void main() 17 | { 18 | vec4 color = texture(mainTexture, gl_TexCoord[0].st); 19 | vec4 light = texture(lightTexture, gl_TexCoord[1].st); 20 | 21 | vec3 lightDir = normalize(mat3(osg_ViewMatrix) * lightDirection); 22 | float diff = max(0.0, dot(normalVec.xyz, lightDir)); 23 | gl_FragColor.rgb = color.rgb * light.rgb * lightColor * diff; 24 | gl_FragColor.a = color.a; 25 | } 26 | -------------------------------------------------------------------------------- /viewer/Default.vert: -------------------------------------------------------------------------------- 1 | varying vec4 gl_TexCoord[gl_MaxTextureCoords]; 2 | uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords]; 3 | attribute vec3 tangent; 4 | 5 | varying vec4 eyeVec; 6 | varying vec3 normalVec, tangentVec, binormalVec; 7 | 8 | void main() 9 | { 10 | gl_Position = ftransform(); 11 | gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; 12 | gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; 13 | normalVec = normalize(vec3(gl_NormalMatrix * gl_Normal)); 14 | tangentVec = normalize(vec3(gl_NormalMatrix * tangent)); 15 | binormalVec = cross(normalVec, tangentVec); 16 | eyeVec = gl_ModelViewMatrix * gl_Vertex; 17 | } 18 | -------------------------------------------------------------------------------- /viewer/Legacy_Shaders/Bumped_Diffuse.frag: -------------------------------------------------------------------------------- 1 | #version 130 2 | varying vec4 gl_TexCoord[gl_MaxTextureCoords]; 3 | uniform sampler2D mainTexture; 4 | uniform sampler2D normalTexture; 5 | uniform vec3 lightColor, lightDirection; 6 | uniform mat4 osg_ViewMatrix; 7 | 8 | varying vec4 eyeVec; 9 | varying vec3 normalVec, tangentVec, binormalVec; 10 | 11 | void main() 12 | { 13 | vec2 uv = gl_TexCoord[0].st; 14 | vec4 color = texture(mainTexture, uv); 15 | vec4 normal = texture(normalTexture, uv); 16 | 17 | vec3 lightDir = normalize(mat3(osg_ViewMatrix) * lightDirection); 18 | float diff = max(0.0, dot(normalVec.xyz, lightDir)); 19 | gl_FragColor.rgb = color.rgb * lightColor * diff; 20 | gl_FragColor.a = color.a; 21 | } 22 | 23 | // Legacy Shaders/Bumped Specular 24 | // Legacy Shaders/Reflective/Bumped Specular 25 | // Legacy Shaders/Transparent/Diffuse 26 | // Legacy Shaders/Transparent/Cutout/Diffuse 27 | // Legacy Shaders/Transparent/Bumped Diffuse 28 | // Legacy Shaders/Transparent/Cutout/Bumped Specular 29 | // Legacy Shaders/Reflective/Bumped Diffuse 30 | -------------------------------------------------------------------------------- /viewer/Terrain.frag: -------------------------------------------------------------------------------- 1 | #version 130 2 | varying vec4 gl_TexCoord[gl_MaxTextureCoords]; 3 | uniform sampler2DArray splatTexture; 4 | uniform sampler2D lightTexture; 5 | uniform sampler2D alphaTexture; 6 | uniform vec4 splatTilingOffsets[4]; 7 | 8 | uniform vec3 lightColor, lightDirection; 9 | uniform mat4 osg_ViewMatrix; 10 | 11 | varying vec4 eyeVec; 12 | varying vec3 normalVec; 13 | 14 | void main() 15 | { 16 | ivec3 size = textureSize(splatTexture, 0); 17 | vec4 alpha = texture(alphaTexture, gl_TexCoord[0].st); 18 | vec4 color = vec4(0.0, 0.0, 0.0, 1.0); 19 | vec4 light = texture(lightTexture, gl_TexCoord[1].st); 20 | for ( int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "user_data_classes.h" 7 | 8 | void applyUserShaders( ShaderDataProxyMap& sdMap, const std::string& dbPath ) 9 | { 10 | static std::map< std::string, osg::ref_ptr > s_sharedShaderPrograms; 11 | for ( ShaderDataProxyMap::iterator itr=sdMap.begin(); itr!=sdMap.end(); ++itr ) 12 | { 13 | std::string name = itr->first->shaderName; 14 | if ( s_sharedShaderPrograms.find(name)==s_sharedShaderPrograms.end() ) 15 | { 16 | std::string vertFile, fragFile, shaderPath = dbPath + "/shaders/" + name; 17 | std::replace( shaderPath.begin(), shaderPath.end(), ' ', '_'); 18 | vertFile = osgDB::findDataFile(shaderPath + ".vert"); 19 | fragFile = osgDB::findDataFile(shaderPath + ".frag"); 20 | if ( vertFile.empty() ) vertFile = dbPath + "/shaders/Default.vert"; 21 | if ( fragFile.empty() ) 22 | { 23 | fragFile = dbPath + "/shaders/Default.frag"; 24 | OSG_NOTICE << "Missed fragment shader for " << name << std::endl; 25 | } 26 | 27 | osg::ref_ptr program = new osg::Program; 28 | program->addShader( osgDB::readShaderFile(osg::Shader::VERTEX, vertFile) ); 29 | program->addShader( osgDB::readShaderFile(osg::Shader::FRAGMENT, fragFile) ); 30 | program->addBindAttribLocation( "tangent", 6 ); 31 | s_sharedShaderPrograms[name] = program; 32 | } 33 | itr->second->removeAttribute( itr->first ); 34 | itr->second->setAttributeAndModes( s_sharedShaderPrograms[name].get() ); 35 | } 36 | } 37 | 38 | bool ShaderData_readLocalData( osg::Object& obj, osgDB::Input& fr ) 39 | { 40 | ShaderDataProxy& proxy = static_cast(obj); 41 | bool iteratorAdvanced = false; 42 | 43 | if ( fr.matchSequence("ShaderName %s") ) 44 | { 45 | proxy.shaderName = fr[1].getStr(); 46 | iteratorAdvanced = true; fr += 2; 47 | } 48 | 49 | // TODO 50 | return iteratorAdvanced; 51 | } 52 | 53 | bool ShaderData_writeLocalData( const osg::Object&, osgDB::Output& ) 54 | { return false; } 55 | 56 | REGISTER_DOTOSGWRAPPER( ShaderData_Proxy ) 57 | ( 58 | new ShaderDataProxy, 59 | "nwTools::ShaderData", 60 | "Object StateAttribute Program nwTools::ShaderData", 61 | ShaderData_readLocalData, ShaderData_writeLocalData 62 | ); 63 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_BufferHandler.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Core/SPK_BufferHandler.h" 24 | 25 | namespace SPK 26 | { 27 | bool BufferHandler::bufferCreation = true; 28 | 29 | void BufferHandler::enableBuffersCreation(bool creation) 30 | { 31 | bufferCreation = creation; 32 | } 33 | 34 | bool BufferHandler::isBuffersCreationEnabled() 35 | { 36 | return bufferCreation; 37 | } 38 | 39 | bool BufferHandler::prepareBuffers(const Group& group) 40 | { 41 | if (!checkBuffers(group)) 42 | { 43 | if (isBuffersCreationEnabled()) 44 | { 45 | destroyBuffers(group); 46 | createBuffers(group); 47 | return true; 48 | } 49 | return false; 50 | } 51 | return true; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_DEF.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | #include "Core/SPK_DEF.h" 23 | 24 | 25 | namespace SPK 26 | { 27 | unsigned int randomSeed = 1; 28 | } 29 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_Emitter.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Core/SPK_Emitter.h" 24 | #include "Extensions/Zones/SPK_Point.h" 25 | 26 | 27 | namespace SPK 28 | { 29 | Emitter::Emitter() : 30 | Registerable(), 31 | Transformable(), 32 | zone(&getDefaultZone()), 33 | full(true), 34 | tank(-1), 35 | flow(0.0f), 36 | forceMin(0.0f), 37 | forceMax(0.0f), 38 | fraction(random(0.0f,1.0f)), 39 | active(true) 40 | {} 41 | 42 | void Emitter::registerChildren(bool registerAll) 43 | { 44 | Registerable::registerChildren(registerAll); 45 | registerChild(zone,registerAll); 46 | } 47 | 48 | void Emitter::copyChildren(const Registerable& object,bool createBase) 49 | { 50 | const Emitter& emitter = dynamic_cast(object); 51 | Registerable::copyChildren(emitter,createBase); 52 | zone = dynamic_cast(copyChild(emitter.zone,createBase)); 53 | } 54 | 55 | void Emitter::destroyChildren(bool keepChildren) 56 | { 57 | destroyChild(zone,keepChildren); 58 | Registerable::destroyChildren(keepChildren); 59 | } 60 | 61 | Registerable* Emitter::findByName(const std::string& name) 62 | { 63 | Registerable* object = Registerable::findByName(name); 64 | if (object != NULL) 65 | return object; 66 | 67 | return zone->findByName(name); 68 | } 69 | 70 | void Emitter::changeTank(int deltaTank) 71 | { 72 | if (tank >= 0) 73 | { 74 | tank += deltaTank; 75 | if (tank < 0) 76 | tank = 0; 77 | } 78 | } 79 | 80 | void Emitter::changeFlow(float deltaFlow) 81 | { 82 | if (flow >= 0.0f) 83 | { 84 | flow += deltaFlow; 85 | if (flow < 0.0f) 86 | flow = 0.0f; 87 | } 88 | } 89 | 90 | void Emitter::setZone(Zone* zone,bool full) 91 | { 92 | decrementChildReference(this->zone); 93 | incrementChildReference(zone); 94 | 95 | if (zone == NULL) 96 | zone = &getDefaultZone(); 97 | 98 | this->zone = zone; 99 | this->full = full; 100 | } 101 | 102 | Zone& Emitter::getDefaultZone() 103 | { 104 | static Point defaultZone; 105 | return defaultZone; 106 | } 107 | 108 | unsigned int Emitter::updateNumber(float deltaTime) 109 | { 110 | int nbBorn; 111 | if (flow < 0.0f) 112 | { 113 | nbBorn = std::max(0,tank); 114 | tank = 0; 115 | } 116 | else if (tank != 0) 117 | { 118 | fraction += flow * deltaTime; 119 | nbBorn = static_cast(fraction); 120 | if (tank >= 0) 121 | { 122 | nbBorn = std::min(tank,nbBorn); 123 | tank -= nbBorn; 124 | } 125 | fraction -= nbBorn; 126 | } 127 | else 128 | nbBorn = 0; 129 | 130 | return static_cast(nbBorn); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_Modifier.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | #include "Core/SPK_Modifier.h" 23 | 24 | namespace SPK 25 | { 26 | Vector3D Modifier::intersection; 27 | Vector3D Modifier::normal; 28 | 29 | Modifier::Modifier(int availableTriggers,ModifierTrigger trigger,bool needsIntersection,bool needsNormal,Zone* zone) : 30 | Registerable(), 31 | Transformable(), 32 | BufferHandler(), 33 | availableTriggers(availableTriggers), 34 | trigger(trigger), 35 | zone(zone), 36 | needsIntersection(needsIntersection), 37 | needsNormal(needsNormal), 38 | full(false), 39 | active(true), 40 | local(false) 41 | {} 42 | 43 | void Modifier::registerChildren(bool registerAll) 44 | { 45 | Registerable::registerChildren(registerAll); 46 | registerChild(zone,registerAll); 47 | } 48 | 49 | void Modifier::copyChildren(const Registerable& object,bool createBase) 50 | { 51 | const Modifier& modifier = dynamic_cast(object); 52 | Registerable::copyChildren(modifier,createBase); 53 | zone = dynamic_cast(copyChild(modifier.zone,createBase)); 54 | } 55 | 56 | void Modifier::destroyChildren(bool keepChildren) 57 | { 58 | destroyChild(zone,keepChildren); 59 | Registerable::destroyChildren(keepChildren); 60 | } 61 | 62 | Registerable* Modifier::findByName(const std::string& name) 63 | { 64 | Registerable* object = Registerable::findByName(name); 65 | if ((object != NULL)||(zone == NULL)) 66 | return object; 67 | 68 | return zone->findByName(name); 69 | } 70 | 71 | void Modifier::setZone(Zone* zone,bool full) 72 | { 73 | decrementChildReference(this->zone); 74 | incrementChildReference(zone); 75 | 76 | this->zone = zone; 77 | this->full = full; 78 | } 79 | 80 | bool Modifier::setTrigger(ModifierTrigger trigger) 81 | { 82 | if ((trigger & availableTriggers) != 0) 83 | { 84 | this->trigger = trigger; 85 | return true; 86 | } 87 | 88 | return false; 89 | } 90 | 91 | void Modifier::beginProcess(Group& group) 92 | { 93 | savedActive = active; 94 | 95 | if (!active) 96 | return; 97 | 98 | if (!prepareBuffers(group)) 99 | active = false; // if buffers of the modifier in the group are not ready, the modifier is made incative for the frame 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_RegWrapper.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_REGWRAPPER 24 | #define H_SPK_REGWRAPPER 25 | 26 | #include "Core/SPK_DEF.h" 27 | #include "Core/SPK_Registerable.h" 28 | 29 | 30 | namespace SPK 31 | { 32 | /** 33 | * @class RegWrapper 34 | * @brief A Wrapper class that allows to use any type of object as a Registerable 35 | * 36 | * It simply encapsulates an object of type defined at compilation time.
37 | * It allows to define the behavior of these Group attributes when a copy of the Group occurs.
38 | *
39 | * The user can use it to define an attribute of a custom Registerable child class that needs to act as a Registerable.
40 | *
41 | * WARNING : T must obviously not be itself a Registerable. 42 | * 43 | * @since 1.03.00 44 | */ 45 | template 46 | class RegWrapper : public Registerable 47 | { 48 | SPK_IMPLEMENT_REGISTERABLE(RegWrapper) 49 | 50 | public : 51 | 52 | ////////////////// 53 | // Constructors // 54 | ////////////////// 55 | 56 | /** 57 | * @brief Default constructor of RegWrapper 58 | * @param object : the inner object 59 | */ 60 | RegWrapper(const T& object = T()); 61 | 62 | /** 63 | * @brief Creates and registers a new RegWrapper 64 | * @param object : the inner object 65 | * @return A new registered RegWrapper 66 | * @since 1.04.00 67 | */ 68 | static RegWrapper* create(const T& object = T()); 69 | 70 | ///////////// 71 | // Getters // 72 | ///////////// 73 | 74 | /** 75 | * @brief Gets a reference on the inner object 76 | * @return a reference on the inner object 77 | */ 78 | T& get(); 79 | 80 | /** 81 | * @brief Gets a constant reference on the inner object 82 | * @return a constant reference on the inner object 83 | */ 84 | const T& get() const; 85 | 86 | private : 87 | 88 | T object; 89 | }; 90 | 91 | 92 | template 93 | inline RegWrapper* RegWrapper::create(const T& object) 94 | { 95 | RegWrapper* obj = new RegWrapper(object); 96 | registerObject(obj); 97 | return obj; 98 | 99 | } 100 | 101 | template 102 | inline T& RegWrapper::get() 103 | { 104 | return object; 105 | } 106 | 107 | template 108 | inline const T& RegWrapper::get() const 109 | { 110 | return object; 111 | } 112 | } 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_Registerable.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Core/SPK_Registerable.h" 24 | #include "Core/SPK_Factory.h" 25 | 26 | namespace SPK 27 | { 28 | const SPK_ID NO_ID(0); 29 | const std::string NO_NAME; 30 | 31 | Registerable::Registerable() : 32 | ID(NO_ID), 33 | nbReferences(0), 34 | shared(false), 35 | destroyable(true), 36 | name(NO_NAME) 37 | {} 38 | 39 | Registerable::Registerable(const Registerable& registerable) : 40 | ID(NO_ID), 41 | nbReferences(0), 42 | shared(registerable.shared), 43 | destroyable(registerable.destroyable), 44 | name(registerable.name) 45 | {} 46 | 47 | Registerable::~Registerable(){} 48 | 49 | Registerable* Registerable::copyChild(Registerable* child,bool createBase) 50 | { 51 | if (child == NULL) 52 | return NULL; 53 | 54 | if (((child->isRegistered())&&(!child->isShared()))||(createBase)) 55 | { 56 | if (SPKFactory::getInstance().isAlreadyProcessed(child)) 57 | { 58 | Registerable* processedClone = SPKFactory::getInstance().getProcessedObject(child); 59 | processedClone->incrementReference(); 60 | return processedClone; 61 | } 62 | 63 | Registerable* cloneChild = child->clone(createBase); 64 | SPKFactory::getInstance().registerObject(cloneChild); 65 | cloneChild->incrementReference(); 66 | SPKFactory::getInstance().markAsProcessed(child,cloneChild); 67 | return cloneChild; 68 | } 69 | 70 | child->incrementReference(); 71 | return child; 72 | } 73 | 74 | bool Registerable::destroyChild(Registerable* child,bool keepChildren) 75 | { 76 | if ((child == NULL)||(keepChildren)) 77 | return false; 78 | 79 | child->decrementReference(); 80 | 81 | if ((child->isRegistered())&& 82 | (child->isDestroyable())&& 83 | (child->getNbReferences() == 0)) 84 | { 85 | SPKFactory::getInstance().unregisterObject(child->getSPKID()); 86 | return true; 87 | } 88 | 89 | return false; 90 | } 91 | 92 | void Registerable::registerChild(Registerable* child,bool registerAll) 93 | { 94 | if (child == NULL) 95 | return; 96 | 97 | if (child->isRegistered()) 98 | { 99 | child->incrementReference(); 100 | child->registerChildren(registerAll); 101 | } 102 | else if (registerAll) 103 | { 104 | SPKFactory::getInstance().registerObject(child); 105 | child->incrementReference(); 106 | child->registerChildren(registerAll); 107 | } 108 | } 109 | 110 | void Registerable::registerObject(Registerable* obj,bool registerAll) 111 | { 112 | if ((obj != NULL)&&(!obj->isRegistered())) 113 | { 114 | SPKFactory::getInstance().registerObject(obj); 115 | obj->registerChildren(registerAll); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_Renderer.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Core/SPK_Renderer.h" 24 | #include "Core/SPK_Particle.h" 25 | 26 | namespace SPK 27 | { 28 | Renderer::Renderer() : 29 | Registerable(), 30 | BufferHandler(), 31 | active(true), 32 | renderingHintsMask(DEPTH_TEST | DEPTH_WRITE), 33 | alphaThreshold(1.0f) 34 | {} 35 | 36 | Renderer::~Renderer(){} 37 | } 38 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_Vector3D.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Core/SPK_Vector3D.h" 24 | 25 | namespace SPK 26 | { 27 | Vector3D::Vector3D(float x,float y,float z) : 28 | x(x), 29 | y(y), 30 | z(z) {} 31 | 32 | Vector3D& Vector3D::operator+=(const Vector3D& v) 33 | { 34 | x += v.x; 35 | y += v.y; 36 | z += v.z; 37 | return *this; 38 | } 39 | 40 | Vector3D& Vector3D::operator-=(const Vector3D& v) 41 | { 42 | x -= v.x; 43 | y -= v.y; 44 | z -= v.z; 45 | return *this; 46 | } 47 | 48 | Vector3D& Vector3D::operator+=(float f) 49 | { 50 | x += f; 51 | y += f; 52 | z += f; 53 | return *this; 54 | } 55 | 56 | Vector3D& Vector3D::operator-=(float f) 57 | { 58 | x -= f; 59 | y -= f; 60 | z -= f; 61 | return *this; 62 | } 63 | 64 | Vector3D& Vector3D::operator*=(float f) 65 | { 66 | x *= f; 67 | y *= f; 68 | z *= f; 69 | return *this; 70 | } 71 | 72 | Vector3D& Vector3D::operator/=(float f) 73 | { 74 | f = 1.0f / f; 75 | x *= f; 76 | y *= f; 77 | z *= f; 78 | return *this; 79 | } 80 | 81 | float& Vector3D::operator[](size_t index) 82 | { 83 | switch(index) 84 | { 85 | case 0 : return x; 86 | case 1 : return y; 87 | default : return z; 88 | } 89 | } 90 | 91 | const float& Vector3D::operator[](size_t index) const 92 | { 93 | switch(index) 94 | { 95 | case 0 : return x; 96 | case 1 : return y; 97 | default : return z; 98 | } 99 | } 100 | 101 | void Vector3D::set(float x,float y,float z) 102 | { 103 | this->x = x; 104 | this->y = y; 105 | this->z = z; 106 | } 107 | 108 | bool Vector3D::normalize() 109 | { 110 | float norm = getNorm(); 111 | if (norm != 0.0f) 112 | { 113 | x /= norm; 114 | y /= norm; 115 | z /= norm; 116 | return true; 117 | } 118 | return false; 119 | } 120 | 121 | void Vector3D::revert() 122 | { 123 | x = -x; 124 | y = -y; 125 | z = -z; 126 | } 127 | 128 | void Vector3D::abs() 129 | { 130 | if (x < 0.0f) x = -x; 131 | if (y < 0.0f) y = -y; 132 | if (z < 0.0f) z = -z; 133 | } 134 | 135 | void Vector3D::crossProduct(const Vector3D& v) 136 | { 137 | Vector3D result; 138 | 139 | result.x = y * v.z - z * v.y; 140 | result.y = z * v.x - x * v.z; 141 | result.z = x * v.y - y * v.x; 142 | 143 | *this = result; 144 | } 145 | 146 | float getSqrDist(const Vector3D& v0,const Vector3D& v1) 147 | { 148 | float dx = v0.x - v1.x; 149 | float dy = v0.y - v1.y; 150 | float dz = v0.z - v1.z; 151 | 152 | return dx * dx + dy * dy + dz * dz; 153 | } 154 | 155 | float getDist(const Vector3D& v0,const Vector3D& v1) 156 | { 157 | return std::sqrt(getSqrDist(v0,v1)); 158 | } 159 | 160 | Vector3D crossProduct(const Vector3D& v0,const Vector3D& v1) 161 | { 162 | Vector3D result; 163 | 164 | result.x = v0.y * v1.z - v0.z * v1.y; 165 | result.y = v0.z * v1.x - v0.x * v1.z; 166 | result.z = v0.x * v1.y - v0.y * v1.x; 167 | 168 | return result; 169 | } 170 | 171 | void crossProduct(const Vector3D& v0,const Vector3D& v1,Vector3D& result) 172 | { 173 | result.x = v0.y * v1.z - v0.z * v1.y; 174 | result.y = v0.z * v1.x - v0.x * v1.z; 175 | result.z = v0.x * v1.y - v0.y * v1.x; 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /viewer/spark/Core/SPK_Zone.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Core/SPK_Zone.h" 24 | 25 | namespace SPK 26 | { 27 | const float Zone::APPROXIMATION_VALUE = 0.01f; 28 | 29 | Zone::Zone(const Vector3D& position) : 30 | Registerable(), 31 | Transformable() 32 | { 33 | setPosition(position); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Emitters/SPK_NormalEmitter.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Emitters/SPK_NormalEmitter.h" 24 | #include "Core/SPK_Particle.h" 25 | 26 | namespace SPK 27 | { 28 | NormalEmitter::NormalEmitter(Zone* normalZone,bool inverted) : 29 | Emitter(), 30 | normalZone(normalZone), 31 | inverted(inverted) 32 | {} 33 | 34 | void NormalEmitter::registerChildren(bool registerAll) 35 | { 36 | Emitter::registerChildren(registerAll); 37 | registerChild(normalZone,registerAll); 38 | } 39 | 40 | void NormalEmitter::copyChildren(const Registerable& object,bool createBase) 41 | { 42 | const NormalEmitter& emitter = dynamic_cast(object); 43 | Emitter::copyChildren(emitter,createBase); 44 | normalZone = dynamic_cast(copyChild(emitter.normalZone,createBase)); 45 | } 46 | 47 | void NormalEmitter::destroyChildren(bool keepChildren) 48 | { 49 | destroyChild(normalZone,keepChildren); 50 | Emitter::destroyChildren(keepChildren); 51 | } 52 | 53 | Registerable* NormalEmitter::findByName(const std::string& name) 54 | { 55 | Registerable* object = Emitter::findByName(name); 56 | if ((object != NULL)||(normalZone == NULL)) 57 | return object; 58 | 59 | return normalZone->findByName(name); 60 | } 61 | 62 | void NormalEmitter::setNormalZone(Zone* zone) 63 | { 64 | decrementChildReference(normalZone); 65 | incrementChildReference(zone); 66 | 67 | normalZone = zone; 68 | } 69 | 70 | void NormalEmitter::generateVelocity(Particle& particle,float speed) const 71 | { 72 | if (inverted) speed = -speed; 73 | const Zone* zone = (normalZone == NULL ? getZone() : normalZone); 74 | particle.velocity() = zone->computeNormal(particle.position()) * speed; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Emitters/SPK_RandomEmitter.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Emitters/SPK_RandomEmitter.h" 24 | #include "Core/SPK_Particle.h" 25 | 26 | namespace SPK 27 | { 28 | void RandomEmitter::generateVelocity(Particle& particle,float speed) const 29 | { 30 | float norm; 31 | do 32 | { 33 | particle.velocity().set(random(-1.0f,1.0f),random(-1.0f,1.0f),random(-1.0f,1.0f)); 34 | norm = particle.velocity().getNorm(); 35 | } 36 | while((norm > 1.0f) || (norm == 0.0f)); 37 | particle.velocity() *= speed / norm; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Emitters/SPK_RandomEmitter.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_RANDOMEMITTER 24 | #define H_SPK_RANDOMEMITTER 25 | 26 | #include "Core/SPK_Emitter.h" 27 | 28 | 29 | namespace SPK 30 | { 31 | /** 32 | * @class RandomEmitter 33 | * @brief An Emitter that emits in a random direction 34 | * @since 1.02.00 35 | */ 36 | class SPK_PREFIX RandomEmitter : public Emitter 37 | { 38 | SPK_IMPLEMENT_REGISTERABLE(RandomEmitter) 39 | 40 | public : 41 | 42 | /** 43 | * @brief Creates and registers a new RandomEmitter 44 | * @return A new registered RandomEmitter 45 | * @since 1.04.00 46 | */ 47 | static RandomEmitter* create(); 48 | 49 | private : 50 | 51 | virtual void generateVelocity(Particle& particle,float speed) const; 52 | }; 53 | 54 | 55 | inline RandomEmitter* RandomEmitter::create() 56 | { 57 | RandomEmitter* obj = new RandomEmitter; 58 | registerObject(obj); 59 | return obj; 60 | } 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Emitters/SPK_SphericEmitter.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Emitters/SPK_SphericEmitter.h" 24 | #include "Core/SPK_Particle.h" 25 | 26 | namespace SPK 27 | { 28 | const float SphericEmitter::PI = 3.1415926535897932384626433832795f; 29 | 30 | SphericEmitter::SphericEmitter(const Vector3D& direction,float angleA,float angleB) : 31 | Emitter() 32 | { 33 | setDirection(direction); 34 | setAngles(angleA,angleB); 35 | } 36 | 37 | void SphericEmitter::setDirection(const Vector3D& direction) 38 | { 39 | tDirection = direction; 40 | computeMatrix(); 41 | this->direction = tDirection; // as tDirection was normalized in computeMatrix() 42 | notifyForUpdate(); 43 | } 44 | 45 | void SphericEmitter::setAngles(float angleA,float angleB) 46 | { 47 | if (angleB < angleA) 48 | std::swap(angleA,angleB); 49 | 50 | angleA = std::min(2.0f * PI,std::max(0.0f,angleA)); 51 | angleB = std::min(2.0f * PI,std::max(0.0f,angleB)); 52 | 53 | angleMin = angleA; 54 | angleMax = angleB; 55 | 56 | cosAngleMin = std::cos(angleMin * 0.5f); 57 | cosAngleMax = std::cos(angleMax * 0.5f); 58 | } 59 | 60 | void SphericEmitter::computeMatrix() 61 | { 62 | tDirection.normalize(); 63 | if ((tDirection.x == 0.0f)&&(tDirection.y == 0.0f)) 64 | { 65 | 66 | matrix[0] = tDirection.z; 67 | matrix[1] = 0.0f; 68 | matrix[2] = 0.0f; 69 | matrix[3] = 0.0f; 70 | matrix[4] = tDirection.z; 71 | matrix[5] = 0.0f; 72 | matrix[6] = 0.0f; 73 | matrix[7] = 0.0f; 74 | matrix[8] = tDirection.z; 75 | } 76 | else 77 | { 78 | Vector3D axis; 79 | crossProduct(tDirection,Vector3D(0.0f,0.0f,1.0f),axis); 80 | 81 | float cosA = tDirection.z; 82 | float sinA = -axis.getNorm(); 83 | axis /= -sinA; 84 | 85 | float x = axis.x; 86 | float y = axis.y; 87 | float z = axis.z; 88 | 89 | matrix[0] = x * x + cosA * (1.0f - x * x); 90 | matrix[1] = x * y * (1.0f - cosA) - z * sinA; 91 | matrix[2] = tDirection.x; 92 | matrix[3] = x * y * (1.0f - cosA) + z * sinA; 93 | matrix[4] = y * y + cosA * (1.0f - y * y); 94 | matrix[5] = tDirection.y; 95 | matrix[6] = x * z * (1.0f - cosA) - y * sinA; 96 | matrix[7] = y * z * (1.0f - cosA) + x * sinA; 97 | matrix[8] = tDirection.z; 98 | } 99 | } 100 | 101 | void SphericEmitter::generateVelocity(Particle& particle,float speed) const 102 | { 103 | float a = random(cosAngleMax,cosAngleMin); 104 | float theta = std::acos(a); 105 | float phi = random(0.0f,2.0f * PI); 106 | 107 | float sinTheta = std::sin(theta); 108 | float x = sinTheta * std::cos(phi); 109 | float y = sinTheta * std::sin(phi); 110 | float z = std::cos(theta); 111 | 112 | particle.velocity().x = speed * (matrix[0] * x + matrix[1] * y + matrix[2] * z); 113 | particle.velocity().y = speed * (matrix[3] * x + matrix[4] * y + matrix[5] * z); 114 | particle.velocity().z = speed * (matrix[6] * x + matrix[7] * y + matrix[8] * z); 115 | } 116 | 117 | void SphericEmitter::innerUpdateTransform() 118 | { 119 | Emitter::innerUpdateTransform(); 120 | transformDir(tDirection,direction); 121 | computeMatrix(); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Emitters/SPK_StaticEmitter.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_STATICEMITTER 24 | #define H_SPK_STATICEMITTER 25 | 26 | #include "Core/SPK_Emitter.h" 27 | 28 | 29 | namespace SPK 30 | { 31 | /** 32 | * @class StaticEmitter 33 | * @brief An Emitter that emits particles with no initial velocity 34 | * @since 1.05.00 35 | */ 36 | class StaticEmitter : public Emitter 37 | { 38 | SPK_IMPLEMENT_REGISTERABLE(StaticEmitter) 39 | 40 | public : 41 | 42 | /** 43 | * @brief Creates and registers a new StaticEmitter 44 | * @return A new registered StaticEmitter 45 | */ 46 | static StaticEmitter* create(); 47 | 48 | private : 49 | 50 | virtual void generateVelocity(Particle& particle,float speed) const; 51 | }; 52 | 53 | 54 | inline StaticEmitter* StaticEmitter::create() 55 | { 56 | StaticEmitter* obj = new StaticEmitter; 57 | registerObject(obj); 58 | return obj; 59 | } 60 | 61 | inline void StaticEmitter::generateVelocity(Particle& particle,float speed) const 62 | { 63 | particle.velocity().set(0.0f,0.0f,0.0f); // no initial velocity 64 | } 65 | } 66 | 67 | #endif 68 | 69 | 70 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Emitters/SPK_StraightEmitter.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Emitters/SPK_StraightEmitter.h" 24 | 25 | 26 | namespace SPK 27 | { 28 | StraightEmitter::StraightEmitter(const Vector3D& direction) : 29 | Emitter() 30 | { 31 | setDirection(direction); 32 | } 33 | 34 | void StraightEmitter::setDirection(const Vector3D& direction) 35 | { 36 | this->direction = direction; 37 | this->direction.normalize(); 38 | tDirection = this->direction; 39 | notifyForUpdate(); 40 | } 41 | 42 | void StraightEmitter::innerUpdateTransform() 43 | { 44 | Emitter::innerUpdateTransform(); 45 | transformDir(tDirection,direction); 46 | tDirection.normalize(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Emitters/SPK_StraightEmitter.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_STRAIGHEMITTER 24 | #define H_SPK_STRAIGHEMITTER 25 | 26 | #include "Core/SPK_Emitter.h" 27 | #include "Core/SPK_Particle.h" 28 | 29 | 30 | namespace SPK 31 | { 32 | /** 33 | * @class StraightEmitter 34 | * @brief An Emitter that emits in a given direction 35 | */ 36 | class SPK_PREFIX StraightEmitter : public Emitter 37 | { 38 | SPK_IMPLEMENT_REGISTERABLE(StraightEmitter) 39 | 40 | public : 41 | 42 | ///////////////// 43 | // Constructor // 44 | ///////////////// 45 | 46 | /** 47 | * @brief The constructor of StraightEmitter 48 | * @param direction : the direction of the StraighEmitter 49 | */ 50 | StraightEmitter(const Vector3D& direction = Vector3D(0.0f,0.0f,-1.0f)); 51 | 52 | /** 53 | * @brief Creates and registers a new StraightEmitter 54 | * @param direction : the direction of the StraighEmitter 55 | * @since 1.04.00 56 | */ 57 | static StraightEmitter* create(const Vector3D& direction = Vector3D(0.0f,0.0f,-1.0f)); 58 | 59 | ///////////// 60 | // Setters // 61 | ///////////// 62 | 63 | /** 64 | * @brief Sets the direction of this StraightEmitter 65 | * 66 | * Note that it is not necessary to provide a normalized Vector3D. 67 | * This Vector3D only indicates a direction, its norm does not matter. 68 | * 69 | * @param direction : the direction of this StraightEmitter 70 | */ 71 | void setDirection(const Vector3D& direction); 72 | 73 | ///////////// 74 | // Getters // 75 | ///////////// 76 | 77 | /** 78 | * @brief Gets the direction of this StraightEmitter 79 | * @return the direction of this StraightEmitter 80 | */ 81 | const Vector3D& getDirection() const; 82 | 83 | /** 84 | * @brief Gets the transformed direction of this StraightEmitter 85 | * @return the transformed direction of this StraightEmitter 86 | */ 87 | const Vector3D& getTransformedDirection() const; 88 | 89 | protected : 90 | 91 | virtual void innerUpdateTransform(); 92 | 93 | private : 94 | 95 | Vector3D direction; 96 | Vector3D tDirection; 97 | 98 | virtual void generateVelocity(Particle& particle,float speed) const; 99 | }; 100 | 101 | 102 | inline StraightEmitter* StraightEmitter::create(const Vector3D& direction) 103 | { 104 | StraightEmitter* obj = new StraightEmitter(direction); 105 | registerObject(obj); 106 | return obj; 107 | } 108 | 109 | inline const Vector3D& StraightEmitter::getDirection() const 110 | { 111 | return direction; 112 | } 113 | 114 | inline const Vector3D& StraightEmitter::getTransformedDirection() const 115 | { 116 | return tDirection; 117 | } 118 | 119 | inline void StraightEmitter::generateVelocity(Particle& particle,float speed) const 120 | { 121 | particle.velocity() = tDirection; 122 | particle.velocity() *= speed; 123 | } 124 | } 125 | 126 | #endif 127 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Modifiers/SPK_Collision.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Modifiers/SPK_Collision.h" 24 | #include "Core/SPK_Group.h" 25 | 26 | 27 | namespace SPK 28 | { 29 | Collision::Collision(float scale,float elasticity) : 30 | Modifier(), 31 | scale(scale) 32 | { 33 | setElasticity(elasticity); 34 | } 35 | 36 | void Collision::modify(Particle& particle,float deltaTime) const 37 | { 38 | size_t index = particle.getIndex(); 39 | float radius1 = particle.getParamCurrentValue(PARAM_SIZE) * scale * 0.5f; 40 | float m1 = particle.getParamCurrentValue(PARAM_MASS); 41 | Group& group = *particle.getGroup(); 42 | 43 | // Tests collisions with all the particles that are stored before in the pool 44 | for (size_t i = 0; i < index; ++i) 45 | { 46 | Particle& particle2 = group.getParticle(i); 47 | float radius2 = particle2.getParamCurrentValue(PARAM_SIZE) * scale * 0.5f; 48 | 49 | float sqrRadius = radius1 + radius2; 50 | sqrRadius *= sqrRadius; 51 | 52 | // Gets the normal of the collision plane 53 | Vector3D normal = particle.position(); 54 | normal -= particle2.position(); 55 | float sqrDist = normal.getSqrNorm(); 56 | 57 | if (sqrDist < sqrRadius) // particles are intersecting each other 58 | { 59 | Vector3D delta = particle.velocity(); 60 | delta -= particle2.velocity(); 61 | 62 | if (dotProduct(normal,delta) < 0.0f) // particles are moving towards each other 63 | { 64 | float oldSqrDist = getSqrDist(particle.oldPosition(),particle2.oldPosition()); 65 | if (oldSqrDist > sqrDist) 66 | { 67 | // Disables the move from this frame 68 | particle.position() = particle.oldPosition(); 69 | particle2.position() = particle2.oldPosition(); 70 | 71 | normal = particle.position(); 72 | normal -= particle2.position(); 73 | 74 | if (dotProduct(normal,delta) >= 0.0f) 75 | continue; 76 | } 77 | 78 | normal.normalize(); 79 | 80 | // Gets the normal components of the velocities 81 | Vector3D normal1(normal); 82 | Vector3D normal2(normal); 83 | normal1 *= dotProduct(normal,particle.velocity()); 84 | normal2 *= dotProduct(normal,particle2.velocity()); 85 | 86 | // Resolves collision 87 | float m2 = particle2.getParamCurrentValue(PARAM_MASS); 88 | 89 | if (oldSqrDist < sqrRadius && sqrDist < sqrRadius) 90 | { 91 | // Tweak to separate particles that intersects at both t - deltaTime and t 92 | // In that case the collision is no more considered as punctual 93 | if (dotProduct(normal,normal1) < 0.0f) 94 | { 95 | particle.velocity() -= normal1; 96 | particle2.velocity() += normal1; 97 | } 98 | 99 | if (dotProduct(normal,normal2) > 0.0f) 100 | { 101 | particle2.velocity() -= normal2; 102 | particle.velocity() += normal2; 103 | } 104 | } 105 | else 106 | { 107 | // Else classic collision equations are applied 108 | // Tangent components of the velocities are left untouched 109 | particle.velocity() -= (1.0f + (elasticity * m2 - m1) / (m1 + m2)) * normal1; 110 | particle2.velocity() -= (1.0f + (elasticity * m1 - m2) / (m1 + m2)) * normal2; 111 | 112 | normal1 *= ((1.0f + elasticity) * m1) / (m1 + m2); 113 | normal2 *= ((1.0f + elasticity) * m2) / (m1 + m2); 114 | 115 | particle.velocity() += normal2; 116 | particle2.velocity() += normal1; 117 | } 118 | } 119 | } 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Modifiers/SPK_Destroyer.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Modifiers/SPK_Destroyer.h" 24 | #include "Core/SPK_Particle.h" 25 | #include "Core/SPK_Zone.h" 26 | 27 | 28 | namespace SPK 29 | { 30 | Destroyer::Destroyer(Zone* zone,ModifierTrigger trigger) : 31 | Modifier(INSIDE_ZONE | OUTSIDE_ZONE | INTERSECT_ZONE | ENTER_ZONE | EXIT_ZONE,INSIDE_ZONE,true,false,zone) 32 | { 33 | setTrigger(trigger); 34 | } 35 | 36 | void Destroyer::modify(Particle& particle,float deltaTime) const 37 | { 38 | particle.kill(); 39 | if ((trigger != INSIDE_ZONE)||(trigger != OUTSIDE_ZONE)) 40 | particle.position() = intersection; 41 | } 42 | 43 | void Destroyer::modifyWrongSide(Particle& particle,bool inside) const 44 | { 45 | if (isFullZone()) 46 | { 47 | getZone()->moveAtBorder(particle.position(),inside); 48 | particle.kill(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Modifiers/SPK_Destroyer.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_DESTROYER 24 | #define H_SPK_DESTROYER 25 | 26 | #include "Core/SPK_Modifier.h" 27 | 28 | 29 | namespace SPK 30 | { 31 | /** 32 | * @class Destroyer 33 | * @brief A Modifier that destroy particles 34 | */ 35 | class SPK_PREFIX Destroyer : public Modifier 36 | { 37 | SPK_IMPLEMENT_REGISTERABLE(Destroyer) 38 | 39 | public : 40 | 41 | ///////////////// 42 | // Constructor // 43 | ///////////////// 44 | 45 | /** 46 | * @brief Constructor of Destroyer 47 | * @param zone : the Zone of the Destroyer 48 | * @param trigger : the trigger of the Destroyer 49 | */ 50 | Destroyer(Zone* zone = NULL,ModifierTrigger trigger = INSIDE_ZONE); 51 | 52 | /** 53 | * @brief Creates and registers a new Destroyer 54 | * @param zone : the Zone of the Destroyer 55 | * @param trigger : the trigger of the Destroyer 56 | * @return A new registered Destroyer 57 | * @since 1.04.00 58 | */ 59 | static Destroyer* create(Zone* zone = NULL,ModifierTrigger trigger = INSIDE_ZONE); 60 | 61 | private : 62 | 63 | virtual void modify(Particle& particle,float deltaTime) const; 64 | virtual void modifyWrongSide(Particle& particle,bool inside) const; 65 | }; 66 | 67 | 68 | inline Destroyer* Destroyer::create(Zone* zone,ModifierTrigger trigger) 69 | { 70 | Destroyer* obj = new Destroyer(zone,trigger); 71 | registerObject(obj); 72 | return obj; 73 | } 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Modifiers/SPK_LinearForce.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Modifiers/SPK_LinearForce.h" 24 | #include "Core/SPK_Particle.h" 25 | 26 | namespace SPK 27 | { 28 | LinearForce::LinearForce(Zone* zone,ModifierTrigger trigger,const Vector3D& force,ForceFactor type,ModelParam param) : 29 | Modifier(ALWAYS | INSIDE_ZONE | OUTSIDE_ZONE,ALWAYS,false,false,zone), 30 | force(force), 31 | tForce(force), 32 | factorType(type), 33 | factorParam(param) 34 | {} 35 | 36 | void LinearForce::modify(Particle& particle,float deltaTime) const 37 | { 38 | float factor = deltaTime / particle.getParamCurrentValue(PARAM_MASS); 39 | 40 | if (factorType != FACTOR_NONE) 41 | { 42 | float param = particle.getParamCurrentValue(factorParam); 43 | factor *= param; // linearity function of the parameter 44 | if (factorType == FACTOR_SQUARE) 45 | factor *= param; // linearity function of the square of the parameter 46 | } 47 | 48 | particle.velocity() += tForce * factor; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Modifiers/SPK_Obstacle.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Modifiers/SPK_Obstacle.h" 24 | 25 | 26 | namespace SPK 27 | { 28 | Obstacle::Obstacle(Zone* zone,ModifierTrigger trigger,float bouncingRatio,float friction) : 29 | Modifier(INTERSECT_ZONE | ENTER_ZONE | EXIT_ZONE,INTERSECT_ZONE,true,true,zone), 30 | bouncingRatio(bouncingRatio), 31 | friction(friction) 32 | { 33 | setTrigger(trigger); 34 | } 35 | 36 | void Obstacle::modify(Particle& particle,float deltaTime) const 37 | { 38 | Vector3D& velocity = particle.velocity(); 39 | velocity = particle.position(); 40 | velocity -= particle.oldPosition(); 41 | 42 | if (deltaTime != 0.0f) 43 | velocity *= 1.0f / deltaTime; 44 | else 45 | velocity.set(0.0f,0.0f,0.0f); 46 | 47 | float dist = dotProduct(velocity,normal); 48 | 49 | normal *= dist; 50 | velocity -= normal; // tangent component 51 | velocity *= friction; 52 | normal *= bouncingRatio; // normal component 53 | velocity -= normal; 54 | 55 | particle.position() = intersection; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Modifiers/SPK_PointMass.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Modifiers/SPK_PointMass.h" 24 | #include "Core/SPK_Particle.h" 25 | #include "Core/SPK_Zone.h" 26 | 27 | namespace SPK 28 | { 29 | PointMass::PointMass(Zone* zone,ModifierTrigger trigger,float mass,float minDistance) : 30 | Modifier(ALWAYS | INSIDE_ZONE | OUTSIDE_ZONE,ALWAYS,false,false,zone), 31 | position(), 32 | tPosition(), 33 | mass(mass) 34 | { 35 | setTrigger(trigger); 36 | setMinDistance(minDistance); 37 | } 38 | 39 | void PointMass::modify(Particle& particle,float deltaTime) const 40 | { 41 | Vector3D force = tPosition; 42 | if (getZone() != NULL) 43 | force += getZone()->getTransformedPosition(); 44 | 45 | force -= particle.position(); 46 | force *= mass * deltaTime / std::max(sqrMinDistance,force.getSqrNorm()); 47 | particle.velocity() += force; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Modifiers/SPK_Rotator.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_ROTATOR 24 | #define H_SPK_ROTATOR 25 | 26 | #include "Core/SPK_Modifier.h" 27 | 28 | 29 | namespace SPK 30 | { 31 | /** 32 | * @class Rotator 33 | * @brief A Modifier allowing to rotate particle with their rotation speed 34 | * 35 | * Instead of controlling directly the particle angle, this modifier allows to control the particle rotation speed.
36 | * The rotator derives the particle angle from the particle rotation speed.
37 | *
38 | * For this modifier to work, the PARAM_ANGLE must be enabled (and can be random in addition but not mutable or interpolated) 39 | * and the PARAM_ROTATION_SPEED must be at least enabled in the model of the group of particles that are modified. 40 | * 41 | * @since 1.05.00 42 | */ 43 | class Rotator : public Modifier 44 | { 45 | SPK_IMPLEMENT_REGISTERABLE(Rotator) 46 | 47 | public : 48 | 49 | ///////////////// 50 | // Constructor // 51 | ///////////////// 52 | 53 | /** @brief Constructor of Rotator */ 54 | Rotator(); 55 | 56 | /** 57 | * @brief Creates and registers a new Rotator 58 | * @return A new registered Rotator 59 | */ 60 | static Rotator* create(); 61 | 62 | private : 63 | 64 | virtual void modify(Particle& particle,float deltaTime) const; 65 | }; 66 | 67 | 68 | inline Rotator::Rotator() : Modifier(ALWAYS | INSIDE_ZONE | OUTSIDE_ZONE) {} 69 | 70 | inline Rotator* Rotator::create() 71 | { 72 | Rotator* obj = new Rotator; 73 | registerObject(obj); 74 | return obj; 75 | } 76 | 77 | inline void Rotator::modify(Particle& particle,float deltaTime) const 78 | { 79 | float angle = particle.getParamCurrentValue(PARAM_ANGLE) + deltaTime * particle.getParamCurrentValue(PARAM_ROTATION_SPEED); 80 | particle.setParamCurrentValue(PARAM_ANGLE,angle); 81 | } 82 | } 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Modifiers/SPK_Vortex.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | #include "Extensions/Modifiers/SPK_Vortex.h" 23 | #include "Core/SPK_Particle.h" 24 | 25 | namespace SPK 26 | { 27 | Vortex::Vortex(const Vector3D& position,const Vector3D& direction,float rotationSpeed,float attractionSpeed) : 28 | Modifier(ALWAYS | INSIDE_ZONE |OUTSIDE_ZONE,ALWAYS,false,false,NULL), 29 | rotationSpeed(rotationSpeed), 30 | attractionSpeed(attractionSpeed), 31 | angularSpeedEnabled(false), 32 | linearSpeedEnabled(false), 33 | killingParticleEnabled(false), 34 | eyeRadius(0.0f) 35 | { 36 | setPosition(position); 37 | setDirection(direction); 38 | } 39 | 40 | void Vortex::modify(Particle& particle,float deltaTime) const 41 | { 42 | // Distance of the projection point from the position of the vortex 43 | float dist = dotProduct(tDirection,particle.position() - tPosition); 44 | 45 | // Position of the rotation center (orthogonal projection of the particle) 46 | Vector3D rotationCenter = tDirection; 47 | rotationCenter *= dist; 48 | rotationCenter += tPosition; 49 | 50 | // Distance of the particle from the eye of the vortex 51 | dist = getDist(rotationCenter,particle.position()); 52 | 53 | if (dist <= eyeRadius) 54 | { 55 | if (killingParticleEnabled) 56 | particle.kill(); 57 | return; 58 | } 59 | 60 | float angle = angularSpeedEnabled ? rotationSpeed * deltaTime : rotationSpeed * deltaTime / dist; 61 | 62 | // Computes ortho base 63 | Vector3D normal = (particle.position() - rotationCenter) / dist; 64 | Vector3D tangent = crossProduct(tDirection,normal); 65 | 66 | float endRadius = linearSpeedEnabled ? dist * (1.0f - attractionSpeed * deltaTime) : dist - attractionSpeed * deltaTime; 67 | if (endRadius <= eyeRadius) 68 | { 69 | endRadius = eyeRadius; 70 | if (killingParticleEnabled) 71 | particle.kill(); 72 | } 73 | 74 | particle.position() = rotationCenter + normal * endRadius * std::cos(angle) + tangent * endRadius * std::sin(angle); 75 | } 76 | 77 | void Vortex::innerUpdateTransform() 78 | { 79 | Modifier::innerUpdateTransform(); 80 | transformPos(tPosition,position); 81 | transformDir(tDirection,direction); 82 | tDirection.normalize(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Renderers/SPK_LineRendererInterface.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_LINERENDERERINTERFACE 24 | #define H_SPK_LINERENDERERINTERFACE 25 | 26 | #include "Core/SPK_DEF.h" 27 | 28 | namespace SPK 29 | { 30 | /** 31 | * @brief Base Interface for rendering particles with lines 32 | * @since 1.04.00 33 | */ 34 | class LineRendererInterface 35 | { 36 | public : 37 | 38 | ///////////////// 39 | // Constructor // 40 | ///////////////// 41 | 42 | /** 43 | * @brief Constructor of LineRendererInterface 44 | * @param length : the length multiplier of this LineRendererInterface 45 | * @param width : the width of this GLLineRenderer 46 | */ 47 | LineRendererInterface(float length = 1.0f,float width = 1.0f); 48 | 49 | //////////////// 50 | // Destructor // 51 | //////////////// 52 | 53 | /** @brief Destructor of LineRendererInterface */ 54 | virtual ~LineRendererInterface() {} 55 | 56 | ///////////// 57 | // Setters // 58 | ///////////// 59 | 60 | /** 61 | * @brief Sets the length multiplier of this LineRendererInterface 62 | * 63 | * The length multiplier is the value which will be multiplied by the Particle velocity to get the line length in the universe.
64 | * A positive length means the line will be drawn in advance to the Particle, as opposed to a negative length. 65 | * 66 | * @param length : the length multiplier of this GLLineRenderer 67 | */ 68 | void setLength(float length); 69 | 70 | /** 71 | * @brief Sets the width of this LineRendererInterface 72 | * @param width : the width of this LineRendererInterface 73 | */ 74 | virtual void setWidth(float width); 75 | 76 | ///////////// 77 | // Getters // 78 | ///////////// 79 | 80 | /** 81 | * @brief Gets the length multiplier of this LineRendererInterface 82 | * @return the length multiplier of this LineRendererInterface 83 | */ 84 | float getLength() const; 85 | 86 | /** 87 | * @brief Gets the width of this LineRendererInterface 88 | * @return the width of this LineRendererInterface 89 | */ 90 | float getWidth() const; 91 | 92 | protected : 93 | 94 | float length; 95 | float width; 96 | }; 97 | 98 | 99 | inline LineRendererInterface::LineRendererInterface(float length,float width) : 100 | length(length), 101 | width(width) 102 | {} 103 | 104 | inline void LineRendererInterface::setLength(float length) 105 | { 106 | this->length = length; 107 | } 108 | 109 | inline void LineRendererInterface::setWidth(float width) 110 | { 111 | this->width = width; 112 | } 113 | 114 | inline float LineRendererInterface::getLength() const 115 | { 116 | return length; 117 | } 118 | 119 | inline float LineRendererInterface::getWidth() const 120 | { 121 | return width; 122 | } 123 | } 124 | 125 | #endif 126 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Renderers/SPK_Oriented3DRendererInterface.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | #include "Extensions/Renderers/SPK_Oriented3DRendererInterface.h" 23 | 24 | namespace SPK 25 | { 26 | Oriented3DRendererInterface::Oriented3DRendererInterface() : 27 | lookOrientation(LOOK_CAMERA_PLANE), 28 | upOrientation(UP_CAMERA), 29 | lockedAxis(LOCK_UP) 30 | { 31 | lookVector.set(0.0f,0.0f,1.0f); 32 | upVector.set(0.0f,1.0f,0.0f); 33 | } 34 | 35 | void Oriented3DRendererInterface::setOrientation(LookOrientation lookOrientation,UpOrientation upOrientation,LockedAxis lockedAxis) 36 | { 37 | this->lookOrientation = lookOrientation; 38 | this->upOrientation = upOrientation; 39 | this->lockedAxis = lockedAxis; 40 | } 41 | 42 | void Oriented3DRendererInterface::setOrientation(OrientationPreset orientation) 43 | { 44 | this->lookOrientation = static_cast((orientation >> 0x8) & 0xFF); 45 | this->upOrientation = static_cast(orientation & 0xFF); 46 | this->lockedAxis = static_cast((orientation >> 0x10) & 0xFF); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Renderers/SPK_PointRendererInterface.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_POINTRENDERERINTERFACE 24 | #define H_SPK_POINTRENDERERINTERFACE 25 | 26 | #include "Core/SPK_DEF.h" 27 | 28 | namespace SPK 29 | { 30 | /** 31 | * @enum PointType 32 | * @brief Constants defining the type of points to render 33 | */ 34 | enum PointType 35 | { 36 | POINT_SQUARE, /**< Points are renderered as squares */ 37 | POINT_CIRCLE, /**< Points are renderered as circles */ 38 | POINT_SPRITE, /**< Points are renderered as point sprites (textured points) */ 39 | }; 40 | 41 | /** 42 | * @brief Base Interface for rendering particles with points 43 | * @since 1.04.00 44 | */ 45 | class PointRendererInterface 46 | { 47 | public : 48 | 49 | ///////////////// 50 | // Constructor // 51 | ///////////////// 52 | 53 | /** 54 | * @brief Constructor of PointRendererInterface 55 | * @param type : the initial type of this PointRendererInterface (must be supported by default by the platform) 56 | * @param size : the width of this PointRendererInterface 57 | */ 58 | PointRendererInterface(PointType type = POINT_SQUARE,float size = 1.0f); 59 | 60 | //////////////// 61 | // Destructor // 62 | //////////////// 63 | 64 | /** @brief Destructor of PointRendererInterface */ 65 | virtual ~PointRendererInterface() {} 66 | 67 | ///////////// 68 | // Setters // 69 | ///////////// 70 | 71 | /** 72 | * @brief Sets the type of points to use in this PointRendererInterface 73 | * 74 | * If the type is not supported by the platform, false is returned and the type per default is set. 75 | * 76 | * @param type : the type of points to use in this PointRendererInterface 77 | * @return true if the type can be set, false otherwise 78 | */ 79 | virtual bool setType(PointType type); 80 | 81 | /** 82 | * @brief Sets the size of the points in this PointRendererInterface 83 | * @param size : the size of the points in this PointRendererInterface 84 | */ 85 | virtual void setSize(float size); 86 | 87 | ///////////// 88 | // Getters // 89 | ///////////// 90 | 91 | /** 92 | * @brief Gets the type of points in this PointRendererInterface 93 | * @return the type of points in this PointRendererInterface 94 | */ 95 | PointType getType() const; 96 | 97 | /** 98 | * @brief Gets the size of points in this PointRendererInterface 99 | * @return the size of points in this PointRendererInterface 100 | */ 101 | float getSize() const; 102 | 103 | protected : 104 | 105 | PointType type; 106 | float size; 107 | }; 108 | 109 | 110 | inline PointRendererInterface::PointRendererInterface(PointType type,float size) : 111 | type(type), 112 | size(size) 113 | {} 114 | 115 | inline bool PointRendererInterface::setType(PointType type) 116 | { 117 | this->type = type; 118 | return true; 119 | } 120 | 121 | inline void PointRendererInterface::setSize(float size) 122 | { 123 | this->size = size; 124 | } 125 | 126 | inline PointType PointRendererInterface::getType() const 127 | { 128 | return type; 129 | } 130 | 131 | inline float PointRendererInterface::getSize() const 132 | { 133 | return size; 134 | } 135 | } 136 | 137 | #endif 138 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Renderers/SPK_QuadRendererInterface.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | #include "Extensions/Renderers/SPK_QuadRendererInterface.h" 23 | 24 | namespace SPK 25 | { 26 | QuadRendererInterface::QuadRendererInterface(float scaleX,float scaleY) : 27 | scaleX(scaleX), 28 | scaleY(scaleY), 29 | texturingMode(TEXTURE_NONE), 30 | textureAtlasNbX(1), 31 | textureAtlasNbY(1), 32 | textureAtlasW(1.0f), 33 | textureAtlasH(1.0f) 34 | {} 35 | 36 | void QuadRendererInterface::setAtlasDimensions(size_t nbX,size_t nbY) 37 | { 38 | textureAtlasNbX = nbX; 39 | textureAtlasNbY = nbY; 40 | textureAtlasW = 1.0f / nbX; 41 | textureAtlasH = 1.0f / nbY; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_Line.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Zones/SPK_Line.h" 24 | #include "Core/SPK_Particle.h" 25 | 26 | namespace SPK 27 | { 28 | Line::Line(const Vector3D& p0,const Vector3D& p1) : 29 | Zone(Vector3D()) 30 | { 31 | setBounds(p0,p1); 32 | } 33 | 34 | void Line::setPosition(const Vector3D& v) 35 | { 36 | Vector3D displacement = v - getPosition(); 37 | bounds[0] = tBounds[0] += displacement; 38 | bounds[1] = tBounds[1] += displacement; 39 | computeDist(); 40 | Zone::setPosition(v); 41 | } 42 | 43 | void Line::setBounds(const Vector3D& p0,const Vector3D& p1) 44 | { 45 | bounds[0] = tBounds[0] = p0; 46 | bounds[1] = tBounds[1] = p1; 47 | computeDist(); 48 | computePosition(); 49 | } 50 | 51 | void Line::pushBound(const Vector3D& bound) 52 | { 53 | bounds[0] = tBounds[0] = bounds[1]; 54 | bounds[1] = tBounds[1] = bound; 55 | computeDist(); 56 | computePosition(); 57 | } 58 | 59 | void Line::generatePosition(Particle& particle,bool full) const 60 | { 61 | float ratio = random(0.0f,1.0f); 62 | particle.position() = tBounds[0] + tDist * ratio; 63 | } 64 | 65 | Vector3D Line::computeNormal(const Vector3D& point) const 66 | { 67 | float d = -dotProduct(tDist,point); 68 | float sqrNorm = tDist.getSqrNorm(); 69 | float t = 0.0f; 70 | if (sqrNorm > 0.0f) 71 | { 72 | t = -(dotProduct(tDist,tBounds[0]) + d) / sqrNorm; 73 | // t is clamped to the segment 74 | if (t < 0.0f) t = 0.0f; 75 | else if (t > 1.0f) t = 1.0f; 76 | } 77 | 78 | Vector3D normal = point; 79 | normal -= tBounds[0] + t * tDist; 80 | 81 | normalizeOrRandomize(normal); 82 | return normal; 83 | } 84 | 85 | void Line::innerUpdateTransform() 86 | { 87 | Zone::innerUpdateTransform(); 88 | transformPos(tBounds[0],bounds[0]); 89 | transformPos(tBounds[1],bounds[1]); 90 | computeDist(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_Plane.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Zones/SPK_Plane.h" 24 | 25 | namespace SPK 26 | { 27 | Plane::Plane(const Vector3D& position,const Vector3D& normal) : 28 | Zone(position) 29 | { 30 | setNormal(normal); 31 | } 32 | 33 | bool Plane::intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const 34 | { 35 | float dist0 = dotProduct(tNormal,v0 - getTransformedPosition()); 36 | float dist1 = dotProduct(tNormal,v1 - getTransformedPosition()); 37 | 38 | if ((dist0 <= 0.0f) == (dist1 <= 0.0f)) // both points are on the same side 39 | return false; 40 | 41 | if (intersection != NULL) 42 | { 43 | if (dist0 <= 0.0f) 44 | dist0 = -dist0; 45 | else 46 | dist1 = -dist1; 47 | 48 | if (normal != NULL) 49 | *normal = tNormal; 50 | 51 | float ti = dist0 / (dist0 + dist1); 52 | 53 | Vector3D vDir = v1 - v0; 54 | float norm = vDir.getNorm(); 55 | 56 | norm *= ti; 57 | ti = norm < APPROXIMATION_VALUE ? 0.0f : ti * (norm - APPROXIMATION_VALUE) / norm; 58 | 59 | vDir *= ti; 60 | *intersection = v0 + vDir; 61 | } 62 | 63 | return true; 64 | } 65 | 66 | void Plane::moveAtBorder(Vector3D& v,bool inside) const 67 | { 68 | float dist = dotProduct(tNormal,v - getTransformedPosition()); 69 | 70 | if ((dist <= 0.0f) == inside) 71 | inside ? dist += APPROXIMATION_VALUE : dist -= APPROXIMATION_VALUE; 72 | else 73 | inside ? dist -= APPROXIMATION_VALUE : dist += APPROXIMATION_VALUE; 74 | 75 | v += tNormal * -dist; 76 | } 77 | 78 | void Plane::innerUpdateTransform() 79 | { 80 | Zone::innerUpdateTransform(); 81 | transformDir(tNormal,normal); 82 | tNormal.normalize(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_Point.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Zones/SPK_Point.h" 24 | 25 | namespace SPK 26 | { 27 | Point::Point(const Vector3D& position) : 28 | Zone(position) 29 | {} 30 | 31 | Vector3D Point::computeNormal(const Vector3D& point) const 32 | { 33 | Vector3D normal(point - getTransformedPosition()); 34 | normalizeOrRandomize(normal); 35 | 36 | return normal; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_Point.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_POINT 24 | #define H_SPK_POINT 25 | 26 | #include "Core/SPK_Zone.h" 27 | #include "Core/SPK_Particle.h" 28 | 29 | 30 | namespace SPK 31 | { 32 | /** 33 | * @class Point 34 | * @brief A Zone defining a point in the universe 35 | */ 36 | class SPK_PREFIX Point : public Zone 37 | { 38 | SPK_IMPLEMENT_REGISTERABLE(Point) 39 | 40 | public : 41 | 42 | ///////////////// 43 | // Constructor // 44 | ///////////////// 45 | 46 | /** 47 | * @brief Constructor of Point 48 | * @param position : the position of the Point 49 | */ 50 | Point(const Vector3D& position = Vector3D(0.0f,0.0f,0.0f)); 51 | 52 | /** 53 | * @brief Creates and registers a new Point 54 | * @param position : the position of the Point 55 | * @return A new registered Point 56 | * @since 1.04.00 57 | */ 58 | static Point* create(const Vector3D& position = Vector3D(0.0f,0.0f,0.0f)); 59 | 60 | // Interface 61 | virtual void generatePosition(Particle& particle,bool full) const; 62 | virtual bool contains(const Vector3D& v) const; 63 | virtual bool intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const; 64 | virtual void moveAtBorder(Vector3D& v,bool inside) const; 65 | virtual Vector3D computeNormal(const Vector3D& point) const; 66 | }; 67 | 68 | inline Point* Point::create(const Vector3D& position) 69 | { 70 | Point* obj = new Point(position); 71 | registerObject(obj); 72 | return obj; 73 | } 74 | 75 | inline void Point::generatePosition(Particle& particle,bool full) const 76 | { 77 | particle.position() = getTransformedPosition(); 78 | } 79 | 80 | inline bool Point::contains(const Vector3D& v) const 81 | { 82 | return false; 83 | } 84 | 85 | inline bool Point::intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const 86 | { 87 | return false; 88 | } 89 | 90 | inline void Point::moveAtBorder(Vector3D& v,bool inside) const {} 91 | } 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_Ring.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | #include "Extensions/Zones/SPK_Ring.h" 23 | #include "Core/SPK_Particle.h" 24 | 25 | namespace SPK 26 | { 27 | Ring::Ring(const Vector3D& position,const Vector3D& normal,float minRadius,float maxRadius) : 28 | Zone(position) 29 | { 30 | setNormal(normal); 31 | setRadius(minRadius,maxRadius); 32 | } 33 | 34 | void Ring::setRadius(float minRadius,float maxRadius) 35 | { 36 | if (minRadius < 0.0f) minRadius = -minRadius; 37 | if (maxRadius < 0.0f) maxRadius = -maxRadius; 38 | if (minRadius > maxRadius) std::swap(minRadius,maxRadius); 39 | this->minRadius = minRadius; 40 | this->maxRadius = maxRadius; 41 | sqrMinRadius = minRadius * minRadius; 42 | sqrMaxRadius = maxRadius * maxRadius; 43 | } 44 | 45 | void Ring::generatePosition(Particle& particle,bool full) const 46 | { 47 | Vector3D tmp; 48 | do tmp = Vector3D(random(-1.0f,1.0f),random(-1.0f,1.0f),random(-1.0f,1.0f)); 49 | while (tmp.getSqrNorm() > 1.0f); 50 | 51 | crossProduct(tNormal,tmp,particle.position()); 52 | normalizeOrRandomize(particle.position()); 53 | 54 | particle.position() *= std::sqrt(random(sqrMinRadius,sqrMaxRadius)); // to have a uniform distribution 55 | particle.position() += getTransformedPosition(); 56 | } 57 | 58 | bool Ring::intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const 59 | { 60 | float dist0 = dotProduct(tNormal,v0 - getTransformedPosition()); 61 | float dist1 = dotProduct(tNormal,v1 - getTransformedPosition()); 62 | 63 | if ((dist0 <= 0.0f) == (dist1 <= 0.0f)) // both points are on the same side 64 | return false; 65 | 66 | if (dist0 <= 0.0f) 67 | dist0 = -dist0; 68 | else 69 | dist1 = -dist1; 70 | 71 | float ti = dist0 / (dist0 + dist1); 72 | 73 | Vector3D vDir(v1 - v0); 74 | float norm = vDir.getNorm(); 75 | 76 | norm *= ti; 77 | ti = norm < APPROXIMATION_VALUE ? 0.0f : ti * (norm - APPROXIMATION_VALUE) / norm; 78 | 79 | vDir *= ti; 80 | Vector3D inter(v0 + vDir); 81 | 82 | float distFromCenter = getSqrDist(inter,getTransformedPosition()); 83 | if (distFromCenter > sqrMaxRadius || distFromCenter < sqrMinRadius) // intersection is not in the ring 84 | return false; 85 | 86 | if (intersection != NULL) 87 | { 88 | *intersection = inter; 89 | if (normal != NULL) 90 | *normal = tNormal; 91 | } 92 | 93 | return true; 94 | } 95 | 96 | void Ring::moveAtBorder(Vector3D& v,bool inside) const 97 | { 98 | float dist = dotProduct(tNormal,v - getTransformedPosition()); 99 | v += tNormal * -dist; 100 | 101 | float distFromCenter = getSqrDist(v,getTransformedPosition()); 102 | 103 | if (distFromCenter > sqrMaxRadius) 104 | { 105 | distFromCenter = std::sqrt(distFromCenter); 106 | Vector3D vDir(v - getTransformedPosition()); 107 | vDir *= maxRadius / distFromCenter; 108 | v = getTransformedPosition() + vDir; 109 | } 110 | else if (distFromCenter < sqrMinRadius) 111 | { 112 | distFromCenter = std::sqrt(distFromCenter); 113 | Vector3D vDir(v - getTransformedPosition()); 114 | vDir *= minRadius / distFromCenter; 115 | v = getTransformedPosition() + vDir; 116 | } 117 | } 118 | 119 | void Ring::innerUpdateTransform() 120 | { 121 | Zone::innerUpdateTransform(); 122 | transformDir(tNormal,normal); 123 | tNormal.normalize(); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_Sphere.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "Extensions/Zones/SPK_Sphere.h" 24 | #include "Core/SPK_Particle.h" 25 | 26 | namespace SPK 27 | { 28 | Sphere::Sphere(const Vector3D& position,float radius) : 29 | Zone(position) 30 | { 31 | setRadius(radius); 32 | } 33 | 34 | void Sphere::generatePosition(Particle& particle,bool full) const 35 | { 36 | do particle.position() = Vector3D(random(-radius,radius),random(-radius,radius),random(-radius,radius)); 37 | while (particle.position().getSqrNorm() > radius * radius); 38 | 39 | if ((!full)&&(radius > 0.0f)) 40 | particle.position() *= radius / particle.position().getNorm(); 41 | 42 | particle.position() += getTransformedPosition(); 43 | } 44 | 45 | bool Sphere::contains(const Vector3D& v) const 46 | { 47 | return getSqrDist(getTransformedPosition(),v) <= radius * radius; 48 | } 49 | 50 | bool Sphere::intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const 51 | { 52 | float r2 = radius * radius; 53 | float dist0 = getSqrDist(getTransformedPosition(),v0); 54 | float dist1 = getSqrDist(getTransformedPosition(),v1); 55 | 56 | if ((dist0 <= r2) == (dist1 <= r2)) 57 | return false; 58 | 59 | if (intersection != NULL) 60 | { 61 | Vector3D vDir = v1 - v0; 62 | float norm = vDir.getNorm(); 63 | 64 | float d = dotProduct(vDir,getTransformedPosition() - v0) / norm; 65 | float a = std::sqrt(r2 - dist0 + d * d); 66 | 67 | float ti; 68 | if (dist0 <= r2) 69 | ti = d - a; 70 | else 71 | ti = d + a; 72 | 73 | ti /= norm; 74 | 75 | if (ti < 0.0f) ti = 0.0f; 76 | if (ti > 1.0f) ti = 1.0f; 77 | 78 | norm *= ti; 79 | ti = norm < APPROXIMATION_VALUE ? 0.0f : ti * (norm - APPROXIMATION_VALUE) / norm; 80 | 81 | vDir *= ti; 82 | *intersection = v0 + vDir; 83 | 84 | if (normal != NULL) 85 | { 86 | if (dist0 <= r2) 87 | *normal = getTransformedPosition() - *intersection; 88 | else 89 | *normal = *intersection - getTransformedPosition(); 90 | normal->normalize(); 91 | } 92 | } 93 | 94 | return true; 95 | } 96 | 97 | void Sphere::moveAtBorder(Vector3D& v,bool inside) const 98 | { 99 | Vector3D vDir = v - getTransformedPosition(); 100 | float norm = vDir.getNorm(); 101 | 102 | if (inside) 103 | vDir *= (radius + APPROXIMATION_VALUE) / norm; 104 | else 105 | vDir *= (radius - APPROXIMATION_VALUE) / norm; 106 | 107 | v = getTransformedPosition() + vDir; 108 | } 109 | 110 | Vector3D Sphere::computeNormal(const Vector3D& point) const 111 | { 112 | Vector3D normal(point - getTransformedPosition()); 113 | normalizeOrRandomize(normal); 114 | return normal; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_Sphere.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_SPHERE 24 | #define H_SPK_SPHERE 25 | 26 | #include "Core/SPK_Zone.h" 27 | 28 | namespace SPK 29 | { 30 | /** 31 | * @class Sphere 32 | * @brief A Zone defining a sphere in the universe 33 | */ 34 | class SPK_PREFIX Sphere : public Zone 35 | { 36 | SPK_IMPLEMENT_REGISTERABLE(Sphere) 37 | 38 | public : 39 | 40 | ///////////////// 41 | // Constructor // 42 | ///////////////// 43 | 44 | /** 45 | * @brief Constructor of Sphere 46 | * @param position : position of the center of the Sphere 47 | * @param radius : radius of the Sphere 48 | */ 49 | Sphere(const Vector3D& position = Vector3D(0.0f,0.0f,0.0f),float radius = 0.0f); 50 | 51 | /** 52 | * @brief Creates and registers a new Sphere 53 | * @param position : position of the center of the Sphere 54 | * @param radius : radius of the Sphere 55 | * @return A new registered Sphere 56 | * @since 1.04.00 57 | */ 58 | static Sphere* create(const Vector3D& position = Vector3D(0.0f,0.0f,0.0f),float radius = 0.0f); 59 | 60 | //////////// 61 | // Setter // 62 | //////////// 63 | 64 | /** 65 | * @brief Sets the radius of this Sphere 66 | * 67 | * A negative radius will be clamped to 0.0f and the Sphere will therefore acts as a Point. 68 | * 69 | * @param radius : the radius of this Sphere 70 | */ 71 | void setRadius(float radius); 72 | 73 | //////////// 74 | // Getter // 75 | //////////// 76 | 77 | /** 78 | * @brief Gets the radius of this Sphere 79 | * @return the radius of this Sphere 80 | */ 81 | float getRadius() const; 82 | 83 | /////////////// 84 | // Interface // 85 | /////////////// 86 | 87 | virtual void generatePosition(Particle& particle,bool full) const; 88 | virtual bool contains(const Vector3D& v) const; 89 | virtual bool intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const; 90 | virtual void moveAtBorder(Vector3D& v,bool inside) const; 91 | virtual Vector3D computeNormal(const Vector3D& point) const; 92 | 93 | private : 94 | 95 | float radius; 96 | }; 97 | 98 | 99 | inline Sphere* Sphere::create(const Vector3D& position,float radius) 100 | { 101 | Sphere* obj = new Sphere(position,radius); 102 | registerObject(obj); 103 | return obj; 104 | } 105 | 106 | inline void Sphere::setRadius(float radius) 107 | { 108 | this->radius = radius >= 0.0f ? radius : 0.0f; 109 | } 110 | 111 | inline float Sphere::getRadius() const 112 | { 113 | return radius; 114 | } 115 | } 116 | 117 | #endif 118 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_ZoneIntersection.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2009 // 4 | // Julien Fryer - julienfryer@gmail.com // 5 | // Thibault Lescoat - info-tibo orange fr // 6 | // // 7 | // This software is provided 'as-is', without any express or implied // 8 | // warranty. In no event will the authors be held liable for any damages // 9 | // arising from the use of this software. // 10 | // // 11 | // Permission is granted to anyone to use this software for any purpose, // 12 | // including commercial applications, and to alter it and redistribute it // 13 | // freely, subject to the following restrictions: // 14 | // // 15 | // 1. The origin of this software must not be misrepresented; you must not // 16 | // claim that you wrote the original software. If you use this software // 17 | // in a product, an acknowledgment in the product documentation would be // 18 | // appreciated but is not required. // 19 | // 2. Altered source versions must be plainly marked as such, and must not be // 20 | // misrepresented as being the original software. // 21 | // 3. This notice may not be removed or altered from any source distribution. // 22 | ////////////////////////////////////////////////////////////////////////////////// 23 | 24 | 25 | #include "Extensions/Zones/SPK_ZoneIntersection.h" 26 | #include "Core/SPK_Particle.h" 27 | 28 | namespace SPK 29 | { 30 | ZoneIntersection::ZoneIntersection(size_t amountEstimation) 31 | :Zone() 32 | { 33 | Zones.reserve(amountEstimation); 34 | } 35 | 36 | void ZoneIntersection::setPosition(const Vector3D& p) 37 | { 38 | Vector3D decal = p - getPosition(); 39 | Vector3D pos; float nb = 1.0f/Zones.size(); 40 | for(size_t t = 0; t < Zones.size(); t++) 41 | { 42 | Zones[t]->setPosition( Zones[t]->getPosition() + decal ); 43 | pos += Zones[t]->getPosition() * nb; 44 | } 45 | Zone::setPosition(pos); 46 | } 47 | 48 | void ZoneIntersection::addZone(Zone* zone, bool check) 49 | { 50 | if(!zone) return; 51 | if(check && isZoneIncluded(zone)) return; 52 | 53 | Zones.push_back(zone); 54 | incrementChildReference(zone); 55 | } 56 | 57 | void ZoneIntersection::removeZone(Zone* zone) 58 | { 59 | bool found = false; 60 | for (size_t t = 0; t < Zones.size();) 61 | { 62 | if(Zones[t] == zone) 63 | { 64 | Zones.erase(Zones.begin()+t); 65 | found = true; 66 | } 67 | else 68 | t++; 69 | } 70 | if(found) decrementChildReference(zone); 71 | } 72 | 73 | size_t ZoneIntersection::getNumberZones() 74 | { 75 | return Zones.size(); 76 | } 77 | 78 | bool ZoneIntersection::isZoneIncluded(Zone* z) 79 | { 80 | for (size_t t = 0; t < Zones.size(); t++) 81 | if(Zones[t] == z) return true; 82 | return false; 83 | } 84 | 85 | std::vector& ZoneIntersection::getZones() 86 | { 87 | return Zones; 88 | } 89 | 90 | void ZoneIntersection::generatePosition(Particle& particle,bool full) const 91 | { 92 | if(Zones.size() == 0) return; 93 | 94 | Zones[random((int)0,(int)Zones.size()-1)]->generatePosition(particle,full); 95 | } 96 | 97 | bool ZoneIntersection::contains(const Vector3D& v) const 98 | { 99 | for (size_t t = 0; t < Zones.size(); t++) 100 | if( !Zones[t]->contains(v) ) return false; 101 | return true; 102 | } 103 | 104 | bool ZoneIntersection::intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const 105 | { 106 | for (size_t t = 0; t < Zones.size(); t++) 107 | if( !Zones[t]->intersects(v0,v1,intersection,normal) ) return false; 108 | return true; 109 | } 110 | 111 | void ZoneIntersection::moveAtBorder(Vector3D& v,bool inside) const 112 | { 113 | Vector3D point = v; 114 | for (size_t t = 0; t < Zones.size(); t++) 115 | { 116 | point = v; 117 | Zones[t]->moveAtBorder(point,true); 118 | if(contains(point)) 119 | { 120 | v = point; 121 | return; 122 | } 123 | } 124 | 125 | if(Zones.size() != 0) Zones[0]->moveAtBorder(v,inside); 126 | else v = Vector3D(); 127 | } 128 | 129 | Vector3D ZoneIntersection::computeNormal(const Vector3D& point) const 130 | { 131 | Vector3D v = point; 132 | 133 | for (size_t t = 0; t < Zones.size(); t++) 134 | { 135 | v = point; 136 | Zones[t]->moveAtBorder(v,false); 137 | if(contains(point)) 138 | return Zones[t]->computeNormal(v); 139 | } 140 | 141 | if(Zones.size() != 0) return Zones[0]->computeNormal(point); 142 | else return Vector3D(); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_ZoneUnion.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2009 // 4 | // Julien Fryer - julienfryer@gmail.com // 5 | // Thibault Lescoat - info-tibo orange fr // 6 | // // 7 | // This software is provided 'as-is', without any express or implied // 8 | // warranty. In no event will the authors be held liable for any damages // 9 | // arising from the use of this software. // 10 | // // 11 | // Permission is granted to anyone to use this software for any purpose, // 12 | // including commercial applications, and to alter it and redistribute it // 13 | // freely, subject to the following restrictions: // 14 | // // 15 | // 1. The origin of this software must not be misrepresented; you must not // 16 | // claim that you wrote the original software. If you use this software // 17 | // in a product, an acknowledgment in the product documentation would be // 18 | // appreciated but is not required. // 19 | // 2. Altered source versions must be plainly marked as such, and must not be // 20 | // misrepresented as being the original software. // 21 | // 3. This notice may not be removed or altered from any source distribution. // 22 | ////////////////////////////////////////////////////////////////////////////////// 23 | 24 | 25 | #include "Extensions/Zones/SPK_ZoneUnion.h" 26 | #include "Core/SPK_Particle.h" 27 | 28 | namespace SPK 29 | { 30 | ZoneUnion::ZoneUnion(size_t amountEstimation) 31 | :Zone() 32 | { 33 | Zones.reserve(amountEstimation); 34 | } 35 | 36 | void ZoneUnion::setPosition(const Vector3D& p) 37 | { 38 | Vector3D decal = p - getPosition(); 39 | Vector3D pos; float nb = 1.0f/Zones.size(); 40 | for(size_t t = 0; t < Zones.size(); t++) 41 | { 42 | Zones[t]->setPosition( Zones[t]->getPosition() + decal ); 43 | pos += Zones[t]->getPosition() * nb; 44 | } 45 | Zone::setPosition(pos); 46 | } 47 | 48 | void ZoneUnion::addZone(Zone* zone, bool check) 49 | { 50 | if(!zone) return; 51 | if(check && isZoneIncluded(zone)) return; 52 | 53 | Zones.push_back(zone); 54 | incrementChildReference(zone); 55 | } 56 | 57 | void ZoneUnion::removeZone(Zone* zone) 58 | { 59 | bool found = false; 60 | for (size_t t = 0; t < Zones.size();) 61 | { 62 | if(Zones[t] == zone) 63 | { 64 | Zones.erase(Zones.begin()+t); 65 | found = true; 66 | } 67 | else 68 | t++; 69 | } 70 | if(found) decrementChildReference(zone); 71 | } 72 | 73 | size_t ZoneUnion::getNumberZones() 74 | { 75 | return Zones.size(); 76 | } 77 | 78 | bool ZoneUnion::isZoneIncluded(Zone* z) 79 | { 80 | for (size_t t = 0; t < Zones.size(); t++) 81 | if(Zones[t] == z) return true; 82 | return false; 83 | } 84 | 85 | std::vector& ZoneUnion::getZones() 86 | { 87 | return Zones; 88 | } 89 | 90 | void ZoneUnion::generatePosition(Particle& particle,bool full) const 91 | { 92 | if(Zones.size() == 0) return; 93 | 94 | Zones[random((int)0,(int)Zones.size()-1)]->generatePosition(particle,full); 95 | } 96 | 97 | bool ZoneUnion::contains(const Vector3D& v) const 98 | { 99 | for (size_t t = 0; t < Zones.size(); t++) 100 | if( Zones[t]->contains(v) ) return true; 101 | return false; 102 | 103 | } 104 | 105 | bool ZoneUnion::intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const 106 | { 107 | for (size_t t = 0; t < Zones.size(); t++) 108 | if( Zones[t]->intersects(v0,v1,intersection,normal) ) return true; 109 | return false; 110 | } 111 | 112 | void ZoneUnion::moveAtBorder(Vector3D& v,bool inside) const 113 | { 114 | Vector3D point = v; 115 | for (size_t t = 0; t < Zones.size(); t++) 116 | { 117 | point = v; 118 | Zones[t]->moveAtBorder(point,inside); 119 | bool ok=true; 120 | 121 | for (size_t i = 0; i < Zones.size(); i++) 122 | if(Zones[t] != Zones[i] && Zones[i]->contains(point)) ok = false; 123 | 124 | if(ok) 125 | { 126 | v = point; 127 | return; 128 | } 129 | } 130 | 131 | if(Zones.size() != 0) Zones[0]->moveAtBorder(v,inside); 132 | else v = Vector3D(); 133 | } 134 | 135 | Vector3D ZoneUnion::computeNormal(const Vector3D& point) const 136 | { 137 | Vector3D v = point; 138 | 139 | for (size_t t = 0; t < Zones.size(); t++) 140 | { 141 | v = point; 142 | Zones[t]->moveAtBorder(v,false); 143 | bool ok=true; 144 | 145 | for (size_t i = 0; i < Zones.size(); i++) 146 | if(Zones[t] != Zones[i] && Zones[i]->contains(v)) ok = false; 147 | 148 | if(ok) 149 | return Zones[t]->computeNormal(v); 150 | } 151 | 152 | if(Zones.size() != 0) return Zones[0]->computeNormal(point); 153 | else return Vector3D(); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /viewer/spark/Extensions/Zones/SPK_ZoneUnion.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2009 // 4 | // Julien Fryer - julienfryer@gmail.com // 5 | // Thibault Lescoat - info-tibo orange fr // 6 | // // 7 | // This software is provided 'as-is', without any express or implied // 8 | // warranty. In no event will the authors be held liable for any damages // 9 | // arising from the use of this software. // 10 | // // 11 | // Permission is granted to anyone to use this software for any purpose, // 12 | // including commercial applications, and to alter it and redistribute it // 13 | // freely, subject to the following restrictions: // 14 | // // 15 | // 1. The origin of this software must not be misrepresented; you must not // 16 | // claim that you wrote the original software. If you use this software // 17 | // in a product, an acknowledgment in the product documentation would be // 18 | // appreciated but is not required. // 19 | // 2. Altered source versions must be plainly marked as such, and must not be // 20 | // misrepresented as being the original software. // 21 | // 3. This notice may not be removed or altered from any source distribution. // 22 | ////////////////////////////////////////////////////////////////////////////////// 23 | 24 | 25 | #ifndef H_SPK_ZONEUNION 26 | #define H_SPK_ZONEUNION 27 | 28 | #include "Core/SPK_Zone.h" 29 | #include 30 | 31 | namespace SPK 32 | { 33 | /** 34 | * @class ZoneUnion 35 | * @brief A zone allowing to use several zones easily 36 | * 37 | * A ZoneUnion is defined by a list of zones and its position. The position is the isobarycenter 38 | * of all centers. A point is considered to be be inside this zone if it's in at least one of all 39 | * zones in the list. 40 | */ 41 | class SPK_PREFIX ZoneUnion : public Zone 42 | { 43 | SPK_IMPLEMENT_REGISTERABLE(ZoneUnion) 44 | 45 | public : 46 | 47 | ///////////////// 48 | // Constructor // 49 | ///////////////// 50 | 51 | /** 52 | * @brief Constructor of ZoneUnion 53 | * @param amountEstimation : an estimation of the number of zones 54 | * which will be included in this ZoneUnion (used for an optimisation of the vector) 55 | */ 56 | ZoneUnion(size_t amountEstimation); 57 | 58 | /** 59 | * @brief Creates and registers a new ZoneUnion 60 | * @param amountEstimation : an estimation of the number of zones 61 | * which will be included in this ZoneUnion (used for an optimisation of the vector) 62 | */ 63 | static ZoneUnion* create(size_t amountEstimation); 64 | 65 | //////////// 66 | // Setter // 67 | //////////// 68 | 69 | /** 70 | * @brief Sets the position of this ZoneUnion 71 | * All zone inside this one are moved. If there are no zones, nothing will happen. 72 | * @param p : the new position of this ZoneUnion 73 | */ 74 | virtual void setPosition(const Vector3D& p); 75 | 76 | /** 77 | * @brief Add a zone to this ZoneUnion 78 | * @param zone : the zone to add 79 | * @param check : check if the zone is already included 80 | */ 81 | virtual void addZone(Zone* zone, bool check = true); 82 | 83 | /** 84 | * @brief Remove a zone from this ZoneUnion 85 | * @param zone : the zone to remove 86 | */ 87 | virtual void removeZone(Zone* zone); 88 | 89 | //////////// 90 | // Getter // 91 | //////////// 92 | 93 | /** 94 | * @brief Gets the number of zones inside this one 95 | * @return amount of zones 96 | */ 97 | virtual size_t getNumberZones(); 98 | 99 | /** 100 | * @brief Gets whether the specified zone is included in this ZoneUnion 101 | * @return true if included, else false 102 | */ 103 | virtual bool isZoneIncluded(Zone* z); 104 | 105 | /** 106 | * @brief Gets the list 107 | * @return the internal std::list used to store zones 108 | */ 109 | virtual std::vector& getZones(); 110 | 111 | /////////////// 112 | // Interface // 113 | /////////////// 114 | 115 | virtual void generatePosition(Particle& particle,bool full) const; 116 | virtual bool contains(const Vector3D& v) const; 117 | virtual bool intersects(const Vector3D& v0,const Vector3D& v1,Vector3D* intersection,Vector3D* normal) const; 118 | virtual void moveAtBorder(Vector3D& v,bool inside) const; 119 | virtual Vector3D computeNormal(const Vector3D& point) const; 120 | 121 | private : 122 | 123 | std::vector Zones; 124 | }; 125 | 126 | 127 | inline ZoneUnion* ZoneUnion::create(size_t amountEstimation) 128 | { 129 | ZoneUnion* obj = new ZoneUnion(amountEstimation); 130 | registerObject(obj); 131 | return obj; 132 | } 133 | } 134 | 135 | #endif 136 | -------------------------------------------------------------------------------- /viewer/spark/RenderingAPIs/OpenGL/SPK_GLLineRenderer.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "RenderingAPIs/OpenGL/SPK_GLLineRenderer.h" 24 | #include "Core/SPK_Particle.h" 25 | #include "Core/SPK_Group.h" 26 | #include "Core/SPK_ArrayBuffer.h" 27 | 28 | namespace SPK 29 | { 30 | namespace GL 31 | { 32 | const std::string GLLineRenderer::GPU_BUFFER_NAME("SPK_GLLineRenderer_GPU"); 33 | 34 | float* GLLineRenderer::gpuBuffer = NULL; 35 | float* GLLineRenderer::gpuIterator = NULL; 36 | 37 | GLLineRenderer::GLLineRenderer(float length,float width) : 38 | GLRenderer(), 39 | LineRendererInterface(length,width) 40 | {} 41 | 42 | bool GLLineRenderer::checkBuffers(const Group& group) 43 | { 44 | FloatBuffer* fBuffer; 45 | if ((fBuffer = dynamic_cast(group.getBuffer(GPU_BUFFER_NAME))) == NULL) 46 | return false; 47 | 48 | gpuIterator = gpuBuffer = fBuffer->getData(); 49 | return true; 50 | } 51 | 52 | void GLLineRenderer::createBuffers(const Group& group) 53 | { 54 | FloatBuffer* fBuffer = dynamic_cast(group.createBuffer(GPU_BUFFER_NAME,FloatBufferCreator(14),0,false)); 55 | gpuIterator = gpuBuffer = fBuffer->getData(); 56 | } 57 | 58 | void GLLineRenderer::destroyBuffers(const Group& group) 59 | { 60 | group.destroyBuffer(GPU_BUFFER_NAME); 61 | } 62 | 63 | void GLLineRenderer::render(const Group& group) 64 | { 65 | if (!prepareBuffers(group)) 66 | return; 67 | 68 | initBlending(); 69 | initRenderingHints(); 70 | 71 | glLineWidth(width); 72 | glDisable(GL_TEXTURE_2D); 73 | glShadeModel(GL_FLAT); 74 | 75 | for (size_t i = 0; i < group.getNbParticles(); ++i) 76 | { 77 | const Particle& particle = group.getParticle(i); 78 | 79 | *(gpuIterator++) = particle.position().x; 80 | *(gpuIterator++) = particle.position().y; 81 | *(gpuIterator++) = particle.position().z; 82 | 83 | gpuIterator += 4; // skips the first vertex color data as GL_FLAT was forced 84 | 85 | *(gpuIterator++) = particle.position().x + particle.velocity().x * length; 86 | *(gpuIterator++) = particle.position().y + particle.velocity().y * length; 87 | *(gpuIterator++) = particle.position().z + particle.velocity().z * length; 88 | 89 | *(gpuIterator++) = particle.getR(); 90 | *(gpuIterator++) = particle.getG(); 91 | *(gpuIterator++) = particle.getB(); 92 | *(gpuIterator++) = particle.getParamCurrentValue(PARAM_ALPHA); 93 | } 94 | 95 | glEnableClientState(GL_VERTEX_ARRAY); 96 | glEnableClientState(GL_COLOR_ARRAY); 97 | 98 | // interleaves vertex and color data 99 | glVertexPointer(3,GL_FLOAT,7 * sizeof(float),gpuBuffer); 100 | glColorPointer(4,GL_FLOAT,7 * sizeof(float),gpuBuffer + 3); 101 | 102 | glDrawArrays(GL_LINES,0,group.getNbParticles() << 1); 103 | 104 | glDisableClientState(GL_VERTEX_ARRAY); 105 | glDisableClientState(GL_COLOR_ARRAY); 106 | } 107 | }} 108 | -------------------------------------------------------------------------------- /viewer/spark/RenderingAPIs/OpenGL/SPK_GLLineRenderer.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_GLLINERENDERER 24 | #define H_SPK_GLLINERENDERER 25 | 26 | #include "RenderingAPIs/OpenGL/SPK_GLRenderer.h" 27 | #include "Extensions/Renderers/SPK_LineRendererInterface.h" 28 | 29 | namespace SPK 30 | { 31 | namespace GL 32 | { 33 | /** 34 | * @class GLLineRenderer 35 | * @brief A Renderer drawing particles as OpenGL lines 36 | * 37 | * The length of the lines is function of the Particle velocity and is defined in the universe space 38 | * while the width is fixed and defines in the screen space (in pixels).
39 | *
40 | * Below are the parameters of Particle that are used in this Renderer (others have no effects) : 41 | *
    42 | *
  • SPK::PARAM_RED
  • 43 | *
  • SPK::PARAM_GREEN
  • 44 | *
  • SPK::PARAM_BLUE
  • 45 | *
  • SPK::PARAM_ALPHA (only if blending is enabled)
  • 46 | *
47 | */ 48 | class SPK_GL_PREFIX GLLineRenderer : public GLRenderer, public LineRendererInterface 49 | { 50 | SPK_IMPLEMENT_REGISTERABLE(GLLineRenderer) 51 | 52 | public : 53 | 54 | ////////////////// 55 | // Constructors // 56 | ////////////////// 57 | 58 | /** 59 | * @brief Constructor of GLLineRenderer 60 | * @param length : the length multiplier of this GLLineRenderer 61 | * @param width : the width of this GLLineRenderer in pixels 62 | */ 63 | GLLineRenderer(float length = 1.0f,float width = 1.0f); 64 | 65 | /** 66 | * @brief Creates and registers a new GLLineRenderer 67 | * @param length : the length multiplier of this GLLineRenderer 68 | * @param width : the width of this GLLineRenderer in pixels 69 | * @return A new registered GLLineRenderer 70 | * @since 1.04.00 71 | */ 72 | static GLLineRenderer* create(float length = 1.0f,float width = 1.0f); 73 | 74 | /////////////// 75 | // Interface // 76 | /////////////// 77 | 78 | virtual void createBuffers(const Group& group); 79 | virtual void destroyBuffers(const Group& group); 80 | 81 | virtual void render(const Group& group); 82 | 83 | protected : 84 | 85 | virtual bool checkBuffers(const Group& group); 86 | 87 | private : 88 | 89 | // vertex buffers and iterators 90 | static float* gpuBuffer; 91 | static float* gpuIterator; 92 | 93 | // buffers names 94 | static const std::string GPU_BUFFER_NAME; 95 | }; 96 | 97 | 98 | inline GLLineRenderer* GLLineRenderer::create(float length,float width) 99 | { 100 | GLLineRenderer* obj = new GLLineRenderer(length,width); 101 | registerObject(obj); 102 | return obj; 103 | } 104 | }} 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /viewer/spark/RenderingAPIs/OpenGL/SPK_GLPointRenderer.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "RenderingAPIs/OpenGL/SPK_GLPointRenderer.h" 24 | #include "Core/SPK_Particle.h" 25 | #include "Core/SPK_Group.h" 26 | 27 | namespace SPK 28 | { 29 | namespace GL 30 | { 31 | GLPointRenderer::GLPointRenderer(float size) : 32 | GLRenderer(), 33 | PointRendererInterface(POINT_SQUARE,size), 34 | GLExtHandler(), 35 | textureIndex(0), 36 | worldSize(false) 37 | {} 38 | 39 | bool GLPointRenderer::setType(PointType type) 40 | { 41 | if ((type == POINT_SPRITE)&&(!loadGLExtPointSprite())) 42 | return false; 43 | 44 | this->type = type; 45 | return true; 46 | } 47 | 48 | bool GLPointRenderer::enableWorldSize(bool worldSizeEnabled) 49 | { 50 | worldSize = ((worldSizeEnabled)&&(loadGLExtPointParameter())); 51 | return worldSize; 52 | } 53 | 54 | void GLPointRenderer::render(const Group& group) 55 | { 56 | initBlending(); 57 | initRenderingHints(); 58 | 59 | switch(type) 60 | { 61 | case POINT_SQUARE : 62 | glDisable(GL_TEXTURE_2D); 63 | glDisable(GL_POINT_SMOOTH); 64 | if (getPointSpriteGLExt() == SUPPORTED) 65 | disablePointSpriteGLExt(); 66 | break; 67 | 68 | case POINT_SPRITE : 69 | glEnable(GL_TEXTURE_2D); 70 | glBindTexture(GL_TEXTURE_2D,textureIndex); 71 | glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,getTextureBlending()); 72 | enablePointSpriteGLExt(); 73 | break; 74 | 75 | case POINT_CIRCLE : 76 | glDisable(GL_TEXTURE_2D); 77 | glEnable(GL_POINT_SMOOTH); 78 | if (getPointSpriteGLExt() == SUPPORTED) 79 | disablePointSpriteGLExt(); 80 | break; 81 | } 82 | 83 | if (worldSize) 84 | enablePointParameterGLExt(size,true); 85 | else 86 | { 87 | glPointSize(size); 88 | 89 | if (getPointParameterGLExt() == SUPPORTED) 90 | disablePointParameterGLExt(); 91 | } 92 | 93 | glEnableClientState(GL_VERTEX_ARRAY); 94 | glEnableClientState(GL_COLOR_ARRAY); 95 | 96 | glColorPointer(group.getModel()->isEnabled(PARAM_ALPHA) ? 4 : 3,GL_FLOAT,group.getParamStride(),group.getParamAddress(PARAM_RED)); 97 | glVertexPointer(3,GL_FLOAT,group.getPositionStride(),group.getPositionAddress()); 98 | 99 | glDrawArrays(GL_POINTS,0,group.getNbParticles()); 100 | 101 | glDisableClientState(GL_VERTEX_ARRAY); 102 | glDisableClientState(GL_COLOR_ARRAY); 103 | } 104 | }} 105 | -------------------------------------------------------------------------------- /viewer/spark/RenderingAPIs/OpenGL/SPK_GLRenderer.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #include "RenderingAPIs/OpenGL/SPK_GLRenderer.h" 24 | 25 | namespace SPK 26 | { 27 | namespace GL 28 | { 29 | GLRenderer::GLRenderer() : 30 | Renderer(), 31 | blendingEnabled(false), 32 | srcBlending(GL_SRC_ALPHA), 33 | destBlending(GL_ONE_MINUS_SRC_ALPHA), 34 | textureBlending(GL_MODULATE) 35 | {} 36 | 37 | GLRenderer::~GLRenderer() {} 38 | 39 | void GLRenderer::setBlending(BlendingMode blendMode) 40 | { 41 | switch(blendMode) 42 | { 43 | case BLENDING_NONE : 44 | srcBlending = GL_ONE; 45 | destBlending = GL_ZERO; 46 | blendingEnabled = false; 47 | break; 48 | 49 | case BLENDING_ADD : 50 | srcBlending = GL_SRC_ALPHA; 51 | destBlending = GL_ONE; 52 | blendingEnabled = true; 53 | break; 54 | 55 | case BLENDING_ALPHA : 56 | srcBlending = GL_SRC_ALPHA; 57 | destBlending = GL_ONE_MINUS_SRC_ALPHA; 58 | blendingEnabled = true; 59 | break; 60 | } 61 | } 62 | 63 | void GLRenderer::saveGLStates() 64 | { 65 | glPushAttrib(GL_POINT_BIT | 66 | GL_LINE_BIT | 67 | GL_ENABLE_BIT | 68 | GL_COLOR_BUFFER_BIT | 69 | GL_CURRENT_BIT | 70 | GL_TEXTURE_BIT | 71 | GL_DEPTH_BUFFER_BIT | 72 | GL_LIGHTING_BIT | 73 | GL_POLYGON_BIT); 74 | } 75 | 76 | void GLRenderer::restoreGLStates() 77 | { 78 | glPopAttrib(); 79 | } 80 | }} 81 | -------------------------------------------------------------------------------- /viewer/spark/RenderingAPIs/OpenGL/SPK_GL_DEF.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_GL_DEF 24 | #define H_SPK_GL_DEF 25 | 26 | #ifdef _MSC_VER 27 | #pragma warning(disable : 4275) // disables the warning about exporting DLL classes children of non DLL classes 28 | #endif 29 | 30 | // 1.02.02 Compatibility with older versions 31 | #ifdef SPK_DLL 32 | #define SPK_GL_IMPORT 33 | #endif 34 | 35 | #ifdef SPK_GL_EXPORT 36 | #define SPK_GL_PREFIX __declspec(dllexport) 37 | #elif defined(SPK_IMPORT) || defined(SPK_GL_IMPORT) 38 | #define SPK_GL_PREFIX __declspec(dllimport) 39 | #else 40 | #define SPK_GL_PREFIX 41 | #endif 42 | 43 | #ifndef SPK_NO_GL_INC 44 | 45 | #if defined(WIN32) || defined(_WIN32) 46 | #include 47 | #endif 48 | 49 | #if defined(__APPLE__) 50 | #include 51 | #elif defined(macintosh) 52 | #include 53 | #else 54 | #include 55 | #endif 56 | 57 | #if defined(linux) || defined(__linux) 58 | #define GLX_GLXEXT_PROTOTYPES 59 | #include 60 | #endif 61 | 62 | #endif 63 | 64 | // Defines the APIENTRY if not already done 65 | #ifndef APIENTRY 66 | #define APIENTRY 67 | #endif 68 | 69 | /** 70 | * @namespace SPK::GL 71 | * @brief the namespace for openGL dependent SPARK code 72 | * @since 1.01.00 73 | */ 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /viewer/spark/SPK.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | #ifndef H_SPK 23 | #define H_SPK 24 | 25 | // Defines 26 | #include "Core/SPK_DEF.h" 27 | 28 | // Core 29 | #include "Core/SPK_Vector3D.h" 30 | #include "Core/SPK_Buffer.h" // 1.03 31 | #include "Core/SPK_ArrayBuffer.h" // 1.03 32 | #include "Core/SPK_Registerable.h" // 1.03 33 | #include "Core/SPK_Transformable.h" // 1.03 34 | #include "Core/SPK_BufferHandler.h" // 1.04 35 | #include "Core/SPK_RegWrapper.h" // 1.03 36 | #include "Core/SPK_Renderer.h" 37 | #include "Core/SPK_System.h" 38 | #include "Core/SPK_Particle.h" 39 | #include "Core/SPK_Pool.h" 40 | #include "Core/SPK_Zone.h" 41 | #include "Core/SPK_Interpolator.h" // 1.05 42 | #include "Core/SPK_Model.h" 43 | #include "Core/SPK_Emitter.h" 44 | #include "Core/SPK_Modifier.h" 45 | #include "Core/SPK_Group.h" 46 | #include "Core/SPK_Factory.h" // 1.03 47 | 48 | // Zones 49 | #include "Extensions/Zones/SPK_AABox.h" 50 | #include "Extensions/Zones/SPK_Point.h" 51 | #include "Extensions/Zones/SPK_Sphere.h" 52 | #include "Extensions/Zones/SPK_Plane.h" 53 | #include "Extensions/Zones/SPK_Line.h" // 1.01 54 | #include "Extensions/Zones/SPK_Ring.h" // 1.05 55 | #include "Extensions/Zones/SPK_ZoneIntersection.h" 56 | #include "Extensions/Zones/SPK_ZoneUnion.h" 57 | #include "Extensions/Zones/SPK_Cylinder.h" 58 | 59 | // Emitters 60 | #include "Extensions/Emitters/SPK_StraightEmitter.h" 61 | #include "Extensions/Emitters/SPK_SphericEmitter.h" 62 | #include "Extensions/Emitters/SPK_NormalEmitter.h" // 1.02 63 | #include "Extensions/Emitters/SPK_RandomEmitter.h" // 1.02 64 | #include "Extensions/Emitters/SPK_StaticEmitter.h" // 1.05 65 | 66 | // Modifiers 67 | #include "Extensions/Modifiers/SPK_Obstacle.h" 68 | #include "Extensions/Modifiers/SPK_Destroyer.h" 69 | #include "Extensions/Modifiers/SPK_PointMass.h" 70 | #include "Extensions/Modifiers/SPK_ModifierGroup.h" // 1.02 71 | #include "Extensions/Modifiers/SPK_LinearForce.h" // 1.03 72 | #include "Extensions/Modifiers/SPK_Collision.h" // 1.04 73 | #include "Extensions/Modifiers/SPK_Vortex.h" // 1.05 74 | #include "Extensions/Modifiers/SPK_Rotator.h" // 1.05 75 | 76 | // Renderer Interfaces 77 | #include "Extensions/Renderers/SPK_PointRendererInterface.h" // 1.04 78 | #include "Extensions/Renderers/SPK_LineRendererInterface.h" // 1.04 79 | #include "Extensions/Renderers/SPK_Oriented3DRendererInterface.h" // 1.04 80 | #include "Extensions/Renderers/SPK_QuadRendererInterface.h" 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /viewer/spark/SPK_All.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | ////////////////////////////////////////////////////////////////////////////////// 24 | // Note : // 25 | // // 26 | // This file is used to speed up the compilation of a module, lower the size // 27 | // of the output library on certain compilers and allow deeper optimizations // 28 | // by reunifying all compilation units into one (single compilation unit // 29 | // method). // 30 | // // 31 | // Either only this file or all the files below should be compiled, not both or // 32 | // some 'multiple definition of symbols' errors will occur. // 33 | ////////////////////////////////////////////////////////////////////////////////// 34 | 35 | 36 | // Core 37 | #include "Core/SPK_DEF.cpp" 38 | #include "Core/SPK_Vector3D.cpp" 39 | #include "Core/SPK_Registerable.cpp" // 1.03 40 | #include "Core/SPK_Transformable.cpp" // 1.03 41 | #include "Core/SPK_BufferHandler.cpp" // 1.04 42 | #include "Core/SPK_Renderer.cpp" 43 | #include "Core/SPK_System.cpp" 44 | #include "Core/SPK_Particle.cpp" 45 | #include "Core/SPK_Zone.cpp" 46 | #include "Core/SPK_Interpolator.cpp" // 1.05 47 | #include "Core/SPK_Model.cpp" 48 | #include "Core/SPK_Emitter.cpp" 49 | #include "Core/SPK_Modifier.cpp" 50 | #include "Core/SPK_Group.cpp" 51 | #include "Core/SPK_Factory.cpp" // 1.03 52 | 53 | // Zones 54 | #include "Extensions/Zones/SPK_AABox.cpp" 55 | #include "Extensions/Zones/SPK_Point.cpp" 56 | #include "Extensions/Zones/SPK_Sphere.cpp" 57 | #include "Extensions/Zones/SPK_Plane.cpp" 58 | #include "Extensions/Zones/SPK_Line.cpp" // 1.01 59 | #include "Extensions/Zones/SPK_Ring.cpp" // 1.05 60 | #include "Extensions/Zones/SPK_Cylinder.cpp" 61 | #include "Extensions/Zones/SPK_ZoneUnion.cpp" 62 | #include "Extensions/Zones/SPK_ZoneIntersection.cpp" 63 | 64 | // Emitters 65 | #include "Extensions/Emitters/SPK_StraightEmitter.cpp" 66 | #include "Extensions/Emitters/SPK_SphericEmitter.cpp" 67 | #include "Extensions/Emitters/SPK_NormalEmitter.cpp" // 1.02 68 | #include "Extensions/Emitters/SPK_RandomEmitter.cpp" // 1.02 69 | 70 | // Modifiers 71 | #include "Extensions/Modifiers/SPK_Obstacle.cpp" 72 | #include "Extensions/Modifiers/SPK_Destroyer.cpp" 73 | #include "Extensions/Modifiers/SPK_PointMass.cpp" 74 | #include "Extensions/Modifiers/SPK_ModifierGroup.cpp" // 1.02 75 | #include "Extensions/Modifiers/SPK_LinearForce.cpp" // 1.03 76 | #include "Extensions/Modifiers/SPK_Collision.cpp" // 1.04 77 | #include "Extensions/Modifiers/SPK_Vortex.cpp" // 1.05 78 | 79 | // Renderer Interfaces 80 | #include "Extensions/Renderers/SPK_QuadRendererInterface.cpp" // 1.04 81 | #include "Extensions/Renderers/SPK_Oriented3DRendererInterface.cpp" // 1.04 82 | -------------------------------------------------------------------------------- /viewer/spark/SPK_GL.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | #ifndef H_SPK_GL 24 | #define H_SPK_GL 25 | 26 | // Defines 27 | #include "RenderingAPIs/OpenGL/SPK_GL_DEF.h" 28 | 29 | // Extension interface 30 | #include "RenderingAPIs/OpenGL/SPK_GLExtHandler.h" // 1.01 31 | 32 | // Mother Renderer 33 | #include "RenderingAPIs/OpenGL/SPK_GLRenderer.h" 34 | 35 | // Renderers 36 | #include "RenderingAPIs/OpenGL/SPK_GLPointRenderer.h" 37 | #include "RenderingAPIs/OpenGL/SPK_GLLineRenderer.h" 38 | #include "RenderingAPIs/OpenGL/SPK_GLQuadRenderer.h" 39 | #include "RenderingAPIs/OpenGL/SPK_GLLineTrailRenderer.h" // 1.03 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /viewer/spark/SPK_GL_All.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////////// 2 | // SPARK particle engine // 3 | // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // 4 | // // 5 | // This software is provided 'as-is', without any express or implied // 6 | // warranty. In no event will the authors be held liable for any damages // 7 | // arising from the use of this software. // 8 | // // 9 | // Permission is granted to anyone to use this software for any purpose, // 10 | // including commercial applications, and to alter it and redistribute it // 11 | // freely, subject to the following restrictions: // 12 | // // 13 | // 1. The origin of this software must not be misrepresented; you must not // 14 | // claim that you wrote the original software. If you use this software // 15 | // in a product, an acknowledgment in the product documentation would be // 16 | // appreciated but is not required. // 17 | // 2. Altered source versions must be plainly marked as such, and must not be // 18 | // misrepresented as being the original software. // 19 | // 3. This notice may not be removed or altered from any source distribution. // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | ////////////////////////////////////////////////////////////////////////////////// 24 | // Note : // 25 | // // 26 | // This file is used to speed up the compilation of a module, lower the size // 27 | // of the output library on certain compilers and allow deeper optimizations // 28 | // by reunifying all compilation units into one (single compilation unit // 29 | // method). // 30 | // // 31 | // Either only this file or all the files below should be compiled, not both or // 32 | // some 'multiple definition of symbols' errors will occur. // 33 | ////////////////////////////////////////////////////////////////////////////////// 34 | 35 | 36 | // GL 37 | #include "RenderingAPIs/OpenGL/SPK_GLExtHandler.cpp" // 1.01 38 | #include "RenderingAPIs/OpenGL/SPK_GLRenderer.cpp" 39 | #include "RenderingAPIs/OpenGL/SPK_GLLineRenderer.cpp" 40 | #include "RenderingAPIs/OpenGL/SPK_GLPointRenderer.cpp" 41 | #include "RenderingAPIs/OpenGL/SPK_GLQuadRenderer.cpp" 42 | #include "RenderingAPIs/OpenGL/SPK_GLLineTrailRenderer.cpp" // 1.03 43 | -------------------------------------------------------------------------------- /viewer/user_data_classes.h: -------------------------------------------------------------------------------- 1 | #ifndef H_UNITY2OSG_USERDATACLASSES 2 | #define H_UNITY2OSG_USERDATACLASSES 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "Particle.h" 10 | 11 | struct LightUniforms 12 | { 13 | osg::ref_ptr color; 14 | osg::ref_ptr direction; 15 | }; 16 | 17 | struct ShaderDataProxy : public osg::Program 18 | { 19 | virtual osg::Object* cloneType() const { return new ShaderDataProxy; } 20 | virtual const char* libraryName() const { return "nwTools"; } 21 | virtual const char* className() const { return "ShaderData"; } 22 | 23 | std::string shaderName; 24 | }; 25 | 26 | struct TerrainDataProxy : public osg::Geometry 27 | { 28 | virtual osg::Object* cloneType() const { return new TerrainDataProxy; } 29 | virtual const char* libraryName() const { return "nwTools"; } 30 | virtual const char* className() const { return "Terrain"; } 31 | 32 | std::vector heightMap; 33 | std::vector alphaMaps; 34 | std::vector splatTextures; 35 | std::vector splatTilingOffsets; 36 | osg::Vec2s heightMapSize, alphaMapSize; 37 | osg::Vec3 size; 38 | int layers; 39 | }; 40 | 41 | struct ParticleDataProxy : public osg::Geode 42 | { 43 | virtual osg::Object* cloneType() const { return new ParticleDataProxy; } 44 | virtual const char* libraryName() const { return "nwTools"; } 45 | virtual const char* className() const { return "ParticleSystem"; } 46 | 47 | // Basic attributes 48 | float duration, playingSpeed; 49 | int maxParticles; 50 | bool isLooping, isAutoStarted; 51 | osg::Vec3 gravity, rotation; 52 | osg::Vec4 startAttributes; // [life, size, speed, delay] 53 | osg::Vec4 startColor; 54 | 55 | // Emission module // TODO: bursts, curve data 56 | std::vector emissionRate; // [time, value, inTangent, outTangent] 57 | std::string emissionType; 58 | 59 | // Texture Sheet Animation module // TODO: curve data 60 | int tsaCycleCount; 61 | osg::Vec2 tsaNumTiles; 62 | std::vector tsaFrameOverTime; // [time, value, inTangent, outTangent] 63 | std::string tsaAnimationType; 64 | 65 | // Renderer module // TODO: render as mesh 66 | std::vector renderTextures; 67 | std::vector renderTilingOffsets; 68 | osg::Vec4 renderAttributes; // [minSize, maxSize, normalDir, sortFudge] 69 | std::string renderShapeMode, renderSortMode; 70 | }; 71 | 72 | typedef std::map ShaderDataProxyMap; 73 | typedef std::map TerrainDataProxyMap; 74 | typedef std::map ParticleDataProxyMap; 75 | 76 | extern void applyUserShaders( ShaderDataProxyMap& sd, const std::string& dbPath ); 77 | extern void applyUserTerrains( TerrainDataProxyMap& td, const std::string& dbPath ); 78 | extern void applyUserParticles( ParticleDataProxyMap& pd, nwCore::SparkUpdatingHandler*, const std::string& dbPath ); 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /viewer/utilities.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define STB_IMAGE_IMPLEMENTATION 7 | #include "stb_image.h" 8 | 9 | class ReaderWriterSTB : public osgDB::ReaderWriter 10 | { 11 | public: 12 | ReaderWriterSTB() { supportsExtension("psd", "Photoshop extension"); } 13 | virtual const char* className() const { return "Reader based on stb-image"; } 14 | 15 | virtual bool acceptsExtension( const std::string& ext ) const 16 | { return osgDB::equalCaseInsensitive(ext, "psd"); } 17 | 18 | virtual ReadResult readImage( const std::string& fileName, 19 | const osgDB::ReaderWriter::Options* options ) const 20 | { 21 | std::string ext = osgDB::getLowerCaseFileExtension(fileName); 22 | if ( !acceptsExtension(ext) ) return ReadResult::FILE_NOT_HANDLED; 23 | 24 | int w = 0, h = 0, numComponents = 0; 25 | unsigned char* data = stbi_load( fileName.c_str(), &w, &h, &numComponents, 0 ); 26 | if ( !data ) return ReadResult::ERROR_IN_READING_FILE; 27 | 28 | GLenum pixelFormat = GL_RGBA; 29 | switch ( numComponents ) 30 | { 31 | case 1: pixelFormat = GL_LUMINANCE; break; 32 | case 2: pixelFormat = GL_LUMINANCE_ALPHA; break; 33 | case 3: pixelFormat = GL_RGB; break; 34 | default: break; 35 | } 36 | 37 | osg::ref_ptr image = new osg::Image; 38 | image->setImage( w, h, 1, pixelFormat, pixelFormat, GL_UNSIGNED_BYTE, 39 | data, osg::Image::USE_MALLOC_FREE ); 40 | image->flipVertical(); 41 | return image.get(); 42 | } 43 | }; 44 | REGISTER_OSGPLUGIN( psd, ReaderWriterSTB ) 45 | --------------------------------------------------------------------------------