简体   繁体   English

从一个轴旋转3D模型

[英]Rotate the 3D Model from one axis opengl

I want to rotate the 3D Model from left part of the model. 我想从模型的左侧旋转3D模型。 Below is my code but its not working. 下面是我的代码,但无法正常工作。

My current result : https://www.dropbox.com/s/xesh2cszzg36eau/MOV_0009.mp4?m 我当前的结果: https : //www.dropbox.com/s/xesh2cszzg36eau/MOV_0009.mp4?m

My expected result: www.dropbox.com/s/ozt7beo4gz5q293/demo2__A.avi 我的预期结果: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;
        }
    }
}

Below code execute when double tap on left, right, bottom, top on the GLSurfaceView 双击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;
    }
}

Very similar to this: Opengl rotation at a point 与此非常相似: 某个点的Opengl旋转

You're already rotating about the correct axis, just around the wrong pivot. 您已经绕着正确的轴旋转,绕着错误的枢轴旋转。 You want to translate the point to rotate around to be at the origin, then apply the rotation, then translate back again. 您想平移要旋转的点,使其位于原点,然后应用旋转,然后再次平移。

在此处输入图片说明

So to rotate around point x, y... 所以绕点x,y旋转...

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

drawMyObject()

Creating a function drawAxes() which draws 3 r/g/b lines can be very helpful for figuring out transformation orders and will allow you to "see" the otherwise invisible origins, directions and scales after each transform. 创建一个绘制3条r / g / b线的drawAxes()函数对于确定转换顺序非常有帮助,并且可以让您“看到”在每次转换后原本不可见的原点,方向和比例。

EDIT forgot it was android, sorry. 编辑忘记了它是android,抱歉。 Maybe try with this: What is the easiest way to draw line using OpenGL-ES (android) 也许尝试一下: 使用OpenGL-ES(android)绘制线条的最简单方法是什么

Ignore the following unless desktop OpenGL: 除非桌面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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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