Skip to content

Commit

Permalink
Core.graphics momentary override for syncing width and height
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennFolker committed Dec 13, 2024
1 parent 0318f24 commit cf13ed8
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 81 deletions.
13 changes: 13 additions & 0 deletions assets/shaders/confictura/depth-screenspace.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#define HIGHP

in vec2 v_texCoords;

out vec4 fragColor;

uniform sampler2D u_color;
uniform sampler2D u_depth;

void main(){
fragColor = texture(u_color, v_texCoords);
gl_FragDepth = texture(u_depth, v_texCoords).r;
}
11 changes: 11 additions & 0 deletions assets/shaders/confictura/depth-screenspace.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#define HIGHP

in vec4 a_position;
in vec2 a_texCoord0;

out vec2 v_texCoords;

void main(){
v_texCoords = a_texCoord0;
gl_Position = a_position;
}
2 changes: 2 additions & 0 deletions src/confictura/ConficturaMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class ConficturaMod extends Mod{
// <--- Modules only present in clients, typically rendering or auxiliary input utilities. --->
public static RenderContext renderContext;
public static ModelPropDrawer modelPropDrawer;
public static SizedGraphics sizedGraphics;

// <--- Map-editor extension modules. --->
public static CinematicEditor cinematicEditor;
Expand Down Expand Up @@ -188,6 +189,7 @@ public String getName(){
inputAggregator = new InputAggregator();
renderContext = new RenderContext();
modelPropDrawer = new ModelPropDrawer(CShaders.modelProp, 8192, 16384);
sizedGraphics = new SizedGraphics();
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/confictura/content/CPlanets.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public static void load(){
}};

for(int i = 0; i < 3; i++){
new Planet("test" + i, blackHole, 1f){{
new AtmospherePlanet("test" + i, blackHole, 1f){{
generator = new SerpuloPlanetGenerator();
meshLoader = () -> new HexMesh(this, 6);
meshLoader = () -> new AtmosphereHexMesh(generator, 6);
cloudMeshLoader = () -> new MultiMesh(
new HexSkyMesh(this, 11, 0.15f, 0.13f, 5, new Color().set(Pal.spore).mul(0.9f).a(0.75f), 2, 0.45f, 0.9f, 0.38f),
new HexSkyMesh(this, 1, 0.6f, 0.16f, 5, Color.white.cpy().lerp(Pal.spore, 0.55f).a(0.75f), 2, 0.45f, 1f, 0.41f)
Expand Down
2 changes: 2 additions & 0 deletions src/confictura/graphics/CShaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @author GlFolker
*/
public final class CShaders{
public static DepthScreenspaceShader depthScreenspace;
public static DepthShader depth;
public static DepthAtmosphereShader depthAtmosphere;
public static PortalForcefieldShader portalForcefield;
Expand All @@ -30,6 +31,7 @@ public static void load(){
String prevVert = Shader.prependVertexCode, prevFrag = Shader.prependFragmentCode;
Shader.prependVertexCode = Shader.prependFragmentCode = "";

depthScreenspace = new DepthScreenspaceShader();
depth = new DepthShader();
depthAtmosphere = new DepthAtmosphereShader();
portalForcefield = new PortalForcefieldShader();
Expand Down
212 changes: 212 additions & 0 deletions src/confictura/graphics/SizedGraphics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package confictura.graphics;

import arc.*;
import arc.Graphics.Cursor.*;
import arc.graphics.*;
import arc.graphics.gl.*;

import java.lang.reflect.*;

import static arc.Core.*;

public class SizedGraphics extends Graphics{
private static final Method setCursor, setSystemCursor;

protected boolean overriding;
protected int overrideWidth, overrideHeight;

static{
try{
setCursor = Graphics.class.getDeclaredMethod("setCursor", Cursor.class);
setSystemCursor = Graphics.class.getDeclaredMethod("setSystemCursor", SystemCursor.class);

setCursor.setAccessible(true);
setSystemCursor.setAccessible(true);
}catch(NoSuchMethodException e){
throw new RuntimeException(e);
}
}

public void override(int dimension, Runnable run){
override(dimension, dimension, run);
}

public void override(int width, int height, Runnable run){
overriding = true;
overrideWidth = width;
overrideHeight = height;

var prev = graphics;
graphics = this;

try{
run.run();
}finally{
overriding = false;
graphics = prev;
}
}

@Override
public int getWidth(){
return overriding ? overrideWidth : graphics.getWidth();
}

@Override
public int getHeight(){
return overriding ? overrideHeight : graphics.getHeight();
}

@Override
public GL20 getGL20(){
return graphics.getGL20();
}

@Override
public void setGL20(GL20 gl20){
graphics.setGL20(gl20);
}

@Override
public GL30 getGL30(){
return graphics.getGL30();
}

@Override
public void setGL30(GL30 gl30){
graphics.setGL30(gl30);
}

@Override
public int getBackBufferWidth(){
return graphics.getBackBufferWidth();
}

@Override
public int getBackBufferHeight(){
return graphics.getBackBufferHeight();
}

@Override
public long getFrameId(){
return graphics.getFrameId();
}

@Override
public float getDeltaTime(){
return graphics.getDeltaTime();
}

@Override
public int getFramesPerSecond(){
return graphics.getFramesPerSecond();
}

@Override
public GLVersion getGLVersion(){
return graphics.getGLVersion();
}

@Override
public float getPpiX(){
return graphics.getPpiX();
}

@Override
public float getPpiY(){
return graphics.getPpiY();
}

@Override
public float getPpcX(){
return graphics.getPpcX();
}

@Override
public float getPpcY(){
return graphics.getPpcY();
}

@Override
public float getDensity(){
return graphics.getDensity();
}

@Override
public boolean setWindowedMode(int width, int height){
return graphics.setWindowedMode(width, height);
}

@Override
public void setTitle(String title){
graphics.setTitle(title);
}

@Override
public void setBorderless(boolean undecorated){
graphics.setBorderless(undecorated);
}

@Override
public void setResizable(boolean resizable){
graphics.setResizable(resizable);
}

@Override
public void setVSync(boolean vsync){
graphics.setVSync(vsync);
}

@Override
public BufferFormat getBufferFormat(){
return graphics.getBufferFormat();
}

@Override
public boolean supportsExtension(String extension){
return graphics.supportsExtension(extension);
}

@Override
public boolean isContinuousRendering(){
return graphics.isContinuousRendering();
}

@Override
public void setContinuousRendering(boolean isContinuous){
graphics.setContinuousRendering(isContinuous);
}

@Override
public void requestRendering(){
graphics.requestRendering();
}

@Override
public boolean isFullscreen(){
return graphics.isFullscreen();
}

@Override
public Cursor newCursor(Pixmap pixmap, int xHotspot, int yHotspot){
return graphics.newCursor(pixmap, xHotspot, yHotspot);
}

@Override
protected void setCursor(Cursor cursor){
try{
setCursor.invoke(graphics, cursor);
}catch(InvocationTargetException | IllegalAccessException e){
throw new RuntimeException(e);
}
}

@Override
protected void setSystemCursor(SystemCursor systemCursor){
try{
setSystemCursor.invoke(graphics, systemCursor);
}catch(InvocationTargetException | IllegalAccessException e){
throw new RuntimeException(e);
}
}
}
1 change: 1 addition & 0 deletions src/confictura/graphics/gl/CFrameBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void resize(int width, int height){
public void begin(Color clearColor){
begin();
Gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
Gl.clearDepthf(1f);
Gl.clear(Gl.colorBufferBit | (hasDepth ? Gl.depthBufferBit : 0) | (hasStencil ? Gl.stencilBufferBit : 0));
}
}
22 changes: 22 additions & 0 deletions src/confictura/graphics/shaders/DepthScreenspaceShader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package confictura.graphics.shaders;

import confictura.graphics.gl.*;

import static confictura.graphics.CShaders.*;

public class DepthScreenspaceShader extends Gl30Shader{
public CFrameBuffer buffer;

public DepthScreenspaceShader(){
super(file("depth-screenspace.vert"), file("depth-screenspace.frag"));
}

@Override
public void apply(){
buffer.getTexture().bind(1);
buffer.getDepthTexture().bind(0);

setUniformi("u_color", 1);
setUniformi("u_depth", 0);
}
}
33 changes: 30 additions & 3 deletions src/confictura/world/celestial/AtmospherePlanet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import arc.graphics.*;
import arc.graphics.Texture.*;
import arc.graphics.g2d.*;
import arc.graphics.g3d.*;
import arc.graphics.gl.*;
import arc.math.geom.*;
import arc.util.*;
import confictura.graphics.*;
import confictura.graphics.gl.*;
import mindustry.graphics.*;
import mindustry.graphics.g3d.*;
import mindustry.type.*;
Expand All @@ -20,6 +22,7 @@
*/
public class AtmospherePlanet extends Planet{
public @Nullable FrameBuffer depthBuffer;
public @Nullable CFrameBuffer buffer;

public AtmospherePlanet(String name, Planet parent, float radius){
super(name, parent, radius);
Expand All @@ -32,10 +35,15 @@ public AtmospherePlanet(String name, Planet parent, float radius, int sectorSize
@Override
public void load(){
super.load();
if(!headless){
if(depthBuffer == null){
depthBuffer = new FrameBuffer(graphics.getWidth(), graphics.getHeight(), true);
depthBuffer.getTexture().setFilter(TextureFilter.nearest);
}

if(buffer == null){
buffer = new CFrameBuffer(2, 2, true);
buffer.getTexture().setFilter(TextureFilter.nearest);
}
}

@Override
Expand Down Expand Up @@ -67,6 +75,25 @@ public AtmosphereHexMesh(int divisions){

@Override
public void render(PlanetParams params, Mat3D projection, Mat3D transform){
/*buffer.resize(graphics.getWidth(), graphics.getHeight());
buffer.begin(Color.clear);
var shader = Shaders.planet;
shader.planet = AtmospherePlanet.this;
shader.lightDir.set(solarSystem.position).sub(position).rotate(Vec3.Y, getRotation()).nor();
shader.ambientColor.set(solarSystem.lightColor);
shader.bind();
shader.setUniformMatrix4("u_proj", renderer.planets.cam.combined.val);
shader.setUniformMatrix4("u_trans", transform.val);
shader.apply();
mesh.render(shader, Gl.triangles);
buffer.end();
var blit = CShaders.depthScreenspace;
blit.buffer = buffer;
Draw.blit(blit);*/

if(params.alwaysDrawAtmosphere || settings.getBool("atmosphere")){
var depth = CShaders.depth;
depthBuffer.resize(graphics.getWidth(), graphics.getHeight());
Expand All @@ -75,7 +102,7 @@ public void render(PlanetParams params, Mat3D projection, Mat3D transform){

depth.camera = renderer.planets.cam;
depth.bind();
depth.setUniformMatrix4("u_proj", projection.val);
depth.setUniformMatrix4("u_proj", renderer.planets.cam.combined.val);
depth.setUniformMatrix4("u_trans", transform.val);
depth.apply();
mesh.render(depth, Gl.triangles);
Expand All @@ -89,7 +116,7 @@ public void render(PlanetParams params, Mat3D projection, Mat3D transform){
shader.lightDir.set(solarSystem.position).sub(position).rotate(Vec3.Y, getRotation()).nor();
shader.ambientColor.set(solarSystem.lightColor);
shader.bind();
shader.setUniformMatrix4("u_proj", projection.val);
shader.setUniformMatrix4("u_proj", renderer.planets.cam.combined.val);
shader.setUniformMatrix4("u_trans", transform.val);
shader.apply();
mesh.render(shader, Gl.triangles);
Expand Down
Loading

0 comments on commit cf13ed8

Please sign in to comment.