How to associate data with elements in a collection without changing elements?

Usually, when I do OpenGL programming, I have a Mesh class:

public class Mesh {

    // General 3D stuff
    List<Vector3f> vertices = new ArrayList<>();
    List<Vector3f> normals = new ArrayList<>();
    List<Vector2f> texCoords = new ArrayList<>();

    // OpenGL-specific stuff
    protected int vertices;
    protected boolean dirty;
    protected int vertexArrayID = -1;
    protected int vertexBufferID = -1;

    ...

}

However, the application I'm currently working on is much more general, and I want individual OpenGL elements to be separated from regular 3D materials. For example, someone might want to save the Mesh as an STL file and print it in 3D or send it to Raytrace to make it look beautiful. In this case, the data specific to OpenGL is useless.

In other words, this implementation of the Mesh class violates the principle of shared responsibility.

Now one solution I can think of is the following:

public class Mesh {

    // General 3D stuff
    List<Vector3f> vertices = new ArrayList<>();
    List<Vector3f> normals = new ArrayList<>();
    List<Vector2f> texCoords = new ArrayList<>();
}

public class GlMesh extends Mesh implements GlDrawable {

    // OpenGL-specific stuff
    protected int vertices;
    protected boolean dirty;
    protected int vertexArrayID = -1;
    protected int vertexBufferID = -1;
}

, OpenGL, , . Mesh , GlMesh, Mesh, , , OpenGL, .

- :

public class Mesh {

    // General 3D stuff
    List<Vector3f> vertices = new ArrayList<>();
    List<Vector3f> normals = new ArrayList<>();
    List<Vector2f> texCoords = new ArrayList<>();
}

public class GlMesh {

    // OpenGL-specific stuff
    protected int vertices;
    protected boolean dirty;
    protected int vertexArrayID = -1;
    protected int vertexBufferID = -1;
}

Map<Mesh, GlMesh> meshToGlMesh = (whatever)

Mesh GlMesh, .

, ?

+4
2

OpenGL, , , , , , Factory.

GLMesh , Mesh, Mesh, ( GLMesh, Mesh).

, , , , GLMesh. GLMeshFactory , , GLMesh, , Mesh. Mesh MeshFactory - getInstance(), , MeshFactory, ( - , Map , , Mesh).

MeshFactory Mesh , , , . GLMeshFactory ( , ). , .

+1

- :

public class Mesh implements GlMeshSupplier{  
    private final List<Vector3f> vertices = new ArrayList<>();
    private final List<Vector3f> normals = new ArrayList<>();
    private final List<Vector2f> texCoords = new ArrayList<>();

    private GlMesh glMesh;

    @Override
    public GlMesh getGlMesh() {
        return glMesh;
    }

    public void createGlMesh(){
        glMesh = new GlMesh();
    }    

    public List<Vector3f> getVertices() {
        return vertices;
    }

    public List<Vector3f> getNormals() {
        return normals;
    }

    public List<Vector2f> getTexCoords() {
        return texCoords;
    }

    public class GlMesh extends Mesh{
        // OpenGL-specific stuff
        protected int ivertices;
        protected boolean dirty;
        protected int vertexArrayID = -1;
        protected int vertexBufferID = -1;

        private GlMesh(){}

        @Override
        public List<Vector3f> getVertices() {
            return Mesh.this.vertices;
        }

        @Override
        public List<Vector3f> getNormals() {
            return Mesh.this.normals;
        }

        @Override
        public List<Vector2f> getTexCoords() {
            return Mesh.this.texCoords;
        }
    }

}

public interface GlMeshSupplier {
    public GlMesh getGlMesh();
}

Mesh GlMesh:

    //class Mesh
    public Mesh(){
        this.vertices = new ArrayList<>();
        this.normals = new ArrayList<>();
        this.texCoords = new ArrayList<>();
    }

    private Mesh(List<Vector3f> vertices, List<Vector3f> normals, List<Vector2f> texCoords){
        this.vertices = vertices;
        this.normals = normals;
        this.texCoords = texCoords;
    }

    //class GlMesh
    private GlMesh(){
        super(Mesh.this.vertices, Mesh.this.normals, Mesh.this.texCoords);
    }

Mesh GlMesh , . Mesh GlMesh. GlMesh ( Mesh) Mesh.

getVertices(), getNormals() getTexCoords() - , Mesh- Mesh, GlMesh.

Mesh , GlMeshSupplier, Mesh, GlMesh.

0

Source: https://habr.com/ru/post/1665131/


All Articles