简体   繁体   中英

issue in rotating texture around its center in OpenGL

I am trying to rotate the texture around its center and i have achieved by this code but while it is rotating the image is also shown with some angle(so image is cant see properly in some angles) as shown in attached pics can help me to solve this issue

here is my code

    public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    gl.glOrthof(0, 320, 0, 480, 0, 1);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glEnable(GL10.GL_TEXTURE_2D);

}

vertices of square are

float vertices[] = {

                -.25f,.25f,0,
                -.25f,-.25f,0,
                .25f,.25f,0,
                .25f,-.25f,0
        };

and this is my draw method

public void draw(GL10 gl) {
        gl.glPushMatrix();



        gl.glTranslatef(-.125f, -.125f, 0f);
        gl.glRotatef(r,0, 0,-1 );
        gl.glTranslatef(.125f, .125f, 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, 4);

        //Disable the client state before leaving
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        r = r+1;
        gl.glPopMatrix();

    }

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

It looks like your texture coordinates may simply be referencing too much of the texture — which can happen if some part of your code is padding a non power-of-two image up to a power-of-two texture size but not passing on the relevant knowledge. So you're rotating around the centre of the texture but your actual image uses only a portion in the corner of your texture.

I can't see anything in your example code to show how you're loading the image or who is putting values into texturebuffer but those would be smart areas to look at.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM