2018-09-22 10:29:40 +02:00
|
|
|
package it.cavallium.warppi.gui.graphicengine.impl.jogl;
|
2017-01-22 13:46:38 +01:00
|
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
2017-11-19 22:58:37 +01:00
|
|
|
import java.io.File;
|
2017-01-31 22:29:49 +01:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.FloatBuffer;
|
2017-11-19 22:58:37 +01:00
|
|
|
import java.nio.file.Files;
|
2018-09-02 12:45:51 +02:00
|
|
|
|
2017-01-31 22:29:49 +01:00
|
|
|
import javax.imageio.ImageIO;
|
2017-01-22 13:46:38 +01:00
|
|
|
|
2017-01-31 22:29:49 +01:00
|
|
|
import com.jogamp.common.nio.Buffers;
|
|
|
|
import com.jogamp.opengl.GL;
|
|
|
|
import com.jogamp.opengl.GL2ES1;
|
|
|
|
import com.jogamp.opengl.GLException;
|
|
|
|
import com.jogamp.opengl.util.texture.Texture;
|
|
|
|
import com.jogamp.opengl.util.texture.TextureData;
|
|
|
|
import com.jogamp.opengl.util.texture.TextureIO;
|
2017-01-22 13:46:38 +01:00
|
|
|
|
2018-09-04 12:12:41 +02:00
|
|
|
import it.cavallium.warppi.Engine;
|
2018-09-02 13:36:49 +02:00
|
|
|
import it.cavallium.warppi.gui.graphicengine.BinaryFont;
|
|
|
|
import it.cavallium.warppi.gui.graphicengine.Renderer;
|
|
|
|
|
2018-09-22 10:29:40 +02:00
|
|
|
public class JOGLRenderer implements Renderer {
|
2017-01-22 13:46:38 +01:00
|
|
|
|
2017-03-26 22:44:09 +02:00
|
|
|
public static GL2ES1 gl;
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2018-09-12 22:16:33 +02:00
|
|
|
private static final int ELEMENTS_MAX_COUNT_PER_BUFFER = 128;
|
2018-06-14 12:20:01 +02:00
|
|
|
private static final int ELEMENT_VERTICES_COUNT = 6;
|
|
|
|
|
|
|
|
private static final int vertSize = 3;
|
|
|
|
|
|
|
|
private static final int texSize = 2;
|
|
|
|
|
|
|
|
private static final int colSize = 4;
|
|
|
|
|
|
|
|
private static final int vertBuffer = 0;
|
|
|
|
|
|
|
|
private static final int texBuffer = 1;
|
|
|
|
|
|
|
|
private static final int colBuffer = 2;
|
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
private static final int vertMax = JOGLRenderer.vertSize * JOGLRenderer.ELEMENT_VERTICES_COUNT * JOGLRenderer.ELEMENTS_MAX_COUNT_PER_BUFFER;
|
2018-06-14 12:20:01 +02:00
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
private static final int texMax = JOGLRenderer.texSize * JOGLRenderer.ELEMENT_VERTICES_COUNT * JOGLRenderer.ELEMENTS_MAX_COUNT_PER_BUFFER;
|
2018-06-14 12:20:01 +02:00
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
private static final int colMax = JOGLRenderer.colSize * JOGLRenderer.ELEMENT_VERTICES_COUNT * JOGLRenderer.ELEMENTS_MAX_COUNT_PER_BUFFER;
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2018-03-29 19:20:28 +02:00
|
|
|
private int[] handlers;
|
2017-01-31 22:29:49 +01:00
|
|
|
FloatBuffer fbVertices;
|
2018-03-29 00:34:09 +02:00
|
|
|
FloatBuffer fbTextures;
|
|
|
|
FloatBuffer fbColors;
|
2017-01-31 22:29:49 +01:00
|
|
|
int fbElements;
|
|
|
|
|
2017-09-23 23:23:26 +02:00
|
|
|
float[] currentColor = new float[24];
|
2017-04-10 22:50:43 +02:00
|
|
|
float[] currentClearColorARGBf = new float[] { 1f, 197f / 255f, 194f / 255f, 175f / 255f };
|
2017-01-31 22:29:49 +01:00
|
|
|
boolean currentTexEnabled;
|
|
|
|
Texture currentTex;
|
|
|
|
float currentTexWidth;
|
|
|
|
float currentTexHeight;
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2018-09-22 10:29:40 +02:00
|
|
|
JOGLFont currentFont;
|
2017-01-22 13:46:38 +01:00
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glColor3i(final int r, final int gg, final int b) {
|
|
|
|
final float red = r / 255f;
|
|
|
|
final float gre = gg / 255f;
|
|
|
|
final float blu = b / 255f;
|
2017-09-23 23:23:26 +02:00
|
|
|
//currentColor = new float[] { red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, };
|
2017-09-24 18:47:30 +02:00
|
|
|
currentColor = new float[] { red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, }; //OK
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glColor3f(final float red, final float gre, final float blu) {
|
2017-09-23 23:23:26 +02:00
|
|
|
// currentColor = new float[] { red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, };
|
2017-09-24 18:47:30 +02:00
|
|
|
currentColor = new float[] { red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, red, gre, blu, 1.0f, };//OK
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glColor4f(final float red, final float gre, final float blu, final float alp) {
|
2017-09-23 23:23:26 +02:00
|
|
|
// currentColor = new float[] { red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, };
|
2017-09-24 18:47:30 +02:00
|
|
|
currentColor = new float[] { red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, };//ok
|
2017-09-23 23:23:26 +02:00
|
|
|
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glColor(final int rgb) {
|
|
|
|
final int alpha = rgb >> 24 & 0xFF;
|
|
|
|
final int red = rgb >> 16 & 0xFF;
|
|
|
|
final int green = rgb >> 8 & 0xFF;
|
2017-01-31 22:29:49 +01:00
|
|
|
final int blue = rgb & 0xFF;
|
|
|
|
glColor4i(red, green, blue, alpha);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int glGetClearColor() {
|
2017-04-10 22:50:43 +02:00
|
|
|
return (int) (currentClearColorARGBf[0] * 255) << 24 | (int) (currentClearColorARGBf[1] * 255) << 16 | (int) (currentClearColorARGBf[2] * 255) << 8 | (int) (currentClearColorARGBf[3] * 255);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glClearColor(final int rgb) {
|
|
|
|
final float alpha = (rgb >> 24 & 0xFF) / 255f;
|
|
|
|
final float red = (rgb >> 16 & 0xFF) / 255f;
|
|
|
|
final float green = (rgb >> 8 & 0xFF) / 255f;
|
2017-04-10 22:50:43 +02:00
|
|
|
final float blue = (rgb & 0xFF) / 255f;
|
2017-01-31 22:29:49 +01:00
|
|
|
glClearColor4f(red, green, blue, alpha);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glColor4i(final int r, final int g, final int b, final int a) {
|
|
|
|
final float red = r / 255f;
|
|
|
|
final float gre = g / 255f;
|
|
|
|
final float blu = b / 255f;
|
|
|
|
final float alp = a / 255f;
|
2017-09-23 23:23:26 +02:00
|
|
|
//currentColor = new float[] { red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, };
|
2017-09-24 18:47:30 +02:00
|
|
|
currentColor = new float[] { red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, red, gre, blu, alp, };//OK
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glClearColor4i(final int red, final int green, final int blue, final int alpha) {
|
|
|
|
final float ros = red / 255f;
|
|
|
|
final float gre = green / 255f;
|
|
|
|
final float blu = blue / 255f;
|
|
|
|
final float alp = alpha / 255f;
|
2017-04-10 22:50:43 +02:00
|
|
|
currentClearColorARGBf = new float[] { alp, ros, gre, blu };
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glClearColor4f(final float red, final float green, final float blue, final float alpha) {
|
2017-04-10 22:50:43 +02:00
|
|
|
currentClearColorARGBf = new float[] { alpha, red, green, blue };
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glClear(final int screenWidth, final int screenHeight) {
|
2017-01-31 22:29:49 +01:00
|
|
|
glColor(glGetClearColor());
|
|
|
|
glFillColor(0, 0, screenWidth, screenHeight);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glDrawLine(final float x0, final float y0, final float x1, final float y1) {
|
2017-04-10 22:50:43 +02:00
|
|
|
glFillColor(x0, y0, x1 - x0 + 1, y1 - y0 + 1);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glFillRect(final float x, final float y, final float width, final float height, float uvX, float uvY,
|
|
|
|
float uvWidth, float uvHeight) {
|
2017-01-31 22:29:49 +01:00
|
|
|
enableTexture();
|
2018-09-22 11:17:30 +02:00
|
|
|
if (uvWidth < 0)
|
2018-07-28 23:26:07 +02:00
|
|
|
uvX -= uvWidth;
|
2018-09-22 11:17:30 +02:00
|
|
|
if (uvHeight < 0)
|
2018-07-28 23:26:07 +02:00
|
|
|
uvY -= uvHeight;
|
2017-04-10 22:50:43 +02:00
|
|
|
uvWidth /= currentTexWidth;
|
|
|
|
uvX /= currentTexWidth;
|
|
|
|
uvHeight /= currentTexHeight;
|
|
|
|
uvY = 1 - uvY / currentTexHeight - uvHeight;
|
2017-09-23 23:23:26 +02:00
|
|
|
// final float[] vertices = { x, y, 0.0f, x, y + height, 0.0f, x + width, y, 0.0f, x + width, y + height, 0.0f, };
|
|
|
|
// final float[] tex_vertices = { uvX, uvY + uvHeight, uvX, uvY, uvX + uvWidth, uvY + uvHeight, uvX + uvWidth, uvY, };
|
|
|
|
//V0 x, y, 0.0f
|
|
|
|
//V1 x, y + height, 0.0f
|
|
|
|
//V2 x + width, y, 0.0f
|
|
|
|
//V3 x + width, y + height, 0.0f
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-09-23 23:23:26 +02:00
|
|
|
//NV0 = V1
|
|
|
|
//NV1 = V3
|
|
|
|
//NV2 = V0
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-09-23 23:23:26 +02:00
|
|
|
//NV3 = V0
|
|
|
|
//NV4 = V3
|
|
|
|
//NV5 = V2
|
2017-09-24 18:47:30 +02:00
|
|
|
|
|
|
|
final float[] vertices = { x, y + height, 0.0f, x + width, y + height, 0.0f, x, y, 0.0f, x, y, 0.0f, x + width, y + height, 0.0f, x + width, y, 0.0f };
|
|
|
|
final float[] tex_vertices = { uvX, uvY, uvX + uvWidth, uvY, uvX, uvY + uvHeight, uvX, uvY + uvHeight, uvX + uvWidth, uvY, uvX + uvWidth, uvY + uvHeight };
|
2017-01-31 22:29:49 +01:00
|
|
|
fbElements++;
|
|
|
|
fbVertices.put(vertices);
|
2018-03-29 00:34:09 +02:00
|
|
|
fbTextures.put(tex_vertices);
|
|
|
|
fbColors.put(currentColor);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glFillColor(final float x0, final float y0, final float w1, final float h1) {
|
2017-01-31 22:29:49 +01:00
|
|
|
disableTexture();
|
2017-09-23 23:23:26 +02:00
|
|
|
// final float[] vertices = { x0, y0, 0.0f, x0, y0 + h1, 0.0f, x0 + w1, y0, 0.0f, x0 + w1, y0 + h1, 0.0f, };
|
|
|
|
// final float[] tex_vertices = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, };
|
|
|
|
//V0 x0, y0, 0.0f
|
|
|
|
//V1 x0, y0 + h1, 0.0f
|
|
|
|
//V2 x0 + w1, y0, 0.0f
|
|
|
|
//V3 x0 + w1, y0 + h1, 0.0f
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-09-23 23:23:26 +02:00
|
|
|
//NV0 = V1
|
|
|
|
//NV1 = V3
|
|
|
|
//NV2 = V0
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-09-23 23:23:26 +02:00
|
|
|
//NV3 = V0
|
|
|
|
//NV4 = V3
|
|
|
|
//NV5 = V2
|
2017-09-24 18:47:30 +02:00
|
|
|
|
|
|
|
final float[] vertices = { x0, y0 + h1, 0.0f, x0 + w1, y0 + h1, 0.0f, x0, y0, 0.0f, x0, y0, 0.0f, x0 + w1, y0 + h1, 0.0f, x0 + w1, y0, 0.0f, };
|
|
|
|
final float[] tex_vertices = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, };
|
2017-01-31 22:29:49 +01:00
|
|
|
fbElements++;
|
|
|
|
fbVertices.put(vertices);
|
2018-03-29 00:34:09 +02:00
|
|
|
fbTextures.put(tex_vertices);
|
|
|
|
fbColors.put(currentColor);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glDrawStringLeft(final float x, final float y, final String text) {
|
2017-01-31 22:29:49 +01:00
|
|
|
final int txtLen = text.length();
|
2017-04-10 22:50:43 +02:00
|
|
|
final int[] txtArray = currentFont.getCharIndexes(text);
|
2017-01-31 22:29:49 +01:00
|
|
|
int tableIndexX;
|
|
|
|
int tableIndexY;
|
|
|
|
for (int currentCharIndex = 0; currentCharIndex < txtLen; currentCharIndex++) {
|
|
|
|
tableIndexX = txtArray[currentCharIndex] % currentFont.memoryWidthOfEachColumn;
|
|
|
|
tableIndexY = (txtArray[currentCharIndex] - tableIndexX) / currentFont.memoryWidthOfEachColumn;
|
2018-09-22 11:17:30 +02:00
|
|
|
glFillRect(x + (float) currentCharIndex * (float) currentFont.charW, y, currentFont.charW, currentFont.charH, tableIndexX * currentFont.charW, tableIndexY * currentFont.charH, currentFont.charW, currentFont.charH);
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glDrawStringCenter(final float x, final float y, final String text) {
|
|
|
|
glDrawStringLeft(x - currentFont.getStringWidth(text) / 2, y, text);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glDrawStringRight(final float x, final float y, final String text) {
|
2017-01-31 22:29:49 +01:00
|
|
|
glDrawStringLeft(x - currentFont.getStringWidth(text), y, text);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glDrawCharLeft(final int x, final int y, final char ch) {
|
2017-04-10 22:50:43 +02:00
|
|
|
final int index = currentFont.getCharIndex(ch);
|
|
|
|
final int tableIndexX = index % currentFont.memoryWidthOfEachColumn;
|
|
|
|
final int tableIndexY = (index - tableIndexX) / currentFont.memoryWidthOfEachColumn;
|
|
|
|
glFillRect(x, y, currentFont.charW, currentFont.charH, tableIndexX * currentFont.charW, tableIndexY * currentFont.charH, currentFont.charW, currentFont.charH);
|
2017-03-03 23:06:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glDrawCharCenter(final int x, final int y, final char ch) {
|
|
|
|
glDrawCharLeft(x - currentFont.charW / 2, y, ch);
|
2017-03-03 23:06:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public void glDrawCharRight(final int x, final int y, final char ch) {
|
2017-03-03 23:06:43 +01:00
|
|
|
glDrawCharLeft(x - currentFont.charW, y, ch);
|
|
|
|
}
|
|
|
|
|
2017-01-22 13:46:38 +01:00
|
|
|
@Override
|
2017-02-02 10:30:45 +01:00
|
|
|
public BinaryFont getCurrentFont() {
|
2017-01-31 22:29:49 +01:00
|
|
|
return currentFont;
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
2017-12-09 23:00:04 +01:00
|
|
|
@Deprecated
|
2018-09-22 11:17:30 +02:00
|
|
|
static Texture importTexture(final GL gl, final String string) throws IOException {
|
2017-01-31 22:29:49 +01:00
|
|
|
final FileInputStream f = new FileInputStream("test.png");
|
|
|
|
final TextureData tx_dat = TextureIO.newTextureData(gl.getGLProfile(), f, false, TextureIO.PNG);
|
|
|
|
final Texture tex = new Texture(gl, tx_dat);
|
2017-03-26 22:44:09 +02:00
|
|
|
tex.setTexParameteri(gl, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
|
2017-01-31 22:29:49 +01:00
|
|
|
tex.setTexParameteri(gl, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
|
2017-03-26 22:44:09 +02:00
|
|
|
// tex.setTexParameteri(gl, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
|
|
|
|
// tex.setTexParameteri(gl, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
|
2017-01-31 22:29:49 +01:00
|
|
|
return tex;
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
static OpenedTextureData openTexture(final String file, final boolean isResource) throws GLException, IOException {
|
2018-09-22 10:29:40 +02:00
|
|
|
BufferedImage img = ImageIO.read(isResource ? JOGLRenderer.class.getResource("/" + file) : new File(file).toURI().toURL());
|
2017-12-09 23:00:04 +01:00
|
|
|
File f;
|
|
|
|
if (isResource) {
|
|
|
|
f = Files.createTempFile("texture-", ".png").toFile();
|
2018-04-02 18:29:55 +02:00
|
|
|
f.deleteOnExit();
|
2017-12-09 23:00:04 +01:00
|
|
|
ImageIO.write(img, "png", f);
|
2018-09-22 11:17:30 +02:00
|
|
|
} else
|
2017-12-09 23:00:04 +01:00
|
|
|
f = new File(file);
|
2018-05-12 21:18:29 +02:00
|
|
|
final int imgW = img.getWidth();
|
|
|
|
final int imgH = img.getHeight();
|
2017-12-09 23:00:04 +01:00
|
|
|
img = null;
|
2018-09-04 12:12:41 +02:00
|
|
|
Engine.getPlatform().gc();
|
2017-12-09 23:00:04 +01:00
|
|
|
return new OpenedTextureData(imgW, imgH, f, isResource);
|
2017-11-19 22:58:37 +01:00
|
|
|
}
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2017-11-19 22:58:37 +01:00
|
|
|
public static class OpenedTextureData {
|
|
|
|
public final int w;
|
|
|
|
public final int h;
|
2017-12-09 23:00:04 +01:00
|
|
|
public final File f;
|
|
|
|
public final boolean deleteOnExit;
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2017-11-19 22:58:37 +01:00
|
|
|
/**
|
|
|
|
* @param w
|
|
|
|
* @param h
|
2017-12-09 23:00:04 +01:00
|
|
|
* @param f
|
|
|
|
* @param deleteOnExit
|
2017-11-19 22:58:37 +01:00
|
|
|
*/
|
2018-09-22 11:17:30 +02:00
|
|
|
public OpenedTextureData(final int w, final int h, final File f, final boolean deleteOnExit) {
|
2017-11-19 22:58:37 +01:00
|
|
|
this.w = w;
|
|
|
|
this.h = h;
|
2017-12-09 23:00:04 +01:00
|
|
|
this.f = f;
|
|
|
|
this.deleteOnExit = deleteOnExit;
|
2017-11-19 22:58:37 +01:00
|
|
|
}
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
static Texture importTexture(File f, final boolean deleteOnExit) throws GLException, IOException {
|
2017-12-09 23:00:04 +01:00
|
|
|
final Texture tex = TextureIO.newTexture(f, false);
|
2018-09-22 11:17:30 +02:00
|
|
|
if (deleteOnExit && f.exists())
|
2017-12-09 23:00:04 +01:00
|
|
|
try {
|
2018-09-22 11:17:30 +02:00
|
|
|
if (Engine.getPlatform().getSettings().isDebugEnabled())
|
2018-05-12 21:18:29 +02:00
|
|
|
throw new IOException("Delete on exit!");
|
2017-12-09 23:00:04 +01:00
|
|
|
f.delete();
|
2018-05-12 21:18:29 +02:00
|
|
|
} catch (final Exception ex) {
|
2017-12-09 23:00:04 +01:00
|
|
|
f.deleteOnExit();
|
|
|
|
}
|
2018-09-22 11:17:30 +02:00
|
|
|
tex.setTexParameteri(JOGLRenderer.gl, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
|
|
|
|
tex.setTexParameteri(JOGLRenderer.gl, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
|
2017-12-09 23:00:04 +01:00
|
|
|
f = null;
|
2017-03-26 22:44:09 +02:00
|
|
|
return tex;
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:30:47 +02:00
|
|
|
public void initDrawCycle() {
|
|
|
|
final boolean textureChange = precTexEnabled != currentTexEnabled || precTex != currentTex;
|
2017-04-09 22:01:03 +02:00
|
|
|
if (fbVertices == null) {
|
2018-09-22 11:17:30 +02:00
|
|
|
fbVertices = Buffers.newDirectFloatBuffer(JOGLRenderer.vertMax);
|
|
|
|
fbTextures = Buffers.newDirectFloatBuffer(JOGLRenderer.texMax);
|
|
|
|
fbColors = Buffers.newDirectFloatBuffer(JOGLRenderer.colMax);
|
2018-03-29 19:20:28 +02:00
|
|
|
handlers = new int[3];
|
2018-09-22 11:17:30 +02:00
|
|
|
JOGLRenderer.gl.glGenBuffers(3, handlers, 0);
|
2018-03-29 01:05:45 +02:00
|
|
|
}
|
|
|
|
startDrawSegment(false);
|
2018-09-22 11:17:30 +02:00
|
|
|
if (textureChange)
|
2018-03-29 01:05:45 +02:00
|
|
|
changeTexture();
|
2018-03-29 00:30:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void endDrawCycle() {
|
|
|
|
final boolean textureChange = precTexEnabled != currentTexEnabled || precTex != currentTex;
|
|
|
|
if (textureChange) {
|
2018-09-22 11:17:30 +02:00
|
|
|
if (fbElements > 0)
|
2018-03-29 00:30:47 +02:00
|
|
|
endDrawSegment();
|
|
|
|
changeTexture();
|
2018-09-22 11:17:30 +02:00
|
|
|
} else if (fbElements > 0)
|
|
|
|
endDrawSegment();
|
2018-03-29 00:30:47 +02:00
|
|
|
}
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2018-03-29 00:30:47 +02:00
|
|
|
private void changeTexture() {
|
|
|
|
precTexEnabled = currentTexEnabled;
|
|
|
|
precTex = currentTex;
|
|
|
|
if (currentTexEnabled) {
|
2018-09-22 11:17:30 +02:00
|
|
|
JOGLRenderer.gl.glEnable(GL.GL_TEXTURE_2D);
|
|
|
|
currentTex.bind(JOGLRenderer.gl);
|
|
|
|
} else
|
|
|
|
JOGLRenderer.gl.glDisable(GL.GL_TEXTURE_2D);
|
2018-03-29 01:05:45 +02:00
|
|
|
firstBufferTexDataCall = true;
|
2018-03-29 00:30:47 +02:00
|
|
|
}
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
public void startDrawSegment(final boolean continuation) {
|
|
|
|
if (!continuation || cycleEnded)
|
2017-09-23 23:23:26 +02:00
|
|
|
fbElements = 0;
|
|
|
|
cycleEnded = false;
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
2017-04-09 22:01:03 +02:00
|
|
|
private boolean precTexEnabled;
|
|
|
|
private Texture precTex;
|
2017-09-23 23:23:26 +02:00
|
|
|
private boolean cycleEnded = true;
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2018-03-29 00:30:47 +02:00
|
|
|
public void doDrawSegment() {
|
2017-09-23 23:23:26 +02:00
|
|
|
final boolean textureChange = precTexEnabled != currentTexEnabled || precTex != currentTex;
|
2018-09-22 11:17:30 +02:00
|
|
|
final boolean changeRequired = fbElements >= JOGLRenderer.ELEMENTS_MAX_COUNT_PER_BUFFER;
|
2017-09-23 23:23:26 +02:00
|
|
|
if (textureChange) {
|
2018-03-29 00:30:47 +02:00
|
|
|
if (fbElements > 0) {
|
|
|
|
endDrawSegment();
|
|
|
|
startDrawSegment(false);
|
2017-09-23 23:23:26 +02:00
|
|
|
}
|
2018-03-29 00:30:47 +02:00
|
|
|
changeTexture();
|
2018-09-22 11:17:30 +02:00
|
|
|
} else if (fbElements > 0 && changeRequired) {
|
|
|
|
endDrawSegment();
|
|
|
|
startDrawSegment(true);
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
2017-09-23 23:23:26 +02:00
|
|
|
}
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2018-03-29 01:05:45 +02:00
|
|
|
boolean firstBufferDataCall = true;
|
|
|
|
boolean firstBufferTexDataCall = true;
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2018-03-29 00:30:47 +02:00
|
|
|
public void endDrawSegment() {
|
2018-03-29 01:05:45 +02:00
|
|
|
fbVertices.flip();
|
|
|
|
fbTextures.flip();
|
|
|
|
fbColors.flip();
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2018-03-29 01:05:45 +02:00
|
|
|
// gl.glVertexPointer(vertSize, GL.GL_FLOAT, 0, fbVertices);
|
2018-09-22 11:17:30 +02:00
|
|
|
JOGLRenderer.gl.glBindBuffer(GL.GL_ARRAY_BUFFER, handlers[JOGLRenderer.vertBuffer]);
|
|
|
|
if (firstBufferTexDataCall)
|
|
|
|
JOGLRenderer.gl.glBufferData(GL.GL_ARRAY_BUFFER, fbVertices.limit() * Buffers.SIZEOF_FLOAT, fbVertices, GL.GL_STATIC_DRAW);
|
|
|
|
else
|
|
|
|
JOGLRenderer.gl.glBufferSubData(GL.GL_ARRAY_BUFFER, 0, fbVertices.limit() * Buffers.SIZEOF_FLOAT, fbVertices);
|
|
|
|
JOGLRenderer.gl.glVertexPointer(JOGLRenderer.vertSize, GL.GL_FLOAT, 0, 0l);
|
2018-03-29 01:05:45 +02:00
|
|
|
// gl.glTexCoordPointer(texSize, GL.GL_FLOAT, 0, fbTextures);
|
2018-09-22 11:17:30 +02:00
|
|
|
JOGLRenderer.gl.glBindBuffer(GL.GL_ARRAY_BUFFER, handlers[JOGLRenderer.texBuffer]);
|
|
|
|
if (firstBufferTexDataCall)
|
|
|
|
JOGLRenderer.gl.glBufferData(GL.GL_ARRAY_BUFFER, fbTextures.limit() * Buffers.SIZEOF_FLOAT, fbTextures, GL.GL_STATIC_DRAW);
|
|
|
|
else
|
|
|
|
JOGLRenderer.gl.glBufferSubData(GL.GL_ARRAY_BUFFER, 0, fbTextures.limit() * Buffers.SIZEOF_FLOAT, fbTextures);
|
|
|
|
JOGLRenderer.gl.glTexCoordPointer(JOGLRenderer.texSize, GL.GL_FLOAT, 0, 0l);
|
2018-03-29 01:05:45 +02:00
|
|
|
// gl.glColorPointer(colSize, GL.GL_FLOAT, 0, fbColors);
|
2018-09-22 11:17:30 +02:00
|
|
|
JOGLRenderer.gl.glBindBuffer(GL.GL_ARRAY_BUFFER, handlers[JOGLRenderer.colBuffer]);
|
|
|
|
if (firstBufferTexDataCall)
|
|
|
|
JOGLRenderer.gl.glBufferData(GL.GL_ARRAY_BUFFER, fbColors.limit() * Buffers.SIZEOF_FLOAT, fbColors, GL.GL_STATIC_DRAW);
|
|
|
|
else
|
|
|
|
JOGLRenderer.gl.glBufferSubData(GL.GL_ARRAY_BUFFER, 0, fbColors.limit() * Buffers.SIZEOF_FLOAT, fbColors);
|
|
|
|
JOGLRenderer.gl.glColorPointer(JOGLRenderer.colSize, GL.GL_FLOAT, 0, 0l);
|
|
|
|
|
|
|
|
fbVertices.limit(JOGLRenderer.vertMax);
|
|
|
|
fbTextures.limit(JOGLRenderer.texMax);
|
|
|
|
fbColors.limit(JOGLRenderer.colMax);
|
|
|
|
JOGLRenderer.gl.glDrawArrays(GL.GL_TRIANGLES, 0, fbElements * JOGLRenderer.ELEMENT_VERTICES_COUNT);
|
2017-09-23 23:23:26 +02:00
|
|
|
//gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, fbElements * ELEMENT_VERTICES_COUNT);
|
2018-03-29 01:05:45 +02:00
|
|
|
firstBufferDataCall = false;
|
|
|
|
firstBufferTexDataCall = false;
|
2017-09-23 23:23:26 +02:00
|
|
|
cycleEnded = true;
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-04-09 22:01:03 +02:00
|
|
|
// deleteBuffer(fbVertices);
|
|
|
|
// deleteBuffer(txVertices);
|
|
|
|
// deleteBuffer(colVertices);
|
|
|
|
// fbVertices = null;
|
|
|
|
// txVertices = null;
|
|
|
|
// colVertices = null;
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
2017-09-23 23:23:26 +02:00
|
|
|
@Override
|
|
|
|
public void glClearSkin() {
|
|
|
|
if (currentTex != null) {
|
|
|
|
currentTex = null;
|
2018-03-29 00:30:47 +02:00
|
|
|
doDrawSegment();
|
2017-09-23 23:23:26 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-01-31 22:29:49 +01:00
|
|
|
void disableTexture() {
|
|
|
|
currentTexEnabled = false;
|
2018-03-29 00:30:47 +02:00
|
|
|
doDrawSegment();
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void enableTexture() {
|
|
|
|
currentTexEnabled = true;
|
2018-03-29 00:30:47 +02:00
|
|
|
doDrawSegment();
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
void useTexture(final Texture t, final float w, final float h) {
|
2017-01-31 22:29:49 +01:00
|
|
|
currentTex = t;
|
|
|
|
currentTexWidth = w;
|
|
|
|
currentTexHeight = h;
|
2017-09-23 23:23:26 +02:00
|
|
|
enableTexture();
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
2018-03-29 00:30:47 +02:00
|
|
|
}
|