Annotation of /trunk/IdealistViewer/Application/TextureManager.cs
Parent Directory
|
Revision Log
Revision 24 - (view) (download)
| 1 : | teravus | 16 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.IO; | ||
| 4 : | using System.Text; | ||
| 5 : | using OpenMetaverse; | ||
| 6 : | using OpenMetaverse.Rendering; | ||
| 7 : | using IrrlichtNETCP; | ||
| 8 : | |||
| 9 : | |||
| 10 : | namespace IdealistViewer | ||
| 11 : | { | ||
| 12 : | teravus | 22 | |
| 13 : | public delegate void TextureCallback(string texname, SceneNode node); | ||
| 14 : | teravus | 16 | public class TextureManager |
| 15 : | { | ||
| 16 : | teravus | 22 | public event TextureCallback OnTextureLoaded; |
| 17 : | |||
| 18 : | teravus | 16 | private VideoDriver driver = null; |
| 19 : | private string imagefolder = string.Empty; | ||
| 20 : | private Dictionary<UUID, Texture> memoryTextures = new Dictionary<UUID, Texture>(); | ||
| 21 : | private Dictionary<UUID, List<SceneNode>> ouststandingRequests = new Dictionary<UUID, List<SceneNode>>(); | ||
| 22 : | private IrrlichtDevice device = null; | ||
| 23 : | private SLProtocol m_user = null; | ||
| 24 : | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
| 25 : | |||
| 26 : | |||
| 27 : | public TextureManager(IrrlichtDevice pdevice, VideoDriver pDriver, string folder, SLProtocol pm_user) | ||
| 28 : | { | ||
| 29 : | driver = pDriver; | ||
| 30 : | device = pdevice; | ||
| 31 : | imagefolder = folder; | ||
| 32 : | m_user = pm_user; | ||
| 33 : | |||
| 34 : | m_user.OnImageReceived += imageReceivedCallback; | ||
| 35 : | } | ||
| 36 : | |||
| 37 : | public void RequestImage(UUID assetID, SceneNode requestor) | ||
| 38 : | { | ||
| 39 : | m_log.DebugFormat("[TEXTURE]: Object Requested TextureID: {0}", assetID); | ||
| 40 : | Texture tex = null; | ||
| 41 : | |||
| 42 : | lock (memoryTextures) | ||
| 43 : | { | ||
| 44 : | |||
| 45 : | if (memoryTextures.ContainsKey(assetID)) | ||
| 46 : | { | ||
| 47 : | tex = memoryTextures[assetID]; | ||
| 48 : | } | ||
| 49 : | } | ||
| 50 : | |||
| 51 : | if (tex != null) | ||
| 52 : | { | ||
| 53 : | applyTexture(tex, requestor); | ||
| 54 : | m_log.DebugFormat("[TEXTURE]: InProc texture found for TextureID: {0}", assetID); | ||
| 55 : | |||
| 56 : | return; | ||
| 57 : | } | ||
| 58 : | |||
| 59 : | string texturefolderpath = device.FileSystem.WorkingDirectory; //System.IO.Path.Combine(Util.ApplicationDataDirectory, imagefolder); | ||
| 60 : | //device.FileSystem.WorkingDirectory = texturepath; | ||
| 61 : | |||
| 62 : | if (File.Exists(System.IO.Path.Combine(texturefolderpath, assetID.ToString() + ".tga"))) | ||
| 63 : | { | ||
| 64 : | string oldfs = device.FileSystem.WorkingDirectory; | ||
| 65 : | device.FileSystem.WorkingDirectory = texturefolderpath; | ||
| 66 : | tex = driver.GetTexture(System.IO.Path.Combine(texturefolderpath, assetID.ToString() + ".tga")); | ||
| 67 : | if (tex != null) | ||
| 68 : | { | ||
| 69 : | lock (memoryTextures) | ||
| 70 : | { | ||
| 71 : | if (!memoryTextures.ContainsKey(assetID)) | ||
| 72 : | { | ||
| 73 : | memoryTextures.Add(assetID, tex); | ||
| 74 : | } | ||
| 75 : | } | ||
| 76 : | applyTexture(tex, requestor); | ||
| 77 : | m_log.DebugFormat("[TEXTURE]: Disk texture found for TextureID: {0}", assetID); | ||
| 78 : | return; | ||
| 79 : | } | ||
| 80 : | |||
| 81 : | } | ||
| 82 : | |||
| 83 : | lock (ouststandingRequests) | ||
| 84 : | { | ||
| 85 : | if (ouststandingRequests.ContainsKey(assetID)) | ||
| 86 : | { | ||
| 87 : | ouststandingRequests[assetID].Add(requestor); | ||
| 88 : | teravus | 21 | m_log.Debug("[TEXTURE]: Added to OutstandingRequest receivers"); |
| 89 : | teravus | 16 | return; |
| 90 : | } | ||
| 91 : | else | ||
| 92 : | { | ||
| 93 : | List<SceneNode> requestors = new List<SceneNode>(); | ||
| 94 : | requestors.Add(requestor); | ||
| 95 : | ouststandingRequests.Add(assetID,requestors); | ||
| 96 : | teravus | 21 | m_log.Debug("[TEXTURE]: created new OutstandingRequest receivers"); |
| 97 : | teravus | 16 | } |
| 98 : | } | ||
| 99 : | m_log.DebugFormat("[TEXTURE]: Requesting TextureID: {0} from simulator", assetID); | ||
| 100 : | m_user.RequestTexture(assetID); | ||
| 101 : | |||
| 102 : | } | ||
| 103 : | |||
| 104 : | teravus | 22 | public void applyTexture(Texture tex, SceneNode requestor) |
| 105 : | teravus | 16 | { |
| 106 : | teravus | 24 | try |
| 107 : | { | ||
| 108 : | requestor.SetMaterialTexture(0, tex); | ||
| 109 : | //requestor.SetMaterialType(MaterialType.DetailMap); | ||
| 110 : | requestor.SetMaterialFlag(MaterialFlag.Lighting, true); | ||
| 111 : | requestor.SetMaterialFlag(MaterialFlag.NormalizeNormals, true); | ||
| 112 : | requestor.SetMaterialFlag(MaterialFlag.BackFaceCulling, false); | ||
| 113 : | requestor.SetMaterialFlag(MaterialFlag.GouraudShading, true); | ||
| 114 : | teravus | 16 | |
| 115 : | teravus | 24 | } |
| 116 : | catch (AccessViolationException) | ||
| 117 : | { | ||
| 118 : | m_log.Error("[TEXTURE]: Failed to load texture."); | ||
| 119 : | } | ||
| 120 : | teravus | 16 | } |
| 121 : | |||
| 122 : | public void imageReceivedCallback(AssetTexture asset) | ||
| 123 : | { | ||
| 124 : | if (asset == null) | ||
| 125 : | teravus | 21 | { |
| 126 : | m_log.Debug("[TEXTURE]: GotLIBOMV callback but asset was null"); | ||
| 127 : | lock (ouststandingRequests) | ||
| 128 : | { | ||
| 129 : | } | ||
| 130 : | teravus | 16 | return; |
| 131 : | teravus | 21 | } |
| 132 : | teravus | 16 | m_log.Debug("[TEXTURE]: GotLIBOMV callback for asset" + asset.AssetID); |
| 133 : | bool result = false; | ||
| 134 : | |||
| 135 : | try | ||
| 136 : | { | ||
| 137 : | result = asset.Decode(); | ||
| 138 : | } | ||
| 139 : | catch (Exception) | ||
| 140 : | { | ||
| 141 : | teravus | 21 | m_log.Debug("[TEXTURE]: Failed to decode asset " + asset.AssetID); |
| 142 : | teravus | 16 | } |
| 143 : | if (result) | ||
| 144 : | { | ||
| 145 : | |||
| 146 : | string texturefolderpath = device.FileSystem.WorkingDirectory;//System.IO.Path.Combine(Util.ApplicationDataDirectory, imagefolder); | ||
| 147 : | |||
| 148 : | string texturepath = System.IO.Path.Combine(texturefolderpath,asset.AssetID.ToString() + ".tga"); | ||
| 149 : | byte[] imgdata = asset.Image.ExportTGA(); | ||
| 150 : | teravus | 21 | FileStream fi = (File.Open(texturepath, FileMode.Create)); |
| 151 : | BinaryWriter bw = new BinaryWriter(fi); | ||
| 152 : | teravus | 16 | bw.Write(imgdata); |
| 153 : | bw.Flush(); | ||
| 154 : | bw.Close(); | ||
| 155 : | teravus | 21 | //fi.Flush(); |
| 156 : | //fi.Close(); | ||
| 157 : | //fi.Dispose(); | ||
| 158 : | teravus | 16 | |
| 159 : | teravus | 21 | |
| 160 : | teravus | 22 | |
| 161 : | teravus | 16 | List<SceneNode> nodesToUpdate = new List<SceneNode>(); |
| 162 : | lock (ouststandingRequests) | ||
| 163 : | { | ||
| 164 : | if (ouststandingRequests.ContainsKey(asset.AssetID)) | ||
| 165 : | { | ||
| 166 : | nodesToUpdate = ouststandingRequests[asset.AssetID]; | ||
| 167 : | teravus | 21 | ouststandingRequests.Remove(asset.AssetID); |
| 168 : | teravus | 16 | } |
| 169 : | } | ||
| 170 : | lock (nodesToUpdate) | ||
| 171 : | { | ||
| 172 : | for (int i = 0; i < nodesToUpdate.Count; i++) | ||
| 173 : | { | ||
| 174 : | SceneNode node = nodesToUpdate[i]; | ||
| 175 : | |||
| 176 : | if (node != null) | ||
| 177 : | { | ||
| 178 : | teravus | 22 | if (OnTextureLoaded != null) |
| 179 : | { | ||
| 180 : | OnTextureLoaded(asset.AssetID.ToString() + ".tga", node); | ||
| 181 : | } | ||
| 182 : | teravus | 16 | |
| 183 : | } | ||
| 184 : | |||
| 185 : | } | ||
| 186 : | } | ||
| 187 : | } | ||
| 188 : | } | ||
| 189 : | } | ||
| 190 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

