mirror of
https://github.com/MistWeatherMedia/WeatherSTAR-3000-Java.git
synced 2026-11-28 14:38:08 -08:00
Add files via upload
This commit is contained in:
1 parent
7396be94c9
commit
1ecfc3c965
44 files changed
+8125
No files matched your search
@@ -0,0 +1,46 @@
|
||||
package renderer;
|
||||
|
||||
import static org.lwjgl.opengl.GL30.glGenFramebuffers;
|
||||
import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER;
|
||||
import static org.lwjgl.opengl.GL30.glBindFramebuffer;
|
||||
import static org.lwjgl.opengl.GL30.*;
|
||||
|
||||
public class FrameBuffer {
|
||||
private int fboID = 0;
|
||||
private Texture texture = null;
|
||||
|
||||
public FrameBuffer(int width, int height) {
|
||||
fboID = glGenFramebuffers();
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
|
||||
|
||||
this.texture = new Texture(width, height);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this.texture.getId(), 0);
|
||||
|
||||
int rboID = glGenRenderbuffers();
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, rboID);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, width, height);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboID);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
assert false : "Error: Framebuffer is not complete";
|
||||
}
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
public void bind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
|
||||
}
|
||||
|
||||
public void unbind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
public int getFboID() {
|
||||
return fboID;
|
||||
}
|
||||
|
||||
public int getTextureId() {
|
||||
return texture.getId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
package renderer;
|
||||
|
||||
import components.SpriteRenderer;
|
||||
import util.AssetPool;
|
||||
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
import static org.lwjgl.opengl.GL30.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import WS3000.weatherstar3000.Window;
|
||||
|
||||
public class RenderBatch implements Comparable<RenderBatch> {
|
||||
private final int POS_SIZE = 2;
|
||||
private final int COLOR_SIZE = 4;
|
||||
private final int TEX_COORDS_SIZE = 2;
|
||||
private final int TEX_ID_SIZE = 1;
|
||||
|
||||
private final int POS_OFFSET = 0;
|
||||
private final int COLOR_OFFSET = POS_OFFSET + (POS_SIZE * Float.BYTES);
|
||||
private final int TEX_COORDS_OFFSET = COLOR_OFFSET + (COLOR_SIZE * Float.BYTES);
|
||||
private final int TEX_ID_OFFSET = TEX_COORDS_OFFSET + (TEX_COORDS_SIZE * Float.BYTES);
|
||||
|
||||
private final int VERTEX_SIZE = 9;
|
||||
private final int VERTEX_SIZE_BYTES = VERTEX_SIZE * Float.BYTES;
|
||||
|
||||
private SpriteRenderer[] sprites;
|
||||
private int numSprites;
|
||||
private boolean hasRoom;
|
||||
private float[] vertices;
|
||||
private int[] texSlots = {0,1,2,3,4,5,6,7};
|
||||
|
||||
private List<Texture> textures;
|
||||
private int vaoID, vboID;
|
||||
private int maxBatchSize;
|
||||
private Shader shader;
|
||||
private int zIndex;
|
||||
|
||||
public RenderBatch(int maxBatchSize, int zIndex) {
|
||||
this.zIndex = zIndex;
|
||||
shader = AssetPool.getShader("/resources/shaders/default.glsl");
|
||||
shader.compile();
|
||||
this.sprites = new SpriteRenderer[maxBatchSize];
|
||||
this.maxBatchSize = maxBatchSize;
|
||||
|
||||
vertices = new float[maxBatchSize * 4 * VERTEX_SIZE];
|
||||
|
||||
this.numSprites = 0;
|
||||
this.hasRoom = true;
|
||||
this.textures = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
vaoID = glGenVertexArrays();
|
||||
glBindVertexArray(vaoID);
|
||||
|
||||
vboID = glGenBuffers();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vboID);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.length * Float.BYTES, GL_DYNAMIC_DRAW);
|
||||
|
||||
int eboID = glGenBuffers();
|
||||
int[] indices = generateIndices();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, POS_SIZE, GL_FLOAT, false, VERTEX_SIZE_BYTES, POS_OFFSET);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, COLOR_SIZE, GL_FLOAT, false, VERTEX_SIZE_BYTES, COLOR_OFFSET);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, TEX_COORDS_SIZE, GL_FLOAT, false, VERTEX_SIZE_BYTES, TEX_COORDS_OFFSET);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glVertexAttribPointer(3, TEX_ID_SIZE, GL_FLOAT, false, VERTEX_SIZE_BYTES, TEX_ID_OFFSET);
|
||||
glEnableVertexAttribArray(3);
|
||||
}
|
||||
|
||||
public void addSprite(SpriteRenderer spr) {
|
||||
int index = this.numSprites;
|
||||
this.sprites[index] = spr;
|
||||
this.numSprites++;
|
||||
|
||||
if (spr.getTexture() != null) {
|
||||
if (!textures.contains(spr.getTexture())) {
|
||||
textures.add(spr.getTexture());
|
||||
}
|
||||
}
|
||||
|
||||
loadVertexProperties(index);
|
||||
|
||||
if (numSprites >= this.maxBatchSize) {
|
||||
this.hasRoom = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void render() {
|
||||
boolean rebufferData = false;
|
||||
for (int i = 0; i < numSprites; i++) {
|
||||
SpriteRenderer spr = sprites[i];
|
||||
if (spr.isDirty()) {
|
||||
loadVertexProperties(i);
|
||||
spr.setClean();
|
||||
rebufferData = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (rebufferData) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vboID);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices);
|
||||
}
|
||||
|
||||
shader.use();
|
||||
shader.uploadMat4f("uProjection", Window.getScene().camera().getProjectionMatrix());
|
||||
shader.uploadMat4f("uView", Window.getScene().camera().getViewMatrix());
|
||||
|
||||
for (int i = 0; i < textures.size(); i++) {
|
||||
glActiveTexture(GL_TEXTURE0 + i + 1);
|
||||
textures.get(i).bind();
|
||||
}
|
||||
|
||||
shader.uploadIntArray("uTextures", texSlots);
|
||||
|
||||
glBindVertexArray(vaoID);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(3);
|
||||
|
||||
glDrawElements(GL_TRIANGLES, this.numSprites * 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(2);
|
||||
glDisableVertexAttribArray(3);
|
||||
glBindVertexArray(0);
|
||||
|
||||
for (int i = 0; i < textures.size(); i++) {
|
||||
textures.get(i).unBind();
|
||||
}
|
||||
|
||||
shader.detach();
|
||||
}
|
||||
|
||||
private void loadVertexProperties(int index) {
|
||||
SpriteRenderer sprite = this.sprites[index];
|
||||
|
||||
int offset = index * 4 * VERTEX_SIZE;
|
||||
|
||||
Vector4f color = sprite.getColor();
|
||||
Vector2f[] texCoords = sprite.getTexCoords();
|
||||
|
||||
int texId = 0;
|
||||
|
||||
if (sprite.getTexture() != null) {
|
||||
for (int i = 0; i < textures.size(); i++) {
|
||||
if (textures.get(i).equals(sprite.getTexture())) {
|
||||
texId = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float xAdd = 1.0f;
|
||||
float yAdd = 1.0f;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (i == 1) {
|
||||
yAdd = 0.0f;
|
||||
} else if (i == 2) {
|
||||
xAdd = 0.0f;
|
||||
} else if (i == 3) {
|
||||
yAdd = 1.0f;
|
||||
}
|
||||
|
||||
vertices[offset] = sprite.gameObject.transform.position.x + (xAdd * sprite.gameObject.transform.scale.x);
|
||||
vertices[offset + 1] = sprite.gameObject.transform.position.y + (yAdd * sprite.gameObject.transform.scale.y);
|
||||
|
||||
vertices[offset + 2] = color.x;
|
||||
vertices[offset + 3] = color.y;
|
||||
vertices[offset + 4] = color.z;
|
||||
vertices[offset + 5] = color.w;
|
||||
|
||||
vertices[offset + 6] = texCoords[i].x;
|
||||
vertices[offset + 7] = texCoords[i].y;
|
||||
|
||||
vertices[offset + 8] = texId;;
|
||||
|
||||
offset += VERTEX_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
private int[] generateIndices() {
|
||||
int[] elements = new int [6 * maxBatchSize];
|
||||
for (int i = 0; i < maxBatchSize; i++) {
|
||||
loadElementIndices(elements, i);
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
private void loadElementIndices(int[] elements, int index) {
|
||||
int offsetArrayIndex = 6 * index;
|
||||
int offset = 4 * index;
|
||||
|
||||
elements[offsetArrayIndex] = offset + 3;
|
||||
elements[offsetArrayIndex + 1] = offset + 2;
|
||||
elements[offsetArrayIndex + 2] = offset + 0;
|
||||
|
||||
elements[offsetArrayIndex + 3] = offset + 0;
|
||||
elements[offsetArrayIndex + 4] = offset + 2;
|
||||
elements[offsetArrayIndex + 5] = offset + 1;
|
||||
}
|
||||
|
||||
public boolean hasRoom() {
|
||||
return this.hasRoom;
|
||||
}
|
||||
|
||||
public boolean hasTextureRoom() {
|
||||
return this.textures.size() < 8;
|
||||
}
|
||||
|
||||
public boolean hasTexture(Texture tex) {
|
||||
return this.textures.contains(tex);
|
||||
}
|
||||
|
||||
public int zIndex() {
|
||||
return this.zIndex;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.numSprites = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(RenderBatch o) {
|
||||
return Integer.compare(this.zIndex, o.zIndex());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package renderer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import WS3000.weatherstar3000.GameObject;
|
||||
import components.SpriteRenderer;
|
||||
|
||||
public class Renderer {
|
||||
private final int MAX_BATCH_SIZE = 2500;
|
||||
private List<RenderBatch> batches;
|
||||
|
||||
public Renderer() {
|
||||
this.batches = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
public void add(GameObject go) {
|
||||
SpriteRenderer spr = go.getComponent(SpriteRenderer.class);
|
||||
if (spr != null) {
|
||||
add(spr);
|
||||
}
|
||||
}
|
||||
|
||||
private void add(SpriteRenderer sprite) {
|
||||
boolean added = false;
|
||||
for (RenderBatch batch : batches) {
|
||||
if (batch.hasRoom() && batch.zIndex() == sprite.gameObject.zIndex()) {
|
||||
Texture tex = sprite.getTexture();
|
||||
if (tex == null || (batch.hasTexture(tex) || batch.hasTextureRoom())) {
|
||||
batch.addSprite(sprite);
|
||||
added = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!added) {
|
||||
RenderBatch newBatch = new RenderBatch(MAX_BATCH_SIZE, sprite.gameObject.zIndex());
|
||||
newBatch.start();
|
||||
batches.add(newBatch);
|
||||
newBatch.addSprite(sprite);
|
||||
Collections.sort(batches);
|
||||
}
|
||||
}
|
||||
|
||||
public void render() {
|
||||
for (RenderBatch batch : batches) {
|
||||
batch.render();
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
for (RenderBatch batch : batches) {
|
||||
batch.clear(); // Resets numSprites to 0
|
||||
}
|
||||
//this.batches.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package renderer;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.file.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.joml.*;
|
||||
import org.lwjgl.BufferUtils;
|
||||
|
||||
public class Shader {
|
||||
private int shaderProgramID;
|
||||
private boolean beingUsed = false;
|
||||
private String vertexSource;
|
||||
private String fragmentSource;
|
||||
private String filepath;
|
||||
|
||||
public Shader(String filepath) {
|
||||
this.filepath = filepath;
|
||||
try {
|
||||
// Make sure the path starts with a leading slash for root JAR loading
|
||||
String resourcePath = filepath.startsWith("/") ? filepath : "/" + filepath;
|
||||
|
||||
String source;
|
||||
try (InputStream is = Shader.class.getResourceAsStream(resourcePath)) {
|
||||
if (is == null) {
|
||||
throw new IOException("Shader file not found inside JAR: " + resourcePath);
|
||||
}
|
||||
// Safely read all text lines into a single String
|
||||
source = new BufferedReader(new InputStreamReader(is))
|
||||
.lines().collect(Collectors.joining("\n"));
|
||||
}
|
||||
|
||||
String[] splitString = source.split("(#type)( )+([a-zA-Z]+)");
|
||||
|
||||
int index = source.indexOf("#type") + 6;
|
||||
int eol = source.indexOf("\n", index);
|
||||
String firstPattern = source.substring(index, eol).trim();
|
||||
|
||||
index = source.indexOf("#type", eol) + 6;
|
||||
eol = source.indexOf("\n", index);
|
||||
String secondPattern = source.substring(index, eol).trim();
|
||||
|
||||
if (firstPattern.equals("vertex")) {
|
||||
vertexSource = splitString[1];
|
||||
} else if (firstPattern.equals("fragment")) {
|
||||
fragmentSource = splitString[1];
|
||||
} else {
|
||||
throw new IOException("Unexpected token '" + firstPattern + "'");
|
||||
}
|
||||
|
||||
if (secondPattern.equals("vertex")) {
|
||||
vertexSource = splitString[2];
|
||||
} else if (secondPattern.equals("fragment")) {
|
||||
fragmentSource = splitString[2];
|
||||
} else {
|
||||
throw new IOException("Unexpected token '" + secondPattern + "'");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
assert false : "ERROR: Could not open file for shader: " + filepath;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void compile() {
|
||||
int vertexID, fragmentID;
|
||||
vertexID = glCreateShader(GL_VERTEX_SHADER);
|
||||
|
||||
glShaderSource(vertexID, vertexSource);
|
||||
glCompileShader(vertexID);
|
||||
|
||||
int success = glGetShaderi(vertexID, GL_COMPILE_STATUS);
|
||||
if (success == GL_FALSE) {
|
||||
int len = glGetShaderi(vertexID, GL_INFO_LOG_LENGTH);
|
||||
System.out.println("ERROR: " + filepath + "\n\tVertex shader compilation failed.");
|
||||
System.out.println(glGetShaderInfoLog(vertexID, len));
|
||||
assert false : "";
|
||||
}
|
||||
|
||||
fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
glShaderSource(fragmentID, fragmentSource);
|
||||
glCompileShader(fragmentID);
|
||||
|
||||
success = glGetShaderi(fragmentID, GL_COMPILE_STATUS);
|
||||
if (success == GL_FALSE) {
|
||||
int len = glGetShaderi(fragmentID, GL_INFO_LOG_LENGTH);
|
||||
System.out.println("ERROR: " + filepath + "\n\tVertex shader compilation failed.");
|
||||
System.out.println(glGetShaderInfoLog(fragmentID, len));
|
||||
assert false : "";
|
||||
}
|
||||
|
||||
shaderProgramID = glCreateProgram();
|
||||
glAttachShader(shaderProgramID, vertexID);
|
||||
glAttachShader(shaderProgramID, fragmentID);
|
||||
glLinkProgram(shaderProgramID);
|
||||
|
||||
success = glGetProgrami(shaderProgramID, GL_LINK_STATUS);
|
||||
if (success == GL_FALSE) {
|
||||
int len = glGetProgrami(shaderProgramID, GL_LINK_STATUS);
|
||||
System.out.println("ERROR: " + filepath + "\n\tFailed to link shaders.");
|
||||
System.out.println(glGetProgramInfoLog(shaderProgramID, len));
|
||||
assert false : "";
|
||||
}
|
||||
}
|
||||
|
||||
public void use() {
|
||||
if (!beingUsed) {
|
||||
glUseProgram(shaderProgramID);
|
||||
beingUsed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void detach() {
|
||||
glUseProgram(0);
|
||||
beingUsed = false;
|
||||
}
|
||||
|
||||
public void uploadMat4f(String varName, Matrix4f mat4) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
FloatBuffer matBuffer = BufferUtils.createFloatBuffer(16);
|
||||
mat4.get(matBuffer);
|
||||
glUniformMatrix4fv(varLocation, false, matBuffer);
|
||||
}
|
||||
|
||||
public void uploadMat3f(String varName, Matrix3f mat3) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
FloatBuffer matBuffer = BufferUtils.createFloatBuffer(9);
|
||||
mat3.get(matBuffer);
|
||||
glUniformMatrix3fv(varLocation, false, matBuffer);
|
||||
}
|
||||
|
||||
public void uploadVec4f(String varName, Vector4f vec) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform4f(varLocation, vec.x, vec.y, vec.z, vec.w);
|
||||
}
|
||||
|
||||
public void uploadVec3f(String varName, Vector3f vec) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform3f(varLocation, vec.x, vec.y, vec.z);
|
||||
}
|
||||
|
||||
public void uploadVec2f(String varName, Vector2f vec) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform2f(varLocation, vec.x, vec.y);
|
||||
}
|
||||
|
||||
public void uploadFloat(String varName, float val) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform1f(varLocation, val);
|
||||
}
|
||||
|
||||
public void uploadInt(String varName, int val) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform1i(varLocation, val);
|
||||
}
|
||||
|
||||
public void uploadTexture(String varName, int slot) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform1i(varLocation, slot);
|
||||
}
|
||||
|
||||
public void uploadIntArray(String varName, int[] array) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform1iv(varLocation, array);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package renderer;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.ARBTextureRectangle.*;
|
||||
|
||||
import static org.lwjgl.stb.STBImage.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.*;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
|
||||
public class Texture {
|
||||
private String filepath;
|
||||
private int texID;
|
||||
private int width, height;
|
||||
|
||||
public Texture() {
|
||||
texID = -1;
|
||||
width = -1;
|
||||
height = -1;
|
||||
}
|
||||
|
||||
public Texture(int width, int height) {
|
||||
this.filepath = "Generated";
|
||||
|
||||
texID = glGenTextures();
|
||||
glBindTexture(GL_TEXTURE_2D, texID);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
||||
}
|
||||
|
||||
public Texture(String filepath) {
|
||||
this.filepath = filepath;
|
||||
|
||||
texID = glGenTextures();
|
||||
glBindTexture(GL_TEXTURE_2D, texID);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
IntBuffer widthBuffer = BufferUtils.createIntBuffer(1);
|
||||
IntBuffer heightBuffer = BufferUtils.createIntBuffer(1);
|
||||
IntBuffer channelsBuffer = BufferUtils.createIntBuffer(1);
|
||||
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
|
||||
// Ensure the path starts with a leading slash for classpath lookup
|
||||
String resourcePath = filepath.startsWith("/") ? filepath : "/" + filepath;
|
||||
ByteBuffer image = null;
|
||||
|
||||
try (InputStream is = Texture.class.getResourceAsStream(resourcePath)) {
|
||||
if (is == null) {
|
||||
throw new IOException("Texture file not found inside JAR: " + resourcePath);
|
||||
}
|
||||
|
||||
// Read bytes from the JAR stream into a native ByteBuffer
|
||||
byte[] bytes = is.readAllBytes();
|
||||
ByteBuffer nativeBuffer = BufferUtils.createByteBuffer(bytes.length);
|
||||
nativeBuffer.put(bytes);
|
||||
nativeBuffer.flip();
|
||||
|
||||
// Tell STB to decode the image from raw memory instead of a disk path
|
||||
image = stbi_load_from_memory(nativeBuffer, widthBuffer, heightBuffer, channelsBuffer, 0);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
assert false : "ERROR: Could not read stream for texture: " + filepath;
|
||||
}
|
||||
|
||||
if (image != null) {
|
||||
this.width = widthBuffer.get(0);
|
||||
this.height = heightBuffer.get(0);
|
||||
int channels = channelsBuffer.get(0);
|
||||
|
||||
if (channels == 3) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this.width, this.height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
|
||||
} else if (channels == 4) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
|
||||
} else {
|
||||
stbi_image_free(image);
|
||||
assert false : "ERROR: (Texture) Unknown number of channels " + channels + ", " + filepath;
|
||||
}
|
||||
stbi_image_free(image);
|
||||
} else {
|
||||
assert false : "ERROR: STB failed to decode image " + filepath + " Reason: " + stbi_failure_reason();
|
||||
}
|
||||
}
|
||||
|
||||
public void bind() {
|
||||
glBindTexture(GL_TEXTURE_2D, texID);
|
||||
}
|
||||
|
||||
public void unBind() {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return this.width;
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return this.filepath;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return this.height;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.texID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null) return false;
|
||||
if (!(o instanceof Texture)) return false;
|
||||
Texture oTex = (Texture) o;
|
||||
return oTex.getWidth() == this.width && oTex.getHeight() == this.height && oTex.getId() == this.texID && oTex.getFilePath().equals(this.filepath);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user