aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java
blob: ebf9c4c87cd0f658a260192b0369aca64b628fda (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
package makamys.neodymium.renderer.attribs;

import org.lwjgl.opengl.GL20;

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

public class AttributeSet {
    private final List<VertexAttribute> attributes = new ArrayList<>();

    public void addAttribute(String name, int size, int elementSize, int type) {
        int index = attributes.size();
        attributes.add(new VertexAttribute(name, index, size, elementSize, type));
    }

    public void enable() {
        int stride = attributes.stream().mapToInt(attrib -> attrib.size * attrib.elementSize).sum();
        int offset = 0;
        for (int i = 0, size = attributes.size(); i < size; i++) {
            VertexAttribute attribute = attributes.get(i);
            GL20.glVertexAttribPointer(i, attribute.size, attribute.type, false, stride, offset);
            offset += attribute.size * attribute.elementSize;
            GL20.glEnableVertexAttribArray(i);
        }
    }

    public void addDefines(Map<String, String> defines) {
        for (VertexAttribute attribute: attributes) {
            defines.put("ATTRIB_" + attribute.name, Integer.toString(attribute.index));
        }
    }

    public static class VertexAttribute {
        public final String name;
        public final int index;
        public final int size;
        public final int elementSize;
        public final int type;

        private VertexAttribute(String name, int index, int size, int elementSize, int type) {
            this.name = name;
            this.index = index;
            this.size = size;
            this.elementSize = elementSize;
            this.type = type;
        }
    }
}