Hello,
Just a while ago I got a book for OpenGL which greatly helped me out with getting programming started. But let’s just get to the point.
I got an issue where the texture that I’m trying to load is completely white. I checked on various other people that had the same issue, but none of them helped.
I’ve tried multiple things, such as disabling GL_TEXTURE_2D and enabling it again, making an InputStream method and setting it to the the decoder, and so on.
And just to get it out of the way, I am using TWL’s PNGDecoder to decode the image.
Inside main class:
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.OpenGLException;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL20;
public class Ironbolt {
private long last = 0;
public String screen = "title";
public static int program;
public static int shader;
private Texture title;
public void initGL(int width, int height, boolean full) {
System.out.println("Initializing...");
try {
System.out.print("Setting display mode " + width + " " + height + " " + full + "\nApplying");
Display.setDisplayMode(new DisplayMode(width, height));
System.out.print(".");
Display.setFullscreen(full);
System.out.print(".");
Display.create();
System.out.println(".");
System.out.println("Initializing keyboard...");
Keyboard.create();
System.out.println("Initializing mouse...");
Mouse.create();
} catch (LWJGLException e) {
System.out.println("LWJGLException: Failed to initialize display");
System.exit(-1);
}
Display.setVSyncEnabled(true);
System.out.println("Enabling OpenGL field values...");
glEnable(GL_TEXTURE_2D);
program = GL20.glCreateProgram();
shader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
GL20.glAttachShader(Ironbolt.program, Ironbolt.shader);
GL20.glUseProgram(program);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, width, height);
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
System.out.println("Done");
}
public void run() {
initGL(1280, 720, false);
title = new Texture("title");
delta();
while (!Display.isCloseRequested()) {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
switch (screen) {
case "title":
Texture.render(title);
}
delta();
Display.update();
}
Display.destroy();
Keyboard.destroy();
Mouse.destroy();
}
public int delta() {
long time = Sys.getTime();
int delta = (int)(time - last);
last = time;
return delta;
}
public static void main(String[] args) {
Ironbolt game = new Ironbolt();
game.run();
}
}
Inside texture class:
import de.matthiasmann.twl.utils.PNGDecoder;
import java.io.IOException;
import java.nio.ByteBuffer;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL30;
public class Texture {
private int id;
private int width;
private int height;
private int imgWidth;
private int imgHeight;
private int texWidth;
private int texHeight;
public Texture(String name) {
try {
System.out.println("Initializing decode of texture " + name);
PNGDecoder decoder = new PNGDecoder(this.getClass().getResourceAsStream("textures/" + name + ".png"));
ByteBuffer buffer = ByteBuffer.allocateDirect(decoder.getWidth() * decoder.getHeight() * 4);
System.out.println("Buffer position: " + buffer.position());
if (buffer.position() != 0) {
System.out.println("Buffer position not neutrulized, setting to 0");
buffer.position(0);
}
decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
System.out.println("Buffer size: " + buffer.capacity());
System.out.println("Width: " + decoder.getWidth());
System.out.println("Height: " + decoder.getHeight());
buffer.flip();
System.out.println("Done");
imgWidth = decoder.getWidth();
imgHeight = decoder.getHeight();
System.out.println("Comparing image with byte buffer...");
if (imgWidth * imgHeight * 4 != buffer.limit()) throw new ArrayIndexOutOfBoundsException("Buffer size is not equal to image size");
System.out.println("Byte buffer size is correct");
int i = 1;
while (i < imgWidth) {
i = i * 2;
}
i = i / 2;
System.out.println("Texture width power is " + i);
texWidth = i;
i = 1;
while (i < imgHeight) {
i = i * 2;
}
i = i / 2;
System.out.println("Texture height power is " + i);
texHeight = i;
createTexture(buffer);
System.out.println("Checking texture state for texture " + id);
if (!glIsTexture(id)) {
System.out.println("Error, texture " + id + " is not a texture");
} else {
System.out.println("Texture state OK. Adding to list...");
}
System.out.println("Done");
} catch (IOException e) {
System.out.println("IOException: Failed to decode image");
}
}
private void createTexture(ByteBuffer buffer) {
System.out.println("Creating texture...");
id = glGenTextures();
bind();
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0,
GL_RGBA8,
width, height,
0,
GL_RGBA, GL_UNSIGNED_BYTE,
buffer);
GL30.glGenerateMipmap(GL_TEXTURE_2D);
System.out.println("Texture created");
}
public int getTextureWidth() {
return texWidth;
}
public int getTextureHeight() {
return texHeight;
}
public int getImageWidth() {
return imgWidth;
}
public int getImageHeight() {
return imgHeight;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void clear() {
glDeleteTextures(id);
}
public void bind() {
glBindTexture(GL_TEXTURE_2D, id);
}
public static void render(Texture texture) {
glEnable(GL_TEXTURE_2D);
glPushMatrix();
if (glIsTexture(texture.id)) {
texture.bind();
} else {
throw new IllegalArgumentException("The texture method is invalid");
}
glTranslated(0, 0, 0);
glBegin(GL_QUADS);
{
glTexCoord2d(0, 0);
glVertex2d(0, 0);
glTexCoord2d(0, texture.getTextureHeight());
glVertex2d(0, texture.getImageHeight());
glTexCoord2d(texture.getTextureWidth(), texture.getTextureHeight());
glVertex2d(texture.getImageWidth(), texture.getImageHeight());
glTexCoord2d(texture.getTextureWidth(), 0);
glVertex2d(texture.getImageWidth(), 0);
}
glEnd();
glPopMatrix();
}
}
Anything you spot that is wrong? Since I debugged pretty much everything and it seems fine.
Any help is appriciated.