[android-developers] Re: OpenGL using multiple classes

2012-08-19 Thread Braindrool
Resolved.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: OpenGL using multiple classes

2012-08-16 Thread bob
You have an infinite loop, and you're getting a stack overflow.

In BlaRenderer, you create a new Player object:

Player player = new Player();

Since it inherits from BlaRenderer, it tries to create a new Player object:

Player player = new Player();

Since it inherits from BlaRenderer, it tries to create a new Player object:

Player player = new Player();

This continues until you are out of stack space.  One solution:

Remove this line from BlaRenderer:

Player player = new Player();



On Wednesday, August 15, 2012 7:37:03 PM UTC-5, Braindrool wrote:

 I've been programming for years, but I have very little experience with 3D 
 graphics. I thought OpenGL would be the most efficient in this project. 
 It's coming along alright, I am still studying the API and documentation. 
 But I have encountered a problem using multiple classes. For instance, here 
 I try to use a separate class for the player model. (Yes, currently it's 
 just a cube.)

 Renderer:

 Code:

 package com.braindrool.bla;

 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
 import java.nio.ShortBuffer;

 import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;

 import android.opengl.GLSurfaceView.Renderer;

 public class BlaRenderer implements Renderer {

   int nrOfVertices;
   float A;
   Player player = new Player();

   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
   // TODO Auto-generated method stub

   gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
   gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

   gl.glEnable(GL10.GL_CULL_FACE);

   gl.glFrontFace(GL10.GL_CCW);
   gl.glCullFace(GL10.GL_BACK);

   gl.glClearColor(0.3f, 0.8f, 0.9f, 1);

   //  initTriangle();
   player.initPlayer();

   }

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

   

   gl.glLoadIdentity();

   gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

   // gl.glRotatef(A * 2, 1f, 0f, 0f);
   // gl.glRotatef(A, 0f, 1f, 0f);
   //
   // gl.glColor4f(0.5f, 0, 0, 1);

   gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
   gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
   gl.glDrawElements(GL10.GL_TRIANGLES, nrOfVertices,
   GL10.GL_UNSIGNED_SHORT, indexBuffer);
   // A++;

   }

   public void onSurfaceChanged(GL10 gl, int width, int height) {
   // TODO Auto-generated method stub
   gl.glViewport(0, 0, width, height);

   }

   private FloatBuffer vertexBuffer;
   private ShortBuffer indexBuffer;

   private FloatBuffer colorBuffer;

   private void initTriangle() {

   float[] coords = { 0, 0.5f, 0, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 
 0.5f,
   0, 0, -0.5f };

   nrOfVertices = coords.length;

   ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices * 3 * 
 4);
   vbb.order(ByteOrder.nativeOrder());
   vertexBuffer = vbb.asFloatBuffer();

   ByteBuffer ibb = ByteBuffer.allocateDirect(nrOfVertices * 2);
   ibb.order(ByteOrder.nativeOrder());
   indexBuffer = ibb.asShortBuffer();

   ByteBuffer cbb = ByteBuffer.allocateDirect(4 * nrOfVertices * 
 4);
   cbb.order(ByteOrder.nativeOrder());
   colorBuffer = cbb.asFloatBuffer();

   float[] colors = { 1f, 0f, 0f, 1f, // point 1
   0f, 1f, 0f, 1f, // point 2
   0f, 0f, 1f, 1f, // point 3
   };

   short[] indices = new short[] {
   // indices
   0, 1, 2, 0, 2, 3, 0, 3, 1, 3, 1, 2

   };

   vertexBuffer.put(coords);
   indexBuffer.put(indices);
   colorBuffer.put(colors);

   vertexBuffer.position(0);
   indexBuffer.position(0);
   colorBuffer.position(0);

   }

 }

 And now the player class.

 Code:

 package com.braindrool.bla;

 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
 import java.nio.ShortBuffer;

 import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;

 public class Player extends BlaRenderer{

   private FloatBuffer vertexBuffer;
   private ShortBuffer indexBuffer;
   private FloatBuffer colorBuffer;
   private int nrOfVertices;
   float A;

   public void initPlayer() {

   float[] coords = {

   -0.5f, 0.5f, 0, // P0
   -0.5f, 0, 0, // P1
   0.5f, 0, 0, // P2
 

[android-developers] Re: OpenGL using multiple classes

2012-08-16 Thread Braindrool
I think you are overestimating my knowledge of Java. I don't quite 
understand how to incorporate multiple classes. How do I include my 
initPlayer() function?

On Wednesday, August 15, 2012 7:37:03 PM UTC-5, Braindrool wrote:

 I've been programming for years, but I have very little experience with 3D 
 graphics. I thought OpenGL would be the most efficient in this project. 
 It's coming along alright, I am still studying the API and documentation. 
 But I have encountered a problem using multiple classes. For instance, here 
 I try to use a separate class for the player model. (Yes, currently it's 
 just a cube.)

 Renderer:

 Code:

 package com.braindrool.bla;

 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
 import java.nio.ShortBuffer;

 import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;

 import android.opengl.GLSurfaceView.Renderer;

 public class BlaRenderer implements Renderer {

   int nrOfVertices;
   float A;
   Player player = new Player();

   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
   // TODO Auto-generated method stub

   gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
   gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

   gl.glEnable(GL10.GL_CULL_FACE);

   gl.glFrontFace(GL10.GL_CCW);
   gl.glCullFace(GL10.GL_BACK);

   gl.glClearColor(0.3f, 0.8f, 0.9f, 1);

   //  initTriangle();
   player.initPlayer();

   }

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

   

   gl.glLoadIdentity();

   gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

   // gl.glRotatef(A * 2, 1f, 0f, 0f);
   // gl.glRotatef(A, 0f, 1f, 0f);
   //
   // gl.glColor4f(0.5f, 0, 0, 1);

   gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
   gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
   gl.glDrawElements(GL10.GL_TRIANGLES, nrOfVertices,
   GL10.GL_UNSIGNED_SHORT, indexBuffer);
   // A++;

   }

   public void onSurfaceChanged(GL10 gl, int width, int height) {
   // TODO Auto-generated method stub
   gl.glViewport(0, 0, width, height);

   }

   private FloatBuffer vertexBuffer;
   private ShortBuffer indexBuffer;

   private FloatBuffer colorBuffer;

   private void initTriangle() {

   float[] coords = { 0, 0.5f, 0, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 
 0.5f,
   0, 0, -0.5f };

   nrOfVertices = coords.length;

   ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices * 3 * 
 4);
   vbb.order(ByteOrder.nativeOrder());
   vertexBuffer = vbb.asFloatBuffer();

   ByteBuffer ibb = ByteBuffer.allocateDirect(nrOfVertices * 2);
   ibb.order(ByteOrder.nativeOrder());
   indexBuffer = ibb.asShortBuffer();

   ByteBuffer cbb = ByteBuffer.allocateDirect(4 * nrOfVertices * 
 4);
   cbb.order(ByteOrder.nativeOrder());
   colorBuffer = cbb.asFloatBuffer();

   float[] colors = { 1f, 0f, 0f, 1f, // point 1
   0f, 1f, 0f, 1f, // point 2
   0f, 0f, 1f, 1f, // point 3
   };

   short[] indices = new short[] {
   // indices
   0, 1, 2, 0, 2, 3, 0, 3, 1, 3, 1, 2

   };

   vertexBuffer.put(coords);
   indexBuffer.put(indices);
   colorBuffer.put(colors);

   vertexBuffer.position(0);
   indexBuffer.position(0);
   colorBuffer.position(0);

   }

 }

 And now the player class.

 Code:

 package com.braindrool.bla;

 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
 import java.nio.ShortBuffer;

 import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;

 public class Player extends BlaRenderer{

   private FloatBuffer vertexBuffer;
   private ShortBuffer indexBuffer;
   private FloatBuffer colorBuffer;
   private int nrOfVertices;
   float A;

   public void initPlayer() {

   float[] coords = {

   -0.5f, 0.5f, 0, // P0
   -0.5f, 0, 0, // P1
   0.5f, 0, 0, // P2
   0.5f, 0.5f, 0, // P3
   0.5f, 0.5f, 0.5f, // P4
   -0.5f, 0.5f, 0.5f, // P5
   -0.5f, 0, 0.5f, // P6
   0.5f, 0, 0.5f // P7
   };

   nrOfVertices = coords.length;