我的顶点只有一个三角形显示?

时间:2013-04-10 19:41:58

标签: android opengl-es textures vertices

这是我的代码为2个三角形的树与索引,只有广场的下半部分显示,我无法弄清楚我搞砸了,我已经有一段时间了,谢谢你的时间

public class sword extends GLGame {

@Override
public Screen getStartScreen() {
    return new LightScreen(this);
}

class LightScreen extends GLScreen {
    float angle;
    Vertices3 cube,trees;
    PointLight light;
    EulerCamera camera;
    Texture tree;
    Texture buttonTexture;
    SpriteBatcher batcher;
    Camera2D guiCamera;
    TextureRegion buttonRegion;
    Vector2 touchPos;
    float lastX = -1;
    float lastY = -1;

    public Context mContext;
    protected float[] mVMatrix = new float[16];
    protected float[] mPMatrix = new float[16];
    public float swordtouchx,swordtouchy,swordtouchz;
    SkyBox skybox;

    public LightScreen(Game game) {
        super(game);
        mContext = getApplicationContext();
        tree = new Texture(glGame,"tree.png");
        cube = ObjLoader.load(glGame, "swordal.obj");
        light = new PointLight();
        light.setPosition(3, 3, -3);
        camera = new EulerCamera(80, 480
                / (float) 320, 1, 100);
        camera.getPosition().set(0, 0, 0);
        batcher = new SpriteBatcher(glGraphics, 400);
        guiCamera = new Camera2D(glGraphics, 480, 320);

        touchPos = new Vector2();

        GL10 gl = glGraphics.getGL();
        skybox = new SkyBox(mContext);
        skybox.loadTexture(gl);
        trees = new Vertices3(glGraphics,4, 6, false, true,false);
        float[] vertices = { -0.5f, -0.5f, 0.5f, 0, 1,
                0.5f, -0.5f, 0.5f, 1, 1,
                0.5f,  0.5f, 0.5f, 1, 0,
               -0.5f,  0.5f, 0.5f, 0, 0};
         short[] indices = { 0, 1, 2, 3, 2, 0};
        trees.setVertices(vertices, 0, 20);
        trees.setIndices(indices, 0, indices.length);

    }

    @Override
    public void resume() {

    }



    @Override
    public void update(float deltaTime) {
        angle += deltaTime * 5;
        game.getInput().getTouchEvents();
        float x = game.getInput().getTouchX(0);
        float y = game.getInput().getTouchY(0);
        guiCamera.touchToWorld(touchPos.set(x, y));
        swordtouchx = x;
        swordtouchy = y;

        if(game.getInput().isTouchDown(0)) {
            swordtouchy = -1f;
            if(touchPos.x < 64 && touchPos.y < 64) {
                camera.rotate(5, 0);

            } else if(touchPos.x < 128 && touchPos.x > 64 && touchPos.y <64){
                camera.rotate(-5, 0);



            }
        } else { 

            lastX = -1;
            lastY = -1;
        }
        List<TouchEvent> events = game.getInput().getTouchEvents();
        int len = events.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = events.get(i);
            if (event.type == TouchEvent.TOUCH_UP){
                swordtouchy = 600;
            }
        }
        if(camera.getDirection().z < 0){
            swordtouchz = camera.getDirection().z-1.5f;
        }else{
            swordtouchz = camera.getDirection().z +1.5f;
        }
        if(camera.getDirection().x < 0){
            swordtouchx = camera.getDirection().x -0.5f;
        }else{
            swordtouchx = camera.getDirection().x +0.5f;
        }
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glViewport(0, 0,glGraphics.getWidth(), glGraphics.getHeight());


        camera.setMatrices(gl);

        //skybox.draw(gl);


        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        //gl.glEnable(GL10.GL_LIGHTING);

        trees.bind();
        tree.bind();
        float o = 0;
        for(int i = 0;i<50;i++){
            gl.glPushMatrix();
            gl.glTranslatef(o, -2.5f, 5);
            trees.draw(GL10.GL_TRIANGLES, 0, trees.getNumVertices());

            gl.glPopMatrix();
            o+=1f;}

        trees.unbind();

        gl.glDisable(GL10.GL_BLEND);
        //gl.glDisable(GL10.GL_TEXTURE_2D);
        cube.bind();
        //light.enable(gl, GL10.GL_LIGHT0);




            gl.glTranslatef( swordtouchx,camera.getPosition().y + swordtouchy, swordtouchz);
        //gl.glRotatef(90, 0, 0, 1);
        gl.glRotatef(90, 1, 0, 0);

        cube.draw(GL10.GL_TRIANGLES, 0, cube.getNumVertices());
        cube.unbind();


        guiCamera.setViewportAndMatrices();

        gl.glDisable(GL10.GL_DEPTH_TEST);

    }

    @Override
    public void pause() {
    }

    @Override
    public void dispose() {
    }
    protected void setSkybox(int resourceId) {

    }
}

}

1 个答案:

答案 0 :(得分:2)

看看你给出指数的方式,你似乎混淆了上涨的订单:

short[] indices = { 0, 1, 2, 3, 2, 0};

虽然您的第一个三角形是以逆时针顺序给出的,但您应该按时钟顺序添加第二个三角形。

很可能你的第二个三角形被淘汰了。请尝试指定这样的坐标:

short[] indices = { 0, 1, 2, 3, 0, 2};

除非被告知所以glDisable(GL_CULL_FACE)加速绘制opengl将丢弃任何三角形,法线指向远离观察者,因为它们被建议从那个角度看不见。

因为您的坐标是这样的:

   3        2
    +------+
    |      |
    |      |
    +------+
   0        1

序列3, 2, 0会像这样结束:

  3        2
     ---->
    ^   / 
    |  / 
    | / 
     v    
   0 

相反,这是您正在寻找的这种缠绕顺序:

  3        2
     <----
    |   ^
    |  / 
    | / 
    v  
   0