PDA

View Full Version : Texture Mapping



Arkathorn
08-16-2015, 05:39 AM
I'm trying to draw a simple textured quad, what am I doing wrong?
The code compiles, but renders a blank white quad.

Main.java:

For some reason, the code for this file registers as containing URLs or forbidden words, so it is included in the attached text file.

AssetRegistry.java:

package core;

import org.lwjgl.opengl.GL11;

public abstract class AssetRegistry {
public static TextureAsset texture_metal;
public static TextureAsset texture_wood;
public static TextureAsset texture_glass;

public static void init() {
System.out.println("Initializing Assets");
texture_metal = new TextureAsset("assets/metal.png");
texture_wood = new TextureAsset("assets/wood.png");
texture_glass = new TextureAsset("assets/glass.png");
}

public static void destroyAssets() {
GL11.glDeleteTextures(texture_metal.getTextureHand le());
}
}


TextureAsset.java:

package core;

import de.matthiasmann.twl.utils.PNGDecoder;
import org.lwjgl.opengl.GL11;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

//A texture asset
public class TextureAsset {
private int textureHandle;

public TextureAsset(String path) {
System.out.println("Registering new asset at \"" + path + "\"");
InputStream in = null;
try {
in = new FileInputStream(path);
} catch (FileNotFoundException e) {
System.out.println("Failed to find the file at \"" + path + "\"");
}
try {
PNGDecoder decoder = new PNGDecoder(in);

System.out.println("width="+decoder.getWidth());
System.out.println("height=" + decoder.getHeight());

ByteBuffer buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*dec oder.getHeight());
decoder.decode(buf, decoder.getWidth()*4, PNGDecoder.Format.RGBA);
buf.flip();

textureHandle = GL11.glGenTextures();
System.out.println(textureHandle);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureHandle);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
}

public int getTextureHandle() {
return textureHandle;
}
}

mobeen
08-17-2015, 02:41 AM
Hi,
As far as I can see, the result you are getting is probably because you have not set the texture minification filter. By default it is set to mipmap linear which assumes that you will provide mipmaps. Since you are not providing mipmaps, you should atleast set texture minification filter to GL_LINEAR or GL_NEAREST like this after you bind your texture


GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureHandle);
GL11.glTexParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);

See if this helps.

Arkathorn
08-17-2015, 08:45 AM
Hi,
As far as I can see, the result you are getting is probably because you have not set the texture minification filter. By default it is set to mipmap linear which assumes that you will provide mipmaps. Since you are not providing mipmaps, you should atleast set texture minification filter to GL_LINEAR or GL_NEAREST like this after you bind your texture


GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureHandle);
GL11.glTexParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);

See if this helps.

Thanks, that was the solution.