From 48201a6ea6471eb5951edb735b36156ab3410a15 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 23 Apr 2011 06:12:10 +0200 Subject: Refactored graph: Reduce/remove data copy/recreation; Shader cleanup - Pass the current GL context object where it's required - Introduce RenderState (which has ShaderState) to acquire/change shader related data (Region) - Shader Cleanup: User import for common stuff; use req. version - Reduce/remove data copy/recreation in *Region implementation - UI/RIButton: Use defaults I like :) --- .../graph/curve/opengl/RegionRendererImpl01.java | 129 ++---------- .../jogamp/graph/curve/opengl/RenderStateImpl.java | 126 ++++++++++++ .../graph/curve/opengl/TextRendererImpl01.java | 130 ++---------- .../jogamp/graph/curve/opengl/VBORegion2PES2.java | 220 +++++++++++---------- .../jogamp/graph/curve/opengl/VBORegionSPES2.java | 95 +++++---- .../graph/curve/opengl/shader/AttributeNames.java | 18 ++ .../graph/curve/opengl/shader/UniformNames.java | 10 + .../graph/curve/opengl/shader/attributes.glsl | 12 ++ .../jogamp/graph/curve/opengl/shader/consts.glsl | 10 + .../graph/curve/opengl/shader/curverenderer01.fp | 116 +++++------ .../graph/curve/opengl/shader/curverenderer01.vp | 20 +- .../graph/curve/opengl/shader/precision.glsl | 14 ++ .../jogamp/graph/curve/opengl/shader/uniforms.glsl | 21 ++ .../jogamp/graph/curve/opengl/shader/varyings.glsl | 13 ++ .../jogamp/graph/curve/text/GlyphString.java | 36 ++-- 15 files changed, 519 insertions(+), 451 deletions(-) create mode 100644 src/jogl/classes/jogamp/graph/curve/opengl/RenderStateImpl.java create mode 100644 src/jogl/classes/jogamp/graph/curve/opengl/shader/AttributeNames.java create mode 100644 src/jogl/classes/jogamp/graph/curve/opengl/shader/UniformNames.java create mode 100644 src/jogl/classes/jogamp/graph/curve/opengl/shader/attributes.glsl create mode 100644 src/jogl/classes/jogamp/graph/curve/opengl/shader/consts.glsl create mode 100644 src/jogl/classes/jogamp/graph/curve/opengl/shader/precision.glsl create mode 100644 src/jogl/classes/jogamp/graph/curve/opengl/shader/uniforms.glsl create mode 100644 src/jogl/classes/jogamp/graph/curve/opengl/shader/varyings.glsl (limited to 'src/jogl/classes/jogamp') diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/RegionRendererImpl01.java b/src/jogl/classes/jogamp/graph/curve/opengl/RegionRendererImpl01.java index 683a72d96..4dcc4560e 100755 --- a/src/jogl/classes/jogamp/graph/curve/opengl/RegionRendererImpl01.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/RegionRendererImpl01.java @@ -27,53 +27,29 @@ */ package jogamp.graph.curve.opengl; -import java.nio.FloatBuffer; import javax.media.opengl.GL2ES2; import javax.media.opengl.GLException; -import javax.media.opengl.GLUniformData; -import javax.media.opengl.fixedfunc.GLMatrixFunc; + +import jogamp.graph.curve.opengl.shader.AttributeNames; import com.jogamp.graph.curve.OutlineShape; import com.jogamp.graph.curve.Region; import com.jogamp.graph.curve.opengl.RegionRenderer; -import com.jogamp.graph.geom.Vertex; +import com.jogamp.graph.curve.opengl.RenderState; import com.jogamp.opengl.util.glsl.ShaderCode; import com.jogamp.opengl.util.glsl.ShaderProgram; import com.jogamp.opengl.util.glsl.ShaderState; - public class RegionRendererImpl01 extends RegionRenderer { - /**Sharpness is equivalent to the value of t value of texture coord - * on the off-curve vertex. The high value of sharpness will - * result in high curvature. - */ - private GLUniformData mgl_sharpness = new GLUniformData("p1y", 0.5f); - GLUniformData mgl_alpha = new GLUniformData("g_alpha", 1.0f); - private GLUniformData mgl_color = new GLUniformData("g_color", 3, FloatBuffer.allocate(3)); - private GLUniformData mgl_strength = new GLUniformData("a_strength", 3.0f); - - public RegionRendererImpl01(Vertex.Factory factory, int type) { - super(factory, type); + public RegionRendererImpl01(RenderState rs, int type) { + super(rs, type); + // rs.getSharpness().setData(0.5f); + // rs.getAlpha().setData(1.0f); + // rs.getStrength().setData(3.0f); } - protected boolean initImpl(GL2ES2 gl) { - boolean VBOsupported = gl.isFunctionAvailable("glGenBuffers") && - gl.isFunctionAvailable("glBindBuffer") && - gl.isFunctionAvailable("glBufferData") && - gl.isFunctionAvailable("glDrawElements") && - gl.isFunctionAvailable("glVertexAttribPointer") && - gl.isFunctionAvailable("glDeleteBuffers"); - - if(DEBUG) { - System.err.println("RegionRenderer: VBO Supported = " + VBOsupported); - } - - if(!VBOsupported){ - return false; - } - - gl.glEnable(GL2ES2.GL_BLEND); - gl.glBlendFunc(GL2ES2.GL_SRC_ALPHA, GL2ES2.GL_ONE_MINUS_SRC_ALPHA); + protected boolean initShaderProgram(GL2ES2 gl) { + final ShaderState st = rs.getShaderState(); ShaderCode rsVp = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, 1, RegionRendererImpl01.class, "shader", "shader/bin", "curverenderer01"); @@ -85,60 +61,15 @@ public class RegionRendererImpl01 extends RegionRenderer { sp.add(rsFp); sp.init(gl); - gl.glBindAttribLocation(sp.program(), Region.VERTEX_ATTR_IDX, Region.VERTEX_ATTR_NAME); - gl.glBindAttribLocation(sp.program(), Region.TEXCOORD_ATTR_IDX, Region.TEXCOORD_ATTR_NAME); + st.attachShaderProgram(gl, sp); + st.glBindAttribLocation(gl, AttributeNames.VERTEX_ATTR_IDX, AttributeNames.VERTEX_ATTR_NAME); + st.glBindAttribLocation(gl, AttributeNames.TEXCOORD_ATTR_IDX, AttributeNames.TEXCOORD_ATTR_NAME); if(!sp.link(gl, System.err)) { throw new GLException("RegionRenderer: Couldn't link program: "+sp); - } - - st = new ShaderState(); - st.attachShaderProgram(gl, sp); - + } st.glUseProgram(gl, true); - pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - pmvMatrix.glLoadIdentity(); - pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); - pmvMatrix.glLoadIdentity(); - - pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - pmvMatrix.glLoadIdentity(); - resetModelview(null); - - mgl_PMVMatrix = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); - if(!st.glUniform(gl, mgl_PMVMatrix)) { - if(DEBUG){ - System.err.println("Error setting PMVMatrix in shader: "+st); - } - return false; - } - - if(!st.glUniform(gl, mgl_sharpness)) { - if(DEBUG){ - System.err.println("Error setting sharpness in shader: "+st); - } - return false; - } - - if(!st.glUniform(gl, mgl_alpha)) { - if(DEBUG){ - System.err.println("Error setting global alpha in shader: "+st); - } - return false; - } - - if(!st.glUniform(gl, mgl_color)) { - if(DEBUG){ - System.err.println("Error setting global color in shader: "+st); - } - return false; - } - - if(!st.glUniform(gl, mgl_strength)) { - System.err.println("Error setting antialias strength in shader: "+st); - } - if(DEBUG) { System.err.println("RegionRendererImpl01 initialized: " + Thread.currentThread()+" "+st); } @@ -150,30 +81,6 @@ public class RegionRendererImpl01 extends RegionRenderer { super.disposeImpl(gl); } - @Override - public float getAlpha() { - return mgl_alpha.floatValue(); - } - - @Override - public void setAlpha(GL2ES2 gl, float alpha_t) { - mgl_alpha.setData(alpha_t); - if(null != gl && st.inUse()) { - st.glUniform(gl, mgl_alpha); - } - } - - @Override - public void setColor(GL2ES2 gl, float r, float g, float b){ - FloatBuffer fb = (FloatBuffer) mgl_color.getBuffer(); - fb.put(0, r); - fb.put(1, r); - fb.put(2, r); - if(null != gl && st.inUse()) { - st.glUniform(gl, mgl_color); - } - } - @Override public void renderOutlineShape(GL2ES2 gl, OutlineShape outlineShape, float[] position, int texSize) { @@ -184,10 +91,10 @@ public class RegionRendererImpl01 extends RegionRenderer { Region region = regions.get(hashCode); if(null == region) { - region = createRegion(gl, outlineShape, mgl_sharpness.floatValue()); + region = createRegion(gl, outlineShape); regions.put(hashCode, region); } - region.render(pmvMatrix, vp_width, vp_height, texSize); + region.render(gl, rs, vp_width, vp_height, texSize); } @Override @@ -200,9 +107,9 @@ public class RegionRendererImpl01 extends RegionRenderer { Region region = regions.get(hashCode); if(null == region) { - region = createRegion(gl, outlineShapes, mgl_sharpness.floatValue()); + region = createRegion(gl, outlineShapes); regions.put(hashCode, region); } - region.render(pmvMatrix, vp_width, vp_height, texSize); + region.render(gl, rs, vp_width, vp_height, texSize); } } diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/RenderStateImpl.java b/src/jogl/classes/jogamp/graph/curve/opengl/RenderStateImpl.java new file mode 100644 index 000000000..4c4a325ae --- /dev/null +++ b/src/jogl/classes/jogamp/graph/curve/opengl/RenderStateImpl.java @@ -0,0 +1,126 @@ +/** + * Copyright 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package jogamp.graph.curve.opengl; + +import java.nio.FloatBuffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GLProfile; +import javax.media.opengl.GLUniformData; +import javax.media.opengl.fixedfunc.GLMatrixFunc; + +import jogamp.graph.curve.opengl.shader.UniformNames; + +import com.jogamp.common.os.Platform; +import com.jogamp.common.util.VersionUtil; +import com.jogamp.graph.curve.opengl.RenderState; +import com.jogamp.graph.geom.Vertex; +import com.jogamp.opengl.util.PMVMatrix; +import com.jogamp.opengl.util.glsl.ShaderState; + +public class RenderStateImpl implements RenderState { + + public final ShaderState st; + public final Vertex.Factory pointFactory; + public final PMVMatrix pmvMatrix; + public final GLUniformData mgl_PMVMatrix; + + /** + * Sharpness is equivalent to the texture-coord component t + * on the off-curve vertex. Higher values of sharpness will + * result in higher curvature. + */ + public final GLUniformData mgl_sharpness; + public final GLUniformData mgl_alpha; + public final GLUniformData mgl_colorStatic; + public final GLUniformData mgl_strength; + + public static final RenderState getRenderState(GL gl) { + return (RenderState) gl.getContext().getAttachedObject(RenderState.class.getName()); + } + + public RenderStateImpl(ShaderState st, Vertex.Factory pointFactory, PMVMatrix pmvMatrix) { + this.st = st; + this.pointFactory = pointFactory; + this.pmvMatrix = pmvMatrix; + this.mgl_PMVMatrix = new GLUniformData(UniformNames.gcu_PMVMatrix, 4, 4, pmvMatrix.glGetPMvMatrixf()); + + mgl_sharpness = new GLUniformData(UniformNames.gcu_P1Y, 0.5f); + mgl_alpha = new GLUniformData(UniformNames.gcu_Alpha, 1.0f); + mgl_colorStatic = new GLUniformData(UniformNames.gcu_ColorStatic, 3, FloatBuffer.allocate(3)); + mgl_strength = new GLUniformData(UniformNames.gcu_Strength, 3.0f); + } + + public RenderStateImpl(ShaderState st, Vertex.Factory pointFactory) { + this(st, pointFactory, new PMVMatrix()); + + pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + } + + public final ShaderState getShaderState() { return st; } + public final Vertex.Factory getPointFactory () { return pointFactory; } + public final PMVMatrix getPMVMatrix() { return pmvMatrix; } + public final GLUniformData getPMVMatrixUniform() { return mgl_PMVMatrix; } + public final GLUniformData getSharpness() { return mgl_sharpness; } + public final GLUniformData getAlpha() { return mgl_alpha; } + public final GLUniformData getColorStatic() { return mgl_colorStatic; } + public final GLUniformData getStrength() { return mgl_strength; } + + public final RenderState attachTo(GL gl) { + return (RenderState) gl.getContext().attachObject(RenderState.class.getName(), this); + } + public final boolean detachFrom(GL gl) { + RenderState _rs = (RenderState) gl.getContext().getAttachedObject(RenderState.class.getName()); + if(_rs == this) { + gl.getContext().detachObject(RenderState.class.getName()); + return true; + } + return false; + } + + public StringBuilder toString(StringBuilder sb) { + if(null==sb) { + sb = new StringBuilder(); + } + + sb.append("RenderState["); + st.toString(sb).append(Platform.getNewline()); + sb.append("]"); + + return sb; + } + + public String toString() { + return toString(null).toString(); + } +} diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/TextRendererImpl01.java b/src/jogl/classes/jogamp/graph/curve/opengl/TextRendererImpl01.java index bc94ab180..3bddaed3b 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/TextRendererImpl01.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/TextRendererImpl01.java @@ -27,119 +27,51 @@ */ package jogamp.graph.curve.opengl; -import java.nio.FloatBuffer; - import javax.media.opengl.GL2ES2; import javax.media.opengl.GLException; -import javax.media.opengl.GLUniformData; -import javax.media.opengl.fixedfunc.GLMatrixFunc; +import jogamp.graph.curve.opengl.shader.AttributeNames; import jogamp.graph.curve.text.GlyphString; -import com.jogamp.graph.curve.Region; +import com.jogamp.graph.curve.opengl.RenderState; import com.jogamp.graph.curve.opengl.TextRenderer; import com.jogamp.graph.font.Font; -import com.jogamp.graph.geom.Vertex; import com.jogamp.opengl.util.glsl.ShaderCode; import com.jogamp.opengl.util.glsl.ShaderProgram; +import com.jogamp.opengl.util.glsl.ShaderState; public class TextRendererImpl01 extends TextRenderer { - /**Sharpness is equivalent to the value of t value of texture coord - * on the off-curve vertex. The high value of sharpness will - * result in high curvature. - */ - private GLUniformData mgl_sharpness = new GLUniformData("p1y", 0.5f); - GLUniformData mgl_alpha = new GLUniformData("g_alpha", 1.0f); - private GLUniformData mgl_color = new GLUniformData("g_color", 3, FloatBuffer.allocate(3)); - private GLUniformData mgl_strength = new GLUniformData("a_strength", 1.8f); - - public TextRendererImpl01(Vertex.Factory factory, int type) { - super(factory, type); + public TextRendererImpl01(RenderState rs, int type) { + super(rs, type); + // rs.getSharpness().setData(0.5f); + // rs.getAlpha().setData(1.0f); + // rs.getStrength().setData(3.0f); + rs.getStrength().setData(1.9f); } - + @Override - protected boolean initImpl(GL2ES2 gl){ - boolean VBOsupported = gl.isFunctionAvailable("glGenBuffers") && - gl.isFunctionAvailable("glBindBuffer") && - gl.isFunctionAvailable("glBufferData") && - gl.isFunctionAvailable("glDrawElements") && - gl.isFunctionAvailable("glVertexAttribPointer") && - gl.isFunctionAvailable("glDeleteBuffers"); - - if(DEBUG) { - System.err.println("TextRendererImpl01: VBO Supported = " + VBOsupported); - } - - if(!VBOsupported){ - return false; - } - - gl.glEnable(GL2ES2.GL_BLEND); - gl.glBlendFunc(GL2ES2.GL_SRC_ALPHA, GL2ES2.GL_ONE_MINUS_SRC_ALPHA); + protected boolean initShaderProgram(GL2ES2 gl){ + final ShaderState st = rs.getShaderState(); ShaderCode rsVp = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, 1, TextRendererImpl01.class, "shader", "shader/bin", "curverenderer01"); ShaderCode rsFp = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, 1, TextRendererImpl01.class, "shader", "shader/bin", "curverenderer01"); - + ShaderProgram sp = new ShaderProgram(); sp.add(rsVp); sp.add(rsFp); sp.init(gl); - gl.glBindAttribLocation(sp.program(), Region.VERTEX_ATTR_IDX, Region.VERTEX_ATTR_NAME); - gl.glBindAttribLocation(sp.program(), Region.TEXCOORD_ATTR_IDX, Region.TEXCOORD_ATTR_NAME); + st.attachShaderProgram(gl, sp); + st.glBindAttribLocation(gl, AttributeNames.VERTEX_ATTR_IDX, AttributeNames.VERTEX_ATTR_NAME); + st.glBindAttribLocation(gl, AttributeNames.TEXCOORD_ATTR_IDX, AttributeNames.TEXCOORD_ATTR_NAME); if(!sp.link(gl, System.err)) { throw new GLException("TextRendererImpl01: Couldn't link program: "+sp); } - - st.attachShaderProgram(gl, sp); - st.glUseProgram(gl, true); - pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - pmvMatrix.glLoadIdentity(); - pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); - pmvMatrix.glLoadIdentity(); - - pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - pmvMatrix.glLoadIdentity(); - resetModelview(null); - - mgl_PMVMatrix = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); - if(!st.glUniform(gl, mgl_PMVMatrix)) { - if(DEBUG){ - System.err.println("Error setting PMVMatrix in shader: "+st); - } - return false; - } - - if(!st.glUniform(gl, mgl_sharpness)) { - if(DEBUG){ - System.err.println("Error setting sharpness in shader: "+st); - } - return false; - } - - if(!st.glUniform(gl, mgl_alpha)) { - if(DEBUG){ - System.err.println("Error setting global alpha in shader: "+st); - } - return false; - } - - if(!st.glUniform(gl, mgl_color)) { - if(DEBUG){ - System.err.println("Error setting global color in shader: "+st); - } - return false; - } - - if(!st.glUniform(gl, mgl_strength)) { - System.err.println("Error setting antialias strength in shader: "+st); - } - if(DEBUG) { System.err.println("TextRendererImpl01 initialized: " + Thread.currentThread()+" "+st); } @@ -151,30 +83,6 @@ public class TextRendererImpl01 extends TextRenderer { super.disposeImpl(gl); } - @Override - public float getAlpha() { - return mgl_alpha.floatValue(); - } - - @Override - public void setAlpha(GL2ES2 gl, float alpha_t) { - mgl_alpha.setData(alpha_t); - if(null != gl && st.inUse()) { - st.glUniform(gl, mgl_alpha); - } - } - - @Override - public void setColor(GL2ES2 gl, float r, float g, float b){ - FloatBuffer fb = (FloatBuffer) mgl_color.getBuffer(); - fb.put(0, r); - fb.put(1, r); - fb.put(2, r); - if(null != gl && st.inUse()) { - st.glUniform(gl, mgl_color); - } - } - @Override public void renderString3D(GL2ES2 gl, Font font, String str, float[] position, int fontSize, int texSize) { if(!isInitialized()){ @@ -182,11 +90,11 @@ public class TextRendererImpl01 extends TextRenderer { } GlyphString glyphString = getCachedGlyphString(font, str, fontSize); if(null == glyphString) { - glyphString = createString(gl, font, fontSize, str, mgl_sharpness.floatValue()); - addCachedGlyphString(font, str, fontSize, glyphString); + glyphString = createString(gl, font, fontSize, str); + addCachedGlyphString(gl, font, str, fontSize, glyphString); } - glyphString.renderString3D(pmvMatrix, vp_width, vp_height, texSize); + glyphString.renderString3D(gl, rs, vp_width, vp_height, texSize); } } diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java index 05814965e..181ce77b0 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java @@ -32,15 +32,18 @@ import java.util.ArrayList; import javax.media.opengl.GL2ES2; // FIXME: Subsume GL2GL3.GL_DRAW_FRAMEBUFFER -> GL2ES2.GL_DRAW_FRAMEBUFFER ! import javax.media.opengl.GL; -import javax.media.opengl.GLContext; import javax.media.opengl.GLUniformData; import javax.media.opengl.fixedfunc.GLMatrixFunc; +import jogamp.graph.curve.opengl.shader.AttributeNames; +import jogamp.graph.curve.opengl.shader.UniformNames; + import com.jogamp.graph.geom.AABBox; import com.jogamp.graph.geom.Triangle; import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.curve.Region; +import com.jogamp.graph.curve.opengl.RenderState; import com.jogamp.opengl.util.FBObject; import com.jogamp.opengl.util.GLArrayDataServer; import com.jogamp.opengl.util.PMVMatrix; @@ -51,74 +54,82 @@ public class VBORegion2PES2 implements Region { private ArrayList triangles = new ArrayList(); private ArrayList vertices = new ArrayList(); - private GLArrayDataServer verticeTxtAttr = null; - private GLArrayDataServer texCoordTxtAttr = null; - private GLArrayDataServer indicesTxt = null; - private GLArrayDataServer verticeFboAttr = null; - private GLArrayDataServer texCoordFboAttr = null; - private GLArrayDataServer indicesFbo = null; - - private GLContext context; + private GLArrayDataServer verticeTxtAttr; + private GLArrayDataServer texCoordTxtAttr; + private GLArrayDataServer indicesTxt; + private GLArrayDataServer verticeFboAttr; + private GLArrayDataServer texCoordFboAttr; + private GLArrayDataServer indicesFbo; private boolean flipped = false; - private boolean dirty = false; + private boolean dirty = true; - private AABBox box = null; - private FBObject fbo = null; + private AABBox box; + private FBObject fbo; + private PMVMatrix fboPMVMatrix; + GLUniformData mgl_fboPMVMatrix; + private int tex_width_c = 0; private int tex_height_c = 0; + GLUniformData mgl_ActiveTexture; + int activeTexture; // texture engine 0 == GL.GL_TEXTURE0 - private ShaderState st; - - public VBORegion2PES2(GLContext context, ShaderState st){ - this.context =context; - this.st = st; + public VBORegion2PES2(RenderState rs, int textureEngine) { + fboPMVMatrix = new PMVMatrix(); + mgl_fboPMVMatrix = new GLUniformData(UniformNames.gcu_PMVMatrix, 4, 4, fboPMVMatrix.glGetPMvMatrixf()); - GL2ES2 gl = context.getGL().getGL2ES2(); + activeTexture = GL.GL_TEXTURE0 + textureEngine; + mgl_ActiveTexture = new GLUniformData(UniformNames.gcu_TextureUnit, textureEngine); - indicesFbo = GLArrayDataServer.createGLSL(gl, null, 3, GL2ES2.GL_SHORT, false, - 2, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + final int initialSize = 256; + final ShaderState st = rs.getShaderState(); + + indicesFbo = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, initialSize, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); indicesFbo.puts((short) 0); indicesFbo.puts((short) 1); indicesFbo.puts((short) 3); indicesFbo.puts((short) 1); indicesFbo.puts((short) 2); indicesFbo.puts((short) 3); - indicesFbo.seal(gl, true); + indicesFbo.seal(true); - texCoordFboAttr = GLArrayDataServer.createGLSL(gl, Region.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT, false, - 4, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - texCoordFboAttr.setLocation(Region.TEXCOORD_ATTR_IDX); + texCoordFboAttr = GLArrayDataServer.createGLSL(st, AttributeNames.TEXCOORD_ATTR_NAME, 2, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(texCoordFboAttr); texCoordFboAttr.putf(5); texCoordFboAttr.putf(5); texCoordFboAttr.putf(5); texCoordFboAttr.putf(6); texCoordFboAttr.putf(6); texCoordFboAttr.putf(6); texCoordFboAttr.putf(6); texCoordFboAttr.putf(5); - texCoordFboAttr.seal(gl, true); + texCoordFboAttr.seal(true); - verticeFboAttr = GLArrayDataServer.createGLSL(gl, Region.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT, false, - 4, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - verticeFboAttr.setLocation(Region.VERTEX_ATTR_IDX); - verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); - verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); - verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); - verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); - verticeFboAttr.seal(gl, true); + verticeFboAttr = GLArrayDataServer.createGLSL(st, AttributeNames.VERTEX_ATTR_NAME, 3, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(verticeFboAttr); - verticeFboAttr.enableBuffer(gl, false); - texCoordFboAttr.enableBuffer(gl, false); - indicesFbo.enableBuffer(gl, false); - if(DEBUG) { + + box = new AABBox(); + + indicesTxt = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, initialSize, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + + verticeTxtAttr = GLArrayDataServer.createGLSL(st, AttributeNames.VERTEX_ATTR_NAME, 3, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(verticeTxtAttr); + + texCoordTxtAttr = GLArrayDataServer.createGLSL(st, AttributeNames.TEXCOORD_ATTR_NAME, 2, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(texCoordTxtAttr); + + if(DEBUG_INSTANCE) { System.err.println("VBORegion2PES2 Create: " + this); } } - public void update(){ - GL2ES2 gl = context.getGL().getGL2ES2(); - - destroyTxtAttr(gl); + public void update(GL2ES2 gl){ + if(!dirty) { + return; + } - box = new AABBox(); - - indicesTxt = GLArrayDataServer.createGLSL(gl, null, 3, GL2ES2.GL_SHORT, false, - triangles.size(), GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + // process triangles + indicesTxt.seal(gl, false); + indicesTxt.rewind(); for(Triangle t:triangles){ if(t.getVertices()[0].getId() == Integer.MAX_VALUE){ t.getVertices()[0].setId(numVertices++); @@ -144,10 +155,14 @@ public class VBORegion2PES2 implements Region { } } indicesTxt.seal(gl, true); + indicesTxt.enableBuffer(gl, false); - verticeTxtAttr = GLArrayDataServer.createGLSL(gl, Region.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT, false, - vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - verticeTxtAttr.setLocation(Region.VERTEX_ATTR_IDX); + // process vertices and update bbox + box.reset(); + verticeTxtAttr.seal(gl, false); + verticeTxtAttr.rewind(); + texCoordTxtAttr.seal(gl, false); + texCoordTxtAttr.rewind(); for(Vertex v:vertices){ verticeTxtAttr.putf(v.getX()); if(flipped){ @@ -161,52 +176,65 @@ public class VBORegion2PES2 implements Region { } else { box.resize(v.getX(), v.getY(), v.getZ()); } - } - verticeTxtAttr.seal(gl, true); - - texCoordTxtAttr = GLArrayDataServer.createGLSL(gl, Region.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT, false, - vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - texCoordTxtAttr.setLocation(Region.TEXCOORD_ATTR_IDX); - for(Vertex v:vertices){ - float[] tex = v.getTexCoord(); + + final float[] tex = v.getTexCoord(); texCoordTxtAttr.putf(tex[0]); - texCoordTxtAttr.putf(tex[1]); + texCoordTxtAttr.putf(tex[1]); } texCoordTxtAttr.seal(gl, true); - - // leave the buffers enabled for subsequent render call + texCoordTxtAttr.enableBuffer(gl, false); + verticeTxtAttr.seal(gl, true); + verticeTxtAttr.enableBuffer(gl, false); + + // update all bbox related data + verticeFboAttr.seal(gl, false); + verticeFboAttr.rewind(); + verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.seal(gl, true); + verticeFboAttr.enableBuffer(gl, false); + + fboPMVMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); + fboPMVMatrix.glLoadIdentity(); + fboPMVMatrix.glOrthof(box.getLow()[0], box.getHigh()[0], box.getLow()[1], box.getHigh()[1], -1, 1); + + // push data 2 GPU .. + indicesFbo.seal(gl, true); + indicesFbo.enableBuffer(gl, false); dirty = false; + + // the buffers were disabled, since due to real/fbo switching and other vbo usage } - public void render(PMVMatrix matrix, int vp_width, int vp_height, int width){ - GL2ES2 gl = context.getGL().getGL2ES2(); - if(null == matrix || vp_width <=0 || vp_height <= 0 || width <= 0){ + public void render(GL2ES2 gl, RenderState rs, int vp_width, int vp_height, int width) { + if(vp_width <=0 || vp_height <= 0 || width <= 0){ renderRegion(gl); } else { if(width != tex_width_c){ - renderRegion2FBO(gl, matrix, width); - setupBBox2FboAttr(gl); + renderRegion2FBO(gl, rs, width); } -// System.out.println("Scale: " + matrix.glGetMatrixf().get(1+4*3) +" " + matrix.glGetMatrixf().get(2+4*3)); - renderFBO(gl, matrix, vp_width, vp_height); + // System.out.println("Scale: " + matrix.glGetMatrixf().get(1+4*3) +" " + matrix.glGetMatrixf().get(2+4*3)); + renderFBO(gl, rs, vp_width, vp_height); } } - private void renderFBO(GL2ES2 gl, PMVMatrix matrix, int width, int hight) { - gl.glViewport(0, 0, width, hight); - if(!st.glUniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, matrix.glGetPMvMatrixf()))){ - System.out.println("Cnt set tex based mat"); - } - gl.glEnable(GL2ES2.GL_TEXTURE_2D); - gl.glActiveTexture(GL2ES2.GL_TEXTURE0); - fbo.use(gl); + private void renderFBO(GL2ES2 gl, RenderState rs, int width, int hight) { + final ShaderState st = rs.getShaderState(); - st.glUniform(gl, new GLUniformData("texture", fbo.getTextureName())); - int loc = gl.glGetUniformLocation(st.shaderProgram().program(), "texture"); - gl.glUniform1i(loc, 0); + gl.glViewport(0, 0, width, hight); + gl.glEnable(GL2ES2.GL_TEXTURE_2D); + /* setback: + int[] currentActiveTextureEngine = new int[1]; + gl.glGetIntegerv(GL.GL_ACTIVE_TEXTURE, currentActiveTextureEngine, 0); + */ + gl.glActiveTexture(activeTexture); + st.glUniform(gl, mgl_ActiveTexture); + fbo.use(gl); verticeFboAttr.enableBuffer(gl, true); texCoordFboAttr.enableBuffer(gl, true); indicesFbo.enableBuffer(gl, true); @@ -215,22 +243,15 @@ public class VBORegion2PES2 implements Region { verticeFboAttr.enableBuffer(gl, false); texCoordFboAttr.enableBuffer(gl, false); - indicesFbo.enableBuffer(gl, false); - } - - private void setupBBox2FboAttr(GL2ES2 gl){ - verticeFboAttr.seal(gl, false); - verticeFboAttr.rewind(); - - verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); - verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); - verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); - verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); + indicesFbo.enableBuffer(gl, false); + fbo.unuse(gl); - verticeFboAttr.seal(gl, true); + // setback: gl.glActiveTexture(currentActiveTextureEngine[0]); } - private void renderRegion2FBO(GL2ES2 gl, PMVMatrix m, int tex_width){ + private void renderRegion2FBO(GL2ES2 gl, RenderState rs, int tex_width) { + final ShaderState st = rs.getShaderState(); + tex_width_c = tex_width; tex_height_c = (int)(tex_width_c*box.getHeight()/box.getWidth()); @@ -253,21 +274,15 @@ public class VBORegion2PES2 implements Region { } //render texture - PMVMatrix tex_matrix = new PMVMatrix(); gl.glViewport(0, 0, tex_width_c, tex_height_c); - tex_matrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - tex_matrix.glLoadIdentity(); - tex_matrix.glOrthof(box.getLow()[0], box.getHigh()[0], box.getLow()[1], box.getHigh()[1], -1, 1); - - if(!st.glUniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, tex_matrix.glGetPMvMatrixf()))){ - System.out.println("Cnt set tex based mat"); - } + st.glUniform(gl, mgl_fboPMVMatrix); // use orthogonal matrix gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT | GL2ES2.GL_DEPTH_BUFFER_BIT); renderRegion(gl); - fbo.unbind(gl); + + st.glUniform(gl, rs.getPMVMatrixUniform()); // switch back to real PMV matrix } private void renderRegion(GL2ES2 gl) { @@ -301,11 +316,10 @@ public class VBORegion2PES2 implements Region { return dirty; } - public void destroy() { - if(DEBUG) { + public void destroy(GL2ES2 gl) { + if(DEBUG_INSTANCE) { System.err.println("VBORegion2PES2 Destroy: " + this); - } - GL2ES2 gl = context.getGL().getGL2ES2(); + } destroyFbo(gl); destroyTxtAttr(gl); destroyFboAttr(gl); diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java index 83cd6b80d..1b295de16 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java @@ -31,14 +31,16 @@ import java.util.ArrayList; import javax.media.opengl.GL; import javax.media.opengl.GL2ES2; -import javax.media.opengl.GLContext; + +import jogamp.graph.curve.opengl.shader.AttributeNames; import com.jogamp.graph.curve.Region; +import com.jogamp.graph.curve.opengl.RenderState; import com.jogamp.graph.geom.AABBox; import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.geom.Triangle; import com.jogamp.opengl.util.GLArrayDataServer; -import com.jogamp.opengl.util.PMVMatrix; +import com.jogamp.opengl.util.glsl.ShaderState; public class VBORegionSPES2 implements Region { private int numVertices = 0; @@ -49,25 +51,40 @@ public class VBORegionSPES2 implements Region { private GLArrayDataServer texCoordAttr = null; private GLArrayDataServer indices = null; - private GLContext context; - private boolean flipped = false; - private boolean dirty = false; + private boolean dirty = true; private AABBox box = null; - public VBORegionSPES2(GLContext context){ - this.context =context; + public VBORegionSPES2(RenderState rs){ + box = new AABBox(); + + final int initialSize = 256; + final ShaderState st = rs.getShaderState(); + + indices = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, initialSize, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + + verticeAttr = GLArrayDataServer.createGLSL(st, AttributeNames.VERTEX_ATTR_NAME, 3, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(verticeAttr); + + texCoordAttr = GLArrayDataServer.createGLSL(st, AttributeNames.TEXCOORD_ATTR_NAME, 2, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(texCoordAttr); + + if(DEBUG_INSTANCE) { + System.err.println("VBORegionSPES2 Create: " + this); + } } - public void update(){ - box = new AABBox(); - GL2ES2 gl = context.getGL().getGL2ES2(); + public void update(GL2ES2 gl){ + if(!dirty) { + return; + } - destroy(gl); - - indices = GLArrayDataServer.createGLSL(gl, null, 3, GL2ES2.GL_SHORT, false, - triangles.size(), GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + // process triangles + indices.seal(gl, false); + indices.rewind(); for(Triangle t:triangles){ final Vertex[] t_vertices = t.getVertices(); @@ -95,12 +112,15 @@ public class VBORegionSPES2 implements Region { } } indices.seal(gl, true); + indices.enableBuffer(gl, false); - verticeAttr = GLArrayDataServer.createGLSL(gl, Region.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT, false, - vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - verticeAttr.setLocation(Region.VERTEX_ATTR_IDX); - for(Vertex v:vertices){ - + // process vertices and update bbox + box.reset(); + verticeAttr.seal(gl, false); + verticeAttr.rewind(); + texCoordAttr.seal(gl, false); + texCoordAttr.rewind(); + for(Vertex v:vertices){ if(flipped){ verticeAttr.putf(v.getX()); verticeAttr.putf(-1*v.getY()); @@ -115,29 +135,24 @@ public class VBORegionSPES2 implements Region { box.resize(v.getX(),v.getY(),v.getZ()); } - } - verticeAttr.seal(gl, true); - - texCoordAttr = GLArrayDataServer.createGLSL(gl, Region.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT, false, - vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - texCoordAttr.setLocation(Region.TEXCOORD_ATTR_IDX); - for(Vertex v:vertices){ - float[] tex = v.getTexCoord(); + + final float[] tex = v.getTexCoord(); texCoordAttr.putf(tex[0]); texCoordAttr.putf(tex[1]); } + verticeAttr.seal(gl, true); + verticeAttr.enableBuffer(gl, false); texCoordAttr.seal(gl, true); - - verticeAttr.enableBuffer(gl, false); texCoordAttr.enableBuffer(gl, false); - indices.enableBuffer(gl, false); + + // update all bbox related data: nope dirty = false; + + // the buffers were disabled, since due to real/fbo switching and other vbo usage } - private void render() { - GL2ES2 gl = context.getGL().getGL2ES2(); - + private void render(GL2ES2 gl) { verticeAttr.enableBuffer(gl, true); texCoordAttr.enableBuffer(gl, true); indices.enableBuffer(gl, true); @@ -149,8 +164,8 @@ public class VBORegionSPES2 implements Region { indices.enableBuffer(gl, false); } - public void render(PMVMatrix matrix, int vp_width, int vp_height, int width){ - render(); + public void render(GL2ES2 gl, RenderState rs, int vp_width, int vp_height, int width) { + render(gl); } public void addTriangles(ArrayList tris) { @@ -172,12 +187,10 @@ public class VBORegionSPES2 implements Region { return dirty; } - public void destroy() { - GL2ES2 gl = context.getGL().getGL2ES2(); - destroy(gl); - } - - final void destroy(GL2ES2 gl) { + public final void destroy(GL2ES2 gl) { + if(DEBUG_INSTANCE) { + System.err.println("VBORegionSPES2 Destroy: " + this); + } if(null != verticeAttr) { verticeAttr.destroy(gl); verticeAttr = null; diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/AttributeNames.java b/src/jogl/classes/jogamp/graph/curve/opengl/shader/AttributeNames.java new file mode 100644 index 000000000..9fe084522 --- /dev/null +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/AttributeNames.java @@ -0,0 +1,18 @@ +package jogamp.graph.curve.opengl.shader; + +public class AttributeNames { + /** The vertices index in an OGL object + */ + public static final int VERTEX_ATTR_IDX = 1; // 0 is a generic special 0 .. + public static final String VERTEX_ATTR_NAME = "gca_Vertices"; + + /** The Texture Coord index in an OGL object + */ + public static final int TEXCOORD_ATTR_IDX = 2; + public static final String TEXCOORD_ATTR_NAME = "gca_TexCoords"; + + /** The color index in an OGL object + */ + public static final int COLOR_ATTR_IDX = 3; + public static final String COLOR_ATTR_NAME = "gca_Colors"; +} diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/UniformNames.java b/src/jogl/classes/jogamp/graph/curve/opengl/shader/UniformNames.java new file mode 100644 index 000000000..2e04278ca --- /dev/null +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/UniformNames.java @@ -0,0 +1,10 @@ +package jogamp.graph.curve.opengl.shader; + +public class UniformNames { + public static final String gcu_PMVMatrix = "gcu_PMVMatrix"; // gcu_PMVMatrix[3]; // P, Mv, and Mvi + public static final String gcu_ColorStatic = "gcu_ColorStatic"; + public static final String gcu_Alpha = "gcu_Alpha"; + public static final String gcu_P1Y = "gcu_P1Y"; + public static final String gcu_Strength = "gcu_Strength"; + public static final String gcu_TextureUnit = "gcu_TextureUnit"; +} diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/attributes.glsl b/src/jogl/classes/jogamp/graph/curve/opengl/shader/attributes.glsl new file mode 100644 index 000000000..e5ae2b42e --- /dev/null +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/attributes.glsl @@ -0,0 +1,12 @@ + +#ifndef attributes_glsl +#define attributes_glsl + +#include precision.glsl + +attribute HIGHP vec3 gca_Vertices; +attribute HIGHP vec2 gca_TexCoords; +//attribute HIGHP vec4 gca_Colors; +//attribute HIGHP vec3 gca_Normals; + +#endif // attributes_glsl diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/consts.glsl b/src/jogl/classes/jogamp/graph/curve/opengl/shader/consts.glsl new file mode 100644 index 000000000..4cb41c903 --- /dev/null +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/consts.glsl @@ -0,0 +1,10 @@ + +#ifndef consts_glsl +#define consts_glsl + +#include precision.glsl + +const LOWP int MAX_TEXTURE_UNITS = 8; // <= gl_MaxTextureImageUnits +// const LOWP int MAX_LIGHTS = 8; + +#endif // consts_glsl diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/curverenderer01.fp b/src/jogl/classes/jogamp/graph/curve/opengl/shader/curverenderer01.fp index 166937f7f..fca3bcc04 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/shader/curverenderer01.fp +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/curverenderer01.fp @@ -1,93 +1,93 @@ //Copyright 2010 JogAmp Community. All rights reserved. -//#version 100 -uniform float p1y; -uniform float g_alpha; -uniform vec3 g_color; -uniform float a_strength; +#ifdef GL_ES + #version 100 +#else + #version 130 +#endif -varying vec2 v_texCoord; +#include uniforms.glsl +#include attributes.glsl +#include varyings.glsl -vec3 b_color = vec3(0.0, 0.0, 0.0); - -uniform sampler2D texture; -vec4 weights = vec4(0.075, 0.06, 0.045, 0.025); +const vec3 b_color = vec3(0.0, 0.0, 0.0); +const vec4 weights = vec4(0.075, 0.06, 0.045, 0.025); void main (void) { - vec2 rtex = vec2(abs(v_texCoord.x),abs(v_texCoord.y)); - vec3 c = g_color; + vec2 rtex = vec2(abs(gcv_TexCoord.x),abs(gcv_TexCoord.y)); + vec3 c = gcu_ColorStatic.rgb; float alpha = 0.0; - if((v_texCoord.x == 0.0) && (v_texCoord.y == 0.0)){ - alpha = g_alpha; + if((gcv_TexCoord.x == 0.0) && (gcv_TexCoord.y == 0.0)){ + alpha = gcu_Alpha; } - else if((v_texCoord.x >= 5.0)){ - vec2 dfx = dFdx(v_texCoord); - vec2 dfy = dFdy(v_texCoord); + else if((gcv_TexCoord.x >= 5.0)){ + vec2 dfx = dFdx(gcv_TexCoord); + vec2 dfy = dFdy(gcv_TexCoord); - vec2 size = 1.0/textureSize(texture,0); //version 130 + vec2 size = 1.0/textureSize(gcu_TextureUnit,0); //version 130 - FIXME: replace with uniform value rtex -= 5.0; - vec4 t = texture2D(texture, rtex)* 0.18; + vec4 t = texture2D(gcu_TextureUnit, rtex)* 0.18; - t += texture2D(texture, rtex + size*(vec2(1, 0)))*weights.x; - t += texture2D(texture, rtex - size*(vec2(1, 0)))*weights.x; - t += texture2D(texture, rtex + size*(vec2(0, 1)))*weights.x; - t += texture2D(texture, rtex - size*(vec2(0, 1)))*weights.x; + t += texture2D(gcu_TextureUnit, rtex + size*(vec2(1, 0)))*weights.x; + t += texture2D(gcu_TextureUnit, rtex - size*(vec2(1, 0)))*weights.x; + t += texture2D(gcu_TextureUnit, rtex + size*(vec2(0, 1)))*weights.x; + t += texture2D(gcu_TextureUnit, rtex - size*(vec2(0, 1)))*weights.x; - t += texture2D(texture, rtex + 2.0*size*(vec2(1, 0))) *weights.y; - t += texture2D(texture, rtex - 2.0*size*(vec2(1, 0)))*weights.y; - t += texture2D(texture, rtex + 2.0*size*(vec2(0, 1)))*weights.y; - t += texture2D(texture, rtex - 2.0*size*(vec2(0, 1)))*weights.y; + t += texture2D(gcu_TextureUnit, rtex + 2.0*size*(vec2(1, 0)))*weights.y; + t += texture2D(gcu_TextureUnit, rtex - 2.0*size*(vec2(1, 0)))*weights.y; + t += texture2D(gcu_TextureUnit, rtex + 2.0*size*(vec2(0, 1)))*weights.y; + t += texture2D(gcu_TextureUnit, rtex - 2.0*size*(vec2(0, 1)))*weights.y; - t += texture2D(texture, rtex + 3.0*size*(vec2(1, 0))) *weights.z; - t += texture2D(texture, rtex - 3.0*size*(vec2(1, 0)))*weights.z; - t += texture2D(texture, rtex + 3.0*size*(vec2(0, 1)))*weights.z; - t += texture2D(texture, rtex - 3.0*size*(vec2(0, 1)))*weights.z; + t += texture2D(gcu_TextureUnit, rtex + 3.0*size*(vec2(1, 0)))*weights.z; + t += texture2D(gcu_TextureUnit, rtex - 3.0*size*(vec2(1, 0)))*weights.z; + t += texture2D(gcu_TextureUnit, rtex + 3.0*size*(vec2(0, 1)))*weights.z; + t += texture2D(gcu_TextureUnit, rtex - 3.0*size*(vec2(0, 1)))*weights.z; - t += texture2D(texture, rtex + 4.0*size*(vec2(1, 0))) *weights.w; - t += texture2D(texture, rtex - 4.0*size*(vec2(1, 0)))*weights.w; - t += texture2D(texture, rtex + 4.0*size*(vec2(0, 1)))*weights.w; - t += texture2D(texture, rtex - 4.0*size*(vec2(0, 1)))*weights.w; + t += texture2D(gcu_TextureUnit, rtex + 4.0*size*(vec2(1, 0)))*weights.w; + t += texture2D(gcu_TextureUnit, rtex - 4.0*size*(vec2(1, 0)))*weights.w; + t += texture2D(gcu_TextureUnit, rtex + 4.0*size*(vec2(0, 1)))*weights.w; + t += texture2D(gcu_TextureUnit, rtex - 4.0*size*(vec2(0, 1)))*weights.w; if(t.w == 0.0){ discard; } c = t.xyz; - alpha = g_alpha* t.w; + alpha = gcu_Alpha * t.w; } /////////////////////////////////////////////////////////// - else if ((v_texCoord.x > 0.0) && (rtex.y > 0.0 || rtex.x == 1.0)){ - vec2 dtx = dFdx(rtex); - vec2 dty = dFdy(rtex); + else if ((gcv_TexCoord.x > 0.0) && (rtex.y > 0.0 || rtex.x == 1.0)){ + vec2 dtx = dFdx(rtex); + vec2 dty = dFdy(rtex); - rtex.y -= 0.1; + rtex.y -= 0.1; - if(rtex.y < 0.0) { - if(v_texCoord.y < 0.0) - discard; - else{ - rtex.y = 0.0; - } - } + if(rtex.y < 0.0) { + if(gcv_TexCoord.y < 0.0) + discard; + else{ + rtex.y = 0.0; + } + } - vec2 f = vec2((dtx.y - dtx.x + 2.0*rtex.x*dtx.x), (dty.y - dty.x + 2.0*rtex.x*dty.x)); - float position = rtex.y - (rtex.x * (1.0 - rtex.x)); - float d = position/(length(f)); + vec2 f = vec2((dtx.y - dtx.x + 2.0*rtex.x*dtx.x), (dty.y - dty.x + 2.0*rtex.x*dty.x)); + float position = rtex.y - (rtex.x * (1.0 - rtex.x)); + float d = position/(length(f)); - float a = (0.5 - d * sign(v_texCoord.y)); + float a = (0.5 - d * sign(gcv_TexCoord.y)); if (a >= 1.0) { - alpha = g_alpha; + alpha = gcu_Alpha; } - else if (a <= 0.0) { - alpha = 0.0;//discard; - } - else { - alpha = g_alpha*a; - mix(b_color,g_color, a); + else if (a <= 0.0) { + alpha = 0.0;//discard; + } + else { + alpha = gcu_Alpha * a; + mix(b_color,gcu_ColorStatic.rgb, a); } } diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/curverenderer01.vp b/src/jogl/classes/jogamp/graph/curve/opengl/shader/curverenderer01.vp index bc9ecb41e..298dce7ef 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/shader/curverenderer01.vp +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/curverenderer01.vp @@ -1,13 +1,17 @@ -//#version 100 +//Copyright 2010 JogAmp Community. All rights reserved. -uniform mat4 mgl_PMVMatrix[2]; -attribute vec4 v_position; -attribute vec2 texCoord; +#ifdef GL_ES + #version 100 +#else + #version 110 +#endif -varying vec2 v_texCoord; +#include uniforms.glsl +#include attributes.glsl +#include varyings.glsl void main(void) { - gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * v_position; - v_texCoord = texCoord.st; -} \ No newline at end of file + gl_Position = gcu_PMVMatrix[0] * gcu_PMVMatrix[1] * vec4(gca_Vertices, 1); + gcv_TexCoord = gca_TexCoords; +} diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/precision.glsl b/src/jogl/classes/jogamp/graph/curve/opengl/shader/precision.glsl new file mode 100644 index 000000000..1ac4ed8e9 --- /dev/null +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/precision.glsl @@ -0,0 +1,14 @@ +#ifndef precision_glsl +#define precision_glsl + +#ifdef GL_ES + #define MEDIUMP mediump + #define HIGHP highp + #define LOWP lowp +#else + #define MEDIUMP + #define HIGHP + #define LOWP +#endif + +#endif // precision_glsl diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/uniforms.glsl b/src/jogl/classes/jogamp/graph/curve/opengl/shader/uniforms.glsl new file mode 100644 index 000000000..677c7324f --- /dev/null +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/uniforms.glsl @@ -0,0 +1,21 @@ + +#ifndef uniforms_glsl +#define uniforms_glsl + +#include precision.glsl + +// #include consts.glsl + +uniform HIGHP mat4 gcu_PMVMatrix[3]; // P, Mv, and Mvi +uniform HIGHP vec3 gcu_ColorStatic; +uniform HIGHP float gcu_Alpha; +uniform HIGHP float gcu_P1Y; +uniform HIGHP float gcu_Strength; +uniform sampler2D gcu_TextureUnit; + +// uniform HIGHP mat3 gcu_NormalMatrix; // transpose(inverse(ModelView)).3x3 +// uniform LOWP int gcu_ColorEnabled; +// uniform LOWP int gcu_TexCoordEnabled[MAX_TEXTURE_UNITS]; +// uniform LOWP int gcu_CullFace; + +#endif // uniforms_glsl diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/shader/varyings.glsl b/src/jogl/classes/jogamp/graph/curve/opengl/shader/varyings.glsl new file mode 100644 index 000000000..e70c25266 --- /dev/null +++ b/src/jogl/classes/jogamp/graph/curve/opengl/shader/varyings.glsl @@ -0,0 +1,13 @@ + +#ifndef varyings_glsl +#define varyings_glsl + +#include precision.glsl + +#include consts.glsl + +varying vec4 gcv_FrontColor; +varying vec2 gcv_TexCoord; + +#endif // varyings_glsl + diff --git a/src/jogl/classes/jogamp/graph/curve/text/GlyphString.java b/src/jogl/classes/jogamp/graph/curve/text/GlyphString.java index 705613447..8f7dcf30a 100644 --- a/src/jogl/classes/jogamp/graph/curve/text/GlyphString.java +++ b/src/jogl/classes/jogamp/graph/curve/text/GlyphString.java @@ -32,9 +32,10 @@ import java.util.ArrayList; import com.jogamp.graph.geom.AABBox; import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.geom.Triangle; +import com.jogamp.graph.geom.Vertex.Factory; import com.jogamp.graph.geom.opengl.SVertex; -import javax.media.opengl.GLContext; +import javax.media.opengl.GL2ES2; import jogamp.graph.geom.plane.AffineTransform; import jogamp.graph.geom.plane.Path2D; @@ -43,11 +44,10 @@ import jogamp.graph.geom.plane.PathIterator; import com.jogamp.graph.curve.Region; import com.jogamp.graph.curve.RegionFactory; -import com.jogamp.opengl.util.PMVMatrix; +import com.jogamp.graph.curve.opengl.RenderState; import com.jogamp.opengl.util.glsl.ShaderState; public class GlyphString { - private final Vertex.Factory pointFactory; private ArrayList glyphs = new ArrayList(); private String str = ""; private String fontname = ""; @@ -60,14 +60,11 @@ public class GlyphString { * associated with * @param str the string object */ - public GlyphString(Vertex.Factory factory, String fontname, String str){ - pointFactory = factory; + public GlyphString(String fontname, String str){ this.fontname = fontname; this.str = str; } - public final Vertex.Factory pointFactory() { return pointFactory; } - public void addGlyphShape(GlyphShape glyph){ glyphs.add(glyph); } @@ -76,10 +73,11 @@ public class GlyphString { } /** Creates the Curve based Glyphs from a Font + * @param pointFactory TODO * @param paths a list of FontPath2D objects that define the outline * @param affineTransform a global affine transformation applied to the paths. */ - public void createfromFontPath(Path2D[] paths, AffineTransform affineTransform){ + public void createfromFontPath(Factory pointFactory, Path2D[] paths, AffineTransform affineTransform) { final int numGlyps = paths.length; for (int index=0;index tris = initializeTriangles(shaprness); + ArrayList tris = initializeTriangles(rs.getSharpness().floatValue()); region.addTriangles(tris); int numVertices = region.getNumVertices(); @@ -126,7 +124,7 @@ public class GlyphString { } /** initialize the region */ - region.update(); + region.update(gl); } /** Generate a Hashcode for this object @@ -139,17 +137,17 @@ public class GlyphString { /** Render the Object based using the associated Region * previously generated. */ - public void renderString3D() { - region.render(null, 0, 0, 0); + public void renderString3D(GL2ES2 gl) { + region.render(gl, null, 0, 0, 0); } /** Render the Object based using the associated Region * previously generated. */ - public void renderString3D(PMVMatrix matrix, int vp_width, int vp_height, int size) { - region.render(matrix, vp_width, vp_height, size); + public void renderString3D(GL2ES2 gl, RenderState rs, int vp_width, int vp_height, int size) { + region.render(gl, rs, vp_width, vp_height, size); } - /** Get the Origion of this GlyphString + /** Get the Origin of this GlyphString * @return */ public Vertex getOrigin() { @@ -158,8 +156,8 @@ public class GlyphString { /** Destroy the associated OGL objects */ - public void destroy(){ - region.destroy(); + public void destroy(GL2ES2 gl){ + region.destroy(gl); } public AABBox getBounds(){ -- cgit v1.2.3