예제 #1
0
        private void glControl_Paint(object sender, PaintEventArgs e)
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            Gl.glLoadIdentity();

            // Setup wireframe or solid fill drawing mode
            if (Wireframe)
            {
                Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_LINE);
            }
            else
            {
                Gl.glPolygonMode(Gl.GL_FRONT, Gl.GL_FILL);
            }

            Vector3 center = Vector3.Zero;

            Glu.gluLookAt(
                center.X, (double)scrollZoom.Value * 0.1d + center.Y, center.Z,
                center.X, center.Y, center.Z,
                0d, 0d, 1d);

            // Push the world matrix
            Gl.glPushMatrix();

            Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
            Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);

            // World rotations
            Gl.glRotatef((float)scrollRoll.Value, 1f, 0f, 0f);
            Gl.glRotatef((float)scrollPitch.Value, 0f, 1f, 0f);
            Gl.glRotatef((float)scrollYaw.Value, 0f, 0f, 1f);

            if (Prims != null)
            {
                for (int i = 0; i < Prims.Count; i++)
                {
                    Primitive prim = Prims[i].Prim;

                    if (i == cboPrim.SelectedIndex)
                    {
                        Gl.glColor3f(1f, 0f, 0f);
                    }
                    else
                    {
                        Gl.glColor3f(1f, 1f, 1f);
                    }

                    // Individual prim matrix
                    Gl.glPushMatrix();

                    // The root prim position is sim-relative, while child prim positions are
                    // parent-relative. We want to apply parent-relative translations but not
                    // sim-relative ones
                    if (Prims[i].Prim.ParentID != 0)
                    {
                        // Apply prim translation and rotation
                        Gl.glMultMatrixf(Math3D.CreateTranslationMatrix(prim.Position));
                        Gl.glMultMatrixf(Math3D.CreateRotationMatrix(prim.Rotation));
                    }

                    // Prim scaling
                    Gl.glScalef(prim.Scale.X, prim.Scale.Y, prim.Scale.Z);

                    // Draw the prim faces
                    for (int j = 0; j < Prims[i].Faces.Count; j++)
                    {
                        if (i == cboPrim.SelectedIndex)
                        {
                            // This prim is currently selected in the dropdown
                            //Gl.glColor3f(0f, 1f, 0f);
                            Gl.glColor3f(1f, 1f, 1f);

                            if (j == cboFace.SelectedIndex)
                            {
                                // This face is currently selected in the dropdown
                            }
                            else
                            {
                                // This face is not currently selected in the dropdown
                            }
                        }
                        else
                        {
                            // This prim is not currently selected in the dropdown
                            Gl.glColor3f(1f, 1f, 1f);
                        }

                        #region Texturing

                        Face     face = Prims[i].Faces[j];
                        FaceData data = (FaceData)face.UserData;

                        if (data.TexturePointer != 0)
                        {
                            // Set the color to solid white so the texture is not altered
                            //Gl.glColor3f(1f, 1f, 1f);
                            // Enable texturing for this face
                            Gl.glEnable(Gl.GL_TEXTURE_2D);
                        }
                        else
                        {
                            Gl.glDisable(Gl.GL_TEXTURE_2D);
                        }

                        // Bind the texture
                        Gl.glBindTexture(Gl.GL_TEXTURE_2D, data.TexturePointer);

                        #endregion Texturing

                        Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, data.TexCoords);
                        Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, data.Vertices);
                        Gl.glDrawElements(Gl.GL_TRIANGLES, data.Indices.Length, Gl.GL_UNSIGNED_SHORT, data.Indices);
                    }

                    // Pop the prim matrix
                    Gl.glPopMatrix();
                }
            }

            // Pop the world matrix
            Gl.glPopMatrix();

            Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
            Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);

            Gl.glFlush();
        }
예제 #2
0
        private void openPrimXMLToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Prims = null;
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "Prim Package (*.zip)|*.zip";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(),
                                                         System.IO.Path.GetRandomFileName());

                try
                {
                    // Create a temporary directory
                    Directory.CreateDirectory(tempPath);

                    FastZip fastzip = new FastZip();
                    fastzip.ExtractZip(dialog.FileName, tempPath, String.Empty);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

                // Check for the prims.xml file
                string primsFile = System.IO.Path.Combine(tempPath, "prims.xml");
                if (!File.Exists(primsFile))
                {
                    MessageBox.Show("prims.xml not found in the archive");
                    return;
                }

                LLSD llsd = null;

                try { llsd = LLSDParser.DeserializeXml(File.ReadAllText(primsFile)); }
                catch (Exception ex) { MessageBox.Show(ex.Message); }

                if (llsd != null && llsd.Type == LLSDType.Map)
                {
                    List <Primitive> primList = Helpers.LLSDToPrimList(llsd);
                    Prims = new List <FacetedMesh>(primList.Count);

                    for (int i = 0; i < primList.Count; i++)
                    {
                        // TODO: Can't render sculpted prims without the textures
                        if (primList[i].Sculpt.SculptTexture != UUID.Zero)
                        {
                            continue;
                        }

                        Primitive   prim = primList[i];
                        FacetedMesh mesh = Render.Plugin.GenerateFacetedMesh(prim, DetailLevel.Highest);

                        // Create a FaceData struct for each face that stores the 3D data
                        // in a Tao.OpenGL friendly format
                        for (int j = 0; j < mesh.Faces.Count; j++)
                        {
                            Face     face = mesh.Faces[j];
                            FaceData data = new FaceData();

                            // Vertices for this face
                            data.Vertices = new float[face.Vertices.Count * 3];
                            for (int k = 0; k < face.Vertices.Count; k++)
                            {
                                data.Vertices[k * 3 + 0] = face.Vertices[k].Position.X;
                                data.Vertices[k * 3 + 1] = face.Vertices[k].Position.Y;
                                data.Vertices[k * 3 + 2] = face.Vertices[k].Position.Z;
                            }

                            // Indices for this face
                            data.Indices = face.Indices.ToArray();

                            // Texture transform for this face
                            Primitive.TextureEntryFace teFace = prim.Textures.GetFace((uint)j);
                            Render.Plugin.TransformTexCoords(face.Vertices, face.Center, teFace);

                            // Texcoords for this face
                            data.TexCoords = new float[face.Vertices.Count * 2];
                            for (int k = 0; k < face.Vertices.Count; k++)
                            {
                                data.TexCoords[k * 2 + 0] = face.Vertices[k].TexCoord.X;
                                data.TexCoords[k * 2 + 1] = face.Vertices[k].TexCoord.Y;
                            }

                            // Texture for this face
                            if (LoadTexture(tempPath, teFace.TextureID, ref data.Texture))
                            {
                                Bitmap bitmap = new Bitmap(data.Texture);
                                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                                Rectangle  rectangle  = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                                BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                                Gl.glGenTextures(1, out data.TexturePointer);
                                Gl.glBindTexture(Gl.GL_TEXTURE_2D, data.TexturePointer);

                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR);
                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_GENERATE_MIPMAP, Gl.GL_TRUE);

                                Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGB8, bitmap.Width, bitmap.Height, Gl.GL_BGR, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);

                                bitmap.UnlockBits(bitmapData);
                                bitmap.Dispose();
                            }

                            // Set the UserData for this face to our FaceData struct
                            face.UserData = data;
                            mesh.Faces[j] = face;
                        }

                        Prims.Add(mesh);
                    }

                    // Setup the dropdown list of prims
                    PopulatePrimCombobox();

                    glControl.Invalidate();
                }
                else
                {
                    MessageBox.Show("Failed to load LLSD formatted primitive data from " + dialog.FileName);
                }

                Directory.Delete(tempPath);
            }
        }
예제 #3
0
        private void LoadMesh(FacetedMesh mesh, string basePath)
        {
            // Create a FaceData struct for each face that stores the 3D data
            // in a Tao.OpenGL friendly format
            for (int j = 0; j < mesh.Faces.Count; j++)
            {
                Face face = mesh.Faces[j];
                FaceData data = new FaceData();

                // Vertices for this face
                data.Vertices = new float[face.Vertices.Count * 3];
                for (int k = 0; k < face.Vertices.Count; k++)
                {
                    data.Vertices[k * 3 + 0] = face.Vertices[k].Position.X;
                    data.Vertices[k * 3 + 1] = face.Vertices[k].Position.Y;
                    data.Vertices[k * 3 + 2] = face.Vertices[k].Position.Z;
                }

                // Indices for this face
                data.Indices = face.Indices.ToArray();

                // Texture transform for this face
                Primitive.TextureEntryFace teFace = mesh.Prim.Textures.GetFace((uint)j);
                Render.Plugin.TransformTexCoords(face.Vertices, face.Center, teFace);

                // Texcoords for this face
                data.TexCoords = new float[face.Vertices.Count * 2];
                for (int k = 0; k < face.Vertices.Count; k++)
                {
                    data.TexCoords[k * 2 + 0] = face.Vertices[k].TexCoord.X;
                    data.TexCoords[k * 2 + 1] = face.Vertices[k].TexCoord.Y;
                }

                // Texture for this face
                if (!String.IsNullOrEmpty(basePath) && LoadTexture(basePath, teFace.TextureID, ref data.Texture))
                {
                    Bitmap bitmap = new Bitmap(data.Texture);
                    bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                    BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                    Gl.glGenTextures(1, out data.TexturePointer);
                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, data.TexturePointer);

                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_GENERATE_MIPMAP, Gl.GL_TRUE);

                    Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGB8, bitmap.Width, bitmap.Height, Gl.GL_BGR, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);

                    bitmap.UnlockBits(bitmapData);
                    bitmap.Dispose();
                }

                // Set the UserData for this face to our FaceData struct
                face.UserData = data;
                mesh.Faces[j] = face;
            }

            Prims.Add(mesh);
        }
예제 #4
0
        private void openPrimXMLToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Prims = null;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Prim Package (*.zip)|*.zip";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(),
                    System.IO.Path.GetRandomFileName());

                try
                {
                    // Create a temporary directory
                    Directory.CreateDirectory(tempPath);

                    FastZip fastzip = new FastZip();
                    fastzip.ExtractZip(dialog.FileName, tempPath, String.Empty);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

                // Check for the prims.xml file
                string primsFile = System.IO.Path.Combine(tempPath, "prims.xml");
                if (!File.Exists(primsFile))
                {
                    MessageBox.Show("prims.xml not found in the archive");
                    return;
                }

                OSD osd = null;

                try { osd = OSDParser.DeserializeLLSDXml(File.ReadAllText(primsFile)); }
                catch (Exception ex) { MessageBox.Show(ex.Message); }

                if (osd != null && osd.Type == OSDType.Map)
                {
                    List<Primitive> primList = Helpers.OSDToPrimList(osd);
                    Prims = new List<FacetedMesh>(primList.Count);

                    for (int i = 0; i < primList.Count; i++)
                    {
                        // TODO: Can't render sculpted prims without the textures
                        if (primList[i].Sculpt.SculptTexture != UUID.Zero)
                            continue;

                        Primitive prim = primList[i];
                        FacetedMesh mesh = Render.Plugin.GenerateFacetedMesh(prim, DetailLevel.Highest);

                        // Create a FaceData struct for each face that stores the 3D data
                        // in a Tao.OpenGL friendly format
                        for (int j = 0; j < mesh.Faces.Count; j++)
                        {
                            Face face = mesh.Faces[j];
                            FaceData data = new FaceData();
                            
                            // Vertices for this face
                            data.Vertices = new float[face.Vertices.Count * 3];
                            for (int k = 0; k < face.Vertices.Count; k++)
                            {
                                data.Vertices[k * 3 + 0] = face.Vertices[k].Position.X;
                                data.Vertices[k * 3 + 1] = face.Vertices[k].Position.Y;
                                data.Vertices[k * 3 + 2] = face.Vertices[k].Position.Z;
                            }

                            // Indices for this face
                            data.Indices = face.Indices.ToArray();

                            // Texture transform for this face
                            Primitive.TextureEntryFace teFace = prim.Textures.GetFace((uint)j);
                            Render.Plugin.TransformTexCoords(face.Vertices, face.Center, teFace);

                            // Texcoords for this face
                            data.TexCoords = new float[face.Vertices.Count * 2];
                            for (int k = 0; k < face.Vertices.Count; k++)
                            {
                                data.TexCoords[k * 2 + 0] = face.Vertices[k].TexCoord.X;
                                data.TexCoords[k * 2 + 1] = face.Vertices[k].TexCoord.Y;
                            }

                            // Texture for this face
                            if (LoadTexture(tempPath, teFace.TextureID, ref data.Texture))
                            {
                                Bitmap bitmap = new Bitmap(data.Texture);
                                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                                Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                                BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                                Gl.glGenTextures(1, out data.TexturePointer);
                                Gl.glBindTexture(Gl.GL_TEXTURE_2D, data.TexturePointer);

                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR);
                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
                                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_GENERATE_MIPMAP, Gl.GL_TRUE);

                                Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGB8, bitmap.Width, bitmap.Height, Gl.GL_BGR, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);

                                bitmap.UnlockBits(bitmapData);
                                bitmap.Dispose();
                            }

                            // Set the UserData for this face to our FaceData struct
                            face.UserData = data;
                            mesh.Faces[j] = face;
                        }

                        Prims.Add(mesh);
                    }

                    // Setup the dropdown list of prims
                    PopulatePrimCombobox();

                    glControl.Invalidate();
                }
                else
                {
                    MessageBox.Show("Failed to load LLSD formatted primitive data from " + dialog.FileName);
                }

                Directory.Delete(tempPath);
            }
        }
        private void LoadMesh(FacetedMesh mesh, string basePath)
        {
            // Create a FaceData struct for each face that stores the 3D data
            // in a Tao.OpenGL friendly format
            for (int j = 0; j < mesh.Faces.Count; j++)
            {
                Face     face = mesh.Faces[j];
                FaceData data = new FaceData();

                // Vertices for this face
                data.Vertices = new float[face.Vertices.Count * 3];
                for (int k = 0; k < face.Vertices.Count; k++)
                {
                    data.Vertices[k * 3 + 0] = face.Vertices[k].Position.X;
                    data.Vertices[k * 3 + 1] = face.Vertices[k].Position.Y;
                    data.Vertices[k * 3 + 2] = face.Vertices[k].Position.Z;
                }

                // Indices for this face
                data.Indices = face.Indices.ToArray();

                // Texture transform for this face
                Primitive.TextureEntryFace teFace = mesh.Prim.Textures.GetFace((uint)j);
                Render.Plugin.TransformTexCoords(face.Vertices, face.Center, teFace, mesh.Prim.Scale);

                // Texcoords for this face
                data.TexCoords = new float[face.Vertices.Count * 2];
                for (int k = 0; k < face.Vertices.Count; k++)
                {
                    data.TexCoords[k * 2 + 0] = face.Vertices[k].TexCoord.X;
                    data.TexCoords[k * 2 + 1] = face.Vertices[k].TexCoord.Y;
                }

                // Texture for this face
                if (!String.IsNullOrEmpty(basePath) && LoadTexture(basePath, teFace.TextureID, ref data.Texture))
                {
                    Bitmap bitmap = new Bitmap(data.Texture);
                    bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    Rectangle  rectangle  = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                    BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                    Gl.glGenTextures(1, out data.TexturePointer);
                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, data.TexturePointer);

                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_GENERATE_MIPMAP, Gl.GL_TRUE);

                    Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGB8, bitmap.Width, bitmap.Height, Gl.GL_BGR, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);

                    bitmap.UnlockBits(bitmapData);
                    bitmap.Dispose();
                }

                // Set the UserData for this face to our FaceData struct
                face.UserData = data;
                mesh.Faces[j] = face;
            }

            Prims.Add(mesh);
        }