Example #1
0
        public bool bind(DrawContext dc)
        {
            if (dc == null)
            {
                String message = Logging.getMessage("nullValue.DrawContextIsNull");
                Logging.logger().severe(message);
                throw new IllegalStateException(message);
            }

            Texture t = this.getTextureFromCache(dc);

            if (t == null)
            {
                t = this.initializeTexture(dc, this.imageSource);
                if (t != null)
                {
                    return(true); // texture was bound during initialization.
                }
            }

            if (t != null)
            {
                t.bind(dc.getGL());
            }

            if (t != null && this.width == 0 && this.height == 0)
            {
                this.width     = t.getWidth();
                this.height    = t.getHeight();
                this.texCoords = t.getImageTexCoords();
            }

            return(t != null);
        }
Example #2
0
 public Primitive Clone()
 {
     return(new Primitive(LocalVertices.ToArray(),
                          GlobalVertices.ToArray(),
                          Normals.ToArray(),
                          TextureCoords.ToArray(),
                          Indexes.ToArray(),
                          NormalIndexes.ToArray(),
                          TextureCoordsIndexes.ToArray(),
                          Pivot.Clone()));
 }
Example #3
0
        protected Texture getTextureFromCache(DrawContext dc)
        {
            Texture t = dc.getTextureCache().getTexture(this.imageSource);

            if (t != null && this.width == null)
            {
                this.width     = t.getWidth();
                this.height    = t.getHeight();
                this.texCoords = t.getImageTexCoords();
            }

            return(t);
        }
        /**
         * Creates a {@link Texture} from this instance's {@link TextureData} if the <code>TextureData</code> exists.
         *
         * @param dc the current draw context.
         *
         * @return the newly created texture, or null if this instance has no current <code>TextureData</code> or if texture
         *         creation failed.
         */
        protected Texture makeTextureFromTextureData(DrawContext dc)
        {
            if (dc == null)
            {
                String message = Logging.getMessage("nullValue.DrawContextIsNull");
                Logging.logger().severe(message);
                throw new IllegalStateException(message);
            }

            if (this.getTextureData() == null) // texture not in cache yet texture data is null, can't initialize
            {
                String msg = Logging.getMessage("nullValue.TextureDataIsNull");
                Logging.logger().severe(msg);
                throw new IllegalStateException(msg);
            }

            try
            {
                Texture texture = TextureIO.newTexture(this.getTextureData());
                if (texture == null)
                {
                    this.textureInitializationFailed = true;
                    return(null);
                }

                this.width     = texture.getWidth();
                this.height    = texture.getHeight();
                this.texCoords = texture.getImageTexCoords();

                this.setTextureParameters(dc, texture);

                // Cache the texture and release the texture data.
                dc.getTextureCache().put(this.getImageSource(), texture);
                this.setTextureData(null);

                return(texture);
            }
            catch (Exception e)
            {
                String name = this.isBufferedImageSource() ? "BufferedImage" : this.getImageSource().ToString();
                String msg  = Logging.getMessage("generic.ExceptionAttemptingToCreateTexture", name);
                Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
                return(null);
            }
        }
        /**
         * Returns the {@link Texture} associated with this instance.
         *
         * @param dc the current draw context.
         *
         * @return this instance's texture, or null if the texture does not currently exist.
         */
        protected Texture getTexture(DrawContext dc)
        {
            if (this.getImageSource() == null)
            {
                return(null);
            }

            Texture texture = dc.getTextureCache().getTexture(this.getImageSource());

            if (this.width == null && texture != null)
            {
                this.width     = texture.getWidth();
                this.height    = texture.getHeight();
                this.texCoords = texture.getImageTexCoords();
            }

            return(texture);
        }
Example #6
0
 private void ExpandTextureVertices()
 {
     if (facesTextureCoords.Count != 0)
     {
         foreach (int index in facesTextureCoords)
         {
             Vector3 vector = textureIndices[index];
             TextureCoords.Add(new Vector3(vector.X, vector.Y, vector.Z));
         }
     }
     else
     {
         //Generate same for all
         for (int i = 0; i < Faces.Count; i++)
         {
             TextureCoords.Add(new Vector3(1, 1, 0));
         }
     }
 }
        public void ParseObj(StreamReader data)
        {
            string  parse        = string.Empty;
            WtGroup CurrentGroup = null;

            while ((parse = data.ReadLine()) != null)
            {
                int castword = WordsParser.WordIndex(ref parse, 0);

                if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref Usemtl, 0, 6))
                {
                    WtGroup gp = new WtGroup();
                    CurrentGroup = gp;

                    Groups.Add(CurrentGroup);
                    castword = WordsParser.WordNext(ref parse, castword);
                    CurrentGroup.Material = _getMaterialFromName(WordsParser.WordStringValue(ref parse, castword));
                    continue;
                }

                if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref F, 0, 1))
                {
                    WtFace v;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.V0     = WordsParser.WordIntValue(ref parse, castword) - 1;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.VT0    = WordsParser.WordIntValue(ref parse, castword) - 1;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.VN0    = WordsParser.WordIntValue(ref parse, castword) - 1;



                    castword = WordsParser.WordNext(ref parse, castword);
                    v.V1     = WordsParser.WordIntValue(ref parse, castword) - 1;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.VT1    = WordsParser.WordIntValue(ref parse, castword) - 1;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.VN1    = WordsParser.WordIntValue(ref parse, castword) - 1;

                    castword = WordsParser.WordNext(ref parse, castword);
                    v.V2     = WordsParser.WordIntValue(ref parse, castword) - 1;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.VT2    = WordsParser.WordIntValue(ref parse, castword) - 1;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.VN2    = WordsParser.WordIntValue(ref parse, castword) - 1;


                    //Console.WriteLine("f {0}/{1}/{2} {3}/{4}/{5} {6}/{7}/{8}", v.V0, v.VN0, v.VT0, v.V1, v.VN1,v.VT1, v.V2, v.VN2, v.VT2);
                    CurrentGroup.Faces.Add(v);

                    continue;
                }


                if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref Vn, 0, 2))
                {
                    WtVector3 v;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.X      = WordsParser.WordFloatValue(ref parse, castword);
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.Y      = WordsParser.WordFloatValue(ref parse, castword);
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.Z      = WordsParser.WordFloatValue(ref parse, castword);
                    //Console.WriteLine("VN {0}, {1}, {2}", v.X, v.Y, v.Z);
                    Normals.Add(v);

                    continue;
                }

                if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref Vt, 0, 2))
                {
                    WtVector2 v;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.X      = WordsParser.WordFloatValue(ref parse, castword);
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.Y      = WordsParser.WordFloatValue(ref parse, castword);
                    //Console.WriteLine("VT {0}, {1}", v.X, v.Y);
                    TextureCoords.Add(v);

                    continue;
                }


                if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref V, 0, 1))
                {
                    WtVector3 v;
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.X      = WordsParser.WordFloatValue(ref parse, castword);
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.Y      = WordsParser.WordFloatValue(ref parse, castword);
                    castword = WordsParser.WordNext(ref parse, castword);
                    v.Z      = WordsParser.WordFloatValue(ref parse, castword);
                    //Console.WriteLine("V {0}, {1}, {2}",v.X,v.Y, v.Z);
                    Vertexes.Add(v);

                    continue;
                }
            }
        }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        private void LoadFromFile(string objFilename)
        {
            Filename = objFilename;

            // TODO read material file *.mtl

            var lines = File.ReadAllLines(objFilename);

            var timer = new HighPerformanceTimer();

            timer.Start();

            var min = new Vector3(float.MaxValue);
            var max = new Vector3(float.MinValue);

            for (var i = 0; i < lines.Length; i++)
            {
                var line = lines[i];

                try
                {
                    if (line.StartsWith("# object"))
                    {
                        if (string.IsNullOrEmpty(Name))
                        {
                            Name = line.Substring("# object ".Length);
                        }
                    }

                    // vertex
                    else if (line.StartsWith("v "))
                    {
                        var parts  = line.Substring("v  ".Length).Replace('.', ',').Split(' ');
                        var vertex = new Vector3(float.Parse(parts[0], System.Globalization.NumberStyles.Float), float.Parse(parts[1]), float.Parse(parts[2]));

                        if (vertex.X < min.X)
                        {
                            min.X = vertex.X;
                        }

                        if (vertex.Y < min.Y)
                        {
                            min.Y = vertex.Y;
                        }

                        if (vertex.Z < min.Z)
                        {
                            min.Z = vertex.Z;
                        }

                        if (vertex.X > max.X)
                        {
                            max.X = vertex.X;
                        }

                        if (vertex.Y > max.Y)
                        {
                            max.Y = vertex.Y;
                        }

                        if (vertex.Z > max.Z)
                        {
                            max.Z = vertex.Z;
                        }

                        Vertices.Add(vertex);
                    }

                    // vertex normal
                    else if (line.StartsWith("vn "))
                    {
                        var parts = line.Substring("vn ".Length).Replace('.', ',').Split(' ');
                        Normals.Add(new Vector3(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2])));
                    }

                    // vertex texture coordinates
                    else if (line.StartsWith("vt "))
                    {
                        var parts = line.Substring("vt ".Length).Replace('.', ',').Split(' ');
                        TextureCoords.Add(new Vector2(float.Parse(parts[0]), float.Parse(parts[1])));
                    }

                    // face indices
                    else if (line.StartsWith("f "))
                    {
                        var parts   = line.Substring("f ".Length).Split(' ');
                        var v1Parts = parts[0].Split('/');
                        var v2Parts = parts[1].Split('/');
                        var v3Parts = parts[2].Split('/');

                        Faces.Add(
                            new Face(
                                this,
                                int.Parse(v1Parts[0]) - 1, int.Parse(v2Parts[0]) - 1, int.Parse(v3Parts[0]) - 1,
                                int.Parse(v1Parts[1]) - 1, int.Parse(v2Parts[1]) - 1, int.Parse(v3Parts[1]) - 1,
                                int.Parse(v1Parts[2]) - 1, int.Parse(v2Parts[2]) - 1, int.Parse(v3Parts[2]) - 1
                                )
                            );
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("ObjImporter failed at line='{0}'", line), ex);
                }
            }

            BoundingBox = new AACell {
                Min = min, Max = max
            };

            timer.Stop();

            Log.Instance.AddMsg(LogLevel.Info, string.Format("WavefrontObjMesh '{0}' [vertices: {1}; faces: {2}; size: {3}] loaded in {4}", Name, Vertices.Count, Faces.Count, BoundingBox.Size.ToString(), FormatString.GetDuration((int)timer.Duration)));
        }
Example #9
0
        protected Texture initializeTexture(DrawContext dc, Object imageSource)
        {
            if (dc == null)
            {
                String message = Logging.getMessage("nullValue.DrawContextIsNull");
                Logging.logger().severe(message);
                throw new IllegalStateException(message);
            }

            if (this.textureInitializationFailed)
            {
                return(null);
            }

            Texture t;
            bool    haveMipMapData;
            GL      gl = dc.getGL();

            if (imageSource is String)
            {
                String path = (String)imageSource;

                Object streamOrException = WWIO.getFileOrResourceAsStream(path, this.GetType());
                if (streamOrException == null || streamOrException is Exception)
                {
                    Logging.logger().log(java.util.logging.Level.SEVERE, "generic.ExceptionAttemptingToReadImageFile",
                                         streamOrException != null ? streamOrException : path);
                    this.textureInitializationFailed = true;
                    return(null);
                }

                try
                {
                    TextureData td = OGLUtil.newTextureData(gl.getGLProfile(), (InputStream)streamOrException,
                                                            this.useMipMaps);
                    t = TextureIO.newTexture(td);
                    haveMipMapData = td.getMipmapData() != null;
                }
                catch (Exception e)
                {
                    String msg = Logging.getMessage("layers.TextureLayer.ExceptionAttemptingToReadTextureFile",
                                                    imageSource);
                    Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
                    this.textureInitializationFailed = true;
                    return(null);
                }
            }
            else if (imageSource is BufferedImage)
            {
                try
                {
                    TextureData td = AWTTextureIO.newTextureData(gl.getGLProfile(), (BufferedImage)imageSource,
                                                                 this.useMipMaps);
                    t = TextureIO.newTexture(td);
                    haveMipMapData = td.getMipmapData() != null;
                }
                catch (Exception e)
                {
                    String msg = Logging.getMessage("generic.IOExceptionDuringTextureInitialization");
                    Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
                    this.textureInitializationFailed = true;
                    return(null);
                }
            }
            else if (imageSource is URL)
            {
                try
                {
                    InputStream stream = ((URL)imageSource).openStream();
                    if (stream == null)
                    {
                        Logging.logger().log(java.util.logging.Level.SEVERE, "generic.ExceptionAttemptingToReadImageFile",
                                             imageSource);
                        this.textureInitializationFailed = true;
                        return(null);
                    }

                    TextureData td = OGLUtil.newTextureData(gl.getGLProfile(), stream, this.useMipMaps);
                    t = TextureIO.newTexture(td);
                    haveMipMapData = td.getMipmapData() != null;
                }
                catch (Exception e)
                {
                    String msg = Logging.getMessage("layers.TextureLayer.ExceptionAttemptingToReadTextureFile",
                                                    imageSource);
                    Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
                    this.textureInitializationFailed = true;
                    return(null);
                }
            }
            else
            {
                Logging.logger().log(java.util.logging.Level.SEVERE, "generic.UnrecognizedImageSourceType",
                                     imageSource.GetType().Name);
                this.textureInitializationFailed = true;
                return(null);
            }

            if (t == null) // In case JOGL TextureIO returned null
            {
                Logging.logger().log(java.util.logging.Level.SEVERE, "generic.TextureUnreadable",
                                     imageSource is String ? imageSource : imageSource.GetType().Name);
                this.textureInitializationFailed = true;
                return(null);
            }

            // Textures with the same path are assumed to be identical textures, so key the texture id off the
            // image source.
            dc.getTextureCache().put(imageSource, t);
            t.bind(gl);

            // Enable the appropriate mip-mapping texture filters if the caller has specified that mip-mapping should be
            // enabled, and the texture itself supports mip-mapping.
            bool useMipMapFilter = this.useMipMaps && (haveMipMapData || t.isUsingAutoMipmapGeneration());

            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
                               useMipMapFilter ? GL.GL_LINEAR_MIPMAP_LINEAR : GL.GL_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_CLAMP_TO_EDGE);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);

            if (this.isUseAnisotropy() && useMipMapFilter)
            {
                double maxAnisotropy = dc.getGLRuntimeCapabilities().getMaxTextureAnisotropy();
                if (dc.getGLRuntimeCapabilities().isUseAnisotropicTextureFilter() && maxAnisotropy >= 2.0)
                {
                    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAnisotropy);
                }
            }

            this.width     = t.getWidth();
            this.height    = t.getHeight();
            this.texCoords = t.getImageTexCoords();

            return(t);
        }
Example #10
0
 public MeshData GetMesh()
 {
     return(new MeshData(Faces.ToArray(), Vertices.ToArray(), VertexNomals.ToArray(), TextureCoords.ToArray()));
 }