我的场景颠倒了

时间:2014-01-15 11:12:30

标签: android graphics opengl-es libgdx

我在渲染我正在为一个小项目开发的场景时遇到问题。看来我的场景实际上是倒置的,我无法弄清楚原因。

enter image description here

您可以看到选项按钮实际上是以正确的方向渲染,而场景的其余部分则不是。我试图简单地反转着色器返回的位置的y坐标,但它与棕色立方体的运动混淆。

这是我写的着色器:

#ifndef GL_ES
#version 330
#endif

uniform mat4 u_projectionMatrix;
uniform mat4 u_viewMatrix;

uniform vec3 u_scale;
uniform mat4 u_rotation;
uniform vec3 u_position;
uniform vec2 u_textureScale;
uniform vec2 u_textureOffset;

attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;

varying vec4 v_position;
varying vec4 v_normal;
varying vec2 v_texCoords;

void main()
{
// Not sure why some vec4's need 0 or 1, since it's the w-coordinate (shouldn't this      always be 1 prior to applying the projection matrix?).
v_position = vec4(u_position, 0) + (u_rotation * (a_position * vec4(u_scale, 1)));
v_normal = normalize(u_rotation * vec4(a_normal, 0));
v_texCoords = (a_texCoord0 * u_textureScale) + u_textureOffset;

gl_Position = u_projectionMatrix * u_viewMatrix * v_position;
//gl_Position.y = -gl_Position.y;
} 

这是相机的代码:

public void create() {
...

_camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

_camera.translate(0, -10, 20);
_camera.lookAt(new Vector3(0,0,0));
_camera.near = 0.1f;

...
}

有人可以帮忙吗?

编辑:更改向上矢量的y坐标可修复场景的方向问题,但会使按钮消失,就像它没有渲染一样。这是我用来绘制选项按钮的代码:

public void create() {
    //camera code and other initialization stuff...

    // Create new stage for the ui.
    toggleOptions = new Stage();


    // Add a menu button.
    Table toggleBtn = new Table(skin);
    toggleOptions.addActor(toggleBtn);

    initUI();

    ...
    }

    private void initUI(){

    final TextButton toggleMenuButton = new TextButton("Options", skin);

    menuWidgets.put("toggleMenu", toggleMenuButton);
    toggleMenuButton.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            // Toggle the menu.
            showMenu = !showMenu;
        }
    });

    // Set up button so that is appears in the bottom center of the screen (with some padding).
    toggleMenuButton.setPosition((Gdx.graphics.getWidth() / 2.0f) - toggleMenuButton.getWidth() / 2.0f, padY);


    toggleMenuButton.setWidth(toggleMenuButton.getWidth() + padX);

    // Add the toggle button.
    toggleOptions.addActor(toggleMenuButton);
}

    public void render() {
    ...
    toggleOptions.act();
    toggleOptions.draw();
    ...
    }

编辑2:好的,问题是按钮是在飞机和立方体后面渲染的,因为绘制调用是错误放置的(它应该是最后一次调用,而是在平面绘制和立方体绘制之前调用它)。感谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

_camera.setOrientation(180.0f,0.0f,0.0f,1.0f)