130 lines
4.9 KiB
Java
130 lines
4.9 KiB
Java
package speiger.src.coreengine.rendering.utils;
|
|
|
|
import java.nio.IntBuffer;
|
|
|
|
import org.lwjgl.opengl.GL11;
|
|
import org.lwjgl.opengl.GL13;
|
|
import org.lwjgl.opengl.GL31;
|
|
import org.lwjgl.opengl.GL44;
|
|
|
|
import speiger.src.collections.objects.lists.ObjectArrayList;
|
|
import speiger.src.collections.objects.lists.ObjectList;
|
|
import speiger.src.coreengine.rendering.shader.ShaderTracker;
|
|
import speiger.src.coreengine.rendering.shader.uniform.GlobalUniforms;
|
|
import speiger.src.coreengine.rendering.utils.states.BlendState;
|
|
import speiger.src.coreengine.rendering.utils.states.CullState;
|
|
import speiger.src.coreengine.rendering.utils.states.FloatState;
|
|
import speiger.src.coreengine.rendering.utils.states.GLProvoking;
|
|
import speiger.src.coreengine.rendering.utils.states.GLState;
|
|
import speiger.src.coreengine.rendering.utils.states.GLWireFrame;
|
|
import speiger.src.coreengine.rendering.utils.states.IGLState;
|
|
import speiger.src.coreengine.rendering.utils.states.TextureState;
|
|
import speiger.src.coreengine.rendering.utils.values.GLCullType;
|
|
import speiger.src.coreengine.utils.counters.averager.Counter;
|
|
|
|
public class GLStateTracker {
|
|
static final ObjectList<IGLState> ALL_STATES = new ObjectArrayList<>();
|
|
public static final GLState MULTI_SAMPLING = addState(new GLState(GL13.GL_MULTISAMPLE));
|
|
public static final GLWireFrame WIRE_FRAME = addState(new GLWireFrame(GLCullType.BOTH));
|
|
public static final GLState PROVOKING_VERTEX = addState(new GLProvoking());
|
|
public static final GLState DEBTH_TEST = addState(new GLState(GL11.GL_DEPTH_TEST));
|
|
public static final CullState CULL_FACE = addState(new CullState(GLCullType.BACK));
|
|
public static final BlendState BLEND = addState(new BlendState());
|
|
public static final GLState[] CLIP_PLANE0 = addStates(new GLState(GL11.GL_CLIP_PLANE0), new GLState(GL11.GL_CLIP_PLANE1), new GLState(GL11.GL_CLIP_PLANE2), new GLState(GL11.GL_CLIP_PLANE3), new GLState(GL11.GL_CLIP_PLANE4), new GLState(GL11.GL_CLIP_PLANE5));
|
|
public static final FloatState POINT_SIZE = addState(new FloatState(1F, GL11::glPointSize));
|
|
public static final FloatState LINE_SIZE = addState(new FloatState(1F, GL11::glLineWidth));
|
|
public static final ScissorsManager TESTER = new ScissorsManager(100);
|
|
public static final Counter[] COUNTERS = Counter.createCounters(4);
|
|
public static final ViewPortStack VIEW_PORT = new ViewPortStack();
|
|
public static final ShaderTracker SHADERS = new ShaderTracker();
|
|
public static final GlobalUniforms UNIFORMS = new GlobalUniforms();
|
|
public static final TextureState TEXTURES = addState(new TextureState());
|
|
|
|
public static <T extends IGLState> T addState(T state) {
|
|
ALL_STATES.add(state);
|
|
return state;
|
|
}
|
|
|
|
@SafeVarargs
|
|
public static <T extends IGLState> T[] addStates(T... states) {
|
|
ALL_STATES.addAll(states);
|
|
return states;
|
|
}
|
|
|
|
public static void reapplyState() {
|
|
ALL_STATES.forEach(IGLState::reapply);
|
|
}
|
|
|
|
public static void onFrameEnded() {
|
|
for(int i = 0,m = COUNTERS.length;i < m;i++) {
|
|
COUNTERS[i].onFinished();
|
|
}
|
|
}
|
|
|
|
public static void drawArrays(int mode, int count) {
|
|
drawArrays(mode, 0, count);
|
|
}
|
|
|
|
public static void drawArrays(int mode, int first, int count) {
|
|
addVerties(mode, count);
|
|
GL11.glDrawArrays(mode, first, count);
|
|
}
|
|
|
|
public static void drawElements(int mode, int count, int type, long indices) {
|
|
addVerties(mode, count);
|
|
GL11.glDrawElements(mode, count, type, indices);
|
|
}
|
|
|
|
public static void drawElements(int mode, IntBuffer buffer) {
|
|
addVerties(mode, buffer.remaining());
|
|
GL11.glDrawElements(mode, buffer);
|
|
}
|
|
|
|
public static void drawArraysInstanced(int mode, int first, int count, int instanced) {
|
|
addVerties(mode, count * instanced);
|
|
GL31.glDrawArraysInstanced(mode, first, count, instanced);
|
|
}
|
|
|
|
public static void drawElementsInstanced(int mode, int count, int type, long indices, int instanced) {
|
|
addVerties(mode, count * instanced);
|
|
GL31.glDrawElementsInstanced(mode, count, type, indices, instanced);
|
|
}
|
|
|
|
public static void drawElementsIndirect(int mode, int type, long pointer, int drawCount, int stride) {
|
|
addVerties(mode, drawAmount(pointer, drawCount));
|
|
GL44.glMultiDrawElementsIndirect(mode, type, pointer, drawCount, stride);
|
|
}
|
|
|
|
static int drawAmount(long pointer, int amount) {
|
|
int total = 0;
|
|
return total;
|
|
}
|
|
|
|
static void addVerties(int mode, int count) {
|
|
COUNTERS[0].add(count);
|
|
switch(mode) {
|
|
case GL11.GL_POINTS:
|
|
COUNTERS[1].add(count);
|
|
break;
|
|
case GL11.GL_LINES:
|
|
COUNTERS[2].add(count / 2);
|
|
break;
|
|
case GL11.GL_LINE_LOOP:
|
|
COUNTERS[2].add((count + 2) / 2);
|
|
break;
|
|
case GL11.GL_LINE_STRIP:
|
|
COUNTERS[2].add(count - 1);
|
|
break;
|
|
case GL11.GL_TRIANGLES:
|
|
COUNTERS[3].add(count / 3);
|
|
break;
|
|
case GL11.GL_TRIANGLE_STRIP:
|
|
COUNTERS[3].add(count - 2);
|
|
break;
|
|
case GL11.GL_TRIANGLE_FAN:
|
|
COUNTERS[3].add(count - 2);
|
|
break;
|
|
}
|
|
}
|
|
}
|