corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Data; |
||
5 | using System.Drawing; |
||
6 | using System.Text; |
||
7 | using System.Windows.Forms; |
||
8 | using System.IO; |
||
9 | using System.Xml; |
||
10 | |||
11 | using Tao.OpenGl; |
||
12 | using Tao.Platform.Windows; |
||
13 | |||
14 | using OpenMetaverse; |
||
15 | using OpenMetaverse.Imaging; |
||
16 | using OpenMetaverse.Rendering; |
||
17 | using OpenMetaverse.Assets; |
||
18 | |||
19 | namespace AvatarPreview |
||
20 | { |
||
21 | public partial class frmAvatar : Form |
||
22 | { |
||
23 | GridClient _client = new GridClient(); |
||
24 | Dictionary<string, GLMesh> _meshes = new Dictionary<string, GLMesh>(); |
||
25 | bool _wireframe = true; |
||
26 | bool _showSkirt = false; |
||
27 | |||
28 | public frmAvatar() |
||
29 | { |
||
30 | InitializeComponent(); |
||
31 | |||
32 | glControl.InitializeContexts(); |
||
33 | |||
34 | Gl.glShadeModel(Gl.GL_SMOOTH); |
||
35 | Gl.glClearColor(0f, 0f, 0f, 0f); |
||
36 | |||
37 | Gl.glClearDepth(1.0f); |
||
38 | Gl.glEnable(Gl.GL_DEPTH_TEST); |
||
39 | Gl.glDepthMask(Gl.GL_TRUE); |
||
40 | Gl.glDepthFunc(Gl.GL_LEQUAL); |
||
41 | Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST); |
||
42 | |||
43 | glControl_Resize(null, null); |
||
44 | } |
||
45 | |||
46 | private void lindenLabMeshToolStripMenuItem_Click(object sender, EventArgs e) |
||
47 | { |
||
48 | OpenFileDialog dialog = new OpenFileDialog(); |
||
49 | dialog.Filter = "avatar_lad.xml|avatar_lad.xml"; |
||
50 | |||
51 | if (dialog.ShowDialog() == DialogResult.OK) |
||
52 | { |
||
53 | _meshes.Clear(); |
||
54 | |||
55 | try |
||
56 | { |
||
57 | // Parse through avatar_lad.xml to find all of the mesh references |
||
58 | XmlDocument lad = new XmlDocument(); |
||
59 | lad.Load(dialog.FileName); |
||
60 | |||
61 | XmlNodeList meshes = lad.GetElementsByTagName("mesh"); |
||
62 | |||
63 | foreach (XmlNode meshNode in meshes) |
||
64 | { |
||
65 | string type = meshNode.Attributes.GetNamedItem("type").Value; |
||
66 | int lod = Int32.Parse(meshNode.Attributes.GetNamedItem("lod").Value); |
||
67 | string fileName = meshNode.Attributes.GetNamedItem("file_name").Value; |
||
68 | //string minPixelWidth = meshNode.Attributes.GetNamedItem("min_pixel_width").Value; |
||
69 | |||
70 | // Mash up the filename with the current path |
||
71 | fileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(dialog.FileName), fileName); |
||
72 | |||
73 | GLMesh mesh = (_meshes.ContainsKey(type) ? _meshes[type] : new GLMesh(type)); |
||
74 | |||
75 | if (lod == 0) |
||
76 | { |
||
77 | mesh.LoadMesh(fileName); |
||
78 | } |
||
79 | else |
||
80 | { |
||
81 | mesh.LoadLODMesh(lod, fileName); |
||
82 | } |
||
83 | |||
84 | _meshes[type] = mesh; |
||
85 | glControl_Resize(null, null); |
||
86 | glControl.Invalidate(); |
||
87 | } |
||
88 | } |
||
89 | catch (Exception ex) |
||
90 | { |
||
91 | MessageBox.Show("Failed to load avatar mesh: " + ex.Message); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | private void textureToolStripMenuItem_Click(object sender, EventArgs e) |
||
97 | { |
||
98 | |||
99 | } |
||
100 | |||
101 | private void exitToolStripMenuItem_Click(object sender, EventArgs e) |
||
102 | { |
||
103 | |||
104 | } |
||
105 | |||
106 | private void wireframeToolStripMenuItem_Click(object sender, EventArgs e) |
||
107 | { |
||
108 | wireframeToolStripMenuItem.Checked = !wireframeToolStripMenuItem.Checked; |
||
109 | _wireframe = wireframeToolStripMenuItem.Checked; |
||
110 | |||
111 | glControl.Invalidate(); |
||
112 | } |
||
113 | |||
114 | private void skirtToolStripMenuItem_Click(object sender, EventArgs e) |
||
115 | { |
||
116 | skirtToolStripMenuItem.Checked = !skirtToolStripMenuItem.Checked; |
||
117 | _showSkirt = skirtToolStripMenuItem.Checked; |
||
118 | |||
119 | glControl.Invalidate(); |
||
120 | } |
||
121 | |||
122 | private void aboutToolStripMenuItem_Click(object sender, EventArgs e) |
||
123 | { |
||
124 | MessageBox.Show( |
||
125 | "Written by John Hurliman <jhurliman@jhurliman.org> (http://www.jhurliman.org/)"); |
||
126 | } |
||
127 | |||
128 | private void glControl_Paint(object sender, PaintEventArgs e) |
||
129 | { |
||
130 | Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); |
||
131 | Gl.glLoadIdentity(); |
||
132 | |||
133 | // Setup wireframe or solid fill drawing mode |
||
134 | if (_wireframe) |
||
135 | Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_LINE); |
||
136 | else |
||
137 | Gl.glPolygonMode(Gl.GL_FRONT, Gl.GL_FILL); |
||
138 | |||
139 | // Push the world matrix |
||
140 | Gl.glPushMatrix(); |
||
141 | |||
142 | Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY); |
||
143 | Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY); |
||
144 | |||
145 | // World rotations |
||
146 | Gl.glRotatef((float)scrollRoll.Value, 1f, 0f, 0f); |
||
147 | Gl.glRotatef((float)scrollPitch.Value, 0f, 1f, 0f); |
||
148 | Gl.glRotatef((float)scrollYaw.Value, 0f, 0f, 1f); |
||
149 | |||
150 | if (_meshes.Count > 0) |
||
151 | { |
||
152 | foreach (GLMesh mesh in _meshes.Values) |
||
153 | { |
||
154 | if (!_showSkirt && mesh.Name == "skirtMesh") |
||
155 | continue; |
||
156 | |||
157 | Gl.glColor3f(1f, 1f, 1f); |
||
158 | |||
159 | // Individual prim matrix |
||
160 | Gl.glPushMatrix(); |
||
161 | |||
162 | //Gl.glTranslatef(mesh.Position.X, mesh.Position.Y, mesh.Position.Z); |
||
163 | |||
164 | Gl.glRotatef(mesh.RotationAngles.X, 1f, 0f, 0f); |
||
165 | Gl.glRotatef(mesh.RotationAngles.Y, 0f, 1f, 0f); |
||
166 | Gl.glRotatef(mesh.RotationAngles.Z, 0f, 0f, 1f); |
||
167 | |||
168 | Gl.glScalef(mesh.Scale.X, mesh.Scale.Y, mesh.Scale.Z); |
||
169 | |||
170 | // TODO: Texturing |
||
171 | |||
172 | Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, mesh.RenderData.TexCoords); |
||
173 | Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, mesh.RenderData.Vertices); |
||
174 | Gl.glDrawElements(Gl.GL_TRIANGLES, mesh.RenderData.Indices.Length, Gl.GL_UNSIGNED_SHORT, mesh.RenderData.Indices); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | // Pop the world matrix |
||
179 | Gl.glPopMatrix(); |
||
180 | |||
181 | Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY); |
||
182 | Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY); |
||
183 | |||
184 | Gl.glFlush(); |
||
185 | } |
||
186 | |||
187 | private void glControl_Resize(object sender, EventArgs e) |
||
188 | { |
||
189 | //Gl.glClearColor(0.39f, 0.58f, 0.93f, 1.0f); // Cornflower blue anyone? |
||
190 | Gl.glClearColor(0f, 0f, 0f, 1f); |
||
191 | |||
192 | Gl.glPushMatrix(); |
||
193 | Gl.glMatrixMode(Gl.GL_PROJECTION); |
||
194 | Gl.glLoadIdentity(); |
||
195 | |||
196 | Gl.glViewport(0, 0, glControl.Width, glControl.Height); |
||
197 | |||
198 | Glu.gluPerspective(50.0d, 1.0d, 0.001d, 50d); |
||
199 | |||
200 | Vector3 center = Vector3.Zero; |
||
201 | GLMesh head, lowerBody; |
||
202 | if (_meshes.TryGetValue("headMesh", out head) && _meshes.TryGetValue("lowerBodyMesh", out lowerBody)) |
||
203 | center = (head.RenderData.Center + lowerBody.RenderData.Center) / 2f; |
||
204 | |||
205 | Glu.gluLookAt( |
||
206 | center.X, (double)scrollZoom.Value * 0.1d + center.Y, center.Z, |
||
207 | center.X, (double)scrollZoom.Value * 0.1d + center.Y + 1d, center.Z, |
||
208 | 0d, 0d, 1d); |
||
209 | |||
210 | Gl.glMatrixMode(Gl.GL_MODELVIEW); |
||
211 | } |
||
212 | |||
213 | private void scroll_ValueChanged(object sender, EventArgs e) |
||
214 | { |
||
215 | glControl_Resize(null, null); |
||
216 | glControl.Invalidate(); |
||
217 | } |
||
218 | |||
219 | private void pic_MouseClick(object sender, MouseEventArgs e) |
||
220 | { |
||
221 | PictureBox control = (PictureBox)sender; |
||
222 | |||
223 | OpenFileDialog dialog = new OpenFileDialog(); |
||
224 | // TODO: Setup a dialog.Filter for supported image types |
||
225 | |||
226 | if (dialog.ShowDialog() == DialogResult.OK) |
||
227 | { |
||
228 | try |
||
229 | { |
||
230 | System.Drawing.Image image = System.Drawing.Image.FromFile(dialog.FileName); |
||
231 | |||
232 | #region Dimensions Check |
||
233 | |||
234 | if (control == picEyesBake) |
||
235 | { |
||
236 | // Eyes texture is 128x128 |
||
237 | if (Width != 128 || Height != 128) |
||
238 | { |
||
239 | Bitmap resized = new Bitmap(128, 128, image.PixelFormat); |
||
240 | Graphics graphics = Graphics.FromImage(resized); |
||
241 | |||
242 | graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
||
243 | graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
||
244 | graphics.DrawImage(image, 0, 0, 128, 128); |
||
245 | |||
246 | image.Dispose(); |
||
247 | image = resized; |
||
248 | } |
||
249 | } |
||
250 | else |
||
251 | { |
||
252 | // Other textures are 512x512 |
||
253 | if (Width != 128 || Height != 128) |
||
254 | { |
||
255 | Bitmap resized = new Bitmap(512, 512, image.PixelFormat); |
||
256 | Graphics graphics = Graphics.FromImage(resized); |
||
257 | |||
258 | graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
||
259 | graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
||
260 | graphics.DrawImage(image, 0, 0, 512, 512); |
||
261 | |||
262 | image.Dispose(); |
||
263 | image = resized; |
||
264 | } |
||
265 | } |
||
266 | |||
267 | #endregion Dimensions Check |
||
268 | |||
269 | // Set the control image |
||
270 | control.Image = image; |
||
271 | } |
||
272 | catch (Exception ex) |
||
273 | { |
||
274 | MessageBox.Show("Failed to load image: " + ex.Message); |
||
275 | } |
||
276 | } |
||
277 | else |
||
278 | { |
||
279 | control.Image = null; |
||
280 | } |
||
281 | |||
282 | #region Baking |
||
283 | |||
284 | Dictionary<int, float> paramValues = GetParamValues(); |
||
285 | Dictionary<AvatarTextureIndex, AssetTexture> layers = |
||
286 | new Dictionary<AvatarTextureIndex, AssetTexture>(); |
||
287 | int textureCount = 0; |
||
288 | |||
289 | if ((string)control.Tag == "Head") |
||
290 | { |
||
291 | if (picHair.Image != null) |
||
292 | { |
||
293 | layers.Add(AvatarTextureIndex.Hair, |
||
294 | new AssetTexture(new ManagedImage((Bitmap)picHair.Image))); |
||
295 | ++textureCount; |
||
296 | } |
||
297 | if (picHeadBodypaint.Image != null) |
||
298 | { |
||
299 | layers.Add(AvatarTextureIndex.HeadBodypaint, |
||
300 | new AssetTexture(new ManagedImage((Bitmap)picHeadBodypaint.Image))); |
||
301 | ++textureCount; |
||
302 | } |
||
303 | |||
304 | // Compute the head bake |
||
305 | Baker baker = new Baker(BakeType.Head); |
||
306 | |||
307 | foreach (KeyValuePair<AvatarTextureIndex, AssetTexture> kvp in layers) |
||
308 | { |
||
309 | AppearanceManager.TextureData tdata = new AppearanceManager.TextureData(); |
||
310 | tdata.Texture = kvp.Value; |
||
311 | baker.AddTexture(tdata); |
||
312 | } |
||
313 | |||
314 | baker.Bake(); |
||
315 | |||
316 | if (baker.BakedTexture != null) |
||
317 | { |
||
318 | AssetTexture bakeAsset = baker.BakedTexture; |
||
319 | // Baked textures use the alpha layer for other purposes, so we need to not use it |
||
320 | bakeAsset.Image.Channels = ManagedImage.ImageChannels.Color; |
||
321 | picHeadBake.Image = LoadTGAClass.LoadTGA(new MemoryStream(bakeAsset.Image.ExportTGA())); |
||
322 | } |
||
323 | else |
||
324 | { |
||
325 | MessageBox.Show("Failed to create the bake layer, unknown error"); |
||
326 | } |
||
327 | } |
||
328 | else if ((string)control.Tag == "Upper") |
||
329 | { |
||
330 | if (picUpperBodypaint.Image != null) |
||
331 | { |
||
332 | layers.Add(AvatarTextureIndex.UpperBodypaint, |
||
333 | new AssetTexture(new ManagedImage((Bitmap)picUpperBodypaint.Image))); |
||
334 | ++textureCount; |
||
335 | } |
||
336 | if (picUpperGloves.Image != null) |
||
337 | { |
||
338 | layers.Add(AvatarTextureIndex.UpperGloves, |
||
339 | new AssetTexture(new ManagedImage((Bitmap)picUpperGloves.Image))); |
||
340 | ++textureCount; |
||
341 | } |
||
342 | if (picUpperUndershirt.Image != null) |
||
343 | { |
||
344 | layers.Add(AvatarTextureIndex.UpperUndershirt, |
||
345 | new AssetTexture(new ManagedImage((Bitmap)picUpperUndershirt.Image))); |
||
346 | ++textureCount; |
||
347 | } |
||
348 | if (picUpperShirt.Image != null) |
||
349 | { |
||
350 | layers.Add(AvatarTextureIndex.UpperShirt, |
||
351 | new AssetTexture(new ManagedImage((Bitmap)picUpperShirt.Image))); |
||
352 | ++textureCount; |
||
353 | } |
||
354 | if (picUpperJacket.Image != null) |
||
355 | { |
||
356 | layers.Add(AvatarTextureIndex.UpperJacket, |
||
357 | new AssetTexture(new ManagedImage((Bitmap)picUpperJacket.Image))); |
||
358 | ++textureCount; |
||
359 | } |
||
360 | |||
361 | // Compute the upper body bake |
||
362 | Baker baker = new Baker(BakeType.UpperBody); |
||
363 | |||
364 | foreach (KeyValuePair<AvatarTextureIndex, AssetTexture> kvp in layers) |
||
365 | { |
||
366 | AppearanceManager.TextureData tdata = new AppearanceManager.TextureData(); |
||
367 | tdata.Texture = kvp.Value; |
||
368 | baker.AddTexture(tdata); |
||
369 | } |
||
370 | |||
371 | baker.Bake(); |
||
372 | |||
373 | if (baker.BakedTexture != null) |
||
374 | { |
||
375 | AssetTexture bakeAsset = baker.BakedTexture; |
||
376 | // Baked textures use the alpha layer for other purposes, so we need to not use it |
||
377 | bakeAsset.Image.Channels = ManagedImage.ImageChannels.Color; |
||
378 | picUpperBodyBake.Image = LoadTGAClass.LoadTGA(new MemoryStream(bakeAsset.Image.ExportTGA())); |
||
379 | } |
||
380 | else |
||
381 | { |
||
382 | MessageBox.Show("Failed to create the bake layer, unknown error"); |
||
383 | } |
||
384 | } |
||
385 | else if ((string)control.Tag == "Lower") |
||
386 | { |
||
387 | if (picLowerBodypaint.Image != null) |
||
388 | { |
||
389 | layers.Add(AvatarTextureIndex.LowerBodypaint, |
||
390 | new AssetTexture(new ManagedImage((Bitmap)picLowerBodypaint.Image))); |
||
391 | ++textureCount; |
||
392 | } |
||
393 | if (picLowerUnderpants.Image != null) |
||
394 | { |
||
395 | layers.Add(AvatarTextureIndex.LowerUnderpants, |
||
396 | new AssetTexture(new ManagedImage((Bitmap)picLowerUnderpants.Image))); |
||
397 | ++textureCount; |
||
398 | } |
||
399 | if (picLowerSocks.Image != null) |
||
400 | { |
||
401 | layers.Add(AvatarTextureIndex.LowerSocks, |
||
402 | new AssetTexture(new ManagedImage((Bitmap)picLowerSocks.Image))); |
||
403 | ++textureCount; |
||
404 | } |
||
405 | if (picLowerShoes.Image != null) |
||
406 | { |
||
407 | layers.Add(AvatarTextureIndex.LowerShoes, |
||
408 | new AssetTexture(new ManagedImage((Bitmap)picLowerShoes.Image))); |
||
409 | ++textureCount; |
||
410 | } |
||
411 | if (picLowerPants.Image != null) |
||
412 | { |
||
413 | layers.Add(AvatarTextureIndex.LowerPants, |
||
414 | new AssetTexture(new ManagedImage((Bitmap)picLowerPants.Image))); |
||
415 | ++textureCount; |
||
416 | } |
||
417 | |||
418 | // Compute the lower body bake |
||
419 | Baker baker = new Baker(BakeType.LowerBody); |
||
420 | |||
421 | foreach (KeyValuePair<AvatarTextureIndex, AssetTexture> kvp in layers) |
||
422 | { |
||
423 | AppearanceManager.TextureData tdata = new AppearanceManager.TextureData(); |
||
424 | tdata.Texture = kvp.Value; |
||
425 | baker.AddTexture(tdata); |
||
426 | } |
||
427 | |||
428 | baker.Bake(); |
||
429 | |||
430 | if (baker.BakedTexture != null) |
||
431 | { |
||
432 | AssetTexture bakeAsset = baker.BakedTexture; |
||
433 | // Baked textures use the alpha layer for other purposes, so we need to not use it |
||
434 | bakeAsset.Image.Channels = ManagedImage.ImageChannels.Color; |
||
435 | picLowerBodyBake.Image = LoadTGAClass.LoadTGA(new MemoryStream(bakeAsset.Image.ExportTGA())); |
||
436 | } |
||
437 | else |
||
438 | { |
||
439 | MessageBox.Show("Failed to create the bake layer, unknown error"); |
||
440 | } |
||
441 | } |
||
442 | else if ((string)control.Tag == "Bake") |
||
443 | { |
||
444 | // Bake image has been set manually, no need to manually calculate a bake |
||
445 | // FIXME: |
||
446 | } |
||
447 | |||
448 | #endregion Baking |
||
449 | } |
||
450 | |||
451 | private Dictionary<int, float> GetParamValues() |
||
452 | { |
||
453 | Dictionary<int, float> paramValues = new Dictionary<int, float>(VisualParams.Params.Count); |
||
454 | |||
455 | foreach (KeyValuePair<int, VisualParam> kvp in VisualParams.Params) |
||
456 | { |
||
457 | VisualParam vp = kvp.Value; |
||
458 | paramValues.Add(vp.ParamID, vp.DefaultValue); |
||
459 | } |
||
460 | |||
461 | return paramValues; |
||
462 | } |
||
463 | } |
||
464 | } |