0

我想在OpenGL中改变对象的位置,我找到了这个类,我想写函数改变。当程序调用改变我想改变对象的位置这个类创建一个正方形和纹理......我想改变位置以像素为单位...

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);
    }
}
4

1 回答 1

1

OpenGL 是绘图 API 而不是场景图。在 OpenGL 中没有模型、对象或场景。只有点、线和三角形被绘制到基于像素的帧缓冲区。这意味着,场景中的每一个变化都必须由场景的完整重绘来补充。

因此,如果您想在屏幕上移动某些东西,您可以更改控制位置的值变量并进行完全重绘。

于 2013-09-17T20:16:35.353 回答