Skip to content

Commit

Permalink
Add Gl transformations to SmyLib
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jun 17, 2024
1 parent 9d77efa commit cfc34f2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
import net.smyler.smylib.gui.containers.FlexibleWidgetContainer;
import net.smyler.smylib.gui.containers.WidgetContainer;
import net.smyler.smylib.Color;
import fr.thesmyler.smylibgui.util.RenderUtil;
import fr.thesmyler.terramap.MapContext;
import fr.thesmyler.terramap.input.KeyBindings;
import net.smyler.smylib.gui.DrawContext;
import net.smyler.terramap.util.geo.GeoPointReadOnly;
import net.smyler.terramap.util.geo.WebMercatorUtil;
import net.smyler.smylib.math.Mat2d;
import net.smyler.smylib.math.Vec2dMutable;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.input.Keyboard;

import org.jetbrains.annotations.Nullable;
import org.lwjgl.input.Keyboard;

import static java.lang.Math.*;
import static net.smyler.smylib.SmyLib.getGameClient;
Expand Down Expand Up @@ -71,11 +68,11 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous

if(this.isRotating) {
// If we are processing rotation input, draw pentagons at the corresponding spot
GlStateManager.pushMatrix();
GlStateManager.translate(x + this.rotatePosition.x, y + this.rotatePosition.y, 0);
context.glState().pushViewMatrix();
context.glState().translate(x + this.rotatePosition.x, y + this.rotatePosition.y);
context.drawPolygon(Color.DARK_OVERLAY, ROTATION_POLYGON_VERTICES_OUTER);
context.drawPolygon(Color.DARK_OVERLAY, ROTATION_POLYGON_VERTICES_INNER);
GlStateManager.popMatrix();
context.glState().popViewMatrix();
}

if (this.getMap().isDebugMode()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fr.thesmyler.terramap.gui.widgets.map;

import com.google.gson.JsonObject;
import net.smyler.smylib.gui.DrawContext;
import net.smyler.smylib.gui.GlState;
import net.smyler.smylib.gui.containers.FlexibleWidgetContainer;
import net.smyler.smylib.gui.containers.WidgetContainer;
import net.smyler.smylib.gui.popups.Popup;
Expand Down Expand Up @@ -200,10 +202,11 @@ void updateViewPorts() {
* @param drawX the X coordinate the widget is supposed to be drawn at
* @param drawY the Y coordinate the widget is supposed to be drawn at
*/
protected void applyRotationGl(float drawX, float drawY) {
GlStateManager.translate(drawX + this.map.getWidth() / 2, drawY + this.map.getHeight() / 2, 0);
GlStateManager.rotate(this.rotation, 0, 0, 1);
GlStateManager.translate(-this.renderSpaceDimensionsHalf.x, -this.renderSpaceDimensionsHalf.y, 0);
protected void applyRotationGl(DrawContext context, float drawX, float drawY) {
GlState gl = context.glState();
gl.translate(drawX + this.map.getWidth() / 2, drawY + this.map.getHeight() / 2);
gl.rotate(this.rotation);
gl.translate(-this.renderSpaceDimensionsHalf.x, -this.renderSpaceDimensionsHalf.y);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.smyler.smylib.math.Vec2dReadOnly;
import net.buildtheearth.terraplusplus.projection.GeographicProjection;
import net.buildtheearth.terraplusplus.projection.OutOfProjectionBoundsException;
import net.minecraft.client.renderer.GlStateManager;

import static net.smyler.smylib.SmyLib.getGameClient;

Expand Down Expand Up @@ -41,8 +40,8 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
GeographicProjection projection = TerramapClientContext.getContext().getProjection();
if(projection == null) return;
map.getProfiler().startSection("layer-distortion");
GlStateManager.pushMatrix();
this.applyRotationGl(x, y);
context.glState().pushViewMatrix();
this.applyRotationGl(context, x, y);

double maxX = this.renderSpaceDimensions.x();
double maxY = this.renderSpaceDimensions.y();
Expand All @@ -64,8 +63,8 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
context.drawRectangle(x + dx, y + dy, x + dx + res, y + dy + res, color);
}
}
GlStateManager.popMatrix();

context.glState().popViewMatrix();
map.getProfiler().endSection();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import net.smyler.smylib.math.Vec2dReadOnly;
import net.buildtheearth.terraplusplus.projection.GeographicProjection;
import net.buildtheearth.terraplusplus.projection.OutOfProjectionBoundsException;
import net.minecraft.client.renderer.GlStateManager;

import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.math.Math.clamp;
Expand Down Expand Up @@ -163,8 +162,8 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
return;
}

GlStateManager.pushMatrix();
this.applyRotationGl(x, y);
context.glState().pushViewMatrix();
this.applyRotationGl(context, x, y);


float size = 1f;
Expand All @@ -185,7 +184,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
}

this.cache.cycle();
GlStateManager.popMatrix();
context.glState().popViewMatrix();
map.getProfiler().endSection();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.smyler.smylib.math.Vec2dMutable;
import net.smyler.smylib.math.Vec2dReadOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -77,7 +76,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous

profiler.startSection("render-raster-layer_" + tiledMap.getId());

GlStateManager.pushMatrix();
context.glState().pushViewMatrix();
float widthViewPort = this.getWidth();
float heightViewPort = this.getHeight();
double zoom = this.getMap().getController().getZoom();
Expand All @@ -87,7 +86,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
Vec2dImmutable xvec = rotationMatrix.line1();
Vec2dImmutable yvec = rotationMatrix.line2();

this.applyRotationGl(x, y);
this.applyRotationGl(context, x, y);

Vec2dReadOnly upperLeft = this.getUpperLeftRenderCornerPositionInMercatorSpace();

Expand Down Expand Up @@ -286,7 +285,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
this.lastNeededTiles.forEach(RasterTile::cancelTextureLoading);
this.lastNeededTiles = neededTiles;

GlStateManager.popMatrix();
context.glState().popViewMatrix();
profiler.endSection();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.smyler.terramap.util.geo.GeoPointMutable;
import net.smyler.smylib.math.Vec2dMutable;
import net.smyler.smylib.math.Vec2dReadOnly;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.profiler.Profiler;

public class RenderingDeltaPreviewLayer extends MapLayer {
Expand Down Expand Up @@ -40,13 +39,13 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous
float height = this.getHeight();
this.getLocationPositionInRenderSpace(this.realCenterPosition, this.realCenter);

GlStateManager.pushMatrix();
this.applyRotationGl(x, y);
context.glState().pushViewMatrix();
this.applyRotationGl(context, x, y);
context.drawStrokeLine(Color.RED, 2f,
this.renderSpaceHalfDimensions.x(), this.renderSpaceHalfDimensions.y(),
this.realCenterPosition.x, this.renderSpaceHalfDimensions.y(),
this.realCenterPosition.x, this.realCenterPosition.y);
GlStateManager.popMatrix();
context.glState().popViewMatrix();

float centerHole = 10;
float linesWidth = 1f;
Expand Down
10 changes: 10 additions & 0 deletions smylib/core/src/main/java/net/smyler/smylib/gui/GlState.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ public interface GlState {

void disableColorLogic();

void pushViewMatrix();

void rotate(double angle);

void translate(double x, double y);

void scale(double x, double y);

void popViewMatrix();

}
25 changes: 25 additions & 0 deletions smylib/forge/src/main/java/net/smyler/smylib/gui/LwjglState.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,29 @@ public void disableColorLogic() {
GlStateManager.disableColorLogic();
}

@Override
public void pushViewMatrix() {
GlStateManager.pushMatrix();
}

@Override
public void rotate(double angle) {
GlStateManager.rotate((float)angle, 0, 0, 1f);
}

@Override
public void translate(double x, double y) {
GlStateManager.translate(x, y, 0);
}

@Override
public void scale(double x, double y) {
GlStateManager.scale(x, y, 1d);
}

@Override
public void popViewMatrix() {
GlStateManager.popMatrix();
}

}

0 comments on commit cfc34f2

Please sign in to comment.