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

View File

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

View File

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

View File

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