Question

This is my code for 2 triangles for a tree with indices and only the bottom half of the square is showing, i cant figure out where im messing up, i been up a while, thanks for your time

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) {

    }
}

}

Was it helpful?

Solution

Taking a look at the way you are giving indices it seems you mixed up winding orders:

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

While your first triangle is given in counter clockwise order as it should you are adding the second one in clock wise order.

Most probably your second triangle is just culled away. Try specifying your coordinates like this instead:

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

Unless been told to so glDisable(GL_CULL_FACE) to speed up drawing opengl will discard any triangles with normals pointing away from the viewer as they are suggested to be invisible from that point of view.

As your coordinates are given like that:

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

The sequence 3, 2, 0 will wind up like this:

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

Instead it is this winding order you're looking for:

  3        2
     <----
    |   ^
    |  / 
    | / 
    v  
   0 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top