wtf why is material and texture gone

This commit is contained in:
EvilMuffinHa 2020-05-30 16:16:00 -04:00
parent df45c5e086
commit e6a3dcc50e
2 changed files with 254 additions and 0 deletions

View File

@ -0,0 +1,120 @@
package org.hl.engine.graphics;
import org.hl.engine.math.lalg.Vector4f;
import org.lwjgl.system.MemoryStack;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.GL_CLAMP_TO_EDGE;
import static org.lwjgl.opengl.GL30.glGenerateMipmap;
import static org.lwjgl.stb.STBImage.*;
import static org.lwjgl.stb.STBImage.stbi_image_free;
public class Material {
private static final Vector4f DEFAULT_COLOR = new Vector4f(1.0f, 1.0f, 1.0f, 1.0f);
private Vector4f ambientColor;
private Vector4f diffuseColor;
private Vector4f specularColor;
private float reflectance;
private Texture texture;
private Texture normalMap;
public Material() {
this.ambientColor = DEFAULT_COLOR;
this.diffuseColor = DEFAULT_COLOR;
this.specularColor = DEFAULT_COLOR;
this.texture = null;
this.reflectance = 0;
}
public Material(Vector4f color, float reflectance) {
this(color, color, color, null, reflectance);
}
public Material(Texture texture) {
this(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, texture, 0);
}
public Material(Texture texture, float reflectance) {
this(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, texture, reflectance);
}
public Material(Vector4f ambientColor, Vector4f diffuseColor, Vector4f specularColor, Texture texture, float reflectance) {
this.ambientColor = ambientColor;
this.diffuseColor = diffuseColor;
this.specularColor = specularColor;
this.texture = texture;
this.reflectance = reflectance;
}
public Vector4f getAmbientColor() {
return ambientColor;
}
public void setAmbientColor(Vector4f ambientColor) {
this.ambientColor = ambientColor;
}
public Vector4f getDiffuseColor() {
return diffuseColor;
}
public void setDiffuseColor(Vector4f diffuseColor) {
this.diffuseColor = diffuseColor;
}
public Vector4f getSpecularColor() {
return specularColor;
}
public void setSpecularColor(Vector4f specularColor) {
this.specularColor = specularColor;
}
public float getReflectance() {
return reflectance;
}
public void setReflectance(float reflectance) {
this.reflectance = reflectance;
}
public boolean isTextured() {
return this.texture != null;
}
public Texture getTexture() {
return texture;
}
public void setTexture(Texture texture) {
this.texture = texture;
}
public boolean hasNormalMap() {
return this.normalMap != null;
}
public Texture getNormalMap() {
return normalMap;
}
public void setNormalMap(Texture normalMap) {
this.normalMap = normalMap;
}
public void create() {
texture.create();
}
}

View File

@ -0,0 +1,134 @@
package org.hl.engine.graphics;
import static org.lwjgl.opengl.GL46.*;
import static org.lwjgl.stb.STBImage.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import org.lwjgl.system.MemoryStack;
public class Texture {
private int id;
private int width;
private int height;
private int type;
private String fileName;
private ByteBuffer imageBuffer;
private int pixelFormat;
public Texture(int width, int height, int pixelFormat) {
this.type = 0;
this.width = width;
this.height = height;
this.pixelFormat = pixelFormat;
}
public Texture(String fileName) {
this.type = 1;
this.fileName = fileName;
}
public Texture(ByteBuffer imageBuffer) {
type = 2;
this.imageBuffer = imageBuffer;
}
public void create() {
if (this.type == 0) {
this.id = glGenTextures();
glBindTexture(GL_TEXTURE_2D, this.id);
glTexImage2D(GL_TEXTURE_2D, 0, this.pixelFormat, this.width, this.height, 0, this.pixelFormat, GL_FLOAT, (ByteBuffer) null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
} else if (this.type == 1) {
ByteBuffer buf;
try(MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer w = stack.mallocInt(1);
IntBuffer h = stack.mallocInt(1);
IntBuffer channels = stack.mallocInt(1);
buf = stbi_load(this.fileName, w, h, channels, 4);
if(buf == null) {
System.err.println("Image file [" + this.fileName + "] not loaded: " + stbi_failure_reason());
System.exit(1);
}
this.width = w.get();
this.height = h.get();
}
this.id = createTexture(buf);
stbi_image_free(buf);
} else {
ByteBuffer buf;
try(MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer w = stack.mallocInt(1);
IntBuffer h = stack.mallocInt(1);
IntBuffer channels = stack.mallocInt(1);
buf = stbi_load_from_memory(this.imageBuffer, w, h, channels, 4);
if(buf == null) {
System.err.println("Image file not loaded: " + stbi_failure_reason());
System.exit(1);
}
this.width = w.get();
this.height = h.get();
}
this.id = createTexture(buf);
stbi_image_free(buf);
}
}
private int createTexture(ByteBuffer buf) {
int textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_RGBA, GL_UNSIGNED_BYTE, buf);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return textureID;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getId() {
return id;
}
public void destroy() {
glDeleteTextures(id);
}
}