Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.92 KB | None | 0 0
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22.  
  23. #pragma once
  24.  
  25. #include "../Scene/Component.h"
  26.  
  27. namespace Urho3D
  28. {
  29.  
  30. class Image;
  31. class IndexBuffer;
  32. class Material;
  33. class Node;
  34. class TerrainPatch;
  35.  
  36. /// Heightmap terrain component.
  37. class URHO3D_API Terrain : public Component
  38. {
  39.     URHO3D_OBJECT(Terrain, Component);
  40.  
  41. public:
  42.     /// Construct.
  43.     Terrain(Context* context);
  44.     /// Destruct.
  45.     ~Terrain();
  46.     /// Register object factory.
  47.     static void RegisterObject(Context* context);
  48.  
  49.     /// Handle attribute write access.
  50.     virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  51.     /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  52.     virtual void ApplyAttributes();
  53.     /// Handle enabled/disabled state change.
  54.     virtual void OnSetEnabled();
  55.  
  56.     /// Set patch quads per side. Must be a power of two.
  57.     void SetPatchSize(int size);
  58.     /// Set vertex (XZ) and height (Y) spacing.
  59.     void SetSpacing(const Vector3& spacing);
  60.     /// Set maximum number of LOD levels for terrain patches. This can be between 1-4.
  61.     void SetMaxLodLevels(unsigned levels);
  62.     /// Set LOD level used for terrain patch occlusion. By default (M_MAX_UNSIGNED) the coarsest. Since the LOD level used needs to be fixed, using finer LOD levels may result in false positive occlusion in cases where the actual rendered geometry is coarser, so use with caution.
  63.     void SetOcclusionLodLevel(unsigned level);
  64.     /// Set smoothing of heightmap.
  65.     void SetSmoothing(bool enable);
  66.     /// Set heightmap image. Dimensions should be a power of two + 1. Uses 8-bit grayscale, or optionally red as MSB and green as LSB for 16-bit accuracy. Return true if successful.
  67.     bool SetHeightMap(Image* image);
  68.     /// Set material.
  69.     void SetMaterial(Material* material);
  70.     /// Set draw distance for patches.
  71.     void SetDrawDistance(float distance);
  72.     /// Set shadow draw distance for patches.
  73.     void SetShadowDistance(float distance);
  74.     /// Set LOD bias for patches. Affects which terrain LOD to display.
  75.     void SetLodBias(float bias);
  76.     /// Set view mask for patches. Is and'ed with camera's view mask to see if the object should be rendered.
  77.     void SetViewMask(unsigned mask);
  78.     /// Set light mask for patches. Is and'ed with light's and zone's light mask to see if the object should be lit.
  79.     void SetLightMask(unsigned mask);
  80.     /// Set shadow mask for patches. Is and'ed with light's light mask and zone's shadow mask to see if the object should be rendered to a shadow map.
  81.     void SetShadowMask(unsigned mask);
  82.     /// Set zone mask for patches. Is and'ed with zone's zone mask to see if the object should belong to the zone.
  83.     void SetZoneMask(unsigned mask);
  84.     /// Set maximum number of per-pixel lights for patches. Default 0 is unlimited.
  85.     void SetMaxLights(unsigned num);
  86.     /// Set shadowcaster flag for patches.
  87.     void SetCastShadows(bool enable);
  88.     /// Set occlusion flag for patches. Occlusion uses the coarsest LOD by default.
  89.     void SetOccluder(bool enable);
  90.     /// Set occludee flag for patches.
  91.     void SetOccludee(bool enable);
  92.     /// Apply changes from the heightmap image.
  93.     void ApplyHeightMap();
  94.  
  95.     /// Return patch quads per side.
  96.     int GetPatchSize() const { return patchSize_; }
  97.  
  98.     /// Return vertex and height spacing.
  99.     const Vector3& GetSpacing() const { return spacing_; }
  100.  
  101.     /// Return heightmap size in vertices.
  102.     const IntVector2& GetNumVertices() const { return numVertices_; }
  103.  
  104.     /// Return heightmap size in patches.
  105.     const IntVector2& GetNumPatches() const { return numPatches_; }
  106.  
  107.     /// Return maximum number of LOD levels for terrain patches. This can be between 1-4.
  108.     unsigned GetMaxLodLevels() const { return maxLodLevels_; }
  109.    
  110.     /// Return LOD level used for occlusion.
  111.     unsigned GetOcclusionLodLevel() const { return occlusionLodLevel_; }
  112.    
  113.     /// Return whether smoothing is in use.
  114.     bool GetSmoothing() const { return smoothing_; }
  115.  
  116.     /// Return heightmap image.
  117.     Image* GetHeightMap() const;
  118.     /// Return material.
  119.     Material* GetMaterial() const;
  120.     /// Return patch by index.
  121.     TerrainPatch* GetPatch(unsigned index) const;
  122.     /// Return patch by patch coordinates.
  123.     TerrainPatch* GetPatch(int x, int z) const;
  124.     /// Return height at world coordinates.
  125.     float GetHeight(const Vector3& worldPosition) const;
  126.     /// Return normal at world coordinates.
  127.     Vector3 GetNormal(const Vector3& worldPosition) const;
  128.     /// Convert world position to heightmap pixel position. Note that the internal height data representation is reversed vertically, but in the heightmap image north is at the top.
  129.     IntVector2 WorldToHeightMap(const Vector3& worldPosition) const;
  130.  
  131.     /// Return raw height data.
  132.     SharedArrayPtr<float> GetHeightData() const { return heightData_; }
  133.  
  134.     /// Return draw distance.
  135.     float GetDrawDistance() const { return drawDistance_; }
  136.  
  137.     /// Return shadow draw distance.
  138.     float GetShadowDistance() const { return shadowDistance_; }
  139.  
  140.     /// Return LOD bias.
  141.     float GetLodBias() const { return lodBias_; }
  142.  
  143.     /// Return view mask.
  144.     unsigned GetViewMask() const { return viewMask_; }
  145.  
  146.     /// Return light mask.
  147.     unsigned GetLightMask() const { return lightMask_; }
  148.  
  149.     /// Return shadow mask.
  150.     unsigned GetShadowMask() const { return shadowMask_; }
  151.  
  152.     /// Return zone mask.
  153.     unsigned GetZoneMask() const { return zoneMask_; }
  154.  
  155.     /// Return maximum number of per-pixel lights.
  156.     unsigned GetMaxLights() const { return maxLights_; }
  157.  
  158.     /// Return visible flag.
  159.     bool IsVisible() const { return visible_; }
  160.  
  161.     /// Return shadowcaster flag.
  162.     bool GetCastShadows() const { return castShadows_; }
  163.  
  164.     /// Return occluder flag.
  165.     bool IsOccluder() const { return occluder_; }
  166.  
  167.     /// Return occludee flag.
  168.     bool IsOccludee() const { return occludee_; }
  169.  
  170.     /// Regenerate patch geometry.
  171.     void CreatePatchGeometry(TerrainPatch* patch);
  172.     /// Update patch based on LOD and neighbor LOD.
  173.     void UpdatePatchLod(TerrainPatch* patch);
  174.     /// Set heightmap attribute.
  175.     void SetHeightMapAttr(const ResourceRef& value);
  176.     /// Set material attribute.
  177.     void SetMaterialAttr(const ResourceRef& value);
  178.     /// Set patch size attribute.
  179.     void SetPatchSizeAttr(int value);
  180.     /// Set max LOD levels attribute.
  181.     void SetMaxLodLevelsAttr(unsigned value);
  182.     /// Set occlusion LOD level attribute.
  183.     void SetOcclusionLodLevelAttr(unsigned value);
  184.     /// Return heightmap attribute.
  185.     ResourceRef GetHeightMapAttr() const;
  186.     /// Return material attribute.
  187.     ResourceRef GetMaterialAttr() const;
  188.  
  189. private:
  190.     /// Regenerate terrain geometry.
  191.     void CreateGeometry();
  192.     /// Create index data shared by all patches.
  193.     void CreateIndexData();
  194.     /// Return an uninterpolated terrain height value, clamping to edges.
  195.     float GetRawHeight(int x, int z) const;
  196.     /// Return a source terrain height value, clamping to edges. The source data is used for smoothing.
  197.     float GetSourceHeight(int x, int z) const;
  198.     /// Return interpolated height for a specific LOD level.
  199.     float GetLodHeight(int x, int z, unsigned lodLevel) const;
  200.     /// Get slope-based terrain normal at position.
  201.     Vector3 GetRawNormal(int x, int z) const;
  202.     /// Calculate LOD errors for a patch.
  203.     void CalculateLodErrors(TerrainPatch* patch);
  204.     /// Set neighbors for a patch.
  205.     void SetNeighbors(TerrainPatch* patch);
  206.     /// Set heightmap image and optionally recreate the geometry immediately. Return true if successful.
  207.     bool SetHeightMapInternal(Image* image, bool recreateNow);
  208.     /// Handle heightmap image reload finished.
  209.     void HandleHeightMapReloadFinished(StringHash eventType, VariantMap& eventData);
  210.  
  211.     /// Shared index buffer.
  212.     SharedPtr<IndexBuffer> indexBuffer_;
  213.     /// Heightmap image.
  214.     SharedPtr<Image> heightMap_;
  215.     /// Height data.
  216.     SharedArrayPtr<float> heightData_;
  217.     /// Source height data for smoothing.
  218.     SharedArrayPtr<float> sourceHeightData_;
  219.     /// Material.
  220.     SharedPtr<Material> material_;
  221.     /// Terrain patches.
  222.     Vector<WeakPtr<TerrainPatch> > patches_;
  223.     /// Draw ranges for different LODs and stitching combinations.
  224.     PODVector<Pair<unsigned, unsigned> > drawRanges_;
  225.     /// Vertex and height spacing.
  226.     Vector3 spacing_;
  227.     /// Vertex and height sacing at the time of last update.
  228.     Vector3 lastSpacing_;
  229.     /// Origin of patches on the XZ-plane.
  230.     Vector2 patchWorldOrigin_;
  231.     /// Size of a patch on the XZ-plane.
  232.     Vector2 patchWorldSize_;
  233.     /// Terrain size in vertices.
  234.     IntVector2 numVertices_;
  235.     /// Terrain size in vertices at the time of last update.
  236.     IntVector2 lastNumVertices_;
  237.     /// Terrain size in patches.
  238.     IntVector2 numPatches_;
  239.     /// Patch size, quads per side.
  240.     int patchSize_;
  241.     /// Patch size at the time of last update.
  242.     int lastPatchSize_;
  243.     /// Number of terrain LOD levels.
  244.     unsigned numLodLevels_;
  245.     /// Maximum number of LOD levels.
  246.     unsigned maxLodLevels_;
  247.     /// LOD level used for occlusion.
  248.     unsigned occlusionLodLevel_;
  249.     /// Smoothing enable flag.
  250.     bool smoothing_;
  251.     /// Visible flag.
  252.     bool visible_;
  253.     /// Shadowcaster flag.
  254.     bool castShadows_;
  255.     /// Occluder flag.
  256.     bool occluder_;
  257.     /// Occludee flag.
  258.     bool occludee_;
  259.     /// View mask.
  260.     unsigned viewMask_;
  261.     /// Light mask.
  262.     unsigned lightMask_;
  263.     /// Shadow mask.
  264.     unsigned shadowMask_;
  265.     /// Zone mask.
  266.     unsigned zoneMask_;
  267.     /// Draw distance.
  268.     float drawDistance_;
  269.     /// Shadow distance.
  270.     float shadowDistance_;
  271.     /// LOD bias.
  272.     float lodBias_;
  273.     /// Maximum lights.
  274.     unsigned maxLights_;
  275.     /// Terrain needs regeneration flag.
  276.     bool recreateTerrain_;
  277. };
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement