Search in sources :

Example 6 with Camera

use of com.badlogic.gdx.graphics.Camera in project libgdx by libgdx.

the class OcclusionCullingTest method create.

@Override
public void create() {
    Gdx.input.setOnscreenKeyboardVisible(true);
    super.create();
    GLProfiler.enable();
    StringBuilder sb = new StringBuilder();
    sb.append("Swipe for next test\n");
    sb.append("Long press to toggle debug mode\n");
    sb.append("Ctrl+drag to rotate\n");
    sb.append("Scroll to zoom\n");
    sb.append("Tap to spawn dynamic entity, press\n");
    sb.append("'0' to spawn ").append(KEY_SPAWN_OCCLUDEE_AMOUNT).append(" static entities\n");
    sb.append("'1' to set normal/disabled/occlusion-culling\n");
    sb.append("'2' to change camera\n");
    sb.append("'3' to toggle camera movement\n");
    sb.append("'4' to cycle occlusion buffer sizes\n");
    sb.append("'5' to toggle occlusion buffer image\n");
    sb.append("'6' to toggle shadows\n");
    instructions = sb.toString();
    AssetManager assets = new AssetManager();
    disposables.add(assets);
    for (String modelName : OCCLUDEE_PATHS_DYNAMIC) assets.load(modelName, Model.class);
    assets.load(DEFAULT_TEX_PATH, Texture.class);
    Camera shadowCamera = ((DirectionalShadowLight) light).getCamera();
    shadowCamera.viewportWidth = shadowCamera.viewportHeight = 120;
    // User controlled camera
    overviewCam = camera;
    overviewCam.position.set(overviewCam.direction).nor().scl(-100);
    overviewCam.lookAt(Vector3.Zero);
    overviewCam.far = camera.far *= 2;
    overviewCam.update(true);
    // Animated frustum camera model
    frustumCam = new PerspectiveCamera(FRUSTUM_CAMERA_FOV, camera.viewportWidth, camera.viewportHeight);
    frustumCam.far = FRUSTUM_CAMERA_FAR;
    frustumCam.update(true);
    final Model frustumModel = FrustumCullingTest.createFrustumModel(frustumCam.frustum.planePoints);
    frustumModel.materials.first().set(new ColorAttribute(ColorAttribute.AmbientLight, Color.WHITE));
    disposables.add(frustumModel);
    frustumInstance = new ModelInstance(frustumModel);
    spriteBatch = new SpriteBatch();
    disposables.add(spriteBatch);
    shapeRenderer = new ShapeRenderer();
    disposables.add(shapeRenderer);
    oclBuffer = new OcclusionBuffer(OCL_BUFFER_EXTENTS[0], OCL_BUFFER_EXTENTS[0]);
    disposables.add(oclBuffer);
    occlusionCuller = new OcclusionCuller() {

        @Override
        public boolean isOccluder(btCollisionObject object) {
            return (object.getCollisionFlags() & CF_OCCLUDER_OBJECT) != 0;
        }

        @Override
        public void onObjectVisible(btCollisionObject object) {
            visibleEntities.add(world.entities.get(object.getUserValue()));
        }
    };
    disposables.add(occlusionCuller);
    // Add occluder walls
    final Model occluderModel = modelBuilder.createBox(OCCLUDER_DIM.x, OCCLUDER_DIM.y, OCCLUDER_DIM.z, new Material(ColorAttribute.createDiffuse(Color.WHITE)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
    disposables.add(occluderModel);
    world.addConstructor("wall", new BulletConstructor(occluderModel, 0, new btBoxShape(tmpV1.set(OCCLUDER_DIM).scl(0.5f))));
    float y = OCCLUDER_DIM.y * 0.5f;
    addOccluder("wall", 0, tmpV1.set(20, y, 0));
    addOccluder("wall", -60, tmpV1.set(10, y, 20));
    addOccluder("wall", 60, tmpV1.set(10, y, -20));
    addOccluder("wall", 0, tmpV1.set(-20, y, 0));
    addOccluder("wall", 60, tmpV1.set(-10, y, 20));
    addOccluder("wall", -60, tmpV1.set(-10, y, -20));
    // Add ground
    final Model groundModel = modelBuilder.createBox(GROUND_DIM.x, GROUND_DIM.y, GROUND_DIM.z, new Material(ColorAttribute.createDiffuse(Color.WHITE)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
    btCollisionShape groundShape = new btBoxShape(tmpV1.set(GROUND_DIM).scl(0.5f));
    world.addConstructor("big_ground", new BulletConstructor(groundModel, 0, groundShape));
    BulletEntity e = world.add("big_ground", 0, -GROUND_DIM.y * 0.5f, 0f);
    e.body.setFriction(1f);
    e.setColor(Color.FOREST);
    // Occludee entity constructors. Scale models uniformly and set a default diffuse texture.
    BoundingBox bb = new BoundingBox();
    assets.finishLoadingAsset(DEFAULT_TEX_PATH);
    TextureAttribute defaultTexture = new TextureAttribute(TextureAttribute.Diffuse, assets.get(DEFAULT_TEX_PATH, Texture.class));
    for (int i = 0; i < OCCLUDEE_PATHS_DYNAMIC.length; i++) {
        String modelPath = OCCLUDEE_PATHS_DYNAMIC[i];
        OCCLUDEE_PATHS_STATIC[i] = "static" + modelPath;
        assets.finishLoadingAsset(modelPath);
        Model model = assets.get(modelPath, Model.class);
        if (!model.materials.first().has(TextureAttribute.Diffuse))
            model.materials.first().set(defaultTexture);
        Vector3 dim = model.calculateBoundingBox(bb).getDimensions(tmpV1);
        float scaleFactor = OCCLUDEE_MAX_EXTENT / Math.max(dim.x, Math.max(dim.y, dim.z));
        for (Node node : model.nodes) node.scale.scl(scaleFactor);
        btCollisionShape shape = new btBoxShape(dim.scl(scaleFactor * 0.5f));
        world.addConstructor(modelPath, new BulletConstructor(model, 1, shape));
        world.addConstructor(OCCLUDEE_PATHS_STATIC[i], new BulletConstructor(model, 0, shape));
    }
    // Add occludees
    for (int i = 0; i < STARTING_OCCLUDEE_AMOUNT; i++) addRandomOccludee(false);
}
Also used : com.badlogic.gdx.physics.bullet.collision.btBoxShape(com.badlogic.gdx.physics.bullet.collision.btBoxShape) DirectionalShadowLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight) StringBuilder(com.badlogic.gdx.utils.StringBuilder) Node(com.badlogic.gdx.graphics.g3d.model.Node) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) Texture(com.badlogic.gdx.graphics.Texture) com.badlogic.gdx.physics.bullet.collision.btCollisionShape(com.badlogic.gdx.physics.bullet.collision.btCollisionShape) BoundingBox(com.badlogic.gdx.math.collision.BoundingBox) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) Camera(com.badlogic.gdx.graphics.Camera) com.badlogic.gdx.physics.bullet.collision.btCollisionObject(com.badlogic.gdx.physics.bullet.collision.btCollisionObject) TextureAttribute(com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute) AssetManager(com.badlogic.gdx.assets.AssetManager) Material(com.badlogic.gdx.graphics.g3d.Material) Vector3(com.badlogic.gdx.math.Vector3) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) Model(com.badlogic.gdx.graphics.g3d.Model) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)

Example 7 with Camera

use of com.badlogic.gdx.graphics.Camera in project libgdx by libgdx.

the class Window method keepWithinStage.

void keepWithinStage() {
    if (!keepWithinStage)
        return;
    Stage stage = getStage();
    Camera camera = stage.getCamera();
    if (camera instanceof OrthographicCamera) {
        OrthographicCamera orthographicCamera = (OrthographicCamera) camera;
        float parentWidth = stage.getWidth();
        float parentHeight = stage.getHeight();
        if (getX(Align.right) - camera.position.x > parentWidth / 2 / orthographicCamera.zoom)
            setPosition(camera.position.x + parentWidth / 2 / orthographicCamera.zoom, getY(Align.right), Align.right);
        if (getX(Align.left) - camera.position.x < -parentWidth / 2 / orthographicCamera.zoom)
            setPosition(camera.position.x - parentWidth / 2 / orthographicCamera.zoom, getY(Align.left), Align.left);
        if (getY(Align.top) - camera.position.y > parentHeight / 2 / orthographicCamera.zoom)
            setPosition(getX(Align.top), camera.position.y + parentHeight / 2 / orthographicCamera.zoom, Align.top);
        if (getY(Align.bottom) - camera.position.y < -parentHeight / 2 / orthographicCamera.zoom)
            setPosition(getX(Align.bottom), camera.position.y - parentHeight / 2 / orthographicCamera.zoom, Align.bottom);
    } else if (getParent() == stage.getRoot()) {
        float parentWidth = stage.getWidth();
        float parentHeight = stage.getHeight();
        if (getX() < 0)
            setX(0);
        if (getRight() > parentWidth)
            setX(parentWidth - getWidth());
        if (getY() < 0)
            setY(0);
        if (getTop() > parentHeight)
            setY(parentHeight - getHeight());
    }
}
Also used : Stage(com.badlogic.gdx.scenes.scene2d.Stage) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) Camera(com.badlogic.gdx.graphics.Camera) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera)

Example 8 with Camera

use of com.badlogic.gdx.graphics.Camera in project libgdx by libgdx.

the class NoncontinuousRenderingTest method render.

@Override
public void render() {
    float delta = Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f);
    elapsed += delta;
    float value = elapsed % 1f;
    value = value < 0.5f ? Interpolation.fade.apply(2 * value) : 1 - Interpolation.fade.apply(2 * value - 1);
    //avoid black
    value = 0.2f + value * 0.8f;
    synchronized (this) {
        switch(colorCycle) {
            case 0:
                Gdx.gl.glClearColor(value, 0, 0, 1);
                break;
            case 1:
                Gdx.gl.glClearColor(0, value, 0, 1);
                break;
            case 2:
                Gdx.gl.glClearColor(0, 0, value, 1);
                break;
        }
    }
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Camera cam = stage.getCamera();
    batch.setProjectionMatrix(cam.combined);
    batch.begin();
    batch.draw(region, cam.position.x - texture.getWidth() / 2, cam.position.y - texture.getHeight() / 2, texture.getWidth() / 2f, texture.getHeight() / 2f, (float) texture.getWidth(), (float) texture.getHeight(), 1f, 1f, -((elapsed / 2f) % 1f) * 360f);
    batch.end();
    stage.act(delta);
    stage.draw();
}
Also used : Camera(com.badlogic.gdx.graphics.Camera)

Example 9 with Camera

use of com.badlogic.gdx.graphics.Camera in project libgdx by libgdx.

the class MainShader method bindSpotShadows.

public void bindSpotShadows(final Attributes attributes) {
    final SpotLightsAttribute sla = attributes.get(SpotLightsAttribute.class, SpotLightsAttribute.Type);
    final Array<SpotLight> spots = sla == null ? null : sla.lights;
    if (spotLightsLoc >= 0) {
        for (int i = 0; i < spotLights.length; i++) {
            if (spots == null || spots.size <= i) {
                continue;
            }
            int idx = spotShadowsLoc + i * spotShadowsSize;
            // Shadow
            ObjectMap<SpotLight, LightProperties> spotCameras = shadowSystem.getSpotCameras();
            SpotLight sl = spots.get(i);
            if (shadowSystem.hasLight(sl)) {
                // UVTransform
                final TextureRegion tr = spotCameras.get(sl).region;
                Camera cam = spotCameras.get(sl).camera;
                if (cam != null) {
                    program.setUniformf(idx + spotShadowsUvTransformOffset, tr.getU(), tr.getV(), tr.getU2() - tr.getU(), tr.getV2() - tr.getV());
                    // ProjViewTrans
                    idx = spotShadowMapProjViewTransLoc + i * spotShadowMapProjViewTransSize;
                    program.setUniformMatrix(idx, spotCameras.get(sl).camera.combined);
                }
            }
            if (spotLightsSize <= 0)
                break;
        }
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) LightProperties(com.badlogic.gdx.tests.g3d.shadows.system.BaseShadowSystem.LightProperties) SpotLightsAttribute(com.badlogic.gdx.graphics.g3d.attributes.SpotLightsAttribute) Camera(com.badlogic.gdx.graphics.Camera) SpotLight(com.badlogic.gdx.graphics.g3d.environment.SpotLight)

Example 10 with Camera

use of com.badlogic.gdx.graphics.Camera in project libgdx by libgdx.

the class BaseShadowSystem method processViewport.

/** Set viewport according to allocator.
	 * @param lp LightProperties to process.
	 * @param cameraViewport Set camera viewport if true. */
protected void processViewport(LightProperties lp, boolean cameraViewport) {
    Camera camera = lp.camera;
    ShadowMapRegion r = allocator.nextResult(currentLight);
    if (r == null)
        return;
    TextureRegion region = lp.region;
    region.setTexture(frameBuffers[currentPass].getColorBufferTexture());
    // We don't use HdpiUtils
    // gl commands related to shadow map size and not to screen size
    Gdx.gl.glViewport(r.x, r.y, r.width, r.height);
    Gdx.gl.glScissor(r.x + 1, r.y + 1, r.width - 2, r.height - 2);
    region.setRegion(r.x, r.y, r.width, r.height);
    if (cameraViewport) {
        camera.viewportHeight = r.height;
        camera.viewportWidth = r.width;
        camera.update();
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) ShadowMapRegion(com.badlogic.gdx.tests.g3d.shadows.utils.ShadowMapAllocator.ShadowMapRegion) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) Camera(com.badlogic.gdx.graphics.Camera) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera)

Aggregations

Camera (com.badlogic.gdx.graphics.Camera)15 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)4 Batch (com.badlogic.gdx.graphics.g2d.Batch)4 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)4 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)3 ModelCache (com.badlogic.gdx.graphics.g3d.ModelCache)3 BaseSceneManager (com.ilargia.games.entitas.egdx.base.managers.BaseSceneManager)3 Texture (com.badlogic.gdx.graphics.Texture)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 ModelBatch (com.badlogic.gdx.graphics.g3d.ModelBatch)2 LightProperties (com.badlogic.gdx.tests.g3d.shadows.system.BaseShadowSystem.LightProperties)2 BaseGUIManager (com.ilargia.games.entitas.egdx.base.managers.BaseGUIManager)2 CameraComponent (io.github.voidzombie.nhglib.runtime.ecs.components.graphics.CameraComponent)2 AssetManager (com.badlogic.gdx.assets.AssetManager)1 Material (com.badlogic.gdx.graphics.g3d.Material)1 Model (com.badlogic.gdx.graphics.g3d.Model)1 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)1 RenderableProvider (com.badlogic.gdx.graphics.g3d.RenderableProvider)1 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)1