Ok guys, actually I am stuck on my program since 2 days and I dont know what to do...
Right now, I am rendering a cylinder, I can pan and zoom using this:
1 2 3 4 5 6 7 8
| display() { ... glu.gluLookAt(translationX/gLAutoDrawable.getWidth()*scale*2, -translationY/gLAutoDrawable.getHeight()*scale*2, 2, translationX/gLAutoDrawable.getWidth()*scale*2, -translationY/gLAutoDrawable.getHeight()*scale*2, 0, ... } |
I can also rotate using some custom functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| display() { ...
glu.gluLookAt
...
gl.glMultMatrixf(currentRotation, 0);
...
}
mouseDragged() {
...
if(rotationMode) { float newRotation[] = multiply(rotationX(-dy), rotationY(-dx)); currentRotation = multiply(currentRotation, newRotation); rotationX +=dx; rotationY +=dy; } ... }
private static float[] rotationX(float angleDeg) { float m[] = identity(); float angleRad = (float)Math.toRadians(angleDeg); float ca = (float)Math.cos(angleRad); float sa = (float)Math.sin(angleRad); m[ 5] = ca; m[ 6] = sa; m[ 9] = -sa; m[10] = ca; return m; }
private static float[] rotationY(float angleDeg) { float m[] = identity(); float angleRad = (float)Math.toRadians(angleDeg); float ca = (float)Math.cos(angleRad); float sa = (float)Math.sin(angleRad); m[ 0] = ca; m[ 2] = -sa; m[ 8] = sa; m[10] = ca; return m; }
private static float[] multiply(float m0[], float m1[]) { float m[] = new float[16]; for (int x=0; x < 4; x++) { for(int y=0; y < 4; y++) { m[x*4 + y] = m0[x*4+0] * m1[y+ 0] + m0[x*4+1] * m1[y+ 4] + m0[x*4+2] * m1[y+ 8] + m0[x*4+3] * m1[y+12]; } } return m; } |
This special rotation was mandatory because if I use glRotate to rotate my cylinder a first time over the horizontal plane, lets say, then my modelview coordinates rotates as well, and I dont want to have this, since I still need to rotate over the vertical axes
Now, the problem is that the rotation is performed taking the modelview origin as the rotation point...
This means that if I pan (aka move the camera) I would like to see a rotation over the center of my screen, and not the center of the modelview (that has been panned)
I hope it's clear, otherwise I can post something else to illustrate it better..