Hi!, i need help with vertex arrays. My problem is when i render more than 1 sprite, the image output looks weird.
Here is the VertexArray class
public class VertexArray {
/**
* Default VertexArray
*/
public final static VertexArray DEFAULT = new VertexArray();
/**
* Allow to draw n Vertex per batch
*/
protected final static int MAX_VERTEX = 500;
protected final static int TRIANGLE_VERTEX = 4;
/**
* The diferents types of buffer
*/
public final static int VERTEX = 0;
public final static int TEXTURE = 1;
public final static int COLOR = 2;
/**
* Each Buffer of the vertex array
*/
protected FloatBuffer[] buffer = new FloatBuffer[3];
/**
* Amount of vertexs
*/
protected int vertexAmount = 1;
/**
* Create a new instance of <code>VertexArray</code>
*
* @param bufferType
*/
public VertexArray() {
// Create the buffers
buffer[VERTEX] = BufferUtils.createFloatBuffer( MAX_VERTEX * TRIANGLE_VERTEX * 3 );
buffer[TEXTURE] = BufferUtils.createFloatBuffer( MAX_VERTEX * TRIANGLE_VERTEX * 2 );
buffer[COLOR] = BufferUtils.createFloatBuffer( MAX_VERTEX * TRIANGLE_VERTEX * 4 );
// Create the defaul values
setTextureCoord(0, 0, 0.0f, 1.0f);
setTextureCoord(0, 1, 0.0f, 0.0f);
setTextureCoord(0, 2, 1.0f, 1.0f);
setTextureCoord(0, 3, 1.0f, 0.0f);
setColor(0,0, Color.White );
setColor(0,1, Color.White );
setColor(0,2, Color.White );
setColor(0,3, Color.White );
setVertex(0,0, new Vector3f(0.0f, 0.0f, 0.0f));
setVertex(0,1, new Vector3f(0.0f, 32.0f, 0.0f));
setVertex(0,2, new Vector3f(32.0f, 0.0f, 0.0f));
setVertex(0,3, new Vector3f(32.0f, 32.0f, 0.0f));
update();
}
/**
* Set the amount of vertex to write
*
* @param amount
*/
public void setQuantity( int amount ) {
this.vertexAmount = amount;
}
/**
* Set the complete vertex array
*
* @param vertex
* @param position
* @param width
* @param height
*/
public void setVertex( int vertex, Vector3f position, Vector3f size ) {
setVertex(vertex, 0, new Vector3f(position.x, position.y, position.z));
setVertex(vertex, 1, new Vector3f(position.x, position.y + size.y, position.z));
setVertex(vertex, 2, new Vector3f(position.x + size.x, position.y, position.z));
setVertex(vertex, 3, new Vector3f(position.x + size.x, position.y + size.y, position.z));
}
/**
* Set a vertex position of the array
*
* @param vertex
* @param index
* @param data
*/
public void setVertex( int vertex, int index, Vector3f data ) {
buffer[VERTEX].put(vertex * TRIANGLE_VERTEX + (index * 3), data.x);
buffer[VERTEX].put(vertex * TRIANGLE_VERTEX + (index * 3 + 1), data.y);
buffer[VERTEX].put(vertex * TRIANGLE_VERTEX + (index * 3 + 2), data.z);
}
/**
* set the color of the buffer
*
* @param vertex
* @param color
*/
public void setColor( int vertex, int index, Color data ) {
buffer[COLOR].put(vertex * TRIANGLE_VERTEX + (index * 4), data.r);
buffer[COLOR].put(vertex * TRIANGLE_VERTEX + (index * 4 + 1), data.g);
buffer[COLOR].put(vertex * TRIANGLE_VERTEX + (index * 4 + 2), data.b);
buffer[COLOR].put(vertex * TRIANGLE_VERTEX + (index * 4 + 3), data.a);
}
/**
* Set the texture of the buffer
*
* @param index
* @param tV
* @param tU
*/
public void setTextureCoord( int vertex, int index, float tV, float tU ) {
buffer[TEXTURE].put(vertex * TRIANGLE_VERTEX + (index * 2), tV);
buffer[TEXTURE].put(vertex * TRIANGLE_VERTEX + (index * 2 + 1), tU);
}
/**
* Unlock the buffer for writing
*
* @param bufferID
*/
public void clear(int bufferID) {
buffer[bufferID].clear();
}
/**
* Update a buffer
*
* @param bufferID
*/
public void update(int bufferID) {
buffer[bufferID].flip();
}
/**
* Update all the buffers
*/
public void update() {
for (int i = 0; i < buffer.length; i++) {
buffer[i].flip();
}
}
/**
* Clear all the buffers
*/
public void clear() {
for (int i = 0; i < buffer.length; i++) {
buffer[i].clear();
}
}
/**
* Merge this array with another
*
* @param array
*/
public void merge( VertexArray array ) {
}
}
Font String Generator
public class RendererFontNode extends RendererNode {
/**
* String of the node
*/
private String textBlock;
/**
* Font class
*/
private Font fontClass;
/**
* Constructor
*
* @param text
* @param font
*/
protected RendererFontNode(String text, Vector2f position, Font font, Texture texture, Color[] color) {
this.textBlock = text;
this.fontClass = font;
this.color = color;
this.texture = texture;
this.position = new Vector3f(position.x, position.y, 90.0f);
updateGeometry();
}
/**
* Update the text
*
* @param text
*/
public void setText( String text ) {
this.textBlock = text;
updateGeometry();
}
/**
* Set the character color
*
* @param index
* @param color
*/
protected void setCharacterColor(int index, Color[] color) {
this.vertexArray.setColor(index, 0, color[0]);
this.vertexArray.setColor(index, 1, color[1]);
this.vertexArray.setColor(index, 2, color[2]);
this.vertexArray.setColor(index, 3, color[3]);
}
/**
* Set the source of a character
*
* @param index
* @param x
* @param y
* @param width
* @param height
*/
protected void setSource(int index, float x, float y, float width, float height) {
float MinX, MaxX, MinY, MaxY;
// Calculate
MinX = x / (float) texture.getImage().getWidth();
MinY = y / (float) texture.getImage().getHeight();
MaxX = (x + width) / (float) texture.getImage().getWidth();
MaxY = (y + height) / (float) texture.getImage().getHeight();
// Update. the buffers
vertexArray.setTextureCoord(index, 0, MinX, MinY);
vertexArray.setTextureCoord(index, 1, MinX, MaxY);
vertexArray.setTextureCoord(index, 2, MaxX, MinY);
vertexArray.setTextureCoord(index, 3, MaxX, MaxY);
}
/**
* Update the node geometry
*/
protected void updateGeometry() {
Vector3f currentPosition = position;
Vector3f currentSize = new Vector3f(0.0f, 0.0f, 1.0f);
// Clear all the buffers
this.vertexArray.clear();
// Set the amount of vertex
this.vertexArray.setQuantity(textBlock.length());
// For each character
for (int i = 0; i < textBlock.length(); i++) {
// Update the color
setCharacterColor(i, color);
// Get the CharacterKey
FontKey key = fontClass.getKey(textBlock.charAt(i));
// Update the Position
currentSize.x = key.width;
currentSize.y = key.height;
this.vertexArray.setVertex(i, currentPosition, currentSize);
currentPosition.x += key.width;
// Update the Texture Coordination
setSource(i, key.x, key.y, key.width, key.height);
}
// Update all the buffers
this.vertexArray.update();
}
}
The Vertex Renderer
// Render the vertexArray
GL11.glVertexPointer(3, 0, vertexArray.buffer[VertexArray.VERTEX]);
if (nodeTexture != null) {
if (rendererState.vertexUsage[1] == false) {
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
rendererState.vertexUsage[1] = true;
}
GL11.glTexCoordPointer(2, 0, vertexArray.buffer[VertexArray.TEXTURE]);
} else {
if (rendererState.vertexUsage[1] == true) {
GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
rendererState.vertexUsage[1] = false;
}
}
GL11.glColorPointer(4, 0, vertexArray.buffer[VertexArray.COLOR]);
GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, VertexArray.TRIANGLE_VERTEX * vertexArray.vertexAmount);
Help plz xD