Filsoft
Filsoft

Reputation: 1

Android OpenGL change position

I want to change position of object in OpenGL,I found this class and i want to write function change.When program call change i want to change positon of object This class create a square and texture over...And i want to change position in pixels...

public class Square {

    private FloatBuffer vertexBuffer;       // buffer holding the vertices
    private float vertices[] = {
                    -1.0f, -1.0f,  0.0f,            // V1 - bottom left
                    -1.0f,  1.0f,  0.0f,            // V2 - top left
                     1.0f, -1.0f,  0.0f,            // V3 - bottom right
                     1.0f,  1.0f,  0.0f                     // V4 - top right
    };

    private FloatBuffer textureBuffer;      // buffer holding the texture coordinates
    private float texture[] = {            
                    // Mapping coordinates for the vertices
                    0.0f, 1.0f,             // top left             (V2)
                    0.0f, 0.0f,             // bottom left  (V1)
                    1.0f, 1.0f,             // top right    (V4)
                    1.0f, 0.0f              // bottom right (V3)
    };

    private FloatBuffer textureBuffer1;     // buffer holding the texture coordinates
    private float texture1[] = {                   
                    // Mapping coordinates for the vertices
                    2.0f, 1.0f,             // top left             (V2)
                    2.0f, 0.0f,             // bottom left  (V1)
                    1.0f, 1.0f,             // top right    (V4)
                    1.0f, 0.0f              // bottom right (V3)
    };
    /** The texture pointer */
    private int[] textures = new int[1];

    public Square() {
            // a float has 4 bytes so we allocate for each coordinate 4 bytes
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
            byteBuffer.order(ByteOrder.nativeOrder());

            // allocates the memory from the byte buffer
            vertexBuffer = byteBuffer.asFloatBuffer();

            // fill the vertexBuffer with the vertices
            vertexBuffer.put(vertices);

            // set the cursor position to the beginning of the buffer
            vertexBuffer.position(0);

            byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
            byteBuffer.order(ByteOrder.nativeOrder());
            textureBuffer = byteBuffer.asFloatBuffer();
            textureBuffer.put(texture);
            textureBuffer.position(0);


            byteBuffer = ByteBuffer.allocateDirect(texture1.length * 4);
            byteBuffer.order(ByteOrder.nativeOrder());
            textureBuffer1 = byteBuffer.asFloatBuffer();
            textureBuffer1.put(texture1);
            textureBuffer1.position(0);
    }

    /**
     * Load the texture for the square
     * @param gl
     * @param context
     */
    public void loadGLTexture(GL10 gl, Context context) {
            // loading texture
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                            R.drawable.a);

            // generate one texture pointer
            gl.glGenTextures(1, textures, 0);
            // ...and bind it to our array
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

            // create nearest filtered texture
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);



            // Use Android GLUtils to specify a two-dimensional texture image from our bitmap
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

            // Clean up
            bitmap.recycle();
    }


    /** The draw method for the square with the GL context */
    public void draw(GL10 gl) {
            // bind the previously generated texture
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

            // Point to our buffers
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            // Set the face rotation
            gl.glFrontFace(GL10.GL_CW);

            // Point to our vertex buffer

            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

            // Draw the vertices as triangle strip
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

            //Disable the client state before leaving
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }
}

Upvotes: 0

Views: 934

Answers (1)

datenwolf
datenwolf

Reputation: 162164

OpenGL is a drawing API not a scene graph. In OpenGL there are no models, objects or scene. There are only points, lines and triangles, drawn to a pixel based framebuffer. What this means is, that every change in your scene must be complemented by a full redraw of the scene.

So if you want to move something on the screen, you change the value variable(s) controlling the position and do a full redraw.

Upvotes: 1

Related Questions