diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/Feature.java b/src/main/java/com/gtnewhorizons/angelica/glsm/Feature.java index b1f77ea00..20727594b 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/Feature.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/Feature.java @@ -82,7 +82,14 @@ public class Feature { , GLStateManager.depthTest // GL_DEPTH_TEST flag // GL_DITHER flag , GLStateManager.fogMode // GL_FOG flag - // GL_LIGHTi where 0 <= i < GL_MAX_LIGHTS + , GLStateManager.lightStates[0] // GL_LIGHT0 + , GLStateManager.lightStates[1] // GL_LIGHT1 + , GLStateManager.lightStates[2] // GL_LIGHT2 + , GLStateManager.lightStates[3] // GL_LIGHT3 + , GLStateManager.lightStates[4] // GL_LIGHT4 + , GLStateManager.lightStates[5] // GL_LIGHT5 + , GLStateManager.lightStates[6] // GL_LIGHT6 + , GLStateManager.lightStates[7] // GL_LIGHT7 , GLStateManager.lightingState // GL_LIGHTING flag // GL_LINE_SMOOTH flag // GL_LINE_STIPPLE flag @@ -149,9 +156,25 @@ public class Feature { // GL_LIGHT_MODEL_TWO_SIDE setting GLStateManager.lightingState // GL_LIGHTING enable bit // Enable bit for each light + , GLStateManager.lightStates[0] // GL_LIGHT0 + , GLStateManager.lightStates[1] // GL_LIGHT1 + , GLStateManager.lightStates[2] // GL_LIGHT2 + , GLStateManager.lightStates[3] // GL_LIGHT3 + , GLStateManager.lightStates[4] // GL_LIGHT4 + , GLStateManager.lightStates[5] // GL_LIGHT5 + , GLStateManager.lightStates[6] // GL_LIGHT6 + , GLStateManager.lightStates[7] // GL_LIGHT7 // Ambient, diffuse, and specular intensity for each light // Direction, position, exponent, and cutoff angle for each light // Constant, linear, and quadratic attenuation factors for each light + , GLStateManager.lightDataStates[0] + , GLStateManager.lightDataStates[1] + , GLStateManager.lightDataStates[2] + , GLStateManager.lightDataStates[3] + , GLStateManager.lightDataStates[4] + , GLStateManager.lightDataStates[5] + , GLStateManager.lightDataStates[6] + , GLStateManager.lightDataStates[7] // Ambient, diffuse, specular, and emissive color for each material // Ambient, diffuse, and specular color indices for each material // Specular exponent for each material diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java index 574e32ac7..ee4c3e0d6 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java @@ -11,6 +11,7 @@ import com.gtnewhorizons.angelica.glsm.stacks.DepthStateStack; import com.gtnewhorizons.angelica.glsm.stacks.FogStateStack; import com.gtnewhorizons.angelica.glsm.stacks.IStateStack; import com.gtnewhorizons.angelica.glsm.stacks.IntegerStateStack; +import com.gtnewhorizons.angelica.glsm.stacks.LightStateStack; import com.gtnewhorizons.angelica.glsm.stacks.MatrixModeStack; import com.gtnewhorizons.angelica.glsm.stacks.ViewPortStateStack; import com.gtnewhorizons.angelica.glsm.states.Color4; @@ -59,6 +60,7 @@ import org.lwjgl.opengl.GL14; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GLContext; +import java.lang.Math; import java.nio.ByteBuffer; import java.nio.DoubleBuffer; import java.nio.FloatBuffer; @@ -121,6 +123,14 @@ public class GLStateManager { @Getter protected static final Matrix4fStack modelViewMatrix = new Matrix4fStack(MAX_MODELVIEW_STACK_DEPTH); @Getter protected static final Matrix4fStack projectionMatrix = new Matrix4fStack(MAX_PROJECTION_STACK_DEPTH); + @Getter protected static final BooleanStateStack[] lightStates = new BooleanStateStack[8]; + @Getter protected static final LightStateStack[] lightDataStates = new LightStateStack[8]; + static { + for (int i = 0; i < lightStates.length; i ++) { + lightStates[i] = new BooleanStateStack(GL11.GL_LIGHT0 + i); + lightDataStates[i] = new LightStateStack(GL11.GL_LIGHT0 + i); + } + } @Getter protected static final ViewPortStateStack viewportState = new ViewPortStateStack(); @@ -259,6 +269,14 @@ public class GLStateManager { case GL11.GL_DEPTH_TEST -> enableDepthTest(); case GL11.GL_FOG -> enableFog(); case GL11.GL_LIGHTING -> enableLighting(); + case GL11.GL_LIGHT0 -> enableLight(0); + case GL11.GL_LIGHT1 -> enableLight(1); + case GL11.GL_LIGHT2 -> enableLight(2); + case GL11.GL_LIGHT3 -> enableLight(3); + case GL11.GL_LIGHT4 -> enableLight(4); + case GL11.GL_LIGHT5 -> enableLight(5); + case GL11.GL_LIGHT6 -> enableLight(6); + case GL11.GL_LIGHT7 -> enableLight(7); case GL11.GL_SCISSOR_TEST -> enableScissorTest(); case GL11.GL_TEXTURE_2D -> enableTexture(); case GL12.GL_RESCALE_NORMAL -> enableRescaleNormal(); @@ -274,6 +292,14 @@ public class GLStateManager { case GL11.GL_DEPTH_TEST -> disableDepthTest(); case GL11.GL_FOG -> disableFog(); case GL11.GL_LIGHTING -> disableLighting(); + case GL11.GL_LIGHT0 -> disableLight(0); + case GL11.GL_LIGHT1 -> disableLight(1); + case GL11.GL_LIGHT2 -> disableLight(2); + case GL11.GL_LIGHT3 -> disableLight(3); + case GL11.GL_LIGHT4 -> disableLight(4); + case GL11.GL_LIGHT5 -> disableLight(5); + case GL11.GL_LIGHT6 -> disableLight(6); + case GL11.GL_LIGHT7 -> disableLight(7); case GL11.GL_SCISSOR_TEST -> disableScissorTest(); case GL11.GL_TEXTURE_2D -> disableTexture(); case GL12.GL_RESCALE_NORMAL -> disableRescaleNormal(); @@ -292,6 +318,14 @@ public class GLStateManager { case GL11.GL_DEPTH_TEST -> depthTest.isEnabled(); case GL11.GL_FOG -> fogMode.isEnabled(); case GL11.GL_LIGHTING -> lightingState.isEnabled(); + case GL11.GL_LIGHT0 -> lightStates[0].isEnabled(); + case GL11.GL_LIGHT1 -> lightStates[1].isEnabled(); + case GL11.GL_LIGHT2 -> lightStates[2].isEnabled(); + case GL11.GL_LIGHT3 -> lightStates[3].isEnabled(); + case GL11.GL_LIGHT4 -> lightStates[4].isEnabled(); + case GL11.GL_LIGHT5 -> lightStates[5].isEnabled(); + case GL11.GL_LIGHT6 -> lightStates[6].isEnabled(); + case GL11.GL_LIGHT7 -> lightStates[7].isEnabled(); case GL11.GL_SCISSOR_TEST -> scissorTest.isEnabled(); case GL11.GL_TEXTURE_2D -> textures.getTextureUnitStates(activeTextureUnit.getValue()).isEnabled(); case GL12.GL_RESCALE_NORMAL -> rescaleNormalState.isEnabled(); @@ -311,6 +345,14 @@ public class GLStateManager { case GL11.GL_DEPTH_WRITEMASK -> depthState.isEnabled(); case GL11.GL_FOG -> fogMode.isEnabled(); case GL11.GL_LIGHTING -> lightingState.isEnabled(); + case GL11.GL_LIGHT0 -> lightStates[0].isEnabled(); + case GL11.GL_LIGHT1 -> lightStates[1].isEnabled(); + case GL11.GL_LIGHT2 -> lightStates[2].isEnabled(); + case GL11.GL_LIGHT3 -> lightStates[3].isEnabled(); + case GL11.GL_LIGHT4 -> lightStates[4].isEnabled(); + case GL11.GL_LIGHT5 -> lightStates[5].isEnabled(); + case GL11.GL_LIGHT6 -> lightStates[6].isEnabled(); + case GL11.GL_LIGHT7 -> lightStates[7].isEnabled(); case GL11.GL_SCISSOR_TEST -> scissorTest.isEnabled(); case GL11.GL_TEXTURE_2D -> textures.getTextureUnitStates(activeTextureUnit.getValue()).isEnabled(); case GL12.GL_RESCALE_NORMAL -> rescaleNormalState.isEnabled(); @@ -381,6 +423,24 @@ public class GLStateManager { } public static void glGetLight(int light, int pname, FloatBuffer params) { + if (shouldBypassCache()) { + GL11.glGetLight(light, pname, params); + return; + } + + LightStateStack state = lightDataStates[light - GL11.GL_LIGHT0]; + switch (pname) { + case GL11.GL_AMBIENT -> state.ambient.get(0, params); + case GL11.GL_DIFFUSE -> state.diffuse.get(0, params); + case GL11.GL_SPECULAR -> state.specular.get(0, params); + case GL11.GL_POSITION -> state.position.get(0, params); + case GL11.GL_SPOT_DIRECTION -> state.spotDirection.get(0, params); + case GL11.GL_SPOT_EXPONENT -> params.put(state.spotExponent); + case GL11.GL_SPOT_CUTOFF -> params.put(state.spotCutoff); + case GL11.GL_CONSTANT_ATTENUATION -> params.put(state.constantAttenuation); + case GL11.GL_LINEAR_ATTENUATION -> params.put(state.linearAttenuation); + case GL11.GL_QUADRATIC_ATTENUATION -> params.put(state.quadraticAttenuation); + } GL11.glGetLight(light, pname, params); } @@ -595,6 +655,8 @@ public class GLStateManager { return ((b - Byte.MIN_VALUE) & 0xFF) / 255.0F; } + public static float i2f(int i) { return ((i - Integer.MIN_VALUE) & 0xFFFFFF) / 4294967295.0F; } + private static boolean changeColor(float red, float green, float blue, float alpha) { // Helper function for glColor* if (shouldBypassCache() || red != color.getRed() || green != color.getGreen() || blue != color.getBlue() || alpha != color.getAlpha()) { @@ -900,10 +962,18 @@ public class GLStateManager { lightingState.enable(); } + public static void enableLight(int light) { + lightStates[light].enable(); + } + public static void disableLighting() { lightingState.disable(); } + public static void disableLight(int light) { + lightStates[light].disable(); + } + public static void enableRescaleNormal() { rescaleNormalState.enable(); } @@ -1147,6 +1217,10 @@ public class GLStateManager { } } + public static Matrix4f getModelviewMatrix() { + return modelViewMatrix; + } + public static void glLoadIdentity() { GL11.glLoadIdentity(); getMatrixStack().identity(); @@ -1418,10 +1492,61 @@ public class GLStateManager { } public static void glLight(int light, int pname, FloatBuffer params) { - GL11.glLight(light, pname, params); + LightStateStack lightState = lightDataStates[light - GL11.GL_LIGHT0]; + switch (pname) { + case GL11.GL_AMBIENT -> lightState.setAmbient(params); + case GL11.GL_DIFFUSE -> lightState.setDiffuse(params); + case GL11.GL_SPECULAR -> lightState.setSpecular(params); + case GL11.GL_POSITION -> lightState.setPosition(params); + case GL11.GL_SPOT_DIRECTION -> lightState.setSpotDirection(params); + case GL11.GL_SPOT_EXPONENT -> lightState.setSpotExponent(params); + case GL11.GL_SPOT_CUTOFF -> lightState.setSpotCutoff(params); + case GL11.GL_CONSTANT_ATTENUATION -> lightState.setConstantAttenuation(params); + case GL11.GL_LINEAR_ATTENUATION -> lightState.setLinearAttenuation(params); + case GL11.GL_QUADRATIC_ATTENUATION -> lightState.setQuadraticAttenuation(params); + default -> GL11.glLight(light, pname, params); + } } + public static void glLight(int light, int pname, IntBuffer params) { - GL11.glLight(light, pname, params); + LightStateStack lightState = lightDataStates[light - GL11.GL_LIGHT0]; + switch (pname) { + case GL11.GL_AMBIENT -> lightState.setAmbient(params); + case GL11.GL_DIFFUSE -> lightState.setDiffuse(params); + case GL11.GL_SPECULAR -> lightState.setSpecular(params); + case GL11.GL_POSITION -> lightState.setPosition(params); + case GL11.GL_SPOT_DIRECTION -> lightState.setSpotDirection(params); + case GL11.GL_SPOT_EXPONENT -> lightState.setSpotExponent(params); + case GL11.GL_SPOT_CUTOFF -> lightState.setSpotCutoff(params); + case GL11.GL_CONSTANT_ATTENUATION -> lightState.setConstantAttenuation(params); + case GL11.GL_LINEAR_ATTENUATION -> lightState.setLinearAttenuation(params); + case GL11.GL_QUADRATIC_ATTENUATION -> lightState.setQuadraticAttenuation(params); + default -> GL11.glLight(light, pname, params); + } + } + + public static void glLightf(int light, int pname, float param) { + LightStateStack lightState = lightDataStates[light - GL11.GL_LIGHT0]; + switch (pname) { + case GL11.GL_SPOT_EXPONENT -> lightState.setSpotExponent(param); + case GL11.GL_SPOT_CUTOFF -> lightState.setSpotCutoff(param); + case GL11.GL_CONSTANT_ATTENUATION -> lightState.setConstantAttenuation(param); + case GL11.GL_LINEAR_ATTENUATION -> lightState.setLinearAttenuation(param); + case GL11.GL_QUADRATIC_ATTENUATION -> lightState.setQuadraticAttenuation(param); + default -> GL11.glLightf(light, pname, param); + } + } + + public static void glLighti(int light, int pname, int param) { + LightStateStack lightState = lightDataStates[light - GL11.GL_LIGHT0]; + switch (pname) { + case GL11.GL_SPOT_EXPONENT -> lightState.setSpotExponent(param); + case GL11.GL_SPOT_CUTOFF -> lightState.setSpotCutoff(param); + case GL11.GL_CONSTANT_ATTENUATION -> lightState.setConstantAttenuation(param); + case GL11.GL_LINEAR_ATTENUATION -> lightState.setLinearAttenuation(param); + case GL11.GL_QUADRATIC_ATTENUATION -> lightState.setQuadraticAttenuation(param); + default -> GL11.glLighti(light, pname, param); + } } public static void glLightModel(int pname, FloatBuffer params) { diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/LightStateStack.java b/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/LightStateStack.java new file mode 100644 index 000000000..f7a05694e --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/LightStateStack.java @@ -0,0 +1,39 @@ +package com.gtnewhorizons.angelica.glsm.stacks; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.states.LightState; + +public class LightStateStack extends LightState implements IStateStack { + + protected final LightState[] stack; + + protected int pointer; + + public LightStateStack(int light) { + super(light); + stack = new LightState[GLStateManager.MAX_ATTRIB_STACK_DEPTH]; + for (int i = 0; i < GLStateManager.MAX_ATTRIB_STACK_DEPTH; i++) { + stack[i] = new LightState(light); + } + } + + public LightStateStack push() { + if (pointer == stack.length) { + throw new IllegalStateException("Stack overflow size " + (pointer + 1) + " reached"); + } + + stack[pointer++].set(this); + return this; + } + + public LightStateStack pop() { + if (pointer == 0) { + throw new IllegalStateException("Stack underflow"); + } + + set(stack[--pointer]); + return this; + } + + public boolean isEmpty() { return pointer == 0; } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/states/LightState.java b/src/main/java/com/gtnewhorizons/angelica/glsm/states/LightState.java new file mode 100644 index 000000000..fcfe521f3 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/states/LightState.java @@ -0,0 +1,290 @@ +package com.gtnewhorizons.angelica.glsm.states; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import org.joml.Vector3f; +import org.joml.Vector3i; +import org.joml.Vector4f; +import org.joml.Vector4i; +import org.lwjgl.opengl.GL11; + +import java.nio.FloatBuffer; +import java.nio.IntBuffer; + +import static com.gtnewhorizons.angelica.glsm.GLStateManager.i2f; + +public class LightState implements ISettableState { + + private static final Vector4i vector4i = new Vector4i(); + private static final Vector4f vector4f = new Vector4f(); + private static final Vector3i vector3i = new Vector3i(); + private static final Vector3f vector3f = new Vector3f(); + + public int light; + + public Vector4f ambient; + public Vector4f diffuse; + public Vector4f specular; + public Vector4f position; + public Vector3f spotDirection; + public float spotExponent; + public float spotCutoff; + public float constantAttenuation; + public float linearAttenuation; + public float quadraticAttenuation; + + public LightState(int light) { + this( + light, + new Vector4f(0F, 0F, 0F, 1F), + new Vector4f(0F, 0F, 0F, 1F), + new Vector4f(0F, 0F, 0F, 1F), + new Vector4f(0F, 0F, 1F, 0F), + new Vector3f(0F, 0F, -1F), + 0F, + 180F, + 1.0F, + 0.0F, + 0.0F + ); + if (light == GL11.GL_LIGHT0) { + this.diffuse.set(1F, 1F, 1F, 1F); + this.specular.set(1F, 1F, 1F, 1F); + } + } + + public LightState(int light, Vector4f ambient, Vector4f diffuse, Vector4f specular, Vector4f position, Vector3f spotDirection, float spotExponent, float spotCutoff, float constantAttenuation, float linearAttenuation, float quadraticAttenuation) { + this.light = light; + this.ambient = ambient; + this.diffuse = diffuse; + this.specular = specular; + this.position = position; + this.spotDirection = spotDirection; + this.spotExponent = spotExponent; + this.spotCutoff = spotCutoff; + this.constantAttenuation = constantAttenuation; + this.linearAttenuation = linearAttenuation; + this.quadraticAttenuation = quadraticAttenuation; + } + + public void setAmbient(FloatBuffer newBuffer) { + vector4f.set(newBuffer); + if (GLStateManager.shouldBypassCache() || !this.ambient.equals(vector4f)) { + this.ambient.set(vector4f); + GL11.glLight(this.light, GL11.GL_AMBIENT, newBuffer); + } + } + + public void setAmbient(IntBuffer newBuffer) { + vector4i.set(newBuffer); + vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w)); + if (GLStateManager.shouldBypassCache() || !this.ambient.equals(vector4f)) { + this.ambient.set(vector4f); + GL11.glLight(this.light, GL11.GL_AMBIENT, newBuffer); + } + } + + public void setDiffuse(FloatBuffer newBuffer) { + vector4f.set(newBuffer); + if (GLStateManager.shouldBypassCache() || !this.diffuse.equals(vector4f)) { + this.diffuse.set(vector4f); + GL11.glLight(this.light, GL11.GL_DIFFUSE, newBuffer); + } + } + + public void setDiffuse(IntBuffer newBuffer) { + vector4i.set(newBuffer); + vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w)); + if (GLStateManager.shouldBypassCache() || !this.diffuse.equals(vector4f)) { + this.diffuse.set(vector4f); + GL11.glLight(this.light, GL11.GL_DIFFUSE, newBuffer); + } + } + + public void setSpecular(FloatBuffer newBuffer) { + vector4f.set(newBuffer); + if (GLStateManager.shouldBypassCache() || !this.specular.equals(vector4f)) { + this.specular.set(vector4f); + GL11.glLight(this.light, GL11.GL_SPECULAR, newBuffer); + } + } + + public void setSpecular(IntBuffer newBuffer) { + vector4i.set(newBuffer); + vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w)); + if (GLStateManager.shouldBypassCache() || !this.specular.equals(vector4f)) { + this.specular.set(vector4f); + GL11.glLight(this.light, GL11.GL_SPECULAR, newBuffer); + } + } + + public void setPosition(FloatBuffer newBuffer) { + vector4f.set(newBuffer); + if (GLStateManager.shouldBypassCache() || !this.position.equals(vector4f)) { + // The position is stored in eye coordinates(AKA multiplied by the modelview matrix + // OpenGL does this itself, so we are applying it to our cached version, and passing the + // untransformed buffer through to OpenGL. It is very important that OpenGL receives + // the untransformed position to glLight. + vector4f.mul(GLStateManager.getModelviewMatrix()); + this.position.set(vector4f); + GL11.glLight(this.light, GL11.GL_POSITION, newBuffer); + } + } + + public void setPosition(IntBuffer newBuffer) { + vector4i.set(newBuffer); + vector4f.set(i2f(vector4i.x), i2f(vector4i.y), i2f(vector4i.z), i2f(vector4i.w)); + if (GLStateManager.shouldBypassCache() || !this.position.equals(vector4f)) { + vector4f.mul(GLStateManager.getModelviewMatrix()); + this.position.set(vector4f); + GL11.glLight(this.light, GL11.GL_POSITION, newBuffer); + } + } + + public void setSpotDirection(FloatBuffer newBuffer) { + vector3f.set(newBuffer); + if (GLStateManager.shouldBypassCache() || !this.spotDirection.equals(vector3f)) { + this.spotDirection.set(vector3f); + GL11.glLight(this.light, GL11.GL_SPOT_DIRECTION, newBuffer); + } + } + + public void setSpotDirection(IntBuffer newBuffer) { + vector3i.set(newBuffer); + vector3f.set((float) vector3i.x, (float) vector3i.y, (float) vector3i.z); + if (GLStateManager.shouldBypassCache() || !this.spotDirection.equals(vector3f)) { + this.spotDirection.set(vector3f); + GL11.glLight(this.light, GL11.GL_SPOT_DIRECTION, newBuffer); + } + } + + public void setSpotExponent(FloatBuffer newBuffer) { + setSpotExponent(newBuffer.get()); + } + + public void setSpotExponent(IntBuffer newBuffer) { + setSpotExponent((float) newBuffer.get()); + } + + public void setSpotExponent(int i) { + setSpotExponent((float) i); + } + + public void setSpotExponent(float f) { + if (GLStateManager.shouldBypassCache() || Float.compare(f, this.spotExponent) != 0) { + this.spotExponent = f; + GL11.glLightf(this.light, GL11.GL_SPOT_EXPONENT, f); + } + } + + public void setSpotCutoff(FloatBuffer newBuffer) { + setSpotCutoff(newBuffer.get()); + } + + public void setSpotCutoff(IntBuffer newBuffer) { + setSpotCutoff((float) newBuffer.get()); + } + + public void setSpotCutoff(int i) { + setSpotCutoff((float) i); + } + + public void setSpotCutoff(float f) { + if (GLStateManager.shouldBypassCache() || Float.compare(f, this.spotCutoff) != 0) { + this.spotCutoff = f; + GL11.glLightf(this.light, GL11.GL_SPOT_CUTOFF, f); + } + } + + public void setConstantAttenuation(FloatBuffer newBuffer) { + setConstantAttenuation(newBuffer.get()); + } + + public void setConstantAttenuation(IntBuffer newBuffer) { + setConstantAttenuation((float) newBuffer.get()); + } + + public void setConstantAttenuation(int i) { + setConstantAttenuation((float) i); + } + + public void setConstantAttenuation(float f) { + if (GLStateManager.shouldBypassCache() || Float.compare(f, this.constantAttenuation) != 0) { + this.constantAttenuation = f; + GL11.glLightf(this.light, GL11.GL_CONSTANT_ATTENUATION, f); + } + } + + public void setLinearAttenuation(FloatBuffer newBuffer) { + setLinearAttenuation(newBuffer.get()); + } + + public void setLinearAttenuation(IntBuffer newBuffer) { + setLinearAttenuation((float) newBuffer.get()); + } + + public void setLinearAttenuation(int i) { + setLinearAttenuation((float) i); + } + + public void setLinearAttenuation(float f) { + if (GLStateManager.shouldBypassCache() || Float.compare(f, this.linearAttenuation) != 0) { + this.linearAttenuation = f; + GL11.glLightf(this.light, GL11.GL_LINEAR_ATTENUATION, f); + } + } + + public void setQuadraticAttenuation(FloatBuffer newBuffer) { + setQuadraticAttenuation(newBuffer.get()); + } + + public void setQuadraticAttenuation(IntBuffer newBuffer) { + setQuadraticAttenuation((float) newBuffer.get()); + } + + public void setQuadraticAttenuation(int i) { + setQuadraticAttenuation((float) i); + } + + public void setQuadraticAttenuation(float f) { + if (GLStateManager.shouldBypassCache() || Float.compare(f, this.quadraticAttenuation) != 0) { + this.quadraticAttenuation = f; + GL11.glLightf(this.light, GL11.GL_QUADRATIC_ATTENUATION, f); + } + } + + @Override + public LightState set(LightState state) { + this.ambient.set(state.ambient); + this.diffuse.set(state.diffuse); + this.specular.set(state.specular); + this.position.set(state.position); + this.spotDirection.set(state.spotDirection); + this.spotExponent = state.spotExponent; + this.spotCutoff = state.spotCutoff; + this.constantAttenuation = state.constantAttenuation; + this.linearAttenuation = state.linearAttenuation; + this.quadraticAttenuation = state.quadraticAttenuation; + + return this; + } + + @Override + public boolean sameAs(Object state) { + if (this == state) return true; + if (!(state instanceof LightState lightState)) return false; + return this.ambient.equals(lightState.ambient) + && this.diffuse.equals(lightState.diffuse) + && this.specular.equals(lightState.specular) + && this.position.equals(lightState.position) + && this.spotDirection.equals(lightState.spotDirection) + && Float.compare(this.spotCutoff, lightState.spotCutoff) == 0 + && Float.compare(this.spotExponent, lightState.spotExponent) == 0 + && Float.compare(this.linearAttenuation, lightState.linearAttenuation) == 0 + && Float.compare(this.constantAttenuation, lightState.constantAttenuation) == 0 + && Float.compare(this.quadraticAttenuation, lightState.quadraticAttenuation) == 0; + } + + @Override + public LightState copy() { return new LightState(this.light).set(this); } + +} diff --git a/src/main/java/com/gtnewhorizons/angelica/transform/RedirectorTransformer.java b/src/main/java/com/gtnewhorizons/angelica/transform/RedirectorTransformer.java index dc593a108..16405afe6 100644 --- a/src/main/java/com/gtnewhorizons/angelica/transform/RedirectorTransformer.java +++ b/src/main/java/com/gtnewhorizons/angelica/transform/RedirectorTransformer.java @@ -145,6 +145,8 @@ public class RedirectorTransformer implements IClassTransformer { .add("glGetTexParameteri") .add("glIsEnabled") .add("glLight") + .add("glLightf") + .add("glLighti") .add("glLightModel") .add("glLightModelf") .add("glLightModeli") diff --git a/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_Lighting_UnitTest.java b/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_Lighting_UnitTest.java new file mode 100644 index 000000000..34416b612 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_Lighting_UnitTest.java @@ -0,0 +1,162 @@ +package com.gtnewhorizons.angelica.glsm; + +import com.gtnewhorizons.angelica.AngelicaExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.lwjgl.opengl.GL11; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.FloatBuffer; + +import static com.gtnewhorizons.angelica.util.GLSMUtil.verifyLightState; +import static com.gtnewhorizons.angelica.util.GLSMUtil.verifyState; + +@ExtendWith(AngelicaExtension.class) +public class GLSM_Lighting_UnitTest { + + static final FloatBuffer f4b = ByteBuffer.allocateDirect(4 << 2).order(ByteOrder.nativeOrder()).asFloatBuffer(); + static final FloatBuffer f3b = ByteBuffer.allocateDirect(3 << 2).order(ByteOrder.nativeOrder()).asFloatBuffer(); + + @Test + void testLightEnablement() { + verifyState(GL11.GL_LIGHT0, false, "GL_LIGHT0 Initial State"); + GLStateManager.glEnable(GL11.GL_LIGHT0); + verifyState(GL11.GL_LIGHT0, true, "GL_LIGHT0 Enabled"); + + verifyState(GL11.GL_LIGHT1, false, "GL_LIGHT1 Initial State"); + GLStateManager.glEnable(GL11.GL_LIGHT1); + verifyState(GL11.GL_LIGHT1, true, "GL_LIGHT1 Enabled State"); + + GLStateManager.glDisable(GL11.GL_LIGHT0); + GLStateManager.glDisable(GL11.GL_LIGHT1); + } + + @Test + void testLightValues() { + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + + verifyLightState(GL11.GL_LIGHT0, GL11.GL_AMBIENT, new float[]{0F, 0F, 0F, 1F}, "GL_LIGHT0 Initial Ambient"); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, new float[]{1F, 1F, 1F, 1F}, "GL_LIGHT0 Initial Diffuse"); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPECULAR, new float[]{1F, 1F, 1F, 1F}, "GL_LIGHT0 Initial Specular"); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_POSITION, new float[]{0F, 0F, 1F, 0F}, "GL_LIGHT0 Initial Position"); + + verifyLightState(GL11.GL_LIGHT1, GL11.GL_AMBIENT, new float[]{0F, 0F, 0F, 1F}, "GL_LIGHT1 Initial Ambient"); + verifyLightState(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, new float[]{0F, 0F, 0F, 1F}, "GL_LIGHT1 Initial Diffuse"); + verifyLightState(GL11.GL_LIGHT1, GL11.GL_SPECULAR, new float[]{0F, 0F, 0F, 1F}, "GL_LIGHT1 Initial Specular"); + verifyLightState(GL11.GL_LIGHT1, GL11.GL_POSITION, new float[]{0F, 0F, 1F, 0F}, "GL_LIGHT1 Initial Position"); + + newf4b(0.25F, 0.5F, 0.25F, 0.5F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_AMBIENT, new float[]{0.25F, 0.5F, 0.25F, 0.5F}, "GL_LIGHT0 Ambient Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + newf4b(0.3F, 0.4F, 0.5F, 0.6F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_AMBIENT, new float[]{0.3F, 0.4F, 0.5F, 0.6F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_AMBIENT, new float[]{0.25F, 0.5F, 0.25F, 0.5F}, "GL_LIGHT0 attrib pop state check"); + + // The position is stored in eye coordinates, so we're moving around the modelview matrix before changing them + // That way we can test that the client side transformation of them via GLSM is working and matches the OpenGL + // transformation. This doesn't seem like float precision should break this test, but it may be possible for it + // to need tweaked a bit more. + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + GLStateManager.glPushMatrix(); + GLStateManager.glTranslatef(25.0F, 25.0F, 25.0F); + GLStateManager.glRotatef(35.0F, 10.0F, 15.0F, 20.0F); + newf4b(0.25F, 0.5F, 0.25F, 0.0F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_POSITION, new float[]{0.1090F, 0.5189F, 0.3062F, 0.0F}, "GL_LIGHT0 Position Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + newf4b(0.3F, 0.4F, 0.5F, 0.0F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_POSITION, new float[]{0.2824F, 0.4200F, 0.4937F, 0.0F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_POSITION, new float[]{0.1090F, 0.5189F, 0.3062F, 0.0F}, "GL_LIGHT0 attrib pop state check"); + GLStateManager.glPopMatrix(); + + newf4b(0.25F, 0.5F, 0.25F, 0.5F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, new float[]{0.25F, 0.5F, 0.25F, 0.5F}, "GL_LIGHT0 Diffuse Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + newf4b(0.3F, 0.4F, 0.5F, 0.6F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, new float[]{0.3F, 0.4F, 0.5F, 0.6F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, new float[]{0.25F, 0.5F, 0.25F, 0.5F}, "GL_LIGHT0 attrib pop state check"); + + newf4b(0.25F, 0.5F, 0.25F, 0.5F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPECULAR, new float[]{0.25F, 0.5F, 0.25F, 0.5F}, "GL_LIGHT0 Specular Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + newf4b(0.3F, 0.4F, 0.5F, 0.6F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPECULAR, new float[]{0.3F, 0.4F, 0.5F, 0.6F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPECULAR, new float[]{0.25F, 0.5F, 0.25F, 0.5F}, "GL_LIGHT0 attrib pop state check"); + + newf4b(0.25F, 0.5F, 0.25F, 0.0F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_SPOT_DIRECTION, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_DIRECTION, new float[]{0.25F, 0.5F, 0.25F}, "GL_LIGHT0 Spot Direction Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + newf4b(0.3F, 0.4F, 0.5F, 0.0F); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_SPOT_DIRECTION, f4b); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_DIRECTION, new float[]{0.3F, 0.4F, 0.5F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_DIRECTION, new float[]{0.25F, 0.5F, 0.25F}, "GL_LIGHT0 attrib pop state check"); + + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_SPOT_EXPONENT, 1.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_EXPONENT, new float[]{1.0F}, "GL_LIGHT0 Spot Exponent Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_SPOT_EXPONENT, 2.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_EXPONENT, new float[]{2.0F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_EXPONENT, new float[]{1.0F}, "GL_LIGHT0 attrib pop state check"); + + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_SPOT_CUTOFF, 20.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_CUTOFF, new float[]{20.0F}, "GL_LIGHT0 Spot Cutoff Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_SPOT_CUTOFF, 40.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_CUTOFF, new float[]{40.0F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_SPOT_CUTOFF, new float[]{20.0F}, "GL_LIGHT0 attrib pop state check"); + + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_CONSTANT_ATTENUATION, 10.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_CONSTANT_ATTENUATION, new float[]{10.0F}, "GL_LIGHT0 Constant Attenuation Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_CONSTANT_ATTENUATION, 20.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_CONSTANT_ATTENUATION, new float[]{20.0F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_CONSTANT_ATTENUATION, new float[]{10.0F}, "GL_LIGHT0 attrib pop state check"); + + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_LINEAR_ATTENUATION, 10.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_LINEAR_ATTENUATION, new float[]{10.0F}, "GL_LIGHT0 Linear Attenuation Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_LINEAR_ATTENUATION, 20.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_LINEAR_ATTENUATION, new float[]{20.0F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_LINEAR_ATTENUATION, new float[]{10.0F}, "GL_LIGHT0 attrib pop state check"); + + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_QUADRATIC_ATTENUATION, 10.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_QUADRATIC_ATTENUATION, new float[]{10.0F}, "GL_LIGHT0 Quadratic Attenuation Changed State"); + GLStateManager.glPushAttrib(GL11.GL_LIGHTING_BIT); + GLStateManager.glLightf(GL11.GL_LIGHT0, GL11.GL_QUADRATIC_ATTENUATION, 20.0F); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_QUADRATIC_ATTENUATION, new float[]{20.0F}, "GL_LIGHT0 attrib push state changed check"); + GLStateManager.glPopAttrib(); + verifyLightState(GL11.GL_LIGHT0, GL11.GL_QUADRATIC_ATTENUATION, new float[]{10.0F}, "GL_LIGHT0 attrib pop state check"); + + GLStateManager.glPopAttrib(); + } + + static void newf4b(float x, float y, float z, float w) { + f4b.clear(); + f4b.put(x).put(y).put(z).put(w); + f4b.flip(); + } + + static void newf3b(float x, float y, float z) { + f3b.clear(); + f3b.put(x).put(y).put(z); + f3b.flip(); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_PushPop_UnitTest.java b/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_PushPop_UnitTest.java index 6217b9674..89e7dc897 100644 --- a/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_PushPop_UnitTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_PushPop_UnitTest.java @@ -133,10 +133,10 @@ class GLSM_PushPop_UnitTest { GLStateManager.glNormal3f(0.5f, 0.5f, 0.5f); GLStateManager.glTexCoord4f(0.5f, 0.5f, 0.5f, 0.5f); // Current texture coordinates GLStateManager.glRasterPos4f(0.5f, 0.5f, 0.5f, 0.5f); // Current raster position - // GL_CURRENT_RASTER_POSITION_VALID flag - // RGBA color associated with current raster position - // Color index associated with current raster position - // Texture coordinates associated with current raster position + // GL_CURRENT_RASTER_POSITION_VALID flag + // RGBA color associated with current raster position + // Color index associated with current raster position + // Texture coordinates associated with current raster position GLStateManager.glEdgeFlag(false); // GL_EDGE_FLAG flag