GL11 Pointer method with an indirect Buffer.


Last Updated:

  1. hfdjw

    hfdjw Member

    I have this
    Code (Text):
    1. package my.sphere;
    2.  
    3. import android.app.Activity;
    4. import android.opengl.GLSurfaceView;
    5. import android.os.Bundle;
    6.  
    7. public class SpherewallpaperActivity extends Activity {
    8.     /** Called when the activity is first created. */
    9.      private GLSurfaceView glSurfaceView;
    10.     @Override
    11.     public void onCreate(Bundle savedInstanceState) {
    12.         super.onCreate(savedInstanceState);
    13.         setContentView(R.layout.main);
    14.         glSurfaceView = new GLSurfaceView(this);
    15.     GLViewRenderer a =new GLViewRenderer(this);
    16.     glSurfaceView.setRenderer(a);
    17.     setContentView(glSurfaceView);
    18.     }
    19. }
    and this
    Code (Text):
    1.  
    2. import javax.microedition.khronos.egl.EGLConfig;
    3. import javax.microedition.khronos.opengles.GL10;
    4.  
    5. import android.content.Context;
    6. import android.opengl.GLSurfaceView.Renderer;
    7. import android.opengl.GLU;
    8.  
    9. public class GLViewRenderer implements Renderer  {
    10.     private float xRot;
    11.     private float yRot;
    12.     public Sphere mSphere;
    13.     public void setxRot(float xRot) {
    14.         this.xRot += xRot;
    15.     }
    16.  
    17.     public void setyRot(float yRot) {
    18.         this.yRot += yRot;
    19.     }
    20.  
    21.     public GLViewRenderer(Context ctx) {
    22.         //initialize our 3D triangle here
    23.         mSphere = new Sphere(1, 25);
    24.         //Initializing the rate counter object
    25.         //This will help us in calculating the frames per second
    26.      
    27.     }
    28.  
    29.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    30.         //initialize all the things required for openGL configurations
    31.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    
    32.  
    33.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    34.     }
    35.  
    36.     public void onDrawFrame(GL10 gl) {
    37.      
    38.  
    39.         //write the drawing code needed
    40.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
    41.         gl.glLoadIdentity();
    42.  
    43.         /**
    44.          * Change this value in z if you want to see the image zoomed in
    45.          */
    46.         gl.glTranslatef(0.0f, 0.0f, -5.0f);
    47.  
    48.         gl.glRotatef(xRot, 0.0f, 1.0f, 0.0f);
    49.         gl.glRotatef(yRot, 1.0f, 0.0f, 0.0f);
    50.         mSphere.draw(gl);
    51.     }
    52.  
    53.     public void onSurfaceChanged(GL10 gl, int width, int height) {
    54.         if(height == 0) {                      
    55.             height = 1;                        
    56.         }
    57.  
    58.         gl.glViewport(0, 0, width, height);
    59.         gl.glMatrixMode(GL10.GL_PROJECTION);
    60.         gl.glLoadIdentity();                
    61.  
    62.         //Calculate The Aspect Ratio Of The Window
    63.         GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
    64.  
    65.         gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
    66.         gl.glLoadIdentity();                    //Reset The Modelview Matrix
    67.     }
    68.  
    69.     /**
    70.      * Used to stop the FPS counter
    71.      */
    72.     public void pause() {
    73.        
    74.     }
    75.  
    76. }
    77.  
    and this
    Code (Text):
    1. package my.sphere;
    2.  
    3. import java.nio.FloatBuffer;
    4.  
    5. import javax.microedition.khronos.opengles.GL10;
    6.  
    7. import android.util.Log;
    8.  
    9. public class Sphere {
    10.  
    11.     static private FloatBuffer sphereVertex;
    12.     static private FloatBuffer sphereNormal;
    13.     static float sphere_parms[]=new float[3];
    14.  
    15.     double mRaduis;
    16.     double mStep;
    17.     float mVertices[];
    18.     private static double DEG = Math.PI/180;
    19.     int mPoints;
    20.  
    21.     /**
    22.      * The value of step will define the size of each facet as well as the number of facets
    23.      *  
    24.      * @param radius
    25.      * @param step
    26.      */
    27.  
    28.     public Sphere( float radius, double step) {
    29.         this.mRaduis = radius;
    30.         this.mStep = step;
    31.         sphereVertex = FloatBuffer.allocate(40000);
    32.         mPoints = build();
    33.         Log.d("ALIS CHECK!!!!!!", " COUNT:" + mPoints);
    34.     }
    35.  
    36.     public void draw(GL10 gl) {
    37.         gl.glFrontFace(GL10.GL_CW);
    38.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    39.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, sphereVertex);
    40.  
    41.         gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    42.         gl.glDrawArrays(GL10.GL_POINTS, 0, mPoints);
    43.         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    44.     }
    45.  
    46.     private int build() {
    47.  
    48.         /**
    49.          * x = p * sin(phi) * cos(theta)
    50.          * y = p * sin(phi) * sin(theta)
    51.          * z = p * cos(phi)
    52.          */
    53.         double dTheta = mStep * DEG;
    54.         double dPhi = dTheta;
    55.         int points = 0;
    56.  
    57.         for(double phi = -(Math.PI); phi <= Math.PI; phi+=dPhi) {
    58.             //for each stage calculating the slices
    59.             for(double theta = 0.0; theta <= (Math.PI * 2); theta+=dTheta) {
    60.                 sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math.cos(theta)) );
    61.                 sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math.sin(theta)) );
    62.                 sphereVertex.put((float) (mRaduis * Math.cos(phi)) );
    63.                 points++;
    64.  
    65.             }
    66.         }
    67.         sphereVertex.position(0);
    68.         return points;
    69.     }
    70. }
    And when i start the app i get this errors:09-10 11:09:28.712: E/OpenGLES(277): Application my.sphere (SDK target 8) called a GL11 Pointer method with an indirect Buffer.
    09-10 11:09:28.764: W/dalvikvm(277): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
    09-10 11:09:28.964: E/AndroidRuntime(277): FATAL EXCEPTION: GLThread 8
    09-10 11:09:28.964: E/AndroidRuntime(277): java.lang.IllegalArgumentException: Must use a native order direct Buffer
    09-10 11:09:28.964: E/AndroidRuntime(277): at com.google.android.gles_jni.GLImpl.glVertexPointerBounds(Native Method)
    09-10 11:09:28.964: E/AndroidRuntime(277): at com.google.android.gles_jni.GLImpl.glVertexPointer(GLImpl.java:1121)
    09-10 11:09:28.964: E/AndroidRuntime(277): at my.sphere.Sphere.draw(Sphere.java:39)
    09-10 11:09:28.964: E/AndroidRuntime(277): at my.sphere.GLViewRenderer.onDrawFrame(GLViewRenderer.java:51)
    09-10 11:09:28.964: E/AndroidRuntime(277): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1332)
    09-10 11:09:28.964: E/AndroidRuntime(277): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
    Can you help me?

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    You are initializing your buffers incorrectly. You need to do something like this:

    Code (Text):
    1.  
    2. sphereVertex.allocateDirect(mVerticies.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    3.  
    instead of just using the allocate() method.

Share This Page