Annotation of /trunk/IdealistViewer/Application/TextureManager.cs
Parent Directory
|
Revision Log
Revision 61 - (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 : | teravus | 61 | //using OpenMetaverse.Rendering; |
| 9 : | teravus | 16 | using IrrlichtNETCP; |
| 10 : | |||
| 11 : | |||
| 12 : | namespace IdealistViewer | ||
| 13 : | { | ||
| 14 : | teravus | 22 | |
| 15 : | teravus | 61 | public delegate void TextureCallback(string texname, VObject 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 | 61 | private Dictionary<UUID, List<VObject>> ouststandingRequests = new Dictionary<UUID, List<VObject>>(); |
| 24 : | teravus | 16 | 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 : | teravus | 61 | public void RequestImage(UUID assetID, VObject requestor) |
| 40 : | teravus | 16 | { |
| 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 : | teravus | 61 | List<VObject> requestors = new List<VObject>(); |
| 96 : | teravus | 16 | 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 | 61 | public void applyTexture(TextureExtended tex, VObject vObj) |
| 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 : | teravus | 61 | |
| 142 : | int mbcount = vObj.mesh.MeshBufferCount; | ||
| 143 : | for (int j = 0; j < mbcount; j++) | ||
| 144 : | { | ||
| 145 : | vObj.mesh.GetMeshBuffer(j).Material.Texture1 = tex; | ||
| 146 : | } | ||
| 147 : | |||
| 148 : | teravus | 37 | |
| 149 : | teravus | 36 | |
| 150 : | teravus | 61 | //requestor.node.SetMaterialTexture(0, tex); |
| 151 : | teravus | 35 | |
| 152 : | teravus | 16 | |
| 153 : | teravus | 61 | //requestor.node.SetMaterialFlag(MaterialFlag.Lighting, true); |
| 154 : | // requestor.node.SetMaterialFlag(MaterialFlag.NormalizeNormals, true); | ||
| 155 : | //requestor.node.SetMaterialFlag(MaterialFlag.BackFaceCulling, BaseIdealistViewer.backFaceCulling); | ||
| 156 : | //requestor.node.SetMaterialFlag(MaterialFlag.GouraudShading, true); | ||
| 157 : | //if (alphaimage) | ||
| 158 : | //{ | ||
| 159 : | // requestor.node.SetMaterialType(MaterialType.TransparentAlphaChannelRef); | ||
| 160 : | // } | ||
| 161 : | ApplyFaceSettings(vObj,alphaimage); | ||
| 162 : | |||
| 163 : | |||
| 164 : | teravus | 24 | } |
| 165 : | catch (AccessViolationException) | ||
| 166 : | { | ||
| 167 : | m_log.Error("[TEXTURE]: Failed to load texture."); | ||
| 168 : | } | ||
| 169 : | teravus | 16 | } |
| 170 : | teravus | 61 | |
| 171 : | public void ApplyFaceSettings(VObject vObj, bool alpha) | ||
| 172 : | { | ||
| 173 : | if (vObj.prim.Textures != null) | ||
| 174 : | { | ||
| 175 : | if (vObj.prim.Textures.DefaultTexture != null) | ||
| 176 : | { | ||
| 177 : | float shinyval = 0; | ||
| 178 : | switch (vObj.prim.Textures.DefaultTexture.Shiny) | ||
| 179 : | { | ||
| 180 : | case Shininess.Low: | ||
| 181 : | shinyval = 0.8f; | ||
| 182 : | break; | ||
| 183 : | case Shininess.Medium: | ||
| 184 : | shinyval = 0.7f; | ||
| 185 : | break; | ||
| 186 : | case Shininess.High: | ||
| 187 : | shinyval = 0.6f; | ||
| 188 : | break; | ||
| 189 : | } | ||
| 190 : | Color4 coldata = vObj.prim.Textures.DefaultTexture.RGBA; | ||
| 191 : | int mbcount = vObj.mesh.MeshBufferCount; | ||
| 192 : | for (int j = 0; j < mbcount; j++) | ||
| 193 : | { | ||
| 194 : | if (coldata != Color4.White) | ||
| 195 : | { | ||
| 196 : | vObj.mesh.GetMeshBuffer(j).Material.SpecularColor = new Color((int)(coldata.A * 255), Util.Clamp<int>((int)(coldata.R * 255) + 20, 0, 255), Util.Clamp<int>((int)(coldata.G * 255) + 20, 0, 255), Util.Clamp<int>((int)(coldata.B * 255) + 20, 0, 255)); | ||
| 197 : | vObj.mesh.GetMeshBuffer(j).Material.AmbientColor = new Color((int)( coldata.A * 255), (int)(coldata.R * 255), (int)(coldata.B * 255), (int)(coldata.G * 255)); | ||
| 198 : | vObj.mesh.GetMeshBuffer(j).Material.EmissiveColor = new Color((int)(coldata.A * 255), Util.Clamp<int>((int)(coldata.R * 255) - 20, 0, 255), Util.Clamp<int>((int)(coldata.G * 255) - 20, 0, 255), Util.Clamp<int>((int)(coldata.B * 255) - 20, 0, 255)); | ||
| 199 : | |||
| 200 : | } | ||
| 201 : | vObj.mesh.GetMeshBuffer(j).Material.Shininess = shinyval; | ||
| 202 : | if (vObj.prim.Textures.DefaultTexture.Fullbright) | ||
| 203 : | { | ||
| 204 : | vObj.mesh.GetMeshBuffer(j).Material.Lighting = !vObj.prim.Textures.DefaultTexture.Fullbright; | ||
| 205 : | } | ||
| 206 : | |||
| 207 : | } | ||
| 208 : | |||
| 209 : | } | ||
| 210 : | } | ||
| 211 : | SceneNode sn = device.SceneManager.AddMeshSceneNode(vObj.mesh, null, -1); | ||
| 212 : | sn.Position = vObj.node.Position; | ||
| 213 : | sn.Rotation = vObj.node.Rotation; | ||
| 214 : | sn.Scale = vObj.node.Scale; | ||
| 215 : | sn.TriangleSelector = vObj.node.TriangleSelector; | ||
| 216 : | if (alpha) | ||
| 217 : | { | ||
| 218 : | sn.SetMaterialType(MaterialType.TransparentAlphaChannelRef); | ||
| 219 : | } | ||
| 220 : | SceneNode oldnode = vObj.node; | ||
| 221 : | vObj.node = sn; | ||
| 222 : | device.SceneManager.AddToDeletionQueue(oldnode); | ||
| 223 : | |||
| 224 : | |||
| 225 : | } | ||
| 226 : | teravus | 16 | |
| 227 : | public void imageReceivedCallback(AssetTexture asset) | ||
| 228 : | { | ||
| 229 : | if (asset == null) | ||
| 230 : | teravus | 21 | { |
| 231 : | m_log.Debug("[TEXTURE]: GotLIBOMV callback but asset was null"); | ||
| 232 : | lock (ouststandingRequests) | ||
| 233 : | { | ||
| 234 : | } | ||
| 235 : | teravus | 16 | return; |
| 236 : | teravus | 21 | } |
| 237 : | teravus | 16 | m_log.Debug("[TEXTURE]: GotLIBOMV callback for asset" + asset.AssetID); |
| 238 : | bool result = false; | ||
| 239 : | |||
| 240 : | try | ||
| 241 : | { | ||
| 242 : | result = asset.Decode(); | ||
| 243 : | } | ||
| 244 : | catch (Exception) | ||
| 245 : | { | ||
| 246 : | teravus | 21 | m_log.Debug("[TEXTURE]: Failed to decode asset " + asset.AssetID); |
| 247 : | teravus | 16 | } |
| 248 : | if (result) | ||
| 249 : | { | ||
| 250 : | |||
| 251 : | string texturefolderpath = device.FileSystem.WorkingDirectory;//System.IO.Path.Combine(Util.ApplicationDataDirectory, imagefolder); | ||
| 252 : | |||
| 253 : | string texturepath = System.IO.Path.Combine(texturefolderpath,asset.AssetID.ToString() + ".tga"); | ||
| 254 : | byte[] imgdata = asset.Image.ExportTGA(); | ||
| 255 : | teravus | 21 | FileStream fi = (File.Open(texturepath, FileMode.Create)); |
| 256 : | BinaryWriter bw = new BinaryWriter(fi); | ||
| 257 : | teravus | 16 | bw.Write(imgdata); |
| 258 : | bw.Flush(); | ||
| 259 : | bw.Close(); | ||
| 260 : | teravus | 21 | //fi.Flush(); |
| 261 : | //fi.Close(); | ||
| 262 : | //fi.Dispose(); | ||
| 263 : | teravus | 16 | |
| 264 : | teravus | 21 | |
| 265 : | teravus | 22 | |
| 266 : | teravus | 61 | List<VObject> nodesToUpdate = new List<VObject>(); |
| 267 : | teravus | 16 | lock (ouststandingRequests) |
| 268 : | { | ||
| 269 : | if (ouststandingRequests.ContainsKey(asset.AssetID)) | ||
| 270 : | { | ||
| 271 : | nodesToUpdate = ouststandingRequests[asset.AssetID]; | ||
| 272 : | teravus | 21 | ouststandingRequests.Remove(asset.AssetID); |
| 273 : | teravus | 16 | } |
| 274 : | } | ||
| 275 : | lock (nodesToUpdate) | ||
| 276 : | { | ||
| 277 : | for (int i = 0; i < nodesToUpdate.Count; i++) | ||
| 278 : | { | ||
| 279 : | teravus | 61 | VObject vObj = nodesToUpdate[i]; |
| 280 : | teravus | 16 | |
| 281 : | teravus | 61 | if (vObj != null) |
| 282 : | teravus | 16 | { |
| 283 : | teravus | 22 | if (OnTextureLoaded != null) |
| 284 : | { | ||
| 285 : | teravus | 61 | OnTextureLoaded(asset.AssetID.ToString() + ".tga", vObj); |
| 286 : | teravus | 22 | } |
| 287 : | teravus | 16 | |
| 288 : | } | ||
| 289 : | |||
| 290 : | } | ||
| 291 : | } | ||
| 292 : | } | ||
| 293 : | } | ||
| 294 : | } | ||
| 295 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

