I am using a library that loads some images from files, but they are not png or jpeg files. The library can return bytebuffer of the image. So my question is how do I load a texture in opengl using this bytebuffer?
Thanks in advance.
I solved the first problem which was the original purpose of this thread. Now I am facing the problem of loading non power of two textures.
I get error like these when loading those kinds of textures “Number of remaining buffer elements is 113844, must be at least 128164. Because at most 128164 elements can be returned, a buffer with at least 128164 elements is required, regardless of actual returned element count”
I am fine with power of two texture though.
Here is my entire program demo.
package Leo.Story.Game;
import java.io.FileNotFoundException;
import java.io.IOException;
import net.zepheus.nxjava.NXCanvasNode;
import net.zepheus.nxjava.NXFile;
import net.zepheus.nxjava.Texture;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GLContext;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
public class Game {
static NXFile charNX = null;
public static void main(String args[]) {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Texture Demo");
Display.create();
charNX = new NXFile("C:\\Users\\Leo\\Desktop\\map.nx");
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
// BufferedImage bi = ((NXCanvasNode)charNX.resolvePath("Back","Amoria.img","back","1")).getImage();
// ByteBuffer bb = Game.convertImageData(bi);
Texture texture = ((NXCanvasNode)charNX.resolvePath("Back","Amoria.img","back","0")).getTexture();
glMatrixMode(GL_PROJECTION);
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
System.out.println(texture.getWidth() + " " + texture.getHeight());
int texid = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texid);
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR );
// when texture area is large, bilinear filter the original
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// the texture wraps over at the edges (repeat)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.getHeight(), texture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.getBuffer());
boolean ext = GLContext.getCapabilities().GL_EXT_bgra;
System.out.println(ext);
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texid );
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(100, 100); // Upper-left
glTexCoord2f(1, 0);
glVertex2i(100+texture.getWidth(), 100); // Upper-right
glTexCoord2f(1, 1);
glVertex2i(100+texture.getWidth(), 100+texture.getHeight()); // Bottom-right
glTexCoord2f(0, 1);
glVertex2i(100, 100+texture.getHeight()); // Bottom-left
glEnd();
Display.update();
Display.sync(60);
}
// Release the resources of the wood texture
Display.destroy();
System.exit(0);
}
}