Java Examples for org.lwjgl.opengl.GL11.glTexImage2D
The following java examples will help you to understand the usage of org.lwjgl.opengl.GL11.glTexImage2D. These source code samples are taken from different open source projects.
Example 1
| Project: Wolf_game-master File: Texture.java View source code |
public static Texture loadTexture(String name) {
// Load the image
BufferedImage bimg = null;
try {
bimg = ImageIO.read(Texture.class.getResource(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("Unable to load Texture: " + name);
}
// Gather all the pixels
int[] pixels = new int[bimg.getWidth() * bimg.getHeight()];
bimg.getRGB(0, 0, bimg.getWidth(), bimg.getHeight(), pixels, 0, bimg.getWidth());
// Create a ByteBuffer
ByteBuffer buffer = BufferUtils.createByteBuffer(bimg.getWidth() * bimg.getHeight() * 4);
// Iterate through all the pixels and add them to the ByteBuffer
for (int y = 0; y < bimg.getHeight(); y++) {
for (int x = 0; x < bimg.getWidth(); x++) {
// Select the pixel
int pixel = pixels[y * bimg.getWidth() + x];
// Add the RED component
buffer.put((byte) ((pixel >> 16) & 0xFF));
// Add the GREEN component
buffer.put((byte) ((pixel >> 8) & 0xFF));
// Add the BLUE component
buffer.put((byte) (pixel & 0xFF));
// Add the ALPHA component
buffer.put((byte) ((pixel >> 24) & 0xFF));
}
}
// Reset the read location in the buffer so that GL can read from
// beginning.
buffer.flip();
// Generate a texture ID
int textureID = glGenTextures();
// Bind the ID to the context
glBindTexture(GL_TEXTURE_2D, textureID);
// Setup texture scaling filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Send texture data to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, bimg.getWidth(), bimg.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// Return a new Texture.
return new Texture(textureID, bimg.getWidth(), bimg.getHeight());
}Example 2
| Project: complexion-master File: TextureLoader.java View source code |
/**
* Takes a BufferedImage, loads it into the OpenGL texture cache,
* and returns the texture ID.
*/
public static int loadTexture(BufferedImage image) {
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * // 4 for RGBA, 3 for RGB
BYTES_PER_PIXEL);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int pixel = pixels[y * image.getWidth() + x];
// Red component
buffer.put((byte) ((pixel >> 16) & 0xFF));
// Green component
buffer.put((byte) ((pixel >> 8) & 0xFF));
// Blue component
buffer.put((byte) (pixel & 0xFF));
// Alpha component.
buffer.put((byte) ((pixel >> 24) & 0xFF));
// Only for RGBA
}
}
// FOR THE LOVE OF GOD DO NOT FORGET THIS
buffer.flip();
// You now have a ByteBuffer filled with the color data of each pixel.
// Now just create a texture ID and bind it. Then you can load it using
// whatever OpenGL method you want, for example:
// Generate texture ID
int textureID = glGenTextures();
// Bind texture ID
glBindTexture(GL_TEXTURE_2D, textureID);
// Setup wrap mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
// Setup texture scaling filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Send texel data to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// Return the texture ID so we can bind it later again
return textureID;
}Example 3
| Project: FutureCraft-master File: Textures.java View source code |
public static void loadTexture(String path) {
if (textures.containsKey(path)) {
glBindTexture(GL_TEXTURE_2D, textures.get(path));
} else {
int handle = glGenTextures();
//System.out.println(handle);
glBindTexture(GL_TEXTURE_2D, handle);
textures.put(path, handle);
RawImage img = getRawImage(path);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img.width, img.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
}
}Example 4
| Project: Awesome-Kart-master File: DebugMesh.java View source code |
private void loadTex(String s, String texName, int tex) {
//Let's load a texture here.
InputStream is = null;
ByteBuffer buf = null;
try {
is = new FileInputStream("assets/graphics/" + s + "/" + texName + ".png");
PNGDecoder pd = new PNGDecoder(is);
buf = ByteBuffer.allocateDirect(4 * pd.getWidth() * pd.getHeight());
pd.decode(buf, pd.getWidth() * 4, Format.RGBA);
buf.flip();
} catch (Exception e) {
if (!s.equals("lightSphere")) {
System.out.println("error " + e);
}
return;
}
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
glBindTexture(GL_TEXTURE_2D, 0);
}Example 5
| Project: LWJGFont-master File: FontTextureLoader.java View source code |
private static FontTexture makeTexture(Class clazz, String imagePath) throws IOException {
BufferedImage srcImage;
int srcImageType;
srcImage = ImageIO.read(clazz.getResourceAsStream(imagePath));
srcImageType = srcImage.getType();
// target
int target = GL_TEXTURE_2D;
// dst pixel format
int dstPixelFormat = GL_RGBA;
// data type
int format = GL_UNSIGNED_BYTE;
// テクスチャー ID を生成する
int textureID = GL11.glGenTextures();
FontTexture texture = new FontTexture(target, textureID);
// glTexImage2D() の対象となるテクスチャー ID をバインドする
glBindTexture(target, textureID);
// All RGB bytes are aligned to each other and each component is 1 byte
// GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
int width = srcImage.getWidth();
int height = srcImage.getHeight();
texture.setWidth(width);
texture.setHeight(height);
texture.setTextureWidth(width);
texture.setTextureHeight(height);
texture.setAlphaPremultiplied(false);
ByteBuffer byteBuffer;
Pixel pixel = null;
// System.out.println(srcImageType);
if (srcImageType == BufferedImage.TYPE_INT_ARGB) {
throw new RuntimeException("Unsupported type: " + srcImage.getType());
} else if (srcImageType == BufferedImage.TYPE_3BYTE_BGR) {
pixel = new Pixel3ByteBGR();
} else if (srcImageType == BufferedImage.TYPE_4BYTE_ABGR) {
// Photoshop の 8bit/チャネル として処理する
// これは ABGR の各色について、それぞれが 8bit(1Byte) の画像フォーマットとなる。
pixel = new Pixel4ByteABGR();
} else if (srcImageType == BufferedImage.TYPE_CUSTOM) {
// Photoshop の 16bit/チャネル として処理する
// これは ABGR の各色について、それぞれが 16bit(2Byte) の画像フォーマットとなる。
// pixel = new Pixel8ByteABGR();
// この辺の設定は、ひとまず
// MikMikuStudio/engine/src/core/com/jme3/texture/Image.java
// MikMikuStudio/engine/src/jogl2/com/jme3/renderer/jogl/TextureUtil.java
// を参考にしてみた
// pixel = new Pixel4ByteABGR();
/*
dstPixelFormat = GL_RGBA2;
dstPixelFormat = GL_RGBA8;
*/
dstPixelFormat = GL_RGBA16;
/*
format = GL_UNSIGNED_SHORT;
format = GL_SHORT;
format = GL_UNSIGNED_SHORT_4_4_4_4;
*/
// Miku.png (8bit/channel)がこれで動いたので、ひとまず Pixel4ByteABGR で動かす
pixel = new Pixel4ByteABGR();
} else {
throw new RuntimeException("Unsupported type: " + srcImage.getType());
}
byteBuffer = pixel.toBuffer(srcImage);
byteBuffer.order(ByteOrder.nativeOrder());
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int b[] = srcImage.getRaster().getPixel(x, y, pixel.getBuffer());
pixel.writeBuffer(byteBuffer);
}
}
byteBuffer.flip();
// 画像の拡大・縮小時の補間方法を設定する
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// バイト配列と色情報のフォーマットからテクスチャーを生成する
glTexImage2D(target, 0, // テクスチャ内のカラー要素数
dstPixelFormat, width, height, // テクスチャの境界幅。境界が存在しない場合は 0、存在する場合は 1
0, // ピクセル内の色の順序。 参考 http://wisdom.sakura.ne.jp/system/opengl/gl22.html
GL_RGBA, // 各チャネル(色)のデータ型。 参考 http://wisdom.sakura.ne.jp/system/opengl/gl22.html
format, byteBuffer);
// ミップマップの自動生成
// GL30.glGenerateMipmap(GL_TEXTURE_2D);
byteBuffer.clear();
return texture;
}Example 6
| Project: Mace-Swinger-master File: TextureBinder.java View source code |
public static void bindTexture(String URL, int texture) {
{
InputStream in = null;
try {
in = Resources.get(URL);
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
buffer.flip();
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, 0);
} catch (FileNotFoundException ex) {
System.err.println("Failed to find the texture files. " + URL);
ex.printStackTrace();
Display.destroy();
System.exit(1);
} catch (IOException ex) {
System.err.println("Failed to load the texture files. " + URL);
ex.printStackTrace();
Display.destroy();
System.exit(1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}Example 7
| Project: myrobotlab-master File: Texture.java View source code |
public void loadImageData(BufferedImage image, int loadTarget) {
// Flip the image vertically
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
GL11.glTexImage2D(loadTarget, 0, GL11.GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, convertImageData(image));
}Example 8
| Project: Dwarf2D-master File: TextureLoader.java View source code |
/**
* Load a texture into OpenGL from a image reference on disk.
*
* @param path The location of the resource to load
* @param target The GL target to load the texture against
* @param dstPixelFormat The pixel format of the screen
* @param minFilter The minimising filter
* @param magFilter The magnification filter
* @return The loaded texture
* @throws DwarfException Indicates a failure to access the resource
*/
public static Texture getTexture(String path, int target, int dstPixelFormat, int minFilter, int magFilter) throws DwarfException {
try {
int srcPixelFormat;
// create the texture ID for this texture
int textureID = createTextureID();
Texture texture = new Texture(target, textureID);
// bind this texture
glBindTexture(target, textureID);
BufferedImage bufferedImage = loadImage(path);
texture.setWidth(bufferedImage.getWidth());
texture.setHeight(bufferedImage.getHeight());
if (bufferedImage.getColorModel().hasAlpha()) {
srcPixelFormat = GL_RGBA;
} else {
srcPixelFormat = GL_RGB;
}
// convert that image into a byte buffer of texture data
ByteBuffer textureBuffer = convertImageData(bufferedImage, texture);
if (target == GL_TEXTURE_2D) {
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
}
// produce a texture from the byte buffer
glTexImage2D(target, 0, dstPixelFormat, get2Fold(bufferedImage.getWidth()), get2Fold(bufferedImage.getHeight()), 0, srcPixelFormat, GL_UNSIGNED_BYTE, textureBuffer);
return texture;
} catch (Exception ex) {
throw new DwarfException(ex);
}
}Example 9
| Project: lwjgl-basics-master File: Texture.java View source code |
/** Uploads image data with the dimensions of this Texture.
*
* @param dataFormat the format, e.g. GL_RGBA
* @param data the byte data */
public void upload(int dataFormat, ByteBuffer data) {
bind();
setUnpackAlignment();
glTexImage2D(getTarget(), 0, GL_RGBA, width, height, 0, dataFormat, GL_UNSIGNED_BYTE, data);
}