I've been following the VBO tutorial on the wiki, and I tried to optimize my text rendering by switching from drawing to modifying a VBO.
I can't get the VBO to render, and I don't know why.
Here's the code in my font drawing function:
public void drawText(String text, int x, int y) {
if(Screen.vidMode == 0x00){
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Screen.windowWidth, Screen.windowHeight, 0, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, this.fontTextureId);
glBegin(GL_QUADS);
glColor3f(Screen.DEFAULT_COLOR[0], Screen.DEFAULT_COLOR[1], Screen.DEFAULT_COLOR[2]);
int xTmp = x;
for (char c : text.toCharArray()) {
float width = getCharWidth(c);
float height = getCharHeight();
float cw = 1f / getFontImageWidth() * width;
float ch = 1f / getFontImageHeight() * height;
float cx = 1f / getFontImageWidth() * getCharX(c);
float cy = 1f / getFontImageHeight() * getCharY(c);
glTexCoord2f(cx, cy);
glVertex3f(xTmp, y, 0);
glTexCoord2f(cx + cw, cy);
glVertex3f(xTmp + width, y, 0);
glTexCoord2f(cx + cw, cy + ch);
glVertex3f(xTmp + width, y + height, 0);
glTexCoord2f(cx, cy + ch);
glVertex3f(xTmp, y + height, 0);
xTmp += width;
}
glEnd();
glDisable(GL11.GL_TEXTURE_2D);
}else if(Screen.vidMode == 0x01 || Screen.vidMode == 0x02){
int xTmp = x;
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, Screen.txtMem);
FloatBuffer fbuf = BufferUtils.createFloatBuffer(0);
FloatBuffer memory = MemoryUtil.memFloatBuffer(GL15.glGetBufferPointer(GL15.GL_ARRAY_BUFFER, GL15.GL_BUFFER_MAP_POINTER), Screen.txtMemSize);
if(memory == null)
memory = BufferUtils.createFloatBuffer(0);
int i = 32;
for (char c : text.toCharArray()) {
FloatBuffer dupl = fbuf.duplicate();
fbuf = FloatBuffer.allocate(i);
fbuf.put(dupl);
float width = getCharWidth(c);
float height = getCharHeight();
float cw = 1f / getFontImageWidth() * width;
float ch = 1f / getFontImageHeight() * height;
float cx = 1f / getFontImageWidth() * getCharX(c);
float cy = 1f / getFontImageHeight() * getCharY(c);
fbuf.put(cx);
fbuf.put(cy);
fbuf.put(xTmp);
fbuf.put( y);
fbuf.put( 0f);
fbuf.put( Screen.DEFAULT_COLOR[0]);
fbuf.put( Screen.DEFAULT_COLOR[1]);
fbuf.put( Screen.DEFAULT_COLOR[2]);
fbuf.put( cx + cw);
fbuf.put( cy);
fbuf.put( xTmp + width);
fbuf.put( y);
fbuf.put( 0f);
fbuf.put( Screen.DEFAULT_COLOR[0]);
fbuf.put( Screen.DEFAULT_COLOR[1]);
fbuf.put( Screen.DEFAULT_COLOR[2]);
fbuf.put( cx + cw);
fbuf.put( cy + ch);
fbuf.put( xTmp + width);
fbuf.put( y + height);
fbuf.put( 0f);
fbuf.put( Screen.DEFAULT_COLOR[0]);
fbuf.put(Screen.DEFAULT_COLOR[1]);
fbuf.put( Screen.DEFAULT_COLOR[2]);
fbuf.put( cx);
fbuf.put( cy + ch);
fbuf.put( xTmp);
fbuf.put( y + height);
fbuf.put( 0f);
fbuf.put( Screen.DEFAULT_COLOR[0]);
fbuf.put( Screen.DEFAULT_COLOR[1]);
fbuf.put( Screen.DEFAULT_COLOR[2]);
xTmp += width;
Screen.txtMemSize += 32;
i += 32;
}
FloatBuffer newMem = BufferUtils.createFloatBuffer(Screen.txtMemSize);
newMem.put(memory);
newMem.put(fbuf);
newMem.rewind();
newMem.flip();
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, newMem, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
}
And here's the relevant code in my rendering function:
if(Screen.vidMode == 0x01 || Screen.vidMode == 0x02){
CLI.println("In mode 0x02");
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, Screen.txtMem);
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 8*4, 0);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 8*4, 2*4);
GL11.glColorPointer(3, GL11.GL_FLOAT, 8*4, 5*4);
// Unbind VBO
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDrawArrays(GL11.GL_QUADS, 0, Screen.txtMemSize);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDisable(GL11.GL_BLEND);
CLI.println("err "+GL11.glGetError());
}
glGetError returns 0, but the text doesn't render. What am I doing?