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

