0

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;
    }
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
N Sharma
  • 33,489
  • 95
  • 256
  • 444

1 Answers1

1

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();
}
Community
  • 1
  • 1
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • I am using the 3D so i have x, y, z. Let me know what values should i use in `translate(-x, -y)`. Initially x = 0, y = 0, z = 6. can you give an example to draw the axes 3 rgb – N Sharma Sep 06 '13 at 14:57
  • it's the same for 3D, just with 3 coordinates. The only difference is you can choose any point along the axis you want to be the pivot. Just use translate(-x, -y, -z) etc. – jozxyqk Sep 06 '13 at 15:03
  • I used this `gl.glTranslatef(-mOrigin.x, -mOrigin.y, -mOrigin.z); gl.glRotatef(mRotate.x, 0f, 1f, 0f); gl.glRotatef(mRotate.y, 0f, 0f, 1f); gl.glRotatef(mRotate.z, 0f, 0f, 1f); gl.glTranslatef(mOrigin.x, mOrigin.y, mOrigin.z);` but still having same problem. dropbox.com/s/xesh2cszzg36eau/MOV_0009.mp4?m – N Sharma Sep 06 '13 at 15:57
  • A bit of a late reply, but calling drawAxes (mentioned above) right before and after your rotate calls will show you the pivot point. My only guess is mOrigin isn't the point you want to rotate round. – jozxyqk Sep 10 '13 at 03:50
  • I dont have lines of code which draw 3 r/g/b lines. Please help me for this – N Sharma Sep 10 '13 at 05:00
  • 1.0f, 0.0f, 0.0f These are RGB Please confirm it because in android i have to give 4 (RGBA) alpha values as well. Please let me know what value should i pass to alpha. `gl.glBegin(GL10.GL_LINES);` and `gl.glEnd();` are not available in android opengl – N Sharma Sep 10 '13 at 05:56
  • Yes, sorry I missed that. I guess the matrix calls threw me off (I was unaware Android/GLES2 had them). Someone's written some code to draw lines here: http://stackoverflow.com/questions/16027455/what-is-the-easiest-way-to-draw-line-using-opengl-es-android GLES2 certainly makes it more tricky to start learning GL. – jozxyqk Sep 10 '13 at 06:48