views:

64

answers:

2
+1  Q: 

Rendering a square

Hey guys,

The following is giving me triangles not squares, anyone know why??

    //Defining as...
        addcube(2,-2, 1, -1, 10);
        addcube(float lx, float hx, float ly, float hy, floathz){
            final float array1[] = new float[] {
            //Front face
            lx, ly, hz,
            lx, hy, hz,
            hx, hy, hz,
            hx, ly, hz
    };
 }

    //Drawing as...
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;

I've attempted adding gl.glDisable(GL10.GL_CULL_FACE); but that just puts the remaining square in the foreground. I've included the draw methods...

public void onDrawFrame(GL10 gl) {
    onDrawFrameCounter++;
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    bindCameraTexture(gl);
    System.out.println(Global.bearing);
    float bear = Global.bearing;
    gl.glLoadIdentity();
    gl.glNormal3f(0,0,1);
    System.out.println("ARRAY!: " + GLCamTest.array.length);
    p = 0;
    gl.glRotatef(bear, 1, 0, 0);

    int e = 0;  
    for(int q=0; q < Global.cubes;q++){
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    }
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);

    float ratio = (float) width / height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 20);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    gl.glClearColor(0, 0, 0, 0);
    gl.glDisable(GL10.GL_CULL_FACE);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    cubeBuff = makeFloatBuffer(GLCamTest.array);
    texBuff = makeFloatBuffer(camTexCoords);                
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
A: 

Haven't tested it but I suppose you should be passing GL_QUAD_STRIP instead of GL_TRIANGLE_STRIP:

gl.glDrawArrays(GL10.GL_QUAD_STRIP, e, 4);
dark_charlie
No such thing as `GL_QUAD_STRIP` in OpenGL-ES it appears.
Ulkmun
Try GL_QUADS. And if that doesn't work GL_POLYGON should since you're telling it to count 4 vertices in. At the moment you're telling it to try draw triangles.
AaronM
neither of those work. nothing shows.
Ulkmun
+1  A: 

Try using the points in 'zigzag order' i.e.

xl,yl
xl,yh
xh,yl  // this is hypotenuse of first triangle
xh,yh  // this is endpoint of last triangle, so previous line is hypotenuse

you are probably getting some kind of two-triangle broken shape.

you see, the square is made from triangles, and they are drawn in order of the points!

Sanjay Manohar