从一个轴opengl旋转3D模型

时间:2013-09-06 10:34:10

标签: android opengl-es 3d

我想从模型的左侧部分旋转3D模型。下面是我的代码,但它不起作用。

我目前的结果:https://www.dropbox.com/s/xesh2cszzg36eau/MOV_0009.mp4?m

我的预期结果:www.dropbox.com/s/ozt7beo4gz5q293/demo2__A.avi

private class Renderer implements GLSurfaceView.Renderer {
    public Renderer() {
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        setZOrderOnTop(true);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(0.0f,0.0f,0.0f, 0.0f);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glShadeModel(GL10.GL_SMOOTH);
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        mViewWidth = (float)w;
        mViewHeight = (float)h;
        gl.glViewport(0,0,w,h);

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 45, mViewWidth/mViewHeight, 0.1f, 100f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glDisable(GL10.GL_DITHER); 

        gl.glMatrixMode(GL10.GL_MODELVIEW); //making sure OpenGL currently in model view
        gl.glLoadIdentity();                //clear the model view matrix to identity matrix

        if(mOrigin != null && mRotate != null) {
            if(isDoubleClick) {
                isDoubleClick = false;
                gl.glRotatef(mRotate.z, 0, 0, 1);
            } else {
                gl.glTranslatef(mOrigin.x, mOrigin.y, -10.0f + mOrigin.z);
                gl.glRotatef(mRotate.x, 1f, 0f, 0f);
                gl.glRotatef(mRotate.y, 0f, 1f, 0f);
                gl.glRotatef(mRotate.z, 0f, 0f, 1f);
            }
        }

        if(mModel != null) {
            mModel.draw(gl, mContext);
            if(!RendererView.textureFileName.equals(""))
                mModel.bindTextures(mContext, gl);
        }

        if(isPictureTake) {
            w = getWidth();
            h = getHeight();
            b = new int[w*(y+h)];
            bt = new int[w*h];

            IntBuffer ib = IntBuffer.wrap(b);
            ib.position(0);
            gl.glReadPixels(0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
            createBitmapFromGLSurface(mContext);
            isPictureTake = false;
        }
    }
}

下面的代码在GLSurfaceView

上的左,右,底,顶部双击时执行
if(mRotate != null && !AddProductsActivity.optionFlag) {
    if (col == 0 && row == 1 && mRotate.z >= 135) {
        mRotationAxis = Z_AXIS;
        mRotate.z = mRotate.z - 10; //Left Movement
        isDoubleClick = true;
    }
    else if (col == 1 && row == 0 && mRotate.x >= 45) {
        mRotationAxis = X_AXIS;
        mRotate.x = mRotate.x - 10; //Top Movement
        isDoubleClick = true;
    }
    else if (col == 1 && row == 2 && mRotate.x <= 135) {
        mRotationAxis = X_AXIS; //Bottom Movement
        mRotate.x = mRotate.x + 10;
        isDoubleClick = true;
    }
    else if (col == 2 && row == 1 && mRotate.z <= 225) {
        mRotationAxis = Z_AXIS;
        mRotate.z = mRotate.z + 10;  //Right Movement
        isDoubleClick = true;
    }
}

1 个答案:

答案 0 :(得分:1)

非常相似:Opengl rotation at a point

你已经围绕正确的轴旋转,绕着错误的枢轴。您希望将点转换为旋转到原点,然后应用旋转,然后再转换回来。

enter image description here

所以绕点x,y ...旋转

translate(-x, -y)
rotate(...)
translate(x, y)

drawMyObject()

创建绘制3 r / g / b线的函数drawAxes()对于计算转换顺序非常有用,并且允许您在每次转换后“看到”其他不可见的原点,方向和比例。

编辑忘了它是安卓,抱歉。也许试试这个:What is the easiest way to draw line using OpenGL-ES (android)

除非桌面OpenGL:

,否则请忽略以下内容
void drawAxes()
{
    glBegin(GL_LINES);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 1.0f);
    glEnd();
}