Search in sources :

Example 11 with ExtShaderProgram

use of gaiasky.util.gdx.shader.ExtShaderProgram in project gaiasky by langurmonkey.

the class VertGPURenderSystem method renderStud.

@Override
public void renderStud(Array<IRenderable> renderables, ICamera camera, double t) {
    if (isLine()) {
        // Enable GL_LINE_SMOOTH
        Gdx.gl20.glEnable(GL11.GL_LINE_SMOOTH);
        Gdx.gl.glHint(GL20.GL_NICEST, GL11.GL_LINE_SMOOTH_HINT);
        // Enable GL_LINE_WIDTH
        Gdx.gl20.glEnable(GL20.GL_LINE_WIDTH);
    }
    this.camera = camera;
    renderables.forEach(r -> {
        T renderable = (T) r;
        /*
             * ADD LINES
             */
        if (!inGpu(renderable)) {
            // Remove previous line data if present
            if (getOffset(renderable) >= 0) {
                clearMeshData(getOffset(renderable));
                setOffset(renderable, -1);
            }
            // Actually add data
            PointCloudData od = renderable.getPointCloud();
            int nPoints = od.getNumPoints();
            // Initialize or fetch mesh data
            if (getOffset(renderable) < 0) {
                setOffset(renderable, addMeshData(nPoints));
            } else {
                curr = meshes.get(getOffset(renderable));
                // Check we still have capacity, otherwise, reinitialize.
                if (curr.numVertices != od.getNumPoints()) {
                    curr.clear();
                    curr.mesh.dispose();
                    meshes.set(getOffset(renderable), null);
                    setOffset(renderable, addMeshData(nPoints));
                }
            }
            // Coord maps time
            long t0 = od.getDate(0).getEpochSecond();
            long t1 = od.getDate(od.getNumPoints() - 1).getEpochSecond();
            long t01 = t1 - t0;
            // Ensure vertices capacity
            ensureTempVertsSize((nPoints + 2) * curr.vertexSize);
            curr.vertices = tempVerts;
            float[] cc = renderable.getColor();
            for (int point_i = 0; point_i < nPoints; point_i++) {
                coord((float) ((double) (od.getDate(point_i).getEpochSecond() - t0) / (double) t01));
                color(cc[0], cc[1], cc[2], 1.0);
                vertex((float) od.getX(point_i), (float) od.getY(point_i), (float) od.getZ(point_i));
            }
            // Close loop
            if (renderable.isClosedLoop()) {
                coord(1f);
                color(cc[0], cc[1], cc[2], 1.0);
                vertex((float) od.getX(0), (float) od.getY(0), (float) od.getZ(0));
            }
            int count = nPoints * curr.vertexSize;
            setCount(renderable, count);
            curr.mesh.setVertices(curr.vertices, 0, count);
            curr.vertices = null;
            setInGpu(renderable, true);
        }
        curr = meshes.get(getOffset(renderable));
        /*
             * RENDER
             */
        ExtShaderProgram shaderProgram = getShaderProgram();
        shaderProgram.begin();
        // Regular
        if (isLine())
            Gdx.gl.glLineWidth(renderable.getPrimitiveSize() * Settings.settings.scene.lineWidth);
        if (isPoint())
            shaderProgram.setUniformf("u_pointSize", renderable.getPrimitiveSize());
        shaderProgram.setUniformMatrix("u_worldTransform", renderable.getLocalTransform());
        shaderProgram.setUniformMatrix("u_projView", camera.getCamera().combined);
        shaderProgram.setUniformf("u_alpha", (float) (renderable.getAlpha()) * getAlpha(renderable));
        shaderProgram.setUniformf("u_coordPos", renderable instanceof Orbit ? (float) ((Orbit) renderable).coord : 1f);
        shaderProgram.setUniformf("u_period", renderable instanceof Orbit && ((Orbit) renderable).oc != null ? (float) ((Orbit) renderable).oc.period : 0f);
        if (renderable.getParent() != null) {
            Vector3d urp = renderable.getParent().getUnrotatedPos();
            if (urp != null)
                shaderProgram.setUniformf("u_parentPos", (float) urp.x, (float) urp.y, (float) urp.z);
            else
                shaderProgram.setUniformf("u_parentPos", 0, 0, 0);
        }
        // Rel, grav, z-buffer
        addEffectsUniforms(shaderProgram, camera);
        curr.mesh.render(shaderProgram, renderable.getGlPrimitive());
        shaderProgram.end();
    });
}
Also used : PointCloudData(gaiasky.data.util.PointCloudData) Orbit(gaiasky.scenegraph.Orbit) Vector3d(gaiasky.util.math.Vector3d) ExtShaderProgram(gaiasky.util.gdx.shader.ExtShaderProgram)

Example 12 with ExtShaderProgram

use of gaiasky.util.gdx.shader.ExtShaderProgram in project gaiasky by langurmonkey.

the class ExtSpriteBatch method createDefaultShader.

/**
 * Returns a new instance of the default shader used by SpriteBatch for GL2 when no shader is specified.
 */
public static ExtShaderProgram createDefaultShader() {
    String vertexShader = // 
    "attribute vec4 " + ExtShaderProgram.POSITION_ATTRIBUTE + ";\n" + "attribute vec4 " + ExtShaderProgram.COLOR_ATTRIBUTE + // 
    ";\n" + "attribute vec2 " + ExtShaderProgram.TEXCOORD_ATTRIBUTE + // 
    "0;\n" + // 
    "uniform mat4 u_projTrans;\n" + // 
    "varying vec4 v_color;\n" + // 
    "varying vec2 v_texCoords;\n" + // 
    "\n" + // 
    "void main()\n" + // 
    "{\n" + "   v_color = " + ExtShaderProgram.COLOR_ATTRIBUTE + // 
    ";\n" + // 
    "   v_color.a = v_color.a * (255.0/254.0);\n" + "   v_texCoords = " + ExtShaderProgram.TEXCOORD_ATTRIBUTE + // 
    "0;\n" + "   gl_Position =  u_projTrans * " + ExtShaderProgram.POSITION_ATTRIBUTE + // 
    ";\n" + "}\n";
    String fragmentShader = // 
    "#ifdef GL_ES\n" + // 
    "#define LOWP lowp\n" + // 
    "precision mediump float;\n" + // 
    "#else\n" + // 
    "#define LOWP \n" + // 
    "#endif\n" + // 
    "varying LOWP vec4 v_color;\n" + // 
    "varying vec2 v_texCoords;\n" + // 
    "uniform sampler2D u_texture;\n" + // 
    "void main()\n" + // 
    "{\n" + // 
    "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" + "}";
    ExtShaderProgram shader = new ExtShaderProgram(vertexShader, fragmentShader);
    if (!shader.isCompiled())
        throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());
    return shader;
}
Also used : ExtShaderProgram(gaiasky.util.gdx.shader.ExtShaderProgram)

Aggregations

ExtShaderProgram (gaiasky.util.gdx.shader.ExtShaderProgram)12 Orbit (gaiasky.scenegraph.Orbit)3 IQuadRenderable (gaiasky.render.IQuadRenderable)2 OrbitComponent (gaiasky.scenegraph.component.OrbitComponent)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 PointCloudData (gaiasky.data.util.PointCloudData)1 IAnnotationsRenderable (gaiasky.render.IAnnotationsRenderable)1 ILineRenderable (gaiasky.render.ILineRenderable)1 IPointRenderable (gaiasky.render.IPointRenderable)1 IRenderable (gaiasky.render.IRenderable)1 OrbitalElementsGroup (gaiasky.scenegraph.OrbitalElementsGroup)1 SceneGraphNode (gaiasky.scenegraph.SceneGraphNode)1 CatalogInfo (gaiasky.util.CatalogInfo)1 Vector3d (gaiasky.util.math.Vector3d)1