Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.syscy.hengine.rendering.resources;
- //Some imports...
- public class Texture extends Resource {
- //ID's of the default textures which are used if the texture hasn't been loaded and the bind method is called
- private static int defaultDiffuseTextureID = -1;
- private static int defaultNormalTextureID = -1;
- private static int defaultDispTextureID = -1;
- //The @Getter and @Setter is from Lombok and this just generates a Getter and a Setter for the variable
- private static @Getter @Setter boolean useMipMaps = true;
- private int id = -1;
- protected Texture(String fileName) {
- super(fileName);
- if (defaultDiffuseTextureID == -1 || defaultNormalTextureID == -1 || defaultDispTextureID == -1) {
- defaultDiffuseTextureID = loadTexture("default/diffuse.jpg");
- defaultNormalTextureID = loadTexture("default/normal.jpg");
- defaultDispTextureID = loadTexture("default/displacement.jpg");
- }
- }
- //Gets automatically called by the ResourceManager is the texture is currently used but not loaded
- @Override
- public void load() {
- super.load();
- this.id = loadTexture(getFileName());
- }
- //Gets automatically called by the ResourceManager if it isn't used for 10secs
- @Override
- public void unload() {
- super.unload();
- //glDeleteBuffers(id); should be the same. I just tried glDeleteTextures to see if this is the origin of the bug
- glDeleteTextures(id);
- }
- public void bind() {
- bind(0);
- }
- //Binds the texture and updates lastUsedTime
- public void bind(int samplerSlot) {
- assert (samplerSlot >= 0 && samplerSlot <= 31);
- updateLastUsedTime();
- glActiveTexture(GL_TEXTURE0 + samplerSlot);
- glBindTexture(GL_TEXTURE_2D, getID());
- }
- //Returns the id if the id isn't -1
- public int getID() {
- if (id == -1 || !isLoaded()) return getDefaultTexture(getFileName());
- return id;
- }
- private static int getDefaultTexture(String fileName) {
- if(fileName.toLowerCase().contains("normal")) {
- return defaultNormalTextureID;
- } else if(fileName.toLowerCase().contains("disp")) {
- return defaultDispTextureID;
- } else {
- return defaultDiffuseTextureID;
- }
- }
- //Loads the texture :D
- private static int loadTexture(String fileName) {
- try {
- BufferedImage image = ImageIO.read(new File("./data/textures/" + fileName));
- int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
- ByteBuffer buffer = Util.createByteBuffer(image.getHeight() * image.getWidth() * 4);
- boolean hasAlpha = image.getColorModel().hasAlpha();
- for (int y = 0; y < image.getHeight(); y++) {
- for (int x = 0; x < image.getWidth(); x++) {
- int pixel = pixels[y * image.getWidth() + x];
- buffer.put((byte) ((pixel >> 16) & 0xFF));
- buffer.put((byte) ((pixel >> 8) & 0xFF));
- buffer.put((byte) ((pixel) & 0xFF));
- if (hasAlpha) buffer.put((byte) ((pixel >> 24) & 0xFF));
- else
- buffer.put((byte) (0xFF));
- }
- }
- buffer.flip();
- int id = glGenTextures();
- glBindTexture(GL_TEXTURE_2D, id);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- if(useMipMaps && Util.openGLVersion >= 30) {
- glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- } else {
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- }
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
- return id;
- } catch (Exception ex) {
- if(getDefaultTexture(fileName) != -1)
- return getDefaultTexture(fileName);
- else
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement