mixing 2D + 3D

Anything libgdx related goes here!

Re: mixing 2D + 3D

Postby mikepc » Thu Aug 04, 2011 7:48 am

schweet! zigbee and the home bus, finally coming along? :D nice to see what people are doing outside of games


Thank you.
The tablet software communicates with an embedded linux server which has a zigbee usb dongle,
and communicates with our custom HW for device on/off/dimming. For light dimming and simple
on/off switching up to 500W the devices already work, and we are in development for controlling
a dish washer, washing-machine, oven... For the last devices we need a full 3D interface, with a
real model of devices, and that is why I am seeking for mixing 2D/3D, loading models, doing some
key-frame animation. I have a really nice example in 3DMax made by my designer for opening menu's
but still have no idea how to import this in libgdx/android.

As for the code, I wrote my own code for detecting a swipe/drag, and for animation used stage, some
modified Actor class, and tweening out of the box.

I will write again, as soon as I try to imprt 3D objects for menu animation and run into problems :)
mikepc
 
Posts: 6
Joined: Wed Aug 03, 2011 8:10 pm
Location: Serbia

Re: mixing 2D + 3D

Postby radioking » Thu Aug 04, 2011 8:26 am

Hey folks,

thanks for providing screenshot and source!
Using lingdx this way is very interesting as I am not coding a game but a tiny proof-of-concept application for information purposes.
-> I have some more questions on the extensions needed and their usage, but will open another forum thread for this to keep things separated.
Please "give sth. back" to community and contribute your knowledge to our libgdx-users project community wiki:
http://code.google.com/p/libgdx-users/
Please see Mario's note on User Wiki 2.0: http://www.badlogicgames.com/wordpress/?p=2411
radioking
 
Posts: 284
Joined: Wed Aug 03, 2011 10:28 am

Re: mixing 2D + 3D

Postby yatayata » Thu Aug 04, 2011 5:37 pm

In this example I used a spriteBatch for overlay because I build my UIs with Sprites and thus don't know sh*t about Stages, but I suppose you can easily adapt it to work with a Stage.


Code: Select all
 // 3D render
    myPerspectiveCamera.apply(gl);


Stages only work with OrthographicCamera... and i think the
Code: Select all
cameraApply(gl)
only works with PerspectiveCameras? So its not possible to "switch" cameras during a render. at least i wasnt able to comfortably... (maybe have to go back and check camera.update() )

Is there any reason to use Stages or should i rewrite that code to use SpriteBatches and switch to my own Ortho camera? First time writing an app with this framework and it seems like spritebatches give a lot of other benefits such as TransformMatrix per batch.
Please "give something back" to the community and contribute your
knowledge to our libgdx-users project community wiki:
http://code.google.com/p/libgdx-users/
-> Just PM me your Gmail address so that I can add you as contributor
yatayata
 
Posts: 50
Joined: Mon Aug 01, 2011 2:01 pm
Location: tokyo

Re: mixing 2D + 3D

Postby yatayata » Thu Aug 04, 2011 8:41 pm

update: we had a long chat on IRC about this.

- spriteBatches dont currently support the scenegraph, so you lose all that nice groups stuff if you go the route above.

- Groups dont support partial rendering (eg just render the hudGroup from a scene)
so you have to show and hide parts of the scene, and render the stage twice.

- if you use multiple stages, which would nicely separate the HUD and game area, each with their own camera...
then the problem is input gets sent only to one or the other. meaning the inputmultiplexer which sounds a bit scary to me...

so none of these approaches seem ideal right now...

group.render() just like mesh.render() would seem to be the ideal method but not currently supported.
Please "give something back" to the community and contribute your
knowledge to our libgdx-users project community wiki:
http://code.google.com/p/libgdx-users/
-> Just PM me your Gmail address so that I can add you as contributor
yatayata
 
Posts: 50
Joined: Mon Aug 01, 2011 2:01 pm
Location: tokyo

Re: mixing 2D + 3D

Postby mikepc » Fri Aug 05, 2011 6:43 am

So, as It turns out, we still have NO correct way of doing this?
Just some ideas, how about something like
1. doing all needed in 2D (using Actors, Stage...) and render it into texture
2. render the 3D scene directly to screen
3. render the topmost layer from rendered(prepared 2D scene/HUD) texture on the 2D mesh in 3D world

Would it be possible, would it be slow?
mikepc
 
Posts: 6
Joined: Wed Aug 03, 2011 8:10 pm
Location: Serbia

Re: mixing 2D + 3D

Postby Obli » Fri Aug 05, 2011 8:29 am

camera.apply() is part of the Camera.java file, so yes, it's available for both PerspectiveCamera and OrthographicCamera. I really don't understand why you're thinking that there is no easy way to render a 2d overlay over a 3d world... It's just a matter of changing the opengl projection matrices, with or without cameras ;) ...

Want a proof ? I built an quick example to show you. I hope you'll be convinced...

Image

Code: Select all
package overlay2d3d;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer;
import java.util.Random;

/**
 *
 * @author Aurelien Ribon
 */
public class App implements ApplicationListener {
    public static void main(String args[]) {
      new LwjglApplication(new App(), "2D overlay over 3D scene", 800, 400, false);
   }

   // -------------------------------------------------------------------------

   // 3D stuff
   private ImmediateModeRenderer imr;
   private PerspectiveCamera camera3D;

   // 2D stuff
   private SpriteBatch spriteBatch;
   private OrthographicCamera camera2D;
   private Sprite[] ballSprites;

   // Misc
   private float cubeAngle = 0;

   // -------------------------------------------------------------------------

   @Override
   public void create() {
      this.imr = new ImmediateModeRenderer();
      this.camera3D = new PerspectiveCamera(75, 10, 5);
      camera3D.position.set(0, 0, 5);
      camera3D.update();

      this.spriteBatch = new SpriteBatch();
      this.camera2D = new OrthographicCamera(200, 100);
      camera2D.update();

      Texture ballTexture = new Texture(Gdx.files.classpath("overlay2d3d/ball.png"));
      ballTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
      
      Random rand = new Random();
      ballSprites = new Sprite[20];
      for (int i=0; i<ballSprites.length; i++) {
         float x = rand.nextFloat() * 200 - 100;
         float y = rand.nextFloat() * 100 - 50;

         ballSprites[i] = new Sprite(ballTexture);
         ballSprites[i].setSize(10, 10);
         ballSprites[i].setPosition(x-5, y-5);
      }
   }

   @Override
   public void render() {
      // Update
      cubeAngle += Gdx.graphics.getDeltaTime() * 120;

      // Render init
      GL10 gl = Gdx.gl10;
      gl.glEnable(GL10.GL_DEPTH_TEST);
      gl.glClearColor(1, 1, 1, 1);
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

      // 3D render
      camera3D.apply(gl);
      gl.glRotatef(cubeAngle, 0, 1, 0);
      drawCube();

      // 2D render
      spriteBatch.setProjectionMatrix(camera2D.combined);
      spriteBatch.begin();
      for (int i=0; i<ballSprites.length; i++)
         ballSprites[i].draw(spriteBatch);
      spriteBatch.end();
   }

   @Override public void resize(int width, int height) {}
   @Override public void pause() {}
   @Override public void resume() {}
   @Override public void dispose() {}

   // -------------------------------------------------------------------------

   private void drawCube() {
      imr.begin(GL10.GL_TRIANGLES);

      imr.color(1, 0, 0, 1); imr.vertex(-1, -1, +1);
      imr.color(1, 0, 0, 1); imr.vertex(+1, -1, +1);
      imr.color(1, 0, 0, 1); imr.vertex(-1, +1, +1);
      imr.color(1, 0, 0, 1); imr.vertex(+1, -1, +1);
      imr.color(1, 0, 0, 1); imr.vertex(-1, +1, +1);
      imr.color(1, 0, 0, 1); imr.vertex(+1, +1, +1);

      imr.color(0, 1, 0, 1); imr.vertex(+1, -1, +1);
      imr.color(0, 1, 0, 1); imr.vertex(+1, -1, -1);
      imr.color(0, 1, 0, 1); imr.vertex(+1, +1, +1);
      imr.color(0, 1, 0, 1); imr.vertex(+1, -1, -1);
      imr.color(0, 1, 0, 1); imr.vertex(+1, +1, +1);
      imr.color(0, 1, 0, 1); imr.vertex(+1, +1, -1);

      imr.color(0, 0, 1, 1); imr.vertex(+1, -1, -1);
      imr.color(0, 0, 1, 1); imr.vertex(-1, -1, -1);
      imr.color(0, 0, 1, 1); imr.vertex(+1, +1, -1);
      imr.color(0, 0, 1, 1); imr.vertex(-1, -1, -1);
      imr.color(0, 0, 1, 1); imr.vertex(+1, +1, -1);
      imr.color(0, 0, 1, 1); imr.vertex(-1, +1, -1);

      imr.color(1, 0, 1, 1); imr.vertex(-1, -1, -1);
      imr.color(1, 0, 1, 1); imr.vertex(-1, -1, +1);
      imr.color(1, 0, 1, 1); imr.vertex(-1, +1, -1);
      imr.color(1, 0, 1, 1); imr.vertex(-1, -1, +1);
      imr.color(1, 0, 1, 1); imr.vertex(-1, +1, -1);
      imr.color(1, 0, 1, 1); imr.vertex(-1, +1, +1);

      imr.end();
   }
}
Last edited by Obli on Fri Aug 05, 2011 11:53 am, edited 1 time in total.
Obli
 
Posts: 616
Joined: Mon Jan 10, 2011 6:18 pm
Location: Bordeaux, France

Re: mixing 2D + 3D

Postby mikepc » Fri Aug 05, 2011 11:34 am

OK,

so, I will hurry and try it later (when I finish my daily job as porting Android to MIPS platform),
with Scene and Actor classes that I am using for menu animations.
mikepc
 
Posts: 6
Joined: Wed Aug 03, 2011 8:10 pm
Location: Serbia

Re: mixing 2D + 3D

Postby yatayata » Fri Aug 05, 2011 6:17 pm

nice example Obli, thanks.

however this is using spriteBatch for 2D, which means you dont have access to sceneGraph, or I believe all the actor stuff inc tweening.
but if using groups then you have to render the whole scene (hence show/hide groups + double render hack)
if we had a group.render() rather than just stage.render() then we could combine things a bit better.

tx
Please "give something back" to the community and contribute your
knowledge to our libgdx-users project community wiki:
http://code.google.com/p/libgdx-users/
-> Just PM me your Gmail address so that I can add you as contributor
yatayata
 
Posts: 50
Joined: Mon Aug 01, 2011 2:01 pm
Location: tokyo

Re: mixing 2D + 3D

Postby Obli » Fri Aug 05, 2011 6:45 pm

Tweening support ? Raaah, you should have a look to the libgdx contributions forum :mrgreen:
I built a full universal Tweening engine in java, it can be used to tween every attribute (position, rotation, color, opacity, blur, whatever) of every possible object (sprites, cameras, actors, text, whatever again).

http://code.google.com/p/java-universal-tween-engine/

Have fun :mrgreen:

Image
Obli
 
Posts: 616
Joined: Mon Jan 10, 2011 6:18 pm
Location: Bordeaux, France

Re: mixing 2D + 3D

Postby mikepc » Sat Aug 06, 2011 11:21 am

Tweening support

Yeah,

I think I used the same tweening engine for the UI animation
U saw on my youtube channel.

Thank you obli for that 8-)
mikepc
 
Posts: 6
Joined: Wed Aug 03, 2011 8:10 pm
Location: Serbia

Previous

Return to Libgdx

Who is online

Users browsing this forum: No registered users and 1 guest