Search in sources :

Example 11 with SceneGraphNode

use of gaiasky.scenegraph.SceneGraphNode in project gaiasky by langurmonkey.

the class OrbitalElementsGroupRenderSystem method renderStud.

@Override
public void renderStud(Array<IRenderable> renderables, ICamera camera, double t) {
    for (IRenderable renderable : renderables) {
        OrbitalElementsGroup oeg = (OrbitalElementsGroup) renderable;
        if (!inGpu(oeg)) {
            int n = oeg.children.size;
            int offset = addMeshData(n * 4, n * 6);
            setOffset(oeg, offset);
            curr = meshes.get(offset);
            ensureTempVertsSize(n * 4 * curr.vertexSize);
            ensureTempIndicesSize(n * 6);
            AtomicInteger numVerticesAdded = new AtomicInteger(0);
            AtomicInteger numParticlesAdded = new AtomicInteger(0);
            CatalogInfo ci = oeg.getCatalogInfo();
            Array<SceneGraphNode> children = oeg.children;
            children.forEach(child -> {
                Orbit orbit = (Orbit) child;
                OrbitComponent oc = orbit.oc;
                // 4 vertices per particle
                for (int vert = 0; vert < 4; vert++) {
                    // Vertex POSITION
                    tempVerts[curr.vertexIdx + posOffset] = vertPos[vert].getFirst();
                    tempVerts[curr.vertexIdx + posOffset + 1] = vertPos[vert].getSecond();
                    // UV coordinates
                    tempVerts[curr.vertexIdx + uvOffset] = vertUV[vert].getFirst();
                    tempVerts[curr.vertexIdx + uvOffset + 1] = vertUV[vert].getSecond();
                    // COLOR
                    float[] c = oeg.isHighlighted() && ci != null ? ci.getHlColor() : orbit.pointColor;
                    tempVerts[curr.vertexIdx + curr.colorOffset] = Color.toFloatBits(c[0], c[1], c[2], c[3]);
                    // ORBIT ELEMENTS 01
                    tempVerts[curr.vertexIdx + elems01Offset] = (float) Math.sqrt(oc.mu / Math.pow(oc.semimajoraxis * 1000d, 3d));
                    tempVerts[curr.vertexIdx + elems01Offset + 1] = (float) oc.epoch;
                    // In metres
                    tempVerts[curr.vertexIdx + elems01Offset + 2] = (float) (oc.semimajoraxis * 1000d);
                    tempVerts[curr.vertexIdx + elems01Offset + 3] = (float) oc.e;
                    // ORBIT ELEMENTS 02
                    tempVerts[curr.vertexIdx + elems02Offset] = (float) (oc.i * MathUtilsd.degRad);
                    tempVerts[curr.vertexIdx + elems02Offset + 1] = (float) (oc.ascendingnode * MathUtilsd.degRad);
                    tempVerts[curr.vertexIdx + elems02Offset + 2] = (float) (oc.argofpericenter * MathUtilsd.degRad);
                    tempVerts[curr.vertexIdx + elems02Offset + 3] = (float) (oc.meananomaly * MathUtilsd.degRad);
                    // SIZE
                    tempVerts[curr.vertexIdx + sizeOffset] = orbit.pointSize * (oeg.isHighlighted() && ci != null ? ci.hlSizeFactor : 1);
                    curr.vertexIdx += curr.vertexSize;
                    curr.numVertices++;
                    numVerticesAdded.incrementAndGet();
                }
                // Indices
                quadIndices(curr);
                numParticlesAdded.incrementAndGet();
                setInGpu(orbit, true);
            });
            int count = numVerticesAdded.get() * curr.vertexSize;
            setCount(oeg, count);
            curr.mesh.setVertices(tempVerts, 0, count);
            curr.mesh.setIndices(tempIndices, 0, numParticlesAdded.get() * 6);
            setInGpu(oeg, true);
        }
        curr = meshes.get(getOffset(renderable));
        if (curr != null) {
            ExtShaderProgram shaderProgram = getShaderProgram();
            shaderProgram.begin();
            shaderProgram.setUniformMatrix("u_projView", camera.getCamera().combined);
            shaderProgram.setUniformf("u_camPos", camera.getPos().put(aux1));
            shaderProgram.setUniformf("u_alpha", alphas[renderable.getComponentType().getFirstOrdinal()] * renderable.getOpacity());
            shaderProgram.setUniformf("u_falloff", 2.5f);
            shaderProgram.setUniformf("u_sizeFactor", Settings.settings.scene.star.pointSize * 0.08f * oeg.getPointscaling());
            shaderProgram.setUniformf("u_sizeLimits", (float) (particleSizeLimits[0]), (float) (particleSizeLimits[1]));
            // VR scale
            shaderProgram.setUniformf("u_vrScale", (float) Constants.DISTANCE_SCALE_FACTOR);
            // Emulate double, for compatibility
            double curRt = AstroUtils.getJulianDate(GaiaSky.instance.time.getTime());
            float curRt1 = (float) curRt;
            float curRt2 = (float) (curRt - (double) curRt1);
            shaderProgram.setUniformf("u_t", curRt1, curRt2);
            shaderProgram.setUniformMatrix("u_eclToEq", maux.setToRotation(0, 1, 0, -90).mul(Coordinates.equatorialToEclipticF()));
            // Relativistic effects
            addEffectsUniforms(shaderProgram, camera);
            try {
                curr.mesh.render(shaderProgram, GL20.GL_TRIANGLES);
            } catch (IllegalArgumentException e) {
                logger.error(e, "Render exception");
            }
            shaderProgram.end();
        }
    }
}
Also used : OrbitalElementsGroup(gaiasky.scenegraph.OrbitalElementsGroup) IRenderable(gaiasky.render.IRenderable) Orbit(gaiasky.scenegraph.Orbit) SceneGraphNode(gaiasky.scenegraph.SceneGraphNode) ExtShaderProgram(gaiasky.util.gdx.shader.ExtShaderProgram) OrbitComponent(gaiasky.scenegraph.component.OrbitComponent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CatalogInfo(gaiasky.util.CatalogInfo)

Example 12 with SceneGraphNode

use of gaiasky.scenegraph.SceneGraphNode in project gaiasky by langurmonkey.

the class OctreeNode method updateCounts.

/**
 * Updates the number of objects, own objects and children. This operation
 * runs recursively in depth.
 */
public void updateCounts() {
    // Number of own objects
    this.numObjects = 0;
    if (objects != null) {
        for (SceneGraphNode ape : objects) {
            this.numObjects += ape.getStarCount();
        }
    }
    // Number of recursive objects
    this.numObjectsRec = this.numObjects;
    // Children count
    this.numChildren = 0;
    for (int i = 0; i < 8; i++) {
        if (children[i] != null) {
            this.numChildren++;
            // Recursive call
            children[i].updateCounts();
            numObjectsRec += children[i].numObjectsRec;
        }
    }
}
Also used : SceneGraphNode(gaiasky.scenegraph.SceneGraphNode)

Example 13 with SceneGraphNode

use of gaiasky.scenegraph.SceneGraphNode in project gaiasky by langurmonkey.

the class SceneGraphJsonLoader method loadJsonFile.

public static synchronized Array<SceneGraphNode> loadJsonFile(FileHandle jsonFile) throws ReflectionException, FileNotFoundException {
    Array<SceneGraphNode> nodes = new Array<>(false, 20600);
    JsonReader jsonReader = new JsonReader();
    JsonValue model = jsonReader.parse(jsonFile.read());
    // Must have a 'data' element
    if (model.has("data")) {
        String name = model.get("name") != null ? model.get("name").asString() : null;
        String desc = model.get("description") != null ? model.get("description").asString() : null;
        Long size = model.get("size") != null ? model.get("size").asLong() : -1;
        Long nObjects = model.get("nobjects") != null ? model.get("nobjects").asLong() : -1;
        Map<String, Object> params = new HashMap<>();
        params.put("size", size);
        params.put("nobjects", nObjects);
        JsonValue child = model.get("data").child;
        while (child != null) {
            String clazzName = child.getString("loader").replace("gaia.cu9.ari.gaiaorbit", "gaiasky");
            @SuppressWarnings("unchecked") Class<Object> clazz = (Class<Object>) ClassReflection.forName(clazzName);
            JsonValue filesJson = child.get("files");
            if (filesJson != null) {
                String[] files = filesJson.asStringArray();
                Constructor c = ClassReflection.getConstructor(clazz);
                ISceneGraphLoader loader = (ISceneGraphLoader) c.newInstance();
                if (name != null)
                    loader.setName(name);
                if (desc != null)
                    loader.setDescription(desc);
                if (params != null && params.size() > 0)
                    loader.setParams(params);
                // Init loader
                loader.initialize(files);
                JsonValue curr = filesJson;
                while (curr.next != null) {
                    curr = curr.next;
                    String nameAttr = curr.name;
                    Object val = null;
                    Class valueClass = null;
                    if (curr.isDouble()) {
                        val = curr.asDouble();
                        valueClass = Double.class;
                    } else if (curr.isString()) {
                        val = curr.asString();
                        valueClass = String.class;
                    } else if (curr.isNumber()) {
                        val = curr.asLong();
                        valueClass = Long.class;
                    }
                    if (val != null) {
                        String methodName = "set" + TextUtils.propertyToMethodName(nameAttr);
                        Method m = searchMethod(methodName, valueClass, clazz);
                        if (m != null)
                            m.invoke(loader, val);
                        else
                            logger.error("ERROR: No method " + methodName + "(" + valueClass.getName() + ") in class " + clazz + " or its superclass/interfaces.");
                    }
                }
                // Load data
                Array<? extends SceneGraphNode> data = loader.loadData();
                for (SceneGraphNode elem : data) {
                    nodes.add(elem);
                }
            }
            child = child.next;
        }
    } else {
        // Use regular JsonLoader
        JsonLoader loader = new JsonLoader();
        loader.initialize(new String[] { jsonFile.file().getAbsolutePath() });
        // Load data
        Array<? extends SceneGraphNode> data = loader.loadData();
        for (SceneGraphNode elem : data) {
            nodes.add(elem);
        }
    }
    return nodes;
}
Also used : HashMap(java.util.HashMap) Constructor(com.badlogic.gdx.utils.reflect.Constructor) SceneGraphNode(gaiasky.scenegraph.SceneGraphNode) JsonValue(com.badlogic.gdx.utils.JsonValue) Method(com.badlogic.gdx.utils.reflect.Method) Array(com.badlogic.gdx.utils.Array) JsonReader(com.badlogic.gdx.utils.JsonReader)

Example 14 with SceneGraphNode

use of gaiasky.scenegraph.SceneGraphNode in project gaiasky by langurmonkey.

the class StreamingOctreeLoader method loadData.

@Override
public Array<? extends SceneGraphNode> loadData() {
    AbstractOctreeWrapper octreeWrapper = loadOctreeData();
    if (octreeWrapper != null) {
        // Initialize daemon loader thread.
        daemon = new OctreeLoaderThread(octreeWrapper, this);
        daemon.setDaemon(true);
        daemon.setName("gaiasky-worker-octreeload");
        daemon.setPriority(Thread.MIN_PRIORITY);
        daemon.start();
        // Initialize timer to flush the queue at regular intervals.
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                flushLoadQueue();
            }
        }, 1000, 1000);
        // Add octreeWrapper to result list and return.
        Array<SceneGraphNode> result = new Array<>(false, 1);
        result.add(octreeWrapper);
        logger.info(I18n.txt("notif.catalog.init", octreeWrapper.root.countObjects()));
        return result;
    } else {
        return new Array<>(false, 1);
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) SceneGraphNode(gaiasky.scenegraph.SceneGraphNode) AbstractOctreeWrapper(gaiasky.scenegraph.octreewrapper.AbstractOctreeWrapper)

Example 15 with SceneGraphNode

use of gaiasky.scenegraph.SceneGraphNode in project gaiasky by langurmonkey.

the class StreamingOctreeLoader method unloadOctant.

/**
 * Unloads the given octant.
 */
public void unloadOctant(OctreeNode octant, final AbstractOctreeWrapper octreeWrapper) {
    List<SceneGraphNode> objects = octant.objects;
    if (objects != null) {
        GaiaSky.postRunnable(() -> {
            synchronized (octant) {
                try {
                    int unloaded = 0;
                    for (SceneGraphNode object : objects) {
                        int count = object.getStarCount();
                        object.dispose();
                        object.octant = null;
                        octreeWrapper.removeParenthood(object);
                        // Aux info
                        if (GaiaSky.instance != null && GaiaSky.instance.sceneGraph != null)
                            GaiaSky.instance.sceneGraph.removeNodeAuxiliaryInfo(object);
                        nLoadedStars -= count;
                        unloaded += count;
                    }
                    objects.clear();
                    octant.setStatus(LoadStatus.NOT_LOADED);
                    octant.touch(unloaded);
                } catch (Exception e) {
                    logger.error("Error disposing octant's objects " + octant.pageId, e);
                    logger.info(Settings.APPLICATION_NAME + " will attempt to continue");
                }
            }
        });
    }
}
Also used : SceneGraphNode(gaiasky.scenegraph.SceneGraphNode) IOException(java.io.IOException)

Aggregations

SceneGraphNode (gaiasky.scenegraph.SceneGraphNode)25 Array (com.badlogic.gdx.utils.Array)7 IFocus (gaiasky.scenegraph.IFocus)6 StarGroup (gaiasky.scenegraph.StarGroup)5 ParticleGroup (gaiasky.scenegraph.ParticleGroup)4 ArrayList (java.util.ArrayList)4 Actor (com.badlogic.gdx.scenes.scene2d.Actor)3 ChangeEvent (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent)3 FadeNode (gaiasky.scenegraph.FadeNode)3 ISceneGraph (gaiasky.scenegraph.ISceneGraph)3 IOException (java.io.IOException)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)2 NaturalCamera (gaiasky.scenegraph.camera.NaturalCamera)2 AbstractOctreeWrapper (gaiasky.scenegraph.octreewrapper.AbstractOctreeWrapper)2 IParticleRecord (gaiasky.scenegraph.particle.IParticleRecord)2 CatalogInfo (gaiasky.util.CatalogInfo)2 OwnLabel (gaiasky.util.scene2d.OwnLabel)2 Lwjgl3Graphics (com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics)1 Stage (com.badlogic.gdx.scenes.scene2d.Stage)1