Author Topic: The question about billboarding  (Read 2739 times)

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
The question about billboarding
« on: December 28, 2014, 08:34:38 am »
Hello Egon, I  use a single plane  to show tree in my game, I can place it on the "ground" when  not set it billboarding , but when I set it billboarding, It float abover the "ground", and I can't place it  on the "ground", It  seems out of contral ( I try used translate function). Could you help me to resove this question?  Thanks a lot ! :)

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: The question about billboarding
« Reply #1 on: December 28, 2014, 12:57:00 pm »
I think the problem is that objects with billboarding won't keep their Y-Axis parallel to the Y-axis in world coordinates. A solution would be to disable billboarding on the object and to calculate the the rotation yourself. It would be nice if this functionality could be integrated directly into jPCT because it's often needed for things like trees :) .

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: The question about billboarding
« Reply #2 on: December 28, 2014, 01:32:45 pm »
Thank you Lobby, How to  calculate the the rotation? Could you give me some example code ? Thanks !

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: The question about billboarding
« Reply #3 on: December 28, 2014, 02:02:27 pm »
Yes, billboards will face the camera on all axes. You can't lock one. If you need that, you have to calculate it yourself somehow. If you keep track of the camera's rotation around y, it should be pretty easy to do. Or just scrap billboarded planes and use some slightly more complex mesh instead.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: The question about billboarding
« Reply #4 on: December 28, 2014, 02:08:21 pm »
Hi Egon,I have no idea,  could you give me some example code? Thank you very much !

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: The question about billboarding
« Reply #5 on: December 28, 2014, 02:13:09 pm »
I don't have any. It depends on your application and how you setup the camera. If you can somehow keep track of or calculate the camera's rotation around y, all you might have to do is to apply the inverse rotation to your trees. But again, it depends on your scene and it might look crappy anyway when you come close to the trees.
Personally, i would use some more complex, real 3d models instead that don't need this treatment.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: The question about billboarding
« Reply #6 on: December 28, 2014, 02:18:49 pm »
Thanks for you advise ! :)

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: The question about billboarding
« Reply #7 on: December 28, 2014, 03:09:56 pm »
Here, have a little sample that shows how you might calculate the rotation.

Code: [Select]
package info.flowersoft.collisionproblemtext;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.GLSLShader;
import com.threed.jpct.IRenderHook;
import com.threed.jpct.Light;
import com.threed.jpct.Object3D;
import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.World;
import com.threed.jpct.util.ExtendedPrimitives;

import android.support.v7.app.ActionBarActivity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

GLSurfaceView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getSupportActionBar().hide();

view = new GLSurfaceView(this);
view.setEGLContextClientVersion(2);

view.setRenderer(new GLSurfaceView.Renderer() {

private World world;
private FrameBuffer buffer;

private Object3D sprite;

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
world = new World();
world.getCamera().moveCamera(Camera.CAMERA_MOVEIN, 1);
world.getCamera().setOrientation(new SimpleVector(0, 1, 1), new SimpleVector(0, -1, 1));
world.getCamera().moveCamera(Camera.CAMERA_MOVEOUT, 10);
world.getCamera().moveCamera(Camera.CAMERA_MOVELEFT, 5);

Light light = new Light(world);
light.setPosition(new SimpleVector(0, -50, 0));

Object3D plane = ExtendedPrimitives.createPlane(10);
plane.rotateZ(0.01f);
world.addObject(plane);

sprite = ExtendedPrimitives.createSprite();
sprite.setAdditionalColor(RGBColor.BLACK);
sprite.setLighting(Object3D.LIGHTING_NO_LIGHTS);
sprite.scale(2);
sprite.setBillboarding(Object3D.BILLBOARDING_DISABLED);
sprite.setRenderHook(new IRenderHook() {

@Override
public void setTransparency(float transparency) {
}

@Override
public void setCurrentShader(GLSLShader shader) {
}

@Override
public void setCurrentObject3D(Object3D obj) {
SimpleVector dir = world.getCamera().getPosition().calcSub(obj.getTransformedCenter());
dir.y = 0;
dir = dir.normalize();
dir.scalarMul(-1);
SimpleVector up = new SimpleVector(0, -1, 0);
obj.setOrientation(dir, up);
}

@Override
public boolean repeatRendering() {
return false;
}

@Override
public void onDispose() {
}

@Override
public void beforeRendering(int polyID) {
}

@Override
public void afterRendering(int polyID) {
}
});
world.addObject(sprite);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
buffer = new FrameBuffer(width, height);
}

@Override
public void onDrawFrame(GL10 gl) {
world.getCamera().moveCamera(Camera.CAMERA_MOVERIGHT, 0.005f);

buffer.clear();
world.renderScene(buffer);
world.draw(buffer);
buffer.display();
}
});

setContentView(view);
}
}

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: The question about billboarding
« Reply #8 on: December 29, 2014, 02:54:11 am »
Thank you Lobby, I use

                  SimpleVector dir = world.getCamera().getPosition().calcSub(obj.getTransformedCenter());
                  dir.y = 0;
                  dir = dir.normalize();
                  dir.scalarMul(-1);
                  SimpleVector up = new SimpleVector(0, -1, 0);
                  obj.setOrientation(dir, up);

in ondrawframe, but it seems no effect , why? thanks


 Does your method  must set camera in special ? my camera is movement in my game.
« Last Edit: December 29, 2014, 04:42:19 am by gamenewer »

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: The question about billboarding
« Reply #9 on: December 29, 2014, 02:32:37 pm »
A special camera set shouldn't be needed, but the camera shouldn't look at your object from above.

What do you mean by "seems no effect"? Don't you see the object anymore or doesn't it rotate correctly?

If you can't see the object anymore maybe your object is flat and is rotated so that it's absolutely flat for the camera. Then a rotation of the object around Y-Axis may help.

If it doesn't rotate correctly check that you don't enable billboarding anymore, because it would override our own calculations.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: The question about billboarding
« Reply #10 on: December 29, 2014, 02:37:24 pm »
I mean the object  is't  rotate correctly. it seems not rotate. The object is not enalbe billboarding. I'm sorry for my poor english :)

If object must setRenderHook ?  I only calculat in ondrawframe .
« Last Edit: December 29, 2014, 02:47:21 pm by gamenewer »

Offline Lobby

  • int
  • **
  • Posts: 66
    • View Profile
    • flowersoft
Re: The question about billboarding
« Reply #11 on: December 29, 2014, 10:28:42 pm »
I just use the RenderHook for convenience. It shouldn't make a differece whether doing it in onDrawFrame oder within the hook...

Maybe you can test it with using a RenderHook. If this also doesn't work you may create a little sample project that shows the probem.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: The question about billboarding
« Reply #12 on: December 30, 2014, 06:03:29 am »
Thank you Lobby, I will try it and tell you the result  :)

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: The question about billboarding
« Reply #13 on: December 30, 2014, 02:15:00 pm »
Hi Lobby, I use your example , it works  ok. I  will check if  there is something wrong in my code, Thank you very much ! :)