我的问题是这样的:
我已经在openGL(LWJGL)中将图像加载为纹理,每次调用draw方法时,我都可以选择要显示的tileset的位置(在我的情况下是sprite动画的特定帧) ,我想知道最好是读取整个tileset并将其切成仅包含一个sprite的较小的tileset还是用相同的方式处理整个图像。

实际上,我为每个图块将整个图像加载到纹理中,并且每次绘制时,每个图块都在整个图块集上使用GL_QUADS。

一个图块集可以包含许多图块和许多动画图块。

我曾经使用图像,读取一个大的图像,然后将其放入tileet对象中,然后分离每个单个的tile,切割图像,然后将切下的片段放入tile中。

我不知道纹理是否相同。

在那种情况下,创建一个新纹理作为开始时已经存在的问题的切入点是什么?

我想做的是这样的:

MAP加载TILESET
TILESET加载整个图像
将图像切成许多小图像(纹理)
瓷砖只有很小的纹理,是大纹理的一部分。

每次绘制时,每个图块将仅处理较小的图块。如果我将整个图块集传递给每个图块仅告诉用户显示哪个部分,那是正确的还是相同的?

这是我的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();
}
}

最佳答案

在您的情况下,在渲染到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);
 }
}


tileet类将GL句柄保存到tileet图像。

Tile类用于根据所提供的位置及其所属的图块集渲染图块(绑定图像并使用与特定图块对应的texcoords绘制四边形)。

这并不能说明一切,但是我没有太多时间将它们组合在一起,而且我不确定您是如何实现应用程序的。

关于java - 在opengl中处理纹理并将其切成许多图块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18522882/

10-12 22:15