<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.shc.silenceengine.graphics.models;

import com.shc.silenceengine.core.IResource;
import com.shc.silenceengine.core.SilenceEngine;
import com.shc.silenceengine.core.SilenceException;
import com.shc.silenceengine.graphics.Batcher;
import com.shc.silenceengine.graphics.Color;
import com.shc.silenceengine.graphics.Material;
import com.shc.silenceengine.graphics.opengl.Primitive;
import com.shc.silenceengine.graphics.opengl.Texture;
import com.shc.silenceengine.io.FilePath;
import com.shc.silenceengine.math.Transform;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <NAME>
 */
public class Model implements IResource
{
    private List<Mesh> meshes;

    public Model()
    {
        meshes = new ArrayList<>();
    }

    public static Model load(String filename)
    {
        FilePath resource = FilePath.getResourceFile(filename);
        if (resource.exists())
            return load(resource);

        return load(FilePath.getExternalFile(filename));
    }

    public static Model load(FilePath filePath)
    {
        if (filePath.getExtension().equalsIgnoreCase("obj"))
            return new OBJModel(filePath);

        throw new SilenceException("The model type you are trying to load is unsupported.");
    }

    public void render(Transform transform)
    {
        for (Mesh mesh : meshes)
            SilenceEngine.graphics.getStaticMesh(mesh).render(transform);
    }

    public void render(Batcher batcher, Transform transform)
    {
        Mesh m = getMeshes().get(0);
        Material originalMaterial = SilenceEngine.graphics.getCurrentMaterial();
        Texture originalTexture = Texture.CURRENT;

        SilenceEngine.graphics.useMaterial(m.getMaterial());
        m.getMaterial().getDiffuseMap().bind();

        if (transform != null) batcher.applyTransform(transform);
        batcher.begin(m.isWireFrame() ? Primitive.LINES : Primitive.TRIANGLES);
        {
            for (Mesh mesh : getMeshes())
            {
                if (!m.getMaterial().equals(mesh.getMaterial()))
                {
                    batcher.end();

                    SilenceEngine.graphics.useMaterial(mesh.getMaterial());
                    mesh.getMaterial().getDiffuseMap().bind();

                    m = mesh;
                    if (transform != null) batcher.applyTransform(transform);
                    batcher.begin(m.isWireFrame() ? Primitive.LINES : Primitive.TRIANGLES);
                }

                Color color = mesh.getMaterial().getDiffuse();

                for (Face face : mesh.getFaces())
                {
                    batcher.vertex(mesh.getVertices().get((int) face.vertexIndex.x));
                    batcher.normal(mesh.getNormals().get((int) face.normalIndex.x));
                    batcher.texCoord(mesh.getTexcoords().get((int) face.texcoordIndex.x));
                    if (mesh.getMaterial().getDiffuseMap().getID() == Texture.EMPTY.getID())
                        batcher.color(color.x, color.y, color.z, mesh.getMaterial().getDissolve());

                    batcher.vertex(mesh.getVertices().get((int) face.vertexIndex.y));
                    batcher.normal(mesh.getNormals().get((int) face.normalIndex.y));
                    batcher.texCoord(mesh.getTexcoords().get((int) face.texcoordIndex.y));
                    if (mesh.getMaterial().getDiffuseMap().getID() == Texture.EMPTY.getID())
                        batcher.color(color.x, color.y, color.z, mesh.getMaterial().getDissolve());

                    batcher.vertex(mesh.getVertices().get((int) face.vertexIndex.z));
                    batcher.normal(mesh.getNormals().get((int) face.normalIndex.z));
                    batcher.texCoord(mesh.getTexcoords().get((int) face.texcoordIndex.z));
                    if (mesh.getMaterial().getDiffuseMap().getID() == Texture.EMPTY.getID())
                        batcher.color(color.x, color.y, color.z, mesh.getMaterial().getDissolve());
                }
            }
        }
        batcher.end();

        SilenceEngine.graphics.useMaterial(originalMaterial);
        originalTexture.bind();
    }

    public void dispose()
    {
        for (Mesh mesh : getMeshes())
        {
            Material material = mesh.getMaterial();

            if (material.getDiffuseMap() != Texture.EMPTY)
                material.getDiffuseMap().dispose();

            if (material.getNormalMap() != Texture.EMPTY)
                material.getNormalMap().dispose();

            if (material.getSpecularMap() != Texture.EMPTY)
                material.getSpecularMap().dispose();
        }
    }

    public List<Mesh> getMeshes()
    {
        return meshes;
    }

    public boolean prefersStatic()
    {
        return meshes.stream().filter(Mesh::prefersStatic).count() > 0;
    }

    public void setPrefersStatic(boolean prefersStatic)
    {
        meshes.forEach(m -> m.setPreferStatic(prefersStatic));
    }

    public boolean isWireFrame()
    {
        return meshes.stream().filter(Mesh::isWireFrame).count() > 0;
    }

    public void setWireFrame(boolean wireFrame)
    {
        meshes.forEach(m -> m.setWireFrame(wireFrame));
    }
}
<commit_msg>Prevent model from disposing sub textures in material<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.shc.silenceengine.graphics.models;

import com.shc.silenceengine.core.IResource;
import com.shc.silenceengine.core.SilenceEngine;
import com.shc.silenceengine.core.SilenceException;
import com.shc.silenceengine.graphics.Batcher;
import com.shc.silenceengine.graphics.Color;
import com.shc.silenceengine.graphics.Material;
import com.shc.silenceengine.graphics.opengl.Primitive;
import com.shc.silenceengine.graphics.opengl.SubTexture;
import com.shc.silenceengine.graphics.opengl.Texture;
import com.shc.silenceengine.io.FilePath;
import com.shc.silenceengine.math.Transform;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <NAME>
 */
public class Model implements IResource
{
    private List<Mesh> meshes;

    public Model()
    {
        meshes = new ArrayList<>();
    }

    public static Model load(String filename)
    {
        FilePath resource = FilePath.getResourceFile(filename);
        if (resource.exists())
            return load(resource);

        return load(FilePath.getExternalFile(filename));
    }

    public static Model load(FilePath filePath)
    {
        if (filePath.getExtension().equalsIgnoreCase("obj"))
            return new OBJModel(filePath);

        throw new SilenceException("The model type you are trying to load is unsupported.");
    }

    public void render(Transform transform)
    {
        for (Mesh mesh : meshes)
            SilenceEngine.graphics.getStaticMesh(mesh).render(transform);
    }

    public void render(Batcher batcher, Transform transform)
    {
        Mesh m = getMeshes().get(0);
        Material originalMaterial = SilenceEngine.graphics.getCurrentMaterial();
        Texture originalTexture = Texture.CURRENT;

        SilenceEngine.graphics.useMaterial(m.getMaterial());
        m.getMaterial().getDiffuseMap().bind();

        if (transform != null) batcher.applyTransform(transform);
        batcher.begin(m.isWireFrame() ? Primitive.LINES : Primitive.TRIANGLES);
        {
            for (Mesh mesh : getMeshes())
            {
                if (!m.getMaterial().equals(mesh.getMaterial()))
                {
                    batcher.end();

                    SilenceEngine.graphics.useMaterial(mesh.getMaterial());
                    mesh.getMaterial().getDiffuseMap().bind();

                    m = mesh;
                    if (transform != null) batcher.applyTransform(transform);
                    batcher.begin(m.isWireFrame() ? Primitive.LINES : Primitive.TRIANGLES);
                }

                Color color = mesh.getMaterial().getDiffuse();

                for (Face face : mesh.getFaces())
                {
                    batcher.vertex(mesh.getVertices().get((int) face.vertexIndex.x));
                    batcher.normal(mesh.getNormals().get((int) face.normalIndex.x));
                    batcher.texCoord(mesh.getTexcoords().get((int) face.texcoordIndex.x));
                    if (mesh.getMaterial().getDiffuseMap().getID() == Texture.EMPTY.getID())
                        batcher.color(color.x, color.y, color.z, mesh.getMaterial().getDissolve());

                    batcher.vertex(mesh.getVertices().get((int) face.vertexIndex.y));
                    batcher.normal(mesh.getNormals().get((int) face.normalIndex.y));
                    batcher.texCoord(mesh.getTexcoords().get((int) face.texcoordIndex.y));
                    if (mesh.getMaterial().getDiffuseMap().getID() == Texture.EMPTY.getID())
                        batcher.color(color.x, color.y, color.z, mesh.getMaterial().getDissolve());

                    batcher.vertex(mesh.getVertices().get((int) face.vertexIndex.z));
                    batcher.normal(mesh.getNormals().get((int) face.normalIndex.z));
                    batcher.texCoord(mesh.getTexcoords().get((int) face.texcoordIndex.z));
                    if (mesh.getMaterial().getDiffuseMap().getID() == Texture.EMPTY.getID())
                        batcher.color(color.x, color.y, color.z, mesh.getMaterial().getDissolve());
                }
            }
        }
        batcher.end();

        SilenceEngine.graphics.useMaterial(originalMaterial);
        originalTexture.bind();
    }

    public void dispose()
    {
        for (Mesh mesh : getMeshes())
        {
            Material material = mesh.getMaterial();

            if (material.getDiffuseMap() != Texture.EMPTY)
                if (!(material.getDiffuseMap() instanceof SubTexture))
                    material.getDiffuseMap().dispose();

            if (material.getNormalMap() != Texture.EMPTY)
                if (!(material.getNormalMap() instanceof SubTexture))
                    material.getNormalMap().dispose();

            if (material.getSpecularMap() != Texture.EMPTY)
                if (!(material.getSpecularMap() instanceof SubTexture))
                    material.getSpecularMap().dispose();
        }
    }

    public List<Mesh> getMeshes()
    {
        return meshes;
    }

    public boolean prefersStatic()
    {
        return meshes.stream().filter(Mesh::prefersStatic).count() > 0;
    }

    public void setPrefersStatic(boolean prefersStatic)
    {
        meshes.forEach(m -> m.setPreferStatic(prefersStatic));
    }

    public boolean isWireFrame()
    {
        return meshes.stream().filter(Mesh::isWireFrame).count() > 0;
    }

    public void setWireFrame(boolean wireFrame)
    {
        meshes.forEach(m -> m.setWireFrame(wireFrame));
    }
}

{'fraction_non_alphanumeric': 0.1134020618556701, 'fraction_numerical': 0.0016925680873980612, 'mean_word_length': 2.501885775862069, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7659156', 'n_tokens_mistral': 3660, 'n_tokens_neox': 3481, 'n_words': 819}