diff options
author | olamedia <[email protected]> | 2012-12-23 11:30:36 +0600 |
---|---|---|
committer | olamedia <[email protected]> | 2012-12-23 11:30:36 +0600 |
commit | f58bdfcb66353bb77213cab580bc49ef890417ad (patch) | |
tree | 2983a05d564891e92c115a679f9bfbf55465c755 /src/ru/olamedia/asset | |
parent | 5320fd1dad5b77fa227e83fbbe0a958f2c5fc283 (diff) |
0.1.7
Diffstat (limited to 'src/ru/olamedia/asset')
-rw-r--r-- | src/ru/olamedia/asset/Shader.java | 40 | ||||
-rw-r--r-- | src/ru/olamedia/asset/Sprite.java | 115 | ||||
-rw-r--r-- | src/ru/olamedia/asset/SpriteOffset.java | 10 | ||||
-rw-r--r-- | src/ru/olamedia/asset/SpriteRectangle.java | 11 | ||||
-rw-r--r-- | src/ru/olamedia/asset/shader/block.fp | 28 | ||||
-rw-r--r-- | src/ru/olamedia/asset/shader/block.vp | 25 |
6 files changed, 229 insertions, 0 deletions
diff --git a/src/ru/olamedia/asset/Shader.java b/src/ru/olamedia/asset/Shader.java new file mode 100644 index 0000000..8fd9ada --- /dev/null +++ b/src/ru/olamedia/asset/Shader.java @@ -0,0 +1,40 @@ +package ru.olamedia.asset; + +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GLContext; + +import com.jogamp.opengl.util.glsl.ShaderCode; +import com.jogamp.opengl.util.glsl.ShaderProgram; +import com.jogamp.opengl.util.glsl.ShaderState; + +public class Shader { + private ShaderState state; + + public ShaderState getState() { + return state; + } + + public void compile() { + GL2ES2 gl = GLContext.getCurrentGL().getGL2ES2(); + state = new ShaderState(); + state.setVerbose(true); + final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader", "shader/bin", + "block", true); + final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", + "shader/bin", "block", true); + final ShaderProgram sp0 = new ShaderProgram(); + sp0.add(gl, vp0, System.err); + sp0.add(gl, fp0, System.err); + state.attachShaderProgram(gl, sp0, true); + } + + public void enable() { + GL2ES2 gl = GLContext.getCurrentGL().getGL2ES2(); + state.useProgram(gl, true); + } + + public void disable() { + GL2ES2 gl = GLContext.getCurrentGL().getGL2ES2(); + state.useProgram(gl, false); + } +} diff --git a/src/ru/olamedia/asset/Sprite.java b/src/ru/olamedia/asset/Sprite.java new file mode 100644 index 0000000..84839d2 --- /dev/null +++ b/src/ru/olamedia/asset/Sprite.java @@ -0,0 +1,115 @@ +package ru.olamedia.asset; + +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +public class Sprite { + private BufferedImage combinedImage; + Graphics g; + int width; + int height; + int w; + int h; + int sizeX; + int sizeY; + int size; + int length = 0; + SpriteOffset current; + public void dispose(){ + if (null != combinedImage){ + combinedImage = null; + } + if (null != g){ + g.dispose(); + } + } + + int waiting = 0; + + public BufferedImage getImage() { + return combinedImage; + } + + public Sprite(int width, int height, int w, int h) { + this.width = width; + this.height = height; + this.w = w; + this.h = h; + sizeX = width / w; + sizeY = height / h; + size = sizeY * sizeX; + current = new SpriteOffset(0, 0); + combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + g = combinedImage.getGraphics(); + /*g.setColor(Color.darkGray); + g.setPaintMode(); + g.fillRect(0, 0, width, height);*/ + } + + public void next() { + current.x += w; + if (current.x + w > width) { + current.x = 0; + current.y += h; + } + } + + public SpriteRectangle addImage(String path) throws AssetNotFoundException { + final Asset asset = AssetManager.getAsset(path); + BufferedImage image; + try { + image = ImageIO.read(asset.getInputStream()); + return addImage(image); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public SpriteRectangle addImage(BufferedImage img) { + if (current.y + h > height) { + throw new RuntimeException("Out of bounds while creating Sprite"); + } + final int x = (int) current.x; + final int y = (int) current.y; + waiting++; + // img.getWidth(new ImageObserver() { + // @Override + // public boolean imageUpdate(Image img1, int arg1, int arg2, int arg3, + // int arg4, int arg5) { + //System.out.println("Sprite: draw " + img.getWidth(null)); + /*g.setColor(Color.magenta); + g.fillRect(x, y, w, h);*/ + g.drawImage(img, x, y, w, h, null, null); + waiting--; + // return false; + // } + // }); + SpriteRectangle area = new SpriteRectangle((1 / (float) width) * current.x, (1 / (float) height) * current.y, + (1 / (float) width) * (current.x + 16), (1 / (float) height) * (current.y + 16)); + next(); + return area; + } + + public void write(String path) { + /* + * while (waiting > 0) { + * try { + * Thread.sleep(100); + * } catch (InterruptedException e) { + * e.printStackTrace(); + * } + * } + */ + g.dispose(); + try { + ImageIO.write(combinedImage, "PNG", new File(path)); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/ru/olamedia/asset/SpriteOffset.java b/src/ru/olamedia/asset/SpriteOffset.java new file mode 100644 index 0000000..6720daa --- /dev/null +++ b/src/ru/olamedia/asset/SpriteOffset.java @@ -0,0 +1,10 @@ +package ru.olamedia.asset; + +public class SpriteOffset { + public float x; + public float y; + public SpriteOffset(float x, float y) { + this.x = x; + this.y = y; + } +} diff --git a/src/ru/olamedia/asset/SpriteRectangle.java b/src/ru/olamedia/asset/SpriteRectangle.java new file mode 100644 index 0000000..401d6aa --- /dev/null +++ b/src/ru/olamedia/asset/SpriteRectangle.java @@ -0,0 +1,11 @@ +package ru.olamedia.asset; + +public class SpriteRectangle { + public SpriteOffset topLeft; + public SpriteOffset bottomRight; + + public SpriteRectangle(float left, float top, float right, float bottom) { + topLeft = new SpriteOffset(left, top); + bottomRight = new SpriteOffset(right, bottom); + } +} diff --git a/src/ru/olamedia/asset/shader/block.fp b/src/ru/olamedia/asset/shader/block.fp new file mode 100644 index 0000000..291e975 --- /dev/null +++ b/src/ru/olamedia/asset/shader/block.fp @@ -0,0 +1,28 @@ + + +smooth in vec2 texCoord; +//vec4 frontColor; + +uniform sampler2D mesh_ActiveTexture; + +vec4 frontColor = vec4(1.0); +const vec4 texEnvColor = vec4(0.0); + +const vec4 zerov4 = vec4(0.0); +const vec4 onev4 = vec4(1.0); + +vec4 calcTexColor(in vec4 color, in vec4 texColor) { + color.rgb = mix(color.rgb, texEnvColor.rgb, texColor.rgb); + color.a *= texColor.a; + color = clamp(color, zerov4, onev4); + return color; +} + +void main (void) +{ + vec4 texColor; + texColor = texture2D(mesh_ActiveTexture, texCoord.st); + if (texColor.a < 0.1f) discard; + vec4 color = calcTexColor(frontColor, texColor); + gl_FragColor = vec4(gl_FragColor.a) * gl_FragColor + vec4(1.0 - gl_FragColor.a) * texColor; +}
\ No newline at end of file diff --git a/src/ru/olamedia/asset/shader/block.vp b/src/ru/olamedia/asset/shader/block.vp new file mode 100644 index 0000000..989d182 --- /dev/null +++ b/src/ru/olamedia/asset/shader/block.vp @@ -0,0 +1,25 @@ +//precision lowp float; +//precision lowp int; +uniform mat4 pmvMatrix[4]; // P, Mv, Mvi and Mvit +uniform vec4 sunColor; +uniform sampler2D mesh_ActiveTexture; + +attribute vec4 mesh_vertices; +attribute vec4 mesh_colors; + +attribute vec2 mesh_texCoord; + +invariant out vec4 position; +vec4 color; +smooth out vec2 texCoord; + +void main(void) +{ + // Transforming The Vertex Position To ModelView-Space + position = pmvMatrix[1] * mesh_vertices; // vertex eye position + texCoord = mesh_texCoord; + + gl_Position = pmvMatrix[0] * position; + + color = mesh_colors * sunColor; +} |