aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/Meshing/SculptMesh.cs
blob: ff9964ec07f7ce9dc86c69d18174b9da2c96561c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using OpenJPEGNet;
using Image = System.Drawing.Image;

namespace OpenSim.Region.Physics.Meshing
{
    public class SculptMesh : Mesh
    {
        Image idata = null;
        Bitmap bLOD = null;
        Bitmap bBitmap = null;

        Vertex northpole = (Vertex)Vertex.Zero;
        Vertex southpole = (Vertex)Vertex.Zero;

        private int lod = 64;
        private const float RANGE = 128.0f;

        public SculptMesh(byte[] jpegData)
        {
            idata = OpenJPEG.DecodeToImage(jpegData);
            if (idata != null)
                bBitmap = new Bitmap(idata);

            
        }
        private Vertex ColorToVertex(Color input)
        {
            return new Vertex(
                ((float)input.R - 128) / RANGE,
                ((float)input.G - 128) / RANGE,
                ((float)input.B - 128) / RANGE);
        }
        private void LoadPoles()
        {
            northpole = (Vertex)Vertex.Zero;
            for (int x = 0; x < bBitmap.Width; x++)
            {
                northpole += ColorToVertex(GetPixel(0, 0));
            }
            northpole /= bBitmap.Width;

            southpole = (Vertex)Vertex.Zero;
            for (int x = 0; x < bBitmap.Width; x++)
            {
                southpole += ColorToVertex(GetPixel(bBitmap.Height - 1, (bBitmap.Height - 1)));
            }
            southpole /= bBitmap.Width;
        }

        private Color GetPixel(int x, int y)
        {
            return bLOD.GetPixel(x, y);
        }

        public int LOD
        {
            get
            {
                return (int)Math.Log(Scale, 2);
            }
            set
            {
                int power = value;
                if (power == 0)
                    power = 6;
                if (power < 2)
                    power = 2;
                if (power > 9)
                    power = 9;
                int t = (int)Math.Pow(2, power);
                if (t != Scale)
                {
                    lod = t;
                }
            }
        }

        public int Scale
        {
            get
            {
                return lod;
            }
        }
        private void DoLOD()
        {
            int x_max = Math.Min(Scale, bBitmap.Width);
            int y_max = Math.Min(Scale, bBitmap.Height);
            if (bBitmap.Width == x_max && bBitmap.Height == y_max)
                bLOD = bBitmap;

            else if (bLOD == null || x_max != bLOD.Width || y_max != bLOD.Height)//don't resize if you don't need to.
            {
                System.Drawing.Bitmap tile = new System.Drawing.Bitmap(bBitmap.Width * 2, bBitmap.Height, PixelFormat.Format24bppRgb);
                System.Drawing.Bitmap tile_LOD = new System.Drawing.Bitmap(x_max * 2, y_max, PixelFormat.Format24bppRgb);

                bLOD = new System.Drawing.Bitmap(x_max, y_max, PixelFormat.Format24bppRgb);
                bLOD.SetResolution(bBitmap.HorizontalResolution, bBitmap.VerticalResolution);

                System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(tile);
                grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

                grPhoto.DrawImage(bBitmap,
                    new System.Drawing.Rectangle(0, 0, bBitmap.Width / 2, bBitmap.Height),
                    new System.Drawing.Rectangle(bBitmap.Width / 2, 0, bBitmap.Width / 2, bBitmap.Height),
                    System.Drawing.GraphicsUnit.Pixel);

                grPhoto.DrawImage(bBitmap,
                    new System.Drawing.Rectangle((3 * bBitmap.Width) / 2, 0, bBitmap.Width / 2, bBitmap.Height),
                    new System.Drawing.Rectangle(0, 0, bBitmap.Width / 2, bBitmap.Height),
                    System.Drawing.GraphicsUnit.Pixel);

                grPhoto.DrawImage(bBitmap,
                    new System.Drawing.Rectangle(bBitmap.Width / 2, 0, bBitmap.Width, bBitmap.Height),
                    new System.Drawing.Rectangle(0, 0, bBitmap.Width, bBitmap.Height),
                    System.Drawing.GraphicsUnit.Pixel);

                grPhoto = System.Drawing.Graphics.FromImage(tile_LOD);
                grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;

                grPhoto.DrawImage(tile,
                    new System.Drawing.Rectangle(0, 0, tile_LOD.Width, tile_LOD.Height),
                    new System.Drawing.Rectangle(0, 0, tile.Width, tile.Height),
                    System.Drawing.GraphicsUnit.Pixel);

                grPhoto = System.Drawing.Graphics.FromImage(bLOD);
                grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

                grPhoto.DrawImage(tile_LOD,
                    new System.Drawing.Rectangle(0, 0, bLOD.Width, bLOD.Height),
                    new System.Drawing.Rectangle(tile_LOD.Width / 4, 0, tile_LOD.Width / 2, tile_LOD.Height),
                    System.Drawing.GraphicsUnit.Pixel);

                grPhoto.Dispose();
                tile_LOD.Dispose();
                tile.Dispose();
            }

        }
        public void clearStuff()
        {
            this.triangles.Clear();
            this.vertices.Clear();
            normals = new float[0];

        }
        public void processSculptTexture()
        {
            int x_max = Math.Min(Scale, bBitmap.Width);
            int y_max = Math.Min(Scale, bBitmap.Height);

            int COLUMNS = x_max + 1;

            Vertex[] sVertices = new Vertex[COLUMNS * y_max];
            float[] indices = new float[COLUMNS * (y_max - 1) * 6];

            for (int y = 0; y < y_max; y++)
            {
                for (int x = 0; x < x_max; x++)
                {
                    // Create the vertex
                    Vertex v1 = new Vertex(0,0,0);

                    // Create a vertex position from the RGB channels in the current pixel
                    int ypos = y * bLOD.Width;

                    
                    if (y == 0)
                    {
                        v1 = northpole;
                    }
                    else if (y == y_max - 1)
                    {
                        v1 = southpole;
                    }
                    else
                    {
                        v1 = ColorToVertex(GetPixel(x, y));
                    }
                    // Add the vertex for use later
                    Add(v1);
                    sVertices[y * COLUMNS + x] = v1;
                }
                Vertex tempVertex = vertices[y * COLUMNS];
                sVertices[y * COLUMNS + x_max] = tempVertex;
            }

            // Create the Triangles
            int i = 0;

            for (int y = 0; y < y_max - 1; y++)
            {
                int x;

                for (x = 0; x < x_max; x++)
                {
                    Triangle tri1 = new Triangle(sVertices[(y * COLUMNS + x)], sVertices[(y * COLUMNS + (x + 1))],
                                                 sVertices[((y + 1) * COLUMNS + (x + 1))]);
                    //indices[i++] = (ushort)(y * COLUMNS + x);
                    //indices[i++] = (ushort)(y * COLUMNS + (x + 1));
                    //indices[i++] = (ushort)((y + 1) * COLUMNS + (x + 1));
                    Add(tri1);
                    Triangle tri2 = new Triangle(sVertices[(y * COLUMNS + x)],sVertices[((y + 1) * COLUMNS + (x + 1))],
                                                 sVertices[((y + 1) * COLUMNS + x)]);

                    Add(tri2);
                    //indices[i++] = (ushort)(y * COLUMNS + x);
                    //indices[i++] = (ushort)((y + 1) * COLUMNS + (x + 1));
                    //indices[i++] = (ushort)((y + 1) * COLUMNS + x);
                }
                Triangle tri3 = new Triangle(sVertices[(y * x_max + x)], sVertices[(y * x_max + 0)], sVertices[((y + 1) * x_max + 0)]);
                Add(tri3);
                // Wrap the last cell in the row around
                //indices[i++] = (ushort)(y * x_max + x); //a
                //indices[i++] = (ushort)(y * x_max + 0); //b
                //indices[i++] = (ushort)((y + 1) * x_max + 0); //c

                Triangle tri4 = new Triangle(sVertices[(y * x_max + x)], sVertices[((y + 1) * x_max + 0)], sVertices[((y + 1) * x_max + x)]);
                Add(tri4);
                //indices[i++] = (ushort)(y * x_max + x); //a
                //indices[i++] = (ushort)((y + 1) * x_max + 0); //b
                //indices[i++] = (ushort)((y + 1) * x_max + x); //c
            }
        }
    }
}