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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:18:39+00:00 2026-06-01T01:18:39+00:00

Using OpenGL ES I am rendering a simple cube class that draws it at

  • 0

Using OpenGL ES I am rendering a simple cube class that draws it at the centre of the screen. However, I want to be able to draw multiple such cubes on the screen at random positions but don’t know how to. Here is the my custom surface view that renders the cube as a private class. I haven’t included my main ActivityManager since its not the concern.

 public class TouchSurfaceView extends GLSurfaceView  {

private final float TRACKBALL_SCALE_FACTOR=52.0f;
private final float TOUCH_SCALE_FACTOR=100.0f/320;
private MyGLRenderer mRenderer;
private float mPreviousX;
private float mPreviousY;


public TouchSurfaceView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub

        mRenderer=new MyGLRenderer();
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

}

    private class MyGLRenderer implements GLSurfaceView.Renderer{

    private MyGLCube mCube;
    public float mAngleX;
    public float mAngleY; 


    public MyGLRenderer(){
        mCube=new MyGLCube();
    }



    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub


        gl.glDisable(GL10.GL_DITHER);
         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);   

         gl.glMatrixMode(GL10.GL_MODELVIEW);
         gl.glLoadIdentity();
         gl.glClientActiveTexture(DRAWING_CACHE_QUALITY_HIGH);
         // gl.glTranslatef(0, 0, -3.0f);
         gl.glTranslatef(0, 0, -3.0f);
         gl.glRotatef(mAngleX, 0, 1, 0);
         gl.glRotatef(mAngleY, 1, 0, 0);
         mCube.draw(gl);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub
        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);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // TODO Auto-generated method stub
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glClearColor(0.5f, 2, 0.63f, 0.2f);

    }


}
 }

Here is MYGLCube that contains vertices, indices and other stuff for constructing the cube

    public class MyGLCube {


private float vertices[]={
        0.3f,0.3f,-0.3f, // topFront Right
        0.3f,-0.3f,-0.3f, //BottomFront Right
       -0.3f,-0.3f,-0.3f, //BottomFront Left
       -0.3f,0.3f,-0.3f,  //topFront Left
       0.3f,0.3f,0.3f, // topBack Right
       0.3f,-0.3f,0.3f, //BottomBack Right
       -0.3f,-0.3f,0.3f, //BottomBack Left
       -0.3f,0.3f,0.3f,  //topBack Left
 }; 

private float rgbaVals[]={
        1,1,0,.5f,   // topFront Right color
        0.25f,0, 0.8f,1, //BottomFront Right color
        0,1,1,1, //BottomFront Left color
        0.35f,0.26f,1,0.5f,  //topFront Left color
        0.23f,0.62f,3,0.2f,  // topBack Right color
        0.3f,0.43f,1,0.2f,  //BottomBack Right color
        0.2f, 0.3f, 0.73f,0.6f,  //BottomBack Left color
        0.6f, 0.9f, 0.65f, 0.2f //topBack Left color            
};

private short pIndex[]={
          3,4,0,  0,4,1,   3,0,1, 
          3,7,4,   7,6,4,  7,3,6,
          3,1,2,   1,6,2,  6,3,2,
          1,4,5,   5,6,1,  6,5,4            
};
private FloatBuffer vertBuff, cBuff;
private ShortBuffer pBuff;


public MyGLCube(){
  ByteBuffer bBuff=ByteBuffer.allocateDirect(vertices.length*4);    
  bBuff.order(ByteOrder.nativeOrder());
  vertBuff=bBuff.asFloatBuffer();
  vertBuff.put(vertices);
  vertBuff.position(0);

  ByteBuffer pbBuff=ByteBuffer.allocateDirect(pIndex.length*2);
  pbBuff.order(ByteOrder.nativeOrder());
  pBuff=pbBuff.asShortBuffer();
  pBuff.put(pIndex);
  pBuff.position(0);

  ByteBuffer colorBuff=ByteBuffer.allocateDirect(rgbaVals.length*4);
  colorBuff.order(ByteOrder.nativeOrder());
  cBuff=colorBuff.asFloatBuffer();
  cBuff.put(rgbaVals);
  cBuff.position(0);


}

public void draw(GL10 gl){
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glEnable(GL10.GL_SMOOTH);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glCullFace(GL10.GL_BACK);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, cBuff);
    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
 }  



}
  • 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-01T01:18:40+00:00Added an answer on June 1, 2026 at 1:18 am

    You must use glPushMatrix and glPopMatrix to return to previus possition after translate,rotate…

    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub
    
    
        gl.glDisable(GL10.GL_DITHER);
         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);   
    
         gl.glMatrixMode(GL10.GL_MODELVIEW);
         gl.glLoadIdentity();
         gl.glClientActiveTexture(DRAWING_CACHE_QUALITY_HIGH);
    
         //Draw cube 1
         gl.glPushMatrix();
         gl.glTranslatef(-0.5f, 0, -3.0f);
         gl.glRotatef(mAngleX, 0, 1, 0);
         gl.glRotatef(mAngleY, 1, 0, 0);
         mCube.draw(gl);
         gl.glPopMatrix();
    
         //Draw cube 2
         gl.glPushMatrix();
         gl.glTranslatef(0, 0, -3.0f); 
         gl.glRotatef(mAngleX, 0, 1, 0);
         gl.glRotatef(mAngleY, 1, 0, 0);
         mCube.draw(gl);
         gl.glPopMatrix();
    
         //Draw cube 3
         gl.glPushMatrix();
         gl.glTranslatef(0.5f, 0, -3.0f); 
         gl.glRotatef(mAngleX, 0, 1, 0);
         gl.glRotatef(mAngleY, 1, 0, 0);
         mCube.draw(gl);
         gl.glPopMatrix();
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple OpenGL program and trying to draw an instanced array that
I am using off screen rendering using opengl FBO and glut on a MAC
I'm using OpenGL with C++ in dev-c++ IDE. When I draw a circle, nothing
I'm using OpenGL for Android to draw my 2D images. Whenever I draw something
Finally i tried to draw a line using OpenGL ES framework in XCode 4.2
I have done a graphics project using OpenGL and now I want to convert
I want to start developing graphic programing using OpenGl To kick start I am
I am implementing a simple board game (Breakthrough) using OpenGL (plus GLUT and GLUI).
I am new to OpenGL and was trying to create a simple maze that
We have a platform using VCL TFrame as rendering surface for OpenGL. Using FireMonkey,

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.