trying to add textures and still failing
This commit is contained in:
parent
ebf26f18d8
commit
5fc63b6c73
BIN
resources/textures/testimg.png
Normal file
BIN
resources/textures/testimg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.6 KiB |
|
@ -24,26 +24,32 @@ public class Test {
|
|||
0, 1, 2,
|
||||
0, 2, 3
|
||||
|
||||
}, new Material("/resources/textures/testimg.png"));
|
||||
}, new Material(new Texture("resources/textures/testimg.png")));
|
||||
|
||||
public void run() {
|
||||
init();
|
||||
public void run() throws Exception {
|
||||
setup();
|
||||
i = new Input(display);
|
||||
while (!(display.shouldClose()) && !i.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) {
|
||||
update();
|
||||
render();
|
||||
loop();
|
||||
}
|
||||
|
||||
close();
|
||||
|
||||
}
|
||||
public void init() {
|
||||
|
||||
public void loop() {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
public void setup() throws Exception {
|
||||
// System.out.println("Initializing Game ");
|
||||
display = new Display(WIDTH, HEIGHT, windowName);
|
||||
display.create();
|
||||
shader = new Shader("/resources/shaders/mainVertex.glsl", "/resources/shaders/mainFragment.glsl");
|
||||
renderer = new Renderer(shader);
|
||||
display.setBackgroundColor(1F, 0, 0);
|
||||
display.create();
|
||||
mesh.create();
|
||||
shader.create();
|
||||
|
||||
|
@ -70,7 +76,7 @@ public class Test {
|
|||
shader.destroy();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws Exception {
|
||||
new Test().run();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,48 +1,107 @@
|
|||
package org.hl.engine.graphics;
|
||||
|
||||
import org.hl.engine.utils.TextureLoader;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.openvr.Texture;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import org.hl.engine.math.lalg.Vector4f;
|
||||
|
||||
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 String path;
|
||||
|
||||
private BufferedImage image;
|
||||
|
||||
private int width, height;
|
||||
private int textureID;
|
||||
|
||||
public Material(String path) {
|
||||
|
||||
this.path = path;
|
||||
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() {
|
||||
|
||||
this.image = TextureLoader.loadImage(path); //The path is inside the jar file
|
||||
this.width = this.image.getWidth();
|
||||
this.height = this.image.getHeight();
|
||||
this.textureID = TextureLoader.loadTexture(image);
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public int getTextureID() {
|
||||
return textureID;
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
GL11.glDeleteTextures(textureID);
|
||||
texture.create();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ public class Mesh {
|
|||
|
||||
GL30.glDeleteVertexArrays(vertexArrayObject);
|
||||
|
||||
material.destroy();
|
||||
material.getTexture().destroy();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ public class Renderer {
|
|||
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
|
||||
GL13.glBindTexture(GL11.GL_TEXTURE_2D, mesh.getMaterial().getTextureID());
|
||||
GL13.glBindTexture(GL11.GL_TEXTURE_2D, mesh.getMaterial().getTexture().getId());
|
||||
|
||||
shader.bind();
|
||||
|
||||
|
|
133
src/org/hl/engine/graphics/Texture.java
Normal file
133
src/org/hl/engine/graphics/Texture.java
Normal file
|
@ -0,0 +1,133 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -94,12 +94,13 @@ public class Display {
|
|||
|
||||
|
||||
// Creates the window (should go in the init() function of your Main program)
|
||||
public void create() {
|
||||
public void create() throws Exception {
|
||||
|
||||
// initializing glfw
|
||||
if (!glfwInit()) {
|
||||
System.err.println("Failed to initialize GLFW! ");
|
||||
System.exit(1);
|
||||
//System.err.println("Failed to initialize GLFW! ");
|
||||
//System.exit(1);
|
||||
throw new Exception("Failed to initialize GLFW! ");
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
|
@ -111,8 +112,9 @@ public class Display {
|
|||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
||||
window = glfwCreateWindow(this.width, this.height, this.windowName, isFullscreen ? glfwGetPrimaryMonitor():0, 0);
|
||||
if (window == 0) {
|
||||
System.err.println("Failed to create window! ");
|
||||
System.exit(1);
|
||||
//System.err.println("Failed to create window! ");
|
||||
//System.exit(1);
|
||||
throw new Exception("Failed to create window! ");
|
||||
}
|
||||
|
||||
// Setting size of window
|
||||
|
|
55
src/org/hl/engine/math/lalg/Vector4f.java
Normal file
55
src/org/hl/engine/math/lalg/Vector4f.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
package org.hl.engine.math.lalg;
|
||||
|
||||
public class Vector4f {
|
||||
private float x;
|
||||
private float y;
|
||||
private float z;
|
||||
private float a;
|
||||
|
||||
// Just a vector if you know what I mean
|
||||
public Vector4f (float x, float y, float z, float a) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public void setVector(float x, float y, float z, float a) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(float x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public float getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(float y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public float getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setZ(float z) {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public float getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public void setA(float a) {
|
||||
this.a = a;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
#version 460 core
|
||||
#version 410 core
|
||||
|
||||
in vec3 passColor;
|
||||
in vec2 passTextureCoord;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#version 460 core
|
||||
#version 410 core
|
||||
|
||||
in vec3 position;
|
||||
in vec3 color;
|
||||
|
|
Reference in New Issue
Block a user