Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 923643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:13:27+00:00 2026-05-15T19:13:27+00:00

How would I rotate a view in degrees? I’ve got the following code setting

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:13:28+00:00Added an answer on May 15, 2026 at 7:13 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code to animate a view. It basically rotates the view by
Is it possible to rotate a view using a UIPanGestureRecognizer? I would like similar
I have a UIRotationRecognizer on an UIImageView. After I rotate the view I would
I would like to rotate an image in 360 degrees, like this site .
I would like to auto-rotate the top and bottom bars of the navigation view
I would like to rotate a histogram in R, plotted by hist(). The question
I have a TShape on my form. and would like to rotate it. Can
Would it be possible to extract the following information from logs? Start up/Shut down
Objective-C for iOS. How do I rotate an image or view TO a particular
I would like to allow the orientation to rotate as per the shouldAutorotateToInterfaceOrientation call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.