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 8311665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:53:35+00:00 2026-06-08T19:53:35+00:00

I am working on a simple project which dras a cube and rotates it

  • 0

I am working on a simple project which dras a cube and rotates it in OpenGLES 2.0 android 4.1. but the drawed shape wasnt cube. and it is rotating a axis that i wasnt supposed to. i suppose it rotates X,Y axis in camera space. so thanks for your advice.

    public float mAngleX;
public float mAngleY;
private Quadrat quad;
private final float[] MVPMatrix = new float[16]; 
private final float[] projectionMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private final float[] rotationMatrix = new float[16];

public MyRenderer(){
    mAngleX = 0;
    mAngleY = 0;
}

//  @Override
public void onSurfaceCreated(GL10 unused, EGLConfig config){
    // Set the background frame color
    GLES20.glClearColor(0.5f, 0.2f, 0.1f, 1.0f);

    Matrix.setLookAtM(mVMatrix, 0, 0.0f, 0.0f, -5.0f, 0f, 0f, 1f, 0f, 1.0f, 0.0f);

    quad = new Quadrat();     
}           

//  @Override
public void onSurfaceChanged(GL10 unused, int width, int height){
    GLES20.glViewport(0, 0, width, height);        
    float ratio = (float) width / height;
    Matrix.perspectiveM(projectionMatrix, 0, 45.0f, ratio, 1, 9);
}

//@Override 
public void onDrawFrame(GL10 unused) {        
    // Draw background color
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);


    Matrix.setRotateM(rotationMatrix, 0, mAngleX, 0.0f, 1.0f, 0.0f);
    Matrix.multiplyMM(MVPMatrix, 0, rotationMatrix, 0, MVPMatrix, 0);
    Matrix.setRotateM(rotationMatrix, 0, mAngleY, 1.0f, 0.0f, 0.0f);
    Matrix.multiplyMM(MVPMatrix, 0, rotationMatrix, 0, MVPMatrix, 0);

    Matrix.multiplyMM(MVPMatrix, 0, projectionMatrix, 0, mVMatrix, 0);

    quad.draw(MVPMatrix, mVMatrix);     
}

public static int loadShader(int type, String shaderCode){
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
}
}

and the quadrat class is below.

class Quadrat {
private final String vertexShaderCode =
        "uniform mat4 uMVPMatrix;\n" +
        "uniform mat4 uMVMatrix;\n" +
        "uniform vec3 uLightPos;\n" +

        "attribute vec4 aPosition;\n" +
        "attribute vec4 aColor;\n" +
        "attribute vec4 aNormal;\n" +

        "varying vec4 vColor;\n" +

        "void main() {\n" +
            "gl_Position = uMVPMatrix * aPosition;\n" +
        "}\n";

private final String fragmentShaderCode =
        "precision mediump float;\n" +

        "varying vec4 vColor;\n" +

        "void main() {\n" +
            "gl_FragColor = vec4(1.0, 0.9, 0.4, 1.0);\n" +
        "}\n";

private final FloatBuffer vertexBuffer;
private final int mProgram;
private int mPositionHandle;
private int muMVPMatrixHandle;
private int muLightPosHandle;
private int muMVMatrixHandle;

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float quadrateCoords[] = { // in counterclockwise order:
    // front
    -0.5f, -0.5f, 0.5f,
     0.5f, -0.5f, 0.5f,
    -0.5f, 0.5f, 0.5f,
     0.5f, 0.5f, 0.5f,
    // back
    -0.5f, -0.5f, -0.5f,
     0.5f, -0.5f, -0.5f,
    -0.5f, 0.5f, -0.5f,
     0.5f, 0.5f, -0.5f,
    // left
    -0.5f, -0.5f, 0.5f,
    -0.5f, -0.5f, -0.5f,
    -0.5f, 0.5f, 0.5f,
    -0.5f, 0.5f, -0.5f,
    // right  
     0.5f, -0.5f, 0.5f,
     0.5f, -0.5f, -0.5f,
     0.5f, 0.5f, 0.5f,
     0.5f, 0.5f, -0.5f,  
    // up  
    -0.5f, 0.5f, 0.5f,
     0.5f, 0.5f, 0.5f,
    -0.5f, 0.5f, -0.5f,
     0.5f, 0.5f, -0.5f,
    // bottom  
    -0.5f, -0.5f, 0.5f,
     0.5f, -0.5f, 0.5f,
    -0.5f, -0.5f, -0.5f,
     0.5f, -0.5f, -0.5f
};


private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex

public Quadrat() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(quadrateCoords.length * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());

    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();

    // add the coordinates to the FloatBuffer
    vertexBuffer.put(quadrateCoords);
    vertexBuffer.position(0);

    // prepare shaders and OpenGL program
    int vertexShader = MyRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = MyRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables
}

public void draw(float[] mvpMatrix, float[] mVMatrix) {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's aPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");

    // Enable a handle to the triangle vertices
    // Prepare the triangle coordinate data
    GLES20.glEnableVertexAttribArray(mPositionHandle);

 GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false,     vertexStride, vertexBuffer);

vertexStride, vertexBuffer2);

vertexStride, vertexBuffer3);

    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mvpMatrix, 0);        

    muMVMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVMatrix");
    GLES20.glUniformMatrix4fv(muMVMatrixHandle, 1, false, mVMatrix, 0);        

    muLightPosHandle = GLES20.glGetUniformLocation(mProgram, "uLightPos");
    GLES20.glUniform3f(muLightPosHandle, 10.0f, 10.0f, 15.0f);

    // Front
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

    // Back
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 4, 4);

    // Left
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 8, 4);

    // Right
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 12, 4);

    // Top
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 16, 4);

    // Right
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 20, 4);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}    
}
  • 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-06-08T19:53:37+00:00Added an answer on June 8, 2026 at 7:53 pm

    Install the package Api demos and have a look at the example Touch Rotate in

    Api demos > Graphics > OpenGL ES > Touch Rotate
    

    You can reach the source code at

    android-sdk-linux/samples/android-version/ApiDemos/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an android sample project, which makes use of TabActivity. But as
I'm still working on my little Tkinter project which is simple a logging console
I'm working on a bigger project atm, but I made this simple example to
I have a project using WCF which was working fine, but I moved the
I am new to android platform. I am working on a project in which
Actually I'm working on simple project MVC4 + EF in which I have relation
We have a simple project that uses JsonValueProviderFactory which we have working on a
I am working on a project which made use of an old (but nice)
I'm currently working on a simple web project, for which I am using the
I am working on a project in which I must implement a simple web

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.