Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit a845094

Browse files
committed
IFP.ofCaller, ITextureMipmap, etc.
1 parent 57d4f3e commit a845094

28 files changed

+248
-120
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/java/org/overrun/swgl/core/asset/AssetManager.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ void addAsset(String name,
146146
if (isFrozen())
147147
throw new IllegalStateException("Couldn't add asset in frozen state!");
148148
var w = new AssetWrapper(name, type, (Consumer<Asset>) consumer, provider);
149-
assets.put(name, w);
150149
w.assetSupplier = supplier;
150+
assets.put(name, w);
151151
}
152152

153153
/**
@@ -216,6 +216,15 @@ public void reloadAssets(boolean force) {
216216
reloadAssets(force, true);
217217
}
218218

219+
/**
220+
* Reloads all created assets.
221+
*
222+
* @see #reloadAssets(boolean, boolean)
223+
*/
224+
public void reloadAssets() {
225+
reloadAssets(false, false);
226+
}
227+
219228
/**
220229
* Get the asset from created assets.
221230
*

src/main/java/org/overrun/swgl/core/asset/AssetTypes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
package org.overrun.swgl.core.asset;
2626

27+
import org.overrun.swgl.core.asset.tex.Texture2D;
28+
2729
/**
2830
* The builtin asset types.
2931
*
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2022 Overrun Organization
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package org.overrun.swgl.core.asset.tex;
26+
27+
import java.nio.ByteBuffer;
28+
29+
/**
30+
* @author squid233
31+
* @since 0.1.0
32+
*/
33+
@FunctionalInterface
34+
public interface ITextureMipmap {
35+
/**
36+
* Set the texture mipmap loader.
37+
*
38+
* @param target The texture target.
39+
* @param buffer The texture buffer in RGBA.
40+
*/
41+
void set(int target, ByteBuffer buffer);
42+
}

src/main/java/org/overrun/swgl/core/asset/ITextureParam.java renamed to src/main/java/org/overrun/swgl/core/asset/tex/ITextureParam.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25-
package org.overrun.swgl.core.asset;
25+
package org.overrun.swgl.core.asset.tex;
2626

2727
/**
2828
* @author squid233

src/main/java/org/overrun/swgl/core/asset/Texture.java renamed to src/main/java/org/overrun/swgl/core/asset/tex/Texture.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
* SOFTWARE.
2323
*/
2424

25-
package org.overrun.swgl.core.asset;
25+
package org.overrun.swgl.core.asset.tex;
26+
27+
import org.overrun.swgl.core.asset.Asset;
2628

2729
/**
2830
* A swgl texture.

src/main/java/org/overrun/swgl/core/asset/Texture2D.java renamed to src/main/java/org/overrun/swgl/core/asset/tex/Texture2D.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
* SOFTWARE.
2323
*/
2424

25-
package org.overrun.swgl.core.asset;
25+
package org.overrun.swgl.core.asset.tex;
2626

2727
import org.jetbrains.annotations.Nullable;
2828
import org.lwjgl.opengl.GL;
2929
import org.lwjgl.system.MemoryStack;
30+
import org.overrun.swgl.core.asset.AssetManager;
31+
import org.overrun.swgl.core.asset.AssetTypes;
3032
import org.overrun.swgl.core.cfg.GlobalConfig;
31-
import org.overrun.swgl.core.io.HeapManager;
33+
import org.overrun.swgl.core.io.HeapStackFrame;
3234
import org.overrun.swgl.core.io.IFileProvider;
3335

3436
import java.nio.ByteBuffer;
@@ -53,6 +55,8 @@ public class Texture2D extends Texture {
5355
private int width, height;
5456
@Nullable
5557
private ITextureParam param;
58+
@Nullable
59+
private ITextureMipmap mipmap;
5660
public int defaultWidth = 16, defaultHeight = 16;
5761

5862
/**
@@ -86,7 +90,18 @@ public static void createAssetParam(
8690
@Nullable ITextureParam param,
8791
IFileProvider provider
8892
) {
89-
mgr.createAsset(name, AssetTypes.TEXTURE2D, (Texture2D tex) -> tex.setParam(param), provider);
93+
createAsset(mgr, name, (Texture2D tex) -> tex.setParam(param), provider);
94+
}
95+
96+
public static Optional<Texture2D> createGetAssetParam(
97+
AssetManager mgr,
98+
String name,
99+
@Nullable ITextureParam param,
100+
IFileProvider provider
101+
) {
102+
createAssetParam(mgr, name, param, provider);
103+
mgr.reloadAssets();
104+
return mgr.getAsset(name);
90105
}
91106

92107
public static Optional<Texture2D> getAsset(
@@ -136,6 +151,15 @@ public ITextureParam getParam() {
136151
return param;
137152
}
138153

154+
public void setMipmap(@Nullable ITextureMipmap mipmap) {
155+
this.mipmap = mipmap;
156+
}
157+
158+
@Nullable
159+
public ITextureMipmap getMipmap() {
160+
return mipmap;
161+
}
162+
139163
private ByteBuffer fail() {
140164
failed = true;
141165
width = (defaultWidth == 0 ? 16 : defaultWidth);
@@ -161,7 +185,7 @@ private ByteBuffer asBuffer(byte[] bytes,
161185
String name) {
162186
if (bytes == null)
163187
return fail();
164-
try (var heap = new HeapManager();
188+
try (var heap = new HeapStackFrame();
165189
var stack = MemoryStack.stackPush()) {
166190
var xp = stack.mallocInt(1);
167191
var yp = stack.mallocInt(1);
@@ -203,7 +227,9 @@ private void build(ByteBuffer buffer) {
203227
failed ? GL_RGB : GL_RGBA,
204228
GL_UNSIGNED_BYTE,
205229
buffer);
206-
if (GL.getCapabilities().glGenerateMipmap != NULL) {
230+
if (mipmap != null) {
231+
mipmap.set(GL_TEXTURE_2D, buffer);
232+
} else if (GL.getCapabilities().glGenerateMipmap != NULL) {
207233
glGenerateMipmap(GL_TEXTURE_2D);
208234
}
209235
bindTexture2D(lastUnit, lastId);

src/main/java/org/overrun/swgl/core/gl/ims/GLImmeMode.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -248,23 +248,20 @@ void main() {
248248
uniform vec4 lightModelAmbient;
249249
uniform Material material;
250250
""")
251-
.append("#define MAX_LIGHT_SOURCES (").append(lights.length).append(")\n")
252-
.append("uniform Light lights[MAX_LIGHT_SOURCES];\n");
251+
.append("#define MAX_LIGHT_SOURCES (").append(lights.length).append(")\nuniform Light lights[MAX_LIGHT_SOURCES];\n");
253252
final int maxTexUnits = getMaxTexImgUnits();
254253
for (int i = 0; i < maxTexUnits; i++) {
255-
fragSrc.append("uniform sampler2D sampler2D_").append(i).append(";\n");
256-
fragSrc.append("uniform int sampler2D_").append(i).append("_enabled;\n");
254+
fragSrc.append("uniform sampler2D sampler2D_").append(i).append(";\nuniform int sampler2D_")
255+
.append(i).append("_enabled;\n");
257256
}
258257
fragSrc.append("""
259-
bool _isTrue(int b) { return b != 0; }
260-
261-
vec4 calcDiffuseLight(Light light) {
258+
vec4 calcDiffuseLight(Light light, bool isDir) {
262259
vec3 norm = normalize(out_normal);
263260
vec3 lightPos = light.position.xyz;
264261
vec3 lightDir;
265-
if (light.position.w == 0.0) {
262+
if (isDir) {
266263
lightDir = normalize(-lightPos);
267-
} else if (light.position.w == 1.0) {
264+
} else {
268265
lightDir = normalize(lightPos - out_frag_pos);
269266
}
270267
float diff = max(dot(norm, lightDir), 0.0);
@@ -278,16 +275,17 @@ vec4 calcSpecularLight(Light light) {
278275
279276
void main() {
280277
vec4 fragColor = vec4(1.0);
281-
if (_isTrue(HasLighting)) {
278+
if (HasLighting != 0) {
282279
fragColor *= lightModelAmbient.rgb;
283280
for (int i = 0; i < MAX_LIGHT_SOURCES; ++i) {
284281
Light light = lights[i];
285-
if (_isTrue(light.enabled)) {
286-
vec3 ambient = (light.position.w == 1.0 ? light.ambient.rgb : vec3(1.0)) * material.ambient.rgb;
287-
if (_isTrue(HasColorMaterial)) {
282+
if (light.enabled != 0) {
283+
bool isDir = light.position.w == 0.0;
284+
vec3 ambient = (isDir ? vec3(1.0) : light.ambient.rgb) * material.ambient.rgb;
285+
if (HasColorMaterial != 0) {
288286
ambient *= out_color.rgb;
289287
}
290-
vec4 diffuse = calcDiffuseLight(light) * material.diffuse;
288+
vec4 diffuse = calcDiffuseLight(light, isDir) * material.diffuse;
291289
fragColor *= ambient + diffuse;
292290
}
293291
}
@@ -296,12 +294,11 @@ void main() {
296294
}
297295
""");
298296
for (int i = 0; i < maxTexUnits; i++) {
299-
fragSrc.append(" if (_isTrue(sampler2D_").append(i).append("_enabled)) {\n");
300-
fragSrc.append(" fragColor *= texture2D(sampler2D_").append(i).append(", out_tex_coord.st);\n");
301-
fragSrc.append(" }\n");
297+
fragSrc.append(" if (sampler2D_").append(i).append("_enabled != 0) {\n fragColor *= texture2D(sampler2D_")
298+
.append(i).append(", out_tex_coord.st);\n }\n");
302299
}
303300
fragSrc.append("""
304-
if (_isTrue(HasAlphaTest)) {
301+
if (HasAlphaTest != 0) {
305302
if (alphaTestFunc == 512) {
306303
discard;
307304
} else if (alphaTestFunc == 513) {
@@ -600,7 +597,7 @@ private static void prepareDraw() {
600597
setAlphaTestUniform();
601598
setLightUniform();
602599
for (int i = 0, c = getMaxTexImgUnits(); i < c; i++) {
603-
pipeline.getUniformSafe("sampler2D_" + i + "_enabled", I1).set(isTexture2dEnabled(i));
600+
pipeline.getUniformSafe("sampler2D_" + i + "_enabled", I1).set(texCoordArrayState && isTexture2dEnabled(i));
604601
}
605602
pipeline.updateUniforms();
606603

src/main/java/org/overrun/swgl/core/gui/font/SwglEasyFont.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.jetbrains.annotations.Range;
2828
import org.joml.Vector2i;
2929
import org.overrun.swgl.core.asset.Asset;
30-
import org.overrun.swgl.core.asset.Texture2D;
30+
import org.overrun.swgl.core.asset.tex.Texture2D;
3131
import org.overrun.swgl.core.gl.GLStateMgr;
3232
import org.overrun.swgl.core.io.IFileProvider;
3333

@@ -48,7 +48,7 @@
4848
*/
4949
public class SwglEasyFont {
5050
private static final SwglEasyGlyph[] GLYPHS = new SwglEasyGlyph[256];
51-
private static final IFileProvider FILE_PROVIDER = IFileProvider.of(SwglEasyFont.class);
51+
private static final IFileProvider FILE_PROVIDER = IFileProvider.ofCaller();
5252
private static Texture2D texture;
5353
private static int lastId;
5454

src/main/java/org/overrun/swgl/core/io/HeapManager.java renamed to src/main/java/org/overrun/swgl/core/io/HeapStackFrame.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@
3535
/**
3636
* The heap in auto-closeable.
3737
*
38+
* <h3>Example</h3>
39+
* <pre>{@code try (var heap = new HeapStackFrame()) {
40+
* var bb = heap.utilMemAlloc(8196).put(bytes).flip();
41+
* ProcessBuffer(bb);
42+
* }}</pre>
43+
*
3844
* @author squid233
3945
* @since 0.1.0
4046
*/
41-
public class HeapManager implements AutoCloseable {
47+
public class HeapStackFrame implements AutoCloseable {
4248
private final List<Buffer> utilBuffers = new ArrayList<>();
4349

4450
/**

0 commit comments

Comments
 (0)