我的问题是: 我在openGL(LWJGL)中加载了一个图像作为纹理,每次调用draw方法时,我都可以选择我想要显示的tileset的位置(在我的例子中是一个精灵动画的特定帧) ,我想知道是否最好读取整个tileset并将其切割成只有一个精灵的较小的tileset,或者是相同的来处理整个图像。
实际上我将整个图像加载到每个图块的纹理中,每次绘制时,每个图块都在整个图块集上使用GL_QUADS。
图块集可以包含许多图块和许多动画图块。
我曾经处理过图像,读取大图像并将其放入图块集对象中,然后将每个图块分开,剪切图像并将切割的图块放入图块中。
我无法理解它与纹理是否相同。
在这种情况下,创建新纹理的方法是什么?这是现有问题的一个切口?
我想做的是:
MAP加载TILESET TILESET加载整个IMAGE 在许多小图像(纹理)中剪切图像 TILE只有很小的纹理,是大纹理的一部分。
每次绘制每个图块只会处理一个较小的图块。如果我将整个图块集传递给每个图块,告诉它只显示用户的哪个部分,这是正确的还是相同的?
这是我的TileSet类:
package mh.map;
import java.awt.Color;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
import mh.GameWindow;
import tiled.util.TileCutter;
public class TileSet implements Iterable<Tile> {
private String base;
final private Vector<Tile> tiles = new Vector<Tile>();
private long tilebmpFileLastModified;
private TileCutter tileCutter;
private Rectangle tileDimensions;
private int tileSpacing;
private int tileMargin;
private int tilesPerRow;
private String externalSource;
private File tilebmpFile;
private String name;
private Color transparentColor;
private Image tileSetImage;
private Texture texture;
public TileSet() {
this.tileDimensions = new Rectangle();
}
public void importTileTexture(String ref, int width, int height, int spacing, int margin) {
try {
this.texture = GameWindow.getTextureLoader().getTexture(ref);
int texWidth = this.texture.getImageWidth();
int texHeight = this.texture.getImageHeight();
int tilePerRow = texWidth / width;
int rows = texHeight / height;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < tilePerRow; j++) {
Tile t = new Tile(this, j, i);
this.addNewTile(t);
}
}
} catch (IOException e) {
System.err.println("Unable to load texture: " + ref);
e.printStackTrace();
}
}
public int addTile(Tile t) {
if (t.getId() < 0) {
t.setId(this.tiles.size());
}
if (this.tileDimensions.width < t.getWidth()) {
this.tileDimensions.width = t.getWidth();
}
if (this.tileDimensions.height < t.getHeight()) {
this.tileDimensions.height = t.getHeight();
}
this.tiles.add(t);
t.setTileSet(this);
return t.getId();
}
public void addNewTile(Tile t) {
t.setId(-1);
this.addTile(t);
}
public void removeTile(int i) {
this.tiles.set(i, null);
}
public int size() {
return this.tiles.size();
}
public int getMaxTileId() {
return this.tiles.size() - 1;
}
public Tile getTile(int i) {
try {
return this.tiles.get(i);
} catch (ArrayIndexOutOfBoundsException a) {
}
return null;
}
public Tile getFirstTile() {
Tile ret = null;
int i = 0;
while ((ret == null) && (i <= this.getMaxTileId())) {
ret = this.getTile(i);
i++;
}
return ret;
}
public Texture getTexture() {
return this.texture;
}
}
这是Tile.java
package mh.map;
import java.io.IOException;
import java.util.Properties;
import mh.GameWindow;
import mh.interfaces.IImage;
import org.lwjgl.opengl.GL11;
public class Tile implements IImage {
/**
* The texture that stores the image for this sprite (not just one frame)
* @deprecated Use instead {@link TileSet#getTexture()}
*/
private Texture texture;
private int id;
private TileSet set;
private Properties prop;
private int offsetX;
private int offsetY;
/**
* Creates a new Tile with a reference to the <code>TileSet</code> and a specified position in that specific set.
*
* @param ts
* The TileSet
* @param texX
* The x position of the tile in the tileset (in tile unit)
* @param texY
* The y position of the tile in the tileset (in tile unit)
*/
public Tile(TileSet ts, int texX, int texY) {
this.set = ts;
this.offsetX = texX;
this.offsetY = texY;
this.texture = ts.getTexture();
}
@Override
public int getWidth() {
return this.texture.getImageWidth();
}
@Override
public int getHeight() {
return this.texture.getImageHeight();
}
public void draw(double x, double y) {
GL11.glPushMatrix();
this.texture.bind();
GL11.glTranslated(x, y, 0);
GL11.glColor3f(1, 1, 1);
GL11.glBegin(GL11.GL_QUADS);
{
// TODO, USE OFFSETS TO DRAW THE CORRECT PIECE OF THE TEXTURE
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, this.texture.getHeight());
GL11.glVertex2f(0, 32);
GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight());
GL11.glVertex2f(32, 32);
GL11.glTexCoord2f(this.texture.getWidth(), 0);
GL11.glVertex2f(32, 0);
}
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}
public void setId(int i) {
if (i >= 0) {
this.id = i;
}
}
public int getId() {
return this.id;
}
public Properties getProperties() {
return this.prop;
}
public void setProperties(Properties p) {
this.prop = p;
}
public void setTileSet(TileSet ts) {
this.set = ts;
}
}
这是textureloader:
package mh.map;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.HashMap;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import org.lwjgl.opengl.GL11;
/**
* @author Kevin Glass
* @author Brian Matzon
* @author Gianmarco Laggia
*/
public class TextureLoader {
/** The table of textures that have been loaded in this loader */
private final HashMap<String, Texture> table = new HashMap<String, Texture>();
/** The colour model including alpha for the GL image */
private final ColorModel glAlphaColorModel;
/** The colour model for the GL image */
private final ColorModel glColorModel;
/**
* Create a new texture loader based on the game panel
*/
public TextureLoader() {
this.glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false,
Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
this.glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 0 }, false, false, Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
}
/**
* Create a new texture ID
*
* @return A new texture ID
*/
private int createTextureID() {
IntBuffer tmp = this.createIntBuffer(1);
GL11.glGenTextures(tmp);
return tmp.get(0);
}
/**
* Load a texture
*
* @param resourceName
* The location of the resource to load
* @return The loaded texture
* @throws IOException
* Indicates a failure to access the resource
*/
public Texture getTexture(String resourceName) throws IOException {
Texture tex = this.table.get(resourceName);
if (tex != null) {
return tex;
}
tex = this.getTexture(resourceName, GL11.GL_TEXTURE_2D, // target
GL11.GL_RGBA, // dst pixel format
GL11.GL_LINEAR, // min filter (unused)
GL11.GL_LINEAR);
this.table.put(resourceName, tex);
return tex;
}
/**
* Load a texture into OpenGL from a image reference on disk.
*
* @param resourceName
* 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 IOException
* Indicates a failure to access the resource
*/
public Texture getTexture(String resourceName, int target, int dstPixelFormat, int minFilter, int magFilter) throws IOException {
int srcPixelFormat = 0;
// create the texture ID for this texture
int textureID = this.createTextureID();
Texture texture = new Texture(target, textureID);
// bind this texture
GL11.glBindTexture(target, textureID);
BufferedImage bufferedImage = this.loadImage(resourceName);
texture.setWidth(bufferedImage.getWidth());
texture.setHeight(bufferedImage.getHeight());
if (bufferedImage.getColorModel().hasAlpha()) {
srcPixelFormat = GL11.GL_RGBA;
} else {
srcPixelFormat = GL11.GL_RGB;
}
// convert that image into a byte buffer of texture data
ByteBuffer textureBuffer = this.convertImageData(bufferedImage, texture);
if (target == GL11.GL_TEXTURE_2D) {
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
}
// produce a texture from the byte buffer
GL11.glTexImage2D(target, 0, dstPixelFormat, this.get2Fold(bufferedImage.getWidth()), this.get2Fold(bufferedImage.getHeight()), 0, srcPixelFormat,
GL11.GL_UNSIGNED_BYTE, textureBuffer);
return texture;
}
/**
* Get the closest greater power of 2 to the fold number
*
* @param fold
* The target number
* @return The power of 2
*/
private int get2Fold(int fold) {
int ret = 2;
while (ret < fold) {
ret *= 2;
}
return ret;
}
/**
* Convert the buffered image to a texture
*
* @param bufferedImage
* The image to convert to a texture
* @param texture
* The texture to store the data into
* @return A buffer containing the data
*/
private ByteBuffer convertImageData(BufferedImage bufferedImage, Texture texture) {
ByteBuffer imageBuffer = null;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
// find the closest power of 2 for the width and height
// of the produced texture
while (texWidth < bufferedImage.getWidth()) {
texWidth *= 2;
}
while (texHeight < bufferedImage.getHeight()) {
texHeight *= 2;
}
texture.setTextureHeight(texHeight);
texture.setTextureWidth(texWidth);
// create a raster that can be used by OpenGL as a source
// for a texture
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
texImage = new BufferedImage(this.glAlphaColorModel, raster, false, new Hashtable<String, Texture>());
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
texImage = new BufferedImage(this.glColorModel, raster, false, new Hashtable<String, Texture>());
}
// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f, 0f, 0f, 0f));
g.fillRect(0, 0, texWidth, texHeight);
g.drawImage(bufferedImage, 0, 0, null);
// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}
/**
* Load a given resource as a buffered image
*
* @param ref
* The location of the resource to load
* @return The loaded buffered image
* @throws IOException
* Indicates a failure to find a resource
*/
private BufferedImage loadImage(String ref) throws IOException {
URL url = TextureLoader.class.getClassLoader().getResource(ref);
if (url == null) {
url = new URL("file:"+ref);
// throw new IOException("Cannot find: " + ref);
}
System.out.println("URL:" + url);
BufferedImage bufferedImage = ImageIO.read(new BufferedInputStream(url.openStream()));//this.getClass().getClassLoader().getResourceAsStream(ref)));
return bufferedImage;
}
/**
* Creates an integer buffer to hold specified ints - strictly a utility method
*
* @param size
* how many int to contain
* @return created IntBuffer
*/
protected IntBuffer createIntBuffer(int size) {
ByteBuffer temp = ByteBuffer.allocateDirect(4 * size);
temp.order(ByteOrder.nativeOrder());
return temp.asIntBuffer();
}
}
答案 0 :(得分:1)
在您的情况下,在渲染到GL_QUADS时让Tileset
处理纹理坐标可能更有益,而不是将大图像拆分成小图块。这样你在内存中只有一个副本(而不是两个),并且减少了加载时间。
此外,它可以缓存大纹理,只需使用Tileset
类指定的纹理坐标绘制切片,这很可能会加快渲染速度。
例如,当您绘制Map
时,它会调用Tileset.drawTile(tileID)
。 drawTile方法将执行OpenGL绘图调用,并将texcoords设置为您正在绘制的图块的位置。
编辑:一个hacky解决方案,突出我的意思:
public class Tileset {
private int tilesetImageHandle;
private ArrayList<Tile> tiles;
public Tile getTile(int tileIndex) {
return tiles.get(tileIndex);
}
public void bindTexture() {
glBindTexture(tilesetImageHandle);
}
// Methods to retrieve tileset subimage offsets in x and y directions
}
public class Tile {
private Tileset tileset;
private float u, v;
public Tile(Tileset tileset, float u, float v) {
this.tileset = tileset;
this.u = u;
this.v = v;
}
public void draw(Point location) {
tileset.bindTexture();
glPushMatrix(GL_MODEL_VIEW_MATRIX);
glTranslate2f(location.x, location.y);
glBegin(GL_QUADS);
glCoord2f(0.0, 0.0);
glTexCoord2f(u, v);
glCoord2f(0.0, 1.0);
glTexCoord2f(u, v + tileset.getTilesetOffsetY());
glCoord2f(tileset.getTileDimX(), tileset.getTileDimY());
glTexCoord2f(u + tileset.getTilesetOffsetX(), v + tileset.getTilesetOffsetY());
glCoord2f(1.0, 0.0);
glTexCoord2f(u + tileset.getTilesetOffsetX(), v);
glEnd();
glPopMatrix(GL_MODEL_VIEW_MATRIX);
}
}
tileset类将GL句柄保存到tileset图像。
Tile类用于根据提供的位置及其所属的tileset渲染切片(绑定图像并使用与特定切片对应的texcoords绘制四边形)。
这并没有说明一切,但我没有太多时间把它放在一起,我不确定你是如何实现你的应用程序的。