So I currently use a texture class , however this class is quite limiting as you have to load it in before you setup the textures. So the issue comes when for instance I load in a spritesheet create object A , then I load in spritesheet buttons , then I create tile B and the issue comes where B gets a buttons texture and not a tile texture. Here is the example of it using my text renderer.
What happens is that when I load in the new texture every other texture freaks out.
Here is my GB class that generates the VBOS (each tile, text, GUI , image has its own vbo which I will sort out later)
package com.Core.graphics;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import com.Core.Gamecore;
import com.Core.Util.Translationhelper;
import com.Core.graphics.shader.Shaderclass;
import com.Core.graphics.texture.Texture;
public class GB {// Sepeartely used object only.
private FloatBuffer verticies;
private FloatBuffer texcoord;
private int vertid, texid;
public vertex2d translation;
private Texture t;
private int transformid, scalarid, vertcount;
private float scalar;
private Translationhelper th;
private Gamecore gc;
private Shaderclass shader;
public GB(Gamecore gc) {
shader = new Shaderclass();
shader.attachVertexShader("com/Core/graphics/shader/shaders/vertshader.vert");
shader.attachFragmentShader("com/Core/graphics/shader/shaders/fragshader.frag");
shader.link();
transformid = GL20.glGetUniformLocation(shader.programID, "transform");
scalarid = GL20.glGetUniformLocation(shader.programID, "scalar");
scalar = gc.verticalaspect;
th = new Translationhelper();
th.setsize(gc.width, gc.height);
this.gc = gc;
}
public void createobject(vertex2d[] object, Texture t, int texunit) {
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
translation = new vertex2d(0, 0);
verticies = BufferUtils.createFloatBuffer(2 * object.length);
texcoord = BufferUtils.createFloatBuffer(2 * object.length);
for (int i = 0; i < object.length; i++) {
verticies.put(object[i].getx());
verticies.put(object[i].gety());
texcoord.put(object[i].gets());
texcoord.put(object[i].gett());
}
verticies.rewind();
texcoord.rewind();
vertid = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticies, GL15.GL_DYNAMIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
vertcount = object.length;
texid = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texid);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texcoord, GL15.GL_DYNAMIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
this.t = t;
bindtexture(texunit);
}
public void bindtexture(int texunit) {
int loc = GL20.glGetUniformLocation(shader.programID, "tex");
if (loc != -1) {
GL20.glUniform1i(loc, texunit);
if (texunit == 0) {
GL13.glActiveTexture(GL13.GL_TEXTURE0);
} else if (texunit == 1) {
GL13.glActiveTexture(GL13.GL_TEXTURE1);
} else if (texunit == 2) {
GL13.glActiveTexture(GL13.GL_TEXTURE2);
} else if (texunit == 3) {
GL13.glActiveTexture(GL13.GL_TEXTURE3);
}
} else {
System.err.println("WARNING: NO VARIABLE DETECTED WITHIN SHADER");
}
}
public void translate(float f, float g) {
vertex2d transformed = th.transform((f*2) - (gc.width), (
g*2) +(gc.height));
translation.setcoordinates(transformed.getx(), transformed.gety());
}
public void forcetranslate(float f, float g) {
translation.setcoordinates(f, g);
}
public void render() {
shader.bind();
// Bind the vertex buffer
// t.bindtex(t.id, t.width, t.height, t.buffer);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, t.id);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texid);
GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 0, 0);
GL20.glUniform2f(transformid, translation.getx(), translation.gety());
GL20.glUniform1f(scalarid, scalar);
// Draw the textured rectangle
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertcount);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
shader.unbind();
}
public void destroy() {
if (Display.isActive()) {
GL15.glDeleteBuffers(vertid);
GL15.glDeleteBuffers(texid);
}
shader.dispose();
}
}
package com.Core.graphics.texture;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
//Many thanks to SHC for the tutorial on this
public class Texture {
public int id;
public int width;
public int height;
public ByteBuffer buffer;
private Texture(int id, int width, int height, ByteBuffer buffer) {
this.id = id;
this.width = width;
this.height = height;
this.buffer = buffer;
}
public Texture(){
}
public static Texture loadTexture(String name) {
BufferedImage bimg = null;
try {
bimg = ImageIO.read(Texture.class.getClassLoader()
.getResourceAsStream(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("Texture loading failure! " + name);
}
int[] pixels = new int[bimg.getWidth() * bimg.getHeight()];
bimg.getRGB(0, 0, bimg.getWidth(), bimg.getHeight(), pixels, 0,
bimg.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(bimg.getWidth()
* bimg.getHeight() * 4);
System.out.println(pixels[0]);
for (int y = 0; y < bimg.getHeight(); y++) {
for (int x = 0; x < bimg.getWidth(); x++) {
if (pixels[y * bimg.getWidth() + x] != -65281
) {
int pixel = pixels[y * bimg.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF));
buffer.put((byte) ((pixel >> 8) & 0xFF));
buffer.put((byte) (pixel & 0xFF));
buffer.put((byte) ((pixel >> 24) & 0xFF));
}
else if(pixels[y * bimg.getWidth() + x] != -1){
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put((byte)0);
}
else{
buffer.put((byte)-1);
buffer.put((byte)-1);
buffer.put((byte)-1);
buffer.put((byte)-1);
}
}
}
buffer.flip();
int textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);
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_RGBA8, bimg.getWidth(),
bimg.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
return new Texture(textureID, bimg.getWidth(), bimg.getHeight(),buffer);
}
public void bindtex(int textureID,int width , int height ,ByteBuffer buffer){
glBindTexture(GL_TEXTURE_2D, textureID);
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_RGBA8, width,
height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
}
}
At the bottom (bindtex) is my attempt to remedy the issue by reloading the texture in just before I render the vbo.
Im not the best with OpenGL so its going straight over me. From what I can see though its loading the texture which is messing with the other stored textures causing them to render as the last loaded texture