N Sharma
N Sharma

Reputation: 34507

Rotate the 3D Model from one axis opengl

I want to rotate the 3D Model from left part of the model. Below is my code but its not working.

My current result : https://www.dropbox.com/s/xesh2cszzg36eau/MOV_0009.mp4?m

My expected result: 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

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

Upvotes: 0

Views: 982

Answers (1)

jozxyqk
jozxyqk

Reputation: 17334

Very similar to this: Opengl rotation at a point

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.

enter image description here

So to rotate around point 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.

EDIT forgot it was android, sorry. Maybe try with this: What is the easiest way to draw line using OpenGL-ES (android)

Ignore the following unless desktop 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();
}

Upvotes: 1

Related Questions