【问题标题】:rotate texture around its center in opengl在opengl中围绕其中心旋转纹理
【发布时间】:2011-11-08 19:52:53
【问题描述】:

我正在尝试围绕其中心旋转纹理,但它没有给出预期的结果,请您查看代码并让我知道我缺少什么? 这是我的代码

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

}

}

【问题讨论】:

  • 我只看到黑屏

标签: android opengl-es rotation


【解决方案1】:

由于您的平面面向 Z 轴,因此您实际上需要在 Z 轴上旋转它,而不是在 X 轴上。

所以gl.glRotatef(30, 1, 0, 0) 将是gl.glRotatef(30, 0, 0, 1)

哦,平移值实际上是 -150 和 150,而不是 -50 和 50。由于您的平面是 100x100,并且它“开始”于 (100,100),因此 150 将是平面的中心。

但您应该尽量不要使用 x 和 y 变量。然后您的顶点将变为{ 0,0,0, 0,100,0, 100,0,0, 100,100,0 },在这种情况下,您将其转换为 (-50, -50, 0),然后转换为 (50, 50, 0)。

【讨论】:

  • :当我将 Z 轴更改为 1 时出现空白屏幕
  • :正如你所说,在这种情况下我已更改为 150 和 -150,它也仅显示黑屏....
  • 你在做透视投影吗?如果你把它改成正交投影,它仍然只显示黑屏吗?
  • 我只做正射投影
猜你喜欢
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多