Archived
0

[format code]

This commit is contained in:
2015-09-18 14:24:36 +03:00
parent bf0609c492
commit 8dbf4aa4ec
4 changed files with 134 additions and 136 deletions

View File

@@ -1,14 +1,14 @@
package ru.dmitriymx.lwjgl.tools; package ru.dmitriymx.lwjgl.tools;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
public class Tessellator { public class Tessellator {
private static Tessellator instance = new Tessellator(); private static Tessellator instance = new Tessellator();
private int draw_mode; private int draw_mode;
private float[] float_buffer; private float[] float_buffer;
private int float_buffer_position; private int float_buffer_position;
@@ -16,15 +16,15 @@ public class Tessellator {
private int vertex_count; private int vertex_count;
private boolean use_color, use_texture; private boolean use_color, use_texture;
private int display_list_id; private int display_list_id;
private Tessellator() { private Tessellator() {
float_buffer = new float[0x200000]; float_buffer = new float[0x200000];
} }
public static Tessellator getInstance() { public static Tessellator getInstance() {
return instance; return instance;
} }
public void reset() { public void reset() {
use_vbo = false; use_vbo = false;
draw_mode = 0; draw_mode = 0;
@@ -33,7 +33,7 @@ public class Tessellator {
use_color = false; use_color = false;
use_texture = false; use_texture = false;
} }
public void addVertex(float x, float y, float z) { public void addVertex(float x, float y, float z) {
if (use_dl) { if (use_dl) {
dl_add_vertex(x, y, z); dl_add_vertex(x, y, z);
@@ -43,7 +43,7 @@ public class Tessellator {
va_add_vertex(x, y, z); va_add_vertex(x, y, z);
} }
} }
public Tessellator setTexture(float u, float v) { public Tessellator setTexture(float u, float v) {
if (use_dl) { if (use_dl) {
dl_set_texture(u, v); dl_set_texture(u, v);
@@ -55,7 +55,7 @@ public class Tessellator {
return this; return this;
} }
public Tessellator setColor(float red, float green, float blue, float alpha) { public Tessellator setColor(float red, float green, float blue, float alpha) {
if (use_dl) { if (use_dl) {
dl_set_color(red, green, blue, alpha); dl_set_color(red, green, blue, alpha);
@@ -67,20 +67,20 @@ public class Tessellator {
return this; return this;
} }
public Tessellator setColor(float red, float green, float blue) { public Tessellator setColor(float red, float green, float blue) {
return setColor(red, green, blue, 1f); return setColor(red, green, blue, 1f);
} }
// DISPLAY LIST (DL) ====================================================== // DISPLAY LIST (DL) ======================================================
public void startDrawingUseDL(int drawMode) { public void startDrawingUseDL(int drawMode) {
use_dl = true; use_dl = true;
display_list_id = glGenLists(1); display_list_id = glGenLists(1);
glNewList(display_list_id, GL_COMPILE); glNewList(display_list_id, GL_COMPILE);
glBegin(drawMode); glBegin(drawMode);
} }
private void dl_add_vertex(float x, float y, float z) { private void dl_add_vertex(float x, float y, float z) {
glVertex3f(x, y, z); glVertex3f(x, y, z);
} }
@@ -92,7 +92,7 @@ public class Tessellator {
private void dl_set_texture(float u, float v) { private void dl_set_texture(float u, float v) {
glTexCoord2f(u, v); glTexCoord2f(u, v);
} }
public int createDL() { public int createDL() {
if (use_dl) { if (use_dl) {
glEnd(); glEnd();
@@ -104,47 +104,47 @@ public class Tessellator {
throw new IllegalAccessError("Drawing with display list not started"); throw new IllegalAccessError("Drawing with display list not started");
} }
} }
public void removeDL(int displayListId){ public void removeDL(int displayListId) {
glDeleteLists(displayListId, 1); glDeleteLists(displayListId, 1);
} }
public void drawDL(int displayListId) { public void drawDL(int displayListId) {
glCallList(displayListId); glCallList(displayListId);
} }
// VERTEX BUFFER OBJECT (VBO) ============================================= // VERTEX BUFFER OBJECT (VBO) =============================================
public void startDrawingUseVBO(int drawMode) { public void startDrawingUseVBO(int drawMode) {
draw_mode = drawMode; draw_mode = drawMode;
use_vbo = true; use_vbo = true;
} }
private void vbo_add_vertex(float x, float y, float z) { private void vbo_add_vertex(float x, float y, float z) {
float_buffer[float_buffer_position] = x; float_buffer[float_buffer_position] = x;
float_buffer[float_buffer_position + 1] = y; float_buffer[float_buffer_position + 1] = y;
float_buffer[float_buffer_position + 2] = z; float_buffer[float_buffer_position + 2] = z;
float_buffer_position += 9; float_buffer_position += 9;
vertex_count++; vertex_count++;
} }
private void vbo_set_color(float red, float green, float blue, float alpha) { private void vbo_set_color(float red, float green, float blue, float alpha) {
float_buffer[float_buffer_position + 5] = red; float_buffer[float_buffer_position + 5] = red;
float_buffer[float_buffer_position + 6] = green; float_buffer[float_buffer_position + 6] = green;
float_buffer[float_buffer_position + 7] = blue; float_buffer[float_buffer_position + 7] = blue;
float_buffer[float_buffer_position + 8] = alpha; float_buffer[float_buffer_position + 8] = alpha;
use_color = true; use_color = true;
} }
private void vbo_set_texture(float u, float v) { private void vbo_set_texture(float u, float v) {
float_buffer[float_buffer_position + 3] = u; float_buffer[float_buffer_position + 3] = u;
float_buffer[float_buffer_position + 4] = v; float_buffer[float_buffer_position + 4] = v;
use_texture = true; use_texture = true;
} }
public int createVBO() { public int createVBO() {
if (use_vbo) { if (use_vbo) {
if (vertex_count > 0) { if (vertex_count > 0) {
@@ -168,17 +168,16 @@ public class Tessellator {
throw new IllegalAccessError("Drawing with vertex buffer object not started"); throw new IllegalAccessError("Drawing with vertex buffer object not started");
} }
} }
public void removeVBO(long vboData){ public void removeVBO(long vboData) {
glDeleteBuffers((int) (0b11111111 & vboData)); glDeleteBuffers((int) (0b11111111 & vboData));
} }
public void drawVBO(int vboData) { public void drawVBO(int vboData) {
glBindBuffer(GL_ARRAY_BUFFER, (int) (0b11111111 & vboData)); glBindBuffer(GL_ARRAY_BUFFER, (int) (0b11111111 & vboData));
boolean has_color = (0b11111111 & vboData >>> 24) == 1; boolean has_color = (0b11111111 & vboData >>> 24) == 1;
boolean has_texture = (0b11111111 & vboData >>> 32) == 1; boolean has_texture = (0b11111111 & vboData >>> 32) == 1;
if (has_color) { if (has_color) {
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_COLOR_ARRAY);
// 12L -> 3 << 2 // 12L -> 3 << 2
@@ -203,41 +202,41 @@ public class Tessellator {
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
// VERTEX ARRAY (VA) ====================================================== // VERTEX ARRAY (VA) ======================================================
public void startDrawingUseVA(int drawMode) { public void startDrawingUseVA(int drawMode) {
use_va = true; use_va = true;
draw_mode = drawMode; draw_mode = drawMode;
use_color = false; //TODO а надоли? есть же reset() use_color = false; // TODO а надоли? есть же reset()
use_texture = false; use_texture = false;
} }
private void va_add_vertex(float x, float y, float z) { private void va_add_vertex(float x, float y, float z) {
float_buffer[float_buffer_position] = x; float_buffer[float_buffer_position] = x;
float_buffer[float_buffer_position + 1] = y; float_buffer[float_buffer_position + 1] = y;
float_buffer[float_buffer_position + 2] = z; float_buffer[float_buffer_position + 2] = z;
float_buffer_position += 9; float_buffer_position += 9;
vertex_count++; vertex_count++;
} }
private void va_set_color(float red, float green, float blue, float alpha) { private void va_set_color(float red, float green, float blue, float alpha) {
float_buffer[float_buffer_position + 5] = red; float_buffer[float_buffer_position + 5] = red;
float_buffer[float_buffer_position + 6] = green; float_buffer[float_buffer_position + 6] = green;
float_buffer[float_buffer_position + 7] = blue; float_buffer[float_buffer_position + 7] = blue;
float_buffer[float_buffer_position + 8] = alpha; float_buffer[float_buffer_position + 8] = alpha;
use_color = true; use_color = true;
} }
private void va_set_texture(float u, float v) { private void va_set_texture(float u, float v) {
float_buffer[float_buffer_position + 3] = u; float_buffer[float_buffer_position + 3] = u;
float_buffer[float_buffer_position + 4] = v; float_buffer[float_buffer_position + 4] = v;
use_texture = true; use_texture = true;
} }
public void drawVA() { public void drawVA() {
if (use_va) { if (use_va) {
if (vertex_count > 0) { if (vertex_count > 0) {
@@ -263,9 +262,11 @@ public class Tessellator {
glDrawArrays(draw_mode, 0, vertex_count); glDrawArrays(draw_mode, 0, vertex_count);
if (use_texture) glDisableClientState(GL_TEXTURE_COORD_ARRAY); if (use_texture)
if (use_color) glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);
if (use_color)
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
reset(); reset();

View File

@@ -1,19 +1,19 @@
package ru.dmitriymx.lwjgl.tools; package ru.dmitriymx.lwjgl.tools;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
public class Texture { public class Texture {
private static boolean set_blend = false; private static boolean set_blend = false;
private int bind_id; private int bind_id;
private BufferedImage image; private BufferedImage image;
private float one_pixel_w, one_pixel_h; private float one_pixel_w, one_pixel_h;
public Texture(BufferedImage bufferedImage) { public Texture(BufferedImage bufferedImage) {
bind_id = glGenTextures(); bind_id = glGenTextures();
this.image = bufferedImage; this.image = bufferedImage;
@@ -21,27 +21,27 @@ public class Texture {
prepare_image(image); prepare_image(image);
prepare_onePixel(); prepare_onePixel();
} }
public BufferedImage getImage() { public BufferedImage getImage() {
return image; return image;
} }
public void setImage(BufferedImage image) { public void setImage(BufferedImage image) {
this.image = image; this.image = image;
} }
public void bind() { public void bind() {
Texture.bindTexture(bind_id); Texture.bindTexture(bind_id);
} }
public static void bindTexture(int texture_id) { public static void bindTexture(int texture_id) {
glBindTexture(GL_TEXTURE_2D, texture_id); glBindTexture(GL_TEXTURE_2D, texture_id);
} }
public int getBindId() { public int getBindId() {
return bind_id; return bind_id;
} }
public float floatX(int x) { public float floatX(int x) {
return x * one_pixel_w; return x * one_pixel_w;
} }
@@ -49,13 +49,13 @@ public class Texture {
public float floatY(int y) { public float floatY(int y) {
return y * one_pixel_h; return y * one_pixel_h;
} }
public void updateTexture() { public void updateTexture() {
bind(); bind();
prepare_image(image); prepare_image(image);
prepare_onePixel(); prepare_onePixel();
} }
public int getWidth() { public int getWidth() {
return getImage().getWidth(); return getImage().getWidth();
} }
@@ -63,34 +63,34 @@ public class Texture {
public int getHeight() { public int getHeight() {
return getImage().getHeight(); return getImage().getHeight();
} }
public static int singleTexture(BufferedImage bufferedImage) { public static int singleTexture(BufferedImage bufferedImage) {
int bindId = glGenTextures(); int bindId = glGenTextures();
prepare_opengl(bindId); prepare_opengl(bindId);
prepare_image(bufferedImage); prepare_image(bufferedImage);
return bindId; return bindId;
} }
public static void Enable() { public static void Enable() {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
} }
public static void Disable() { public static void Disable() {
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
} }
public static void setAlpha(boolean alpha) { public static void setAlpha(boolean alpha) {
if (alpha) { if (alpha) {
glEnable(GL_BLEND); glEnable(GL_BLEND);
if (!set_blend){ if (!set_blend) {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
set_blend = true; set_blend = true;
} }
} else { } else {
glDisable(GL_BLEND); glDisable(GL_BLEND);
}
} }
}
private static void prepare_opengl(int bindId) { private static void prepare_opengl(int bindId) {
glBindTexture(GL_TEXTURE_2D, bindId); glBindTexture(GL_TEXTURE_2D, bindId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@@ -98,7 +98,7 @@ public class Texture {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
} }
private static void prepare_image(BufferedImage image) { private static void prepare_image(BufferedImage image) {
int width = image.getWidth(); int width = image.getWidth();
int height = image.getHeight(); int height = image.getHeight();
@@ -109,10 +109,9 @@ public class Texture {
image.getRGB(0, 0, width, height, rgb_array, 0, width); image.getRGB(0, 0, width, height, rgb_array, 0, width);
imageData.put(rgb_array); imageData.put(rgb_array);
imageData.position(0).limit(rgb_array_size); imageData.position(0).limit(rgb_array_size);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, imageData);
GL_UNSIGNED_INT_8_8_8_8_REV, imageData);
} }
private void prepare_onePixel() { private void prepare_onePixel() {
one_pixel_w = (100F / image.getWidth()) / 100F; one_pixel_w = (100F / image.getWidth()) / 100F;
one_pixel_h = (100F / image.getHeight()) / 100F; one_pixel_h = (100F / image.getHeight()) / 100F;

View File

@@ -25,13 +25,14 @@ public class FontEngine {
private boolean anti_aliasing = false; private boolean anti_aliasing = false;
private Map<Character, CharData> charMap = new HashMap<>(); private Map<Character, CharData> charMap = new HashMap<>();
private int texture_offset_char_x = 0, texture_offset_char_y = 0, max_height = 0; private int texture_offset_char_x = 0, texture_offset_char_y = 0, max_height = 0;
public class CharData { public class CharData {
public final float texture_offset_x1, texture_offset_y1, texture_offset_x2, texture_offset_y2; public final float texture_offset_x1, texture_offset_y1, texture_offset_x2, texture_offset_y2;
public final int visual_bounds_width, visual_bounds_height, visual_bounds_x, visual_bounds_y; public final int visual_bounds_width, visual_bounds_height, visual_bounds_x, visual_bounds_y;
public final int width_char; public final int width_char;
private CharData(int texture_offset_x, int texture_offset_y, int visual_bounds_width, int visual_bounds_height, int visual_bounds_x, int visual_bounds_y, int width_char) { private CharData(int texture_offset_x, int texture_offset_y, int visual_bounds_width, int visual_bounds_height, int visual_bounds_x,
int visual_bounds_y, int width_char) {
this.texture_offset_x1 = glyphTexture.floatX(texture_offset_x); this.texture_offset_x1 = glyphTexture.floatX(texture_offset_x);
this.texture_offset_y1 = glyphTexture.floatY(texture_offset_y); this.texture_offset_y1 = glyphTexture.floatY(texture_offset_y);
this.texture_offset_x2 = glyphTexture.floatX(texture_offset_x + visual_bounds_width); this.texture_offset_x2 = glyphTexture.floatX(texture_offset_x + visual_bounds_width);
@@ -43,11 +44,11 @@ public class FontEngine {
this.width_char = width_char; this.width_char = width_char;
} }
} }
public static void importFont(InputStream inputStream) throws IOException, FontFormatException { public static void importFont(InputStream inputStream) throws IOException, FontFormatException {
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(Font.createFont(Font.TRUETYPE_FONT, inputStream)); GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(Font.createFont(Font.TRUETYPE_FONT, inputStream));
} }
public FontEngine(String fontName, int size) { public FontEngine(String fontName, int size) {
this(fontName, size, false); this(fontName, size, false);
} }
@@ -55,7 +56,7 @@ public class FontEngine {
public FontEngine(String fontName, int size, boolean useAntiAliasing) { public FontEngine(String fontName, int size, boolean useAntiAliasing) {
this(fontName, size, useAntiAliasing, 64); this(fontName, size, useAntiAliasing, 64);
} }
public FontEngine(String fontName, int size, boolean useAntiAliasing, int initGlyphTextureWidth) { public FontEngine(String fontName, int size, boolean useAntiAliasing, int initGlyphTextureWidth) {
this(new Font(fontName, Font.PLAIN, size), useAntiAliasing, initGlyphTextureWidth); this(new Font(fontName, Font.PLAIN, size), useAntiAliasing, initGlyphTextureWidth);
} }
@@ -63,58 +64,49 @@ public class FontEngine {
public FontEngine(Font font) { public FontEngine(Font font) {
this(font, false); this(font, false);
} }
public FontEngine(Font font, boolean useAntiAliasing) { public FontEngine(Font font, boolean useAntiAliasing) {
this(font, useAntiAliasing, 64); this(font, useAntiAliasing, 64);
} }
public FontEngine(Font font, boolean useAntiAliasing, int initGlyphTextureWidth) { public FontEngine(Font font, boolean useAntiAliasing, int initGlyphTextureWidth) {
this.font = font; this.font = font;
this.anti_aliasing = useAntiAliasing; this.anti_aliasing = useAntiAliasing;
texture_width = texture_height = initGlyphTextureWidth; texture_width = texture_height = initGlyphTextureWidth;
glyphTexture = new Texture(createBlankImage()); glyphTexture = new Texture(createBlankImage());
} }
// public void initializeString(String string) {
// char[] chars = string.toCharArray();
// for (char chr : chars) {
// if (!charMap.containsKey(chr)) {
// cache_char(chr, true);
// }
// }
// glyphTexture.updateTexture();
// }
public Font getFont() { public Font getFont() {
return font; return font;
} }
public void bindTexture() { public void bindTexture() {
glyphTexture.bind(); glyphTexture.bind();
} }
public CharData getCharData(char chr) { public CharData getCharData(char chr) {
return charMap.get(chr); return charMap.get(chr);
} }
public void checkChars(char[] chars) { public void checkChars(char[] chars) {
boolean needUpdTexture = false; boolean needUpdTexture = false;
for (char chr : chars) { for (char chr : chars) {
if (!charMap.containsKey(chr)) { if (!charMap.containsKey(chr)) {
needUpdTexture = true; needUpdTexture = true;
putChar(chr); putChar(chr);
} }
} }
if (needUpdTexture) glyphTexture.updateTexture(); if (needUpdTexture)
glyphTexture.updateTexture();
} }
public FontMetrics getFontMetrics() { public FontMetrics getFontMetrics() {
return g2d.getFontMetrics(); return g2d.getFontMetrics();
} }
private BufferedImage createBlankImage() { private BufferedImage createBlankImage() {
BufferedImage image = new BufferedImage(texture_width, texture_height, BufferedImage.TYPE_INT_ARGB); BufferedImage image = new BufferedImage(texture_width, texture_height, BufferedImage.TYPE_INT_ARGB);
g2d = image.createGraphics(); g2d = image.createGraphics();
@@ -123,59 +115,61 @@ public class FontEngine {
} }
g2d.setColor(Color.WHITE); g2d.setColor(Color.WHITE);
g2d.setFont(font); g2d.setFont(font);
return image; return image;
} }
// Добавляем символ в "базу": рисуем на текстуре и сохраняем метрические данные // Добавляем символ в "базу": рисуем на текстуре и сохраняем метрические
// данные
private void putChar(char chr) { private void putChar(char chr) {
CharData data = drawChar(chr); CharData data = drawChar(chr);
texture_offset_char_x += data.visual_bounds_width + 1; texture_offset_char_x += data.visual_bounds_width + 1;
if (max_height < texture_offset_char_y + data.visual_bounds_height) { if (max_height < texture_offset_char_y + data.visual_bounds_height) {
max_height = texture_offset_char_y + data.visual_bounds_height; max_height = texture_offset_char_y + data.visual_bounds_height;
} }
charMap.put(chr, data); charMap.put(chr, data);
} }
// Рисуем символ на текстуре. Возвращаются данные о символе // Рисуем символ на текстуре. Возвращаются данные о символе
private CharData drawChar(char chr) { private CharData drawChar(char chr) {
GlyphVector glyphVector = font.createGlyphVector(g2d.getFontRenderContext(), String.valueOf(chr)); GlyphVector glyphVector = font.createGlyphVector(g2d.getFontRenderContext(), String.valueOf(chr));
Rectangle visualBounds = glyphVector.getVisualBounds().getBounds(); Rectangle visualBounds = glyphVector.getVisualBounds().getBounds();
FontMetrics fontMetrics = g2d.getFontMetrics(); FontMetrics fontMetrics = g2d.getFontMetrics();
int widthChar = fontMetrics.charWidth(chr) - visualBounds.x; int widthChar = fontMetrics.charWidth(chr) - visualBounds.x;
if (texture_offset_char_y >= texture_height || (texture_offset_char_y + visualBounds.height) > texture_height) { if (texture_offset_char_y >= texture_height || (texture_offset_char_y + visualBounds.height) > texture_height) {
recreateGlyphTexture(); recreateGlyphTexture();
} }
if (texture_offset_char_x >= texture_width || (texture_offset_char_x + visualBounds.width) > texture_width) { if (texture_offset_char_x >= texture_width || (texture_offset_char_x + visualBounds.width) > texture_width) {
texture_offset_char_y = max_height + 1; texture_offset_char_y = max_height + 1;
texture_offset_char_x = 0; texture_offset_char_x = 0;
} }
g2d.drawGlyphVector(glyphVector, texture_offset_char_x - visualBounds.x, texture_offset_char_y - visualBounds.y); g2d.drawGlyphVector(glyphVector, texture_offset_char_x - visualBounds.x, texture_offset_char_y - visualBounds.y);
return new CharData(texture_offset_char_x, texture_offset_char_y, visualBounds.width, visualBounds.height, ~visualBounds.x + 1, ~visualBounds.y + 1, widthChar); return new CharData(texture_offset_char_x, texture_offset_char_y, visualBounds.width, visualBounds.height, ~visualBounds.x + 1,
~visualBounds.y + 1, widthChar);
} }
// Увеличиваем вместимость текстуры путем увеличения ее размеров. // Увеличиваем вместимость текстуры путем увеличения ее размеров.
private void recreateGlyphTexture() { private void recreateGlyphTexture() {
// удваение размеров текстуры // удваение размеров текстуры
texture_width *= 2; texture_width *= 2;
texture_height *= 2; texture_height *= 2;
// создание текстуры по новой // создание текстуры по новой
BufferedImage img = createBlankImage(); BufferedImage img = createBlankImage();
// сброс "офсетов" // сброс "офсетов"
texture_offset_char_x = 0; texture_offset_char_x = 0;
texture_offset_char_y = 0; texture_offset_char_y = 0;
max_height = 0; max_height = 0;
// отрисовка символов по новой // отрисовка символов по новой
for (char chr : charMap.keySet()) { for (char chr : charMap.keySet()) {
putChar(chr); putChar(chr);
} }
// обновление текстуры // обновление текстуры
glyphTexture.setImage(img); glyphTexture.setImage(img);
glyphTexture.updateTexture(); glyphTexture.updateTexture();

View File

@@ -1,41 +1,41 @@
package ru.dmitriymx.lwjgl.tools.fontengine; package ru.dmitriymx.lwjgl.tools.fontengine;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.util.Renderable; import org.lwjgl.util.Renderable;
import ru.dmitriymx.lwjgl.tools.Tessellator; import ru.dmitriymx.lwjgl.tools.Tessellator;
import static org.lwjgl.opengl.GL11.*;
public class SimpleText implements Renderable { public class SimpleText implements Renderable {
private Tessellator tess = Tessellator.getInstance(); private Tessellator tess = Tessellator.getInstance();
private FontEngine engine; private FontEngine engine;
private char[] chars; private char[] chars;
private float[] color4f = new float[]{1f, 1f, 1f, 1f}; private float[] color4f = new float[] { 1f, 1f, 1f, 1f };
public SimpleText(String string, FontEngine engine) { public SimpleText(String string, FontEngine engine) {
this.engine = engine; this.engine = engine;
} }
public void setString(String string) { public void setString(String string) {
this.chars = string.toCharArray(); this.chars = string.toCharArray();
engine.checkChars(chars); engine.checkChars(chars);
} }
public String getString() { public String getString() {
return String.valueOf(chars); return String.valueOf(chars);
} }
public void setColor(float red, float green, float blue, float alpha) { public void setColor(float red, float green, float blue, float alpha) {
this.color4f[0] = red; this.color4f[0] = red;
this.color4f[1] = green; this.color4f[1] = green;
this.color4f[2] = blue; this.color4f[2] = blue;
this.color4f[3] = alpha; this.color4f[3] = alpha;
} }
public float[] getColor() { public float[] getColor() {
return color4f; return color4f;
} }
@Override @Override
public String toString() { public String toString() {
return getClass().getCanonicalName() + "[" + getString() + "]"; return getClass().getCanonicalName() + "[" + getString() + "]";
@@ -45,21 +45,25 @@ public class SimpleText implements Renderable {
public void render() { public void render() {
engine.bindTexture(); engine.bindTexture();
float current_x = 0f; float current_x = 0f;
tess.startDrawingUseVA(GL_QUADS); tess.startDrawingUseVA(GL_QUADS);
for (char ch : chars) { for (char ch : chars) {
FontEngine.CharData charData = engine.getCharData(ch); FontEngine.CharData charData = engine.getCharData(ch);
current_x -= charData.visual_bounds_x; current_x -= charData.visual_bounds_x;
tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x1, charData.texture_offset_y1).addVertex(current_x, charData.visual_bounds_y, 0); tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x1, charData.texture_offset_y1)
tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x2, charData.texture_offset_y1).addVertex(current_x + charData.visual_bounds_width, charData.visual_bounds_y, 0); .addVertex(current_x, charData.visual_bounds_y, 0);
tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x2, charData.texture_offset_y2).addVertex(current_x + charData.visual_bounds_width, charData.visual_bounds_y - charData.visual_bounds_height, 0); tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x2, charData.texture_offset_y1)
tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x1, charData.texture_offset_y2).addVertex(current_x, charData.visual_bounds_y - charData.visual_bounds_height, 0); .addVertex(current_x + charData.visual_bounds_width, charData.visual_bounds_y, 0);
tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x2, charData.texture_offset_y2)
.addVertex(current_x + charData.visual_bounds_width, charData.visual_bounds_y - charData.visual_bounds_height, 0);
tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x1, charData.texture_offset_y2)
.addVertex(current_x, charData.visual_bounds_y - charData.visual_bounds_height, 0);
current_x += charData.width_char; current_x += charData.width_char;
} }
tess.drawVA(); tess.drawVA();
} }