views:

53

answers:

1

Hey guys,

How would I rotate a view in degrees? I've got the following code setting up my view...

                   public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback,
    Camera.PreviewCallback, Renderer {

int onDrawFrameCounter=1;
int[] cameraTexture;
byte[] glCameraFrame=new byte[256*256]; //size of a texture must be a power of 2
FloatBuffer cubeBuff;
FloatBuffer texBuff;
public float startbear;

public GLLayer(Context c) {
    super(c);

    this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    this.setRenderer(this);
    this.getHolder().setFormat(PixelFormat.TRANSPARENT);
}

public void onDrawFrame(GL10 gl) {
    onDrawFrameCounter++;

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    bindCameraTexture(gl);

    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, 4.2f, 0, 0, 0, 0, 1, 0);

    //gl.glRotatef(onDrawFrameCounter,1,0,0); //Rotate the camera image
    //gl.glRotatef((float)Math.sin(onDrawFrameCounter/20.0f)*40,0,1,0); //Rotate the camera image
    //gl.glRotatef((float)Math.cos(onDrawFrameCounter/40.0f)*40,0,0,1); //Rotate the camera image

    gl.glNormal3f(0,0,1);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);          
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,12, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,16, 4);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,20, 4);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);

    float ratio = (float) width / height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, 4.2f, 0, 0, 0, 0, 1, 0);        
}


public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    startbear = Global.bearing;

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

    gl.glClearColor(0, 0, 0, 0);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glEnable(GL10.GL_DEPTH_TEST);

    cubeBuff = makeFloatBuffer(camObjCoord);
    texBuff = makeFloatBuffer(camTexCoords);        
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}   

/**
 * Generates a texture from the black and white array filled by the onPreviewFrame
 * method.
 */
void bindCameraTexture(GL10 gl) {
    synchronized(this) {
        if (cameraTexture==null)
            cameraTexture=new int[1];
        else
            gl.glDeleteTextures(1, cameraTexture, 0);

        gl.glGenTextures(1, cameraTexture, 0);
        int tex = cameraTexture[0];
        gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
        gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE, 256, 256,   0, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE, ByteBuffer.wrap(glCameraFrame));
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    }
}

/**
 * This method is called if a new image from the camera arrived. The camera
 * delivers images in a yuv color format. It is converted to a black and white
 * image with a size of 256x256 pixels (only a fraction of the resulting image
 * is used). Afterwards Rendering the frame (in the main loop thread) is started by
 * setting the newFrameLock to true. 
 */
public void onPreviewFrame(byte[] yuvs, Camera camera) {        
    int bwCounter=0;
    int yuvsCounter=0;
    for (int y=0;y<160;y++) {
        System.arraycopy(yuvs, yuvsCounter, glCameraFrame, bwCounter, 240);
        yuvsCounter=yuvsCounter+240;
        bwCounter=bwCounter+256;
    }
}

FloatBuffer makeFloatBuffer(float[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(arr);
    fb.position(0);
    return fb;
}

final static float camObjCoord[] = new float[] {
            // FRONT
             -2.0f, -1.5f,  12.0f,
              2.0f, -1.5f,  12.0f,
             -2.0f,  1.5f,  12.0f,
              2.0f,  1.5f,  12.0f,
             // BACK
             -2.0f, -1.5f,  8.0f,
             -2.0f,  1.5f,  8.0f,
              2.0f, -1.5f,  8.0f,
              2.0f,  1.5f,  8.0f,
             // LEFT
             -2.0f, -1.5f,  12.0f,
             -2.0f,  1.5f,  12.0f,
             -2.0f, -1.5f,  8.0f,
             -2.0f,  1.5f,  8.0f,
             // RIGHT
              2.0f, -1.5f,  8.0f,
              2.0f,  1.5f,  8.0f,
              2.0f, -1.5f, 12.0f,
              2.0f,  1.5f, 12.0f,
             // TOP
             -2.0f,  1.5f, 12.0f,
              2.0f,  1.5f, 12.0f,
             -2.0f,  1.5f,  8.0f,
              2.0f,  1.5f,  8.0f,
             // BOTTOM
             -2.0f, -1.5f, 12.0f,
             -2.0f, -1.5f,  8.0f,
              2.0f, -1.5f, 12.0f,
              2.0f, -1.5f,  8.0f,
        };
        final static float camTexCoords[] = new float[] {
            // Camera preview
             0.0f, 0.0f,
             0.9375f, 0.0f,
             0.0f, 0.625f,
             0.9375f, 0.625f,

            // BACK
             0.9375f, 0.0f,
             0.9375f, 0.625f,
             0.0f, 0.0f,
             0.0f, 0.625f,
            // LEFT
             0.9375f, 0.0f,
             0.9375f, 0.625f,
             0.0f, 0.0f,
             0.0f, 0.625f,
            // RIGHT
             0.9375f, 0.0f,
             0.9375f, 0.625f,
             0.0f, 0.0f,
             0.0f, 0.625f,
            // TOP
             0.0f, 0.0f,
             0.9375f, 0.0f,
             0.0f, 0.625f,
             0.9375f, 0.625f,
            // BOTTOM
             0.9375f, 0.0f,
             0.9375f, 0.625f,
             0.0f, 0.0f,
             0.0f, 0.625f        
        };

                  }

I'm looking to change my view in degrees.. how would I do so? I assume I'd use glrotate but how would I? (only on the x axis)

// Just a clarification that I'm in fact looking to do a view transformation so that the world around me would rotate X degrees depending on orientation of the camera device.

+1  A: 

You can try rotating the canvas, if you want to rotate the whole view.

canvas.rotate(90); // 90 degrees

If you don't have canvas, implement the onDraw method that it inherits from View:

onDraw(Canvas canvas) {
  canvas.rotate(90); // 90 degrees
} 

Hopefully this works.

xil3
ahhh, yes, your right - I thought he wanted to rotate the scene, but he asked for the view .. ;)
Andreas_D
I don't have a canvas tho :|
Ulkmun
Can you post the rest of the code for that view class? Or is that it?
xil3
I posted the rest
Ulkmun
I just updated my answer - keep in mind that draw gets called the first time it draws the view. You can force it to redraw by calling invalidate();
xil3
Made a mistaken, use onDraw instead - not a good idea to override the draw apparently.
xil3
this doesn't seem to be rotating the openGL stuff tho? I want to rotate it so I can see an openGL object "behind" me.
Ulkmun
Any chance you can get some screenshots (before and after)? Just trying to visualize the exact requirements.
xil3
It's a cube at co-ordinates "behind" the view. I'm using the phones bearing to rotate the view to turn to the "behind" to view the cube when the phone turns.
Ulkmun
So the view would be rotating around the cube?
xil3
Yeah, but I hear its perhaps easier to rotate the world?
Ulkmun
Yeah it is - I'll look into this a bit more and see if I can figure out a solution for you.
xil3
Cool, thanks :)
Ulkmun
Got it working by glrotate BEFORE anything is drawn.
Ulkmun
Oh awesome! Thanks for the update :)
xil3