简体   繁体   中英

rotate texture around its center in opengl

I am trying to rotate the texture around its center but it is not giving the expected results can you please see the code and let me know what i am missing? here is my code

  public class GLRenderer implements Renderer {

Context context;
Square s;
float x = 100 ,y = 100 ;
float w,h;

public GLRenderer(Context  c) {
    context = c;
    s = new Square(x,y);
}



@Override
public void onDrawFrame(GL10 gl) {
    s.draw(gl);

}

@Override
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_PROJECTION);
    gl.glLoadIdentity();
    gl.glEnable(GL10.GL_TEXTURE_2D);

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    s.loadGLTexture(gl, R.drawable.ic_launcher);

}

public class Square{

    int textures[] = new int[1];
    FloatBuffer vertexbuffer;
    FloatBuffer texturebuffer;



    float texture[] ={
             1.0f, 1.0f,
             1.0f, 0.0f,
             0.0f, 1.0f,
             0.0f, 0.0f

    };

    public Square(float x, float y){


        float vertices[] = {
                x,y,0,
                x,y+100,0,
                x+100,y,0,
                x+100,y+100,0
        };

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        vertexbuffer = vbb.asFloatBuffer();
        vertexbuffer.put(vertices);
        vertexbuffer.position(0);

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

    }

    public void draw(GL10 gl) {
        gl.glPushMatrix();
        gl.glTranslatef(-150, -150, 0);
        gl.glRotatef(30,0, 0,1 );
        gl.glTranslatef(150, 150, 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+10;
        gl.glPopMatrix();

    }

    public void loadGLTexture(GL10 gl, int drawable) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                drawable);

        gl.glGenTextures(1, textures, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        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);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();
    }

}

}

Since your plane is facing the Z axis, you actually need to rotate it on the Z axis, and not on the X.

So gl.glRotatef(30, 1, 0, 0) would be gl.glRotatef(30, 0, 0, 1)

Oh, and the translation values are actually -150 and 150, not -50 & 50. Since your plane is 100x100 and it "begins" in (100,100) so 150 would be the center of the plane.

But you should try not using that x and y variables. Then your vertices would become { 0,0,0, 0,100,0, 100,0,0, 100,100,0 } , it's in this case that you'd translate it by (-50, -50, 0) and then (50, 50, 0).

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