OpenGL using multiple classes
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
Braindrool <cawehk... @gmail.com>
Date: Wed, 15 Aug 2012 17:37:03 -0700 (PDT)
Local: Wed, Aug 15 2012 8:37 pm
Subject: OpenGL using multiple classes
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;
ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices * 4); vbb.order(ByteOrder.nativeOrder()); vertexBuffer = vbb.asFloatBuffer();
ByteBuffer ibb = ByteBuffer.allocate(nrOfVertices * 2); ibb.order(ByteOrder.nativeOrder()); indexBuffer = ibb.asShortBuffer();
ByteBuffer cbb = ByteBuffer.allocate(12 * 4); ibb.order(ByteOrder.nativeOrder()); colorBuffer = cbb.asFloatBuffer();
short[] indices = {
0, 1, 2, 3, 0, 2, 0, 6, 1, 0, 5, 6, 5, 7, 6, 5, 4, 7, 4, 3, 7, 3, 2, 7
};
float[] colors = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 };
vertexBuffer.put(coords); indexBuffer.put(indices); colorBuffer.put(colors);
vertexBuffer.position(0); indexBuffer.position(0); colorBuffer.position(0);
}
}
It's a bit sloppy but meh.
The error:
Code:
Player player = new Player()
&
Code:
public class Player extends BlaRenderer{
Help appreciated!
~Braindrool
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Satya Komatineni <satya.komatin... @gmail.com>
Date: Thu, 16 Aug 2012 08:38:43 -0400
Local: Thurs, Aug 16 2012 8:38 am
Subject: Re: [android-developers] OpenGL using multiple classes
Hopefully you have a good reason to structure your classes they way
you have done.
To start BlaRenderer is a proper derived class that will get called.
So this is a concrete class by itself.
Number 2, the player class looks more like a utilitiy class and hence
doesn't need to derive from BlaRenderer. In your code I don't see the
Player class playing the role of a renderer.
Number 3, BlaRenderer has a local variable pointing to a Player class.
So the variables of Player class are physically separate from the
variables of BlaRenderer. if your intention is to have the Player as a
utility class, then
1. Don't inherit from Renderer
2. Remove the local variables in the BlaRenderer which are now
controlled by Renderer
Hope that helps
http://satyakomatineni.com/android/training
http://satyakomatineni.com
http://androidbook.com
http://twitter.com/SatyaKomatineni
On Wed, Aug 15, 2012 at 8:37 PM, Braindrool <cawehk
... @gmail.com> 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;
> ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices * 4);
> vbb.order(ByteOrder.nativeOrder());
> vertexBuffer = vbb.asFloatBuffer();
> ByteBuffer ibb = ByteBuffer.allocate(nrOfVertices * 2);
> ibb.order(ByteOrder.nativeOrder());
> indexBuffer = ibb.asShortBuffer();
> ByteBuffer cbb = ByteBuffer.allocate(12 * 4);
> ibb.order(ByteOrder.nativeOrder());
> colorBuffer = cbb.asFloatBuffer();
> short[] indices = {
> 0, 1, 2, 3, 0, 2, 0, 6, 1, 0, 5, 6, 5, 7, 6, 5, 4, 7, 4, 3, 7, 3, 2, 7
> };
> float[] colors = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 };
> vertexBuffer.put(coords);
> indexBuffer.put(indices);
> colorBuffer.put(colors);
> vertexBuffer.position(0);
> indexBuffer.position(0);
> colorBuffer.position(0);
> }
> }
> It's a bit sloppy but meh.
> The error:
> Code:
> Player player = new Player()
> &
> Code:
> public class Player extends BlaRenderer{
> Help appreciated!
> ~Braindrool
> --
> 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+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Braindrool <cawehk... @gmail.com>
Date: Thu, 16 Aug 2012 14:04:49 -0700 (PDT)
Local: Thurs, Aug 16 2012 5:04 pm
Subject: Re: [android-developers] OpenGL using multiple classes
Don't mean to be a burden here, but think you could elaborate a bit about how to fix? I'm new to Java, and so far I don't like it. But then again I may just not be used to it.
On Thursday, August 16, 2012 7:38:43 AM UTC-5, satya.ko
... @gmail.com wrote:
> Hopefully you have a good reason to structure your classes they way > you have done.
> To start BlaRenderer is a proper derived class that will get called. > So this is a concrete class by itself.
> Number 2, the player class looks more like a utilitiy class and hence > doesn't need to derive from BlaRenderer. In your code I don't see the > Player class playing the role of a renderer.
> Number 3, BlaRenderer has a local variable pointing to a Player class. > So the variables of Player class are physically separate from the > variables of BlaRenderer. if your intention is to have the Player as a > utility class, then
> 1. Don't inherit from Renderer > 2. Remove the local variables in the BlaRenderer which are now > controlled by Renderer
> Hope that helps > http://satyakomatineni.com/android/training > http://satyakomatineni.com > http://androidbook.com > http://twitter.com/SatyaKomatineni
> On Wed, Aug 15, 2012 at 8:37 PM, Braindrool <cawe... @gmail.com<javascript:>> > 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;
> > ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices > * 4); > > vbb.order(ByteOrder.nativeOrder()); > > vertexBuffer = vbb.asFloatBuffer();
> > ByteBuffer ibb = ByteBuffer.allocate(nrOfVertices * 2); > > ibb.order(ByteOrder.nativeOrder()); > > indexBuffer = ibb.asShortBuffer();
> > ByteBuffer cbb = ByteBuffer.allocate(12 * 4); > > ibb.order(ByteOrder.nativeOrder()); > > colorBuffer = cbb.asFloatBuffer();
> > short[] indices = {
> > 0, 1, 2, 3, 0, 2, 0, 6, 1, 0, 5, 6, 5, 7, 6, 5, 4, 7, 4, > 3, 7, 3, 2, 7
> > };
> > float[] colors = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 };
> > vertexBuffer.put(coords); > > indexBuffer.put(indices); > > colorBuffer.put(colors);
> > vertexBuffer.position(0); > > indexBuffer.position(0); > > colorBuffer.position(0);
> > } > > }
> > It's a bit sloppy but meh.
> > The error:
> > Code:
> > Player player = new Player()
> > &
> > Code:
> > public class Player extends BlaRenderer{
> > Help appreciated!
> > ~Braindrool
> > -- > > You received this message because you are subscribed to the Google > > Groups "Android Developers" group. > > To post to this group, send email to android-d...@googlegroups.com<javascript:> > > To unsubscribe from this group, send email to > > android-developers+unsubscribe@googlegroups.com <javascript:> > > For more options, visit this group at > > http://groups.google.com/group/android-developers?hl=en
On Thursday, August 16, 2012 7:38:43 AM UTC-5, satya.ko... @gmail.com wrote:
> Hopefully you have a good reason to structure your classes they way > you have done.
> To start BlaRenderer is a proper derived class that will get called. > So this is a concrete class by itself.
> Number 2, the player class looks more like a utilitiy class and hence > doesn't need to derive from BlaRenderer. In your code I don't see the > Player class playing the role of a renderer.
> Number 3, BlaRenderer has a local variable pointing to a Player class. > So the variables of Player class are physically separate from the > variables of BlaRenderer. if your intention is to have the Player as a > utility class, then
> 1. Don't inherit from Renderer > 2. Remove the local variables in the BlaRenderer which are now > controlled by Renderer
> Hope that helps > http://satyakomatineni.com/android/training > http://satyakomatineni.com > http://androidbook.com > http://twitter.com/SatyaKomatineni
> On Wed, Aug 15, 2012 at 8:37 PM, Braindrool <cawe... @gmail.com<javascript:>> > 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
...
read more »
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
bob <b... @coolfone.comze.com>
Date: Thu, 16 Aug 2012 15:32:59 -0700 (PDT)
Local: Thurs, Aug 16 2012 6:32 pm
Subject: Re: OpenGL using multiple classes
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 > 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;
> ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices * 4); > vbb.order(ByteOrder.nativeOrder()); > vertexBuffer = vbb.asFloatBuffer();
> ByteBuffer ibb = ByteBuffer.allocate(nrOfVertices * 2); > ibb.order(ByteOrder.nativeOrder()); > indexBuffer = ibb.asShortBuffer();
> ByteBuffer cbb = ByteBuffer.allocate(12 * 4); > ibb.order(ByteOrder.nativeOrder()); > colorBuffer = cbb.asFloatBuffer();
> short[] indices = {
> 0, 1, 2, 3, 0, 2, 0, 6, 1, 0, 5, 6, 5, 7, 6, 5, 4, 7, 4, 3, 7, 3, 2, 7
> };
> float[] colors = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 };
> vertexBuffer.put(coords); > indexBuffer.put(indices); > colorBuffer.put(colors);
> vertexBuffer.position(0); > indexBuffer.position(0); > colorBuffer.position(0);
> } > }
> It's a bit sloppy but meh.
> The error:
> Code:
> Player player = new Player()
> &
> Code:
> public class Player extends BlaRenderer{
> Help appreciated!
> ~Braindrool
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Braindrool <cawehk... @gmail.com>
Date: Thu, 16 Aug 2012 15:48:52 -0700 (PDT)
Local: Thurs, Aug 16 2012 6:48 pm
Subject: Re: OpenGL using multiple classes
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;
> ByteBuffer vbb = ByteBuffer.allocateDirect(nrOfVertices * 4); > vbb.order(ByteOrder.nativeOrder()); > vertexBuffer = vbb.asFloatBuffer();
> ByteBuffer ibb = ByteBuffer.allocate(nrOfVertices * 2); > ibb.order(ByteOrder.nativeOrder()); > indexBuffer = ibb.asShortBuffer();
> ByteBuffer cbb = ByteBuffer.allocate(12 * 4); > ibb.order(ByteOrder.nativeOrder()); > colorBuffer = cbb.asFloatBuffer();
> short[] indices = {
> 0, 1, 2, 3, 0, 2, 0, 6, 1, 0, 5, 6, 5, 7, 6, 5, 4, 7, 4, 3, 7, 3, 2, 7
> };
> float[] colors = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 };
> vertexBuffer.put(coords); > indexBuffer.put(indices); > colorBuffer.put(colors);
> vertexBuffer.position(0); > indexBuffer.position(0); > colorBuffer.position(0);
> } > }
> It's a bit sloppy but meh.
> The error:
> Code:
> Player player = new Player()
> &
> Code:
> public class Player extends BlaRenderer{
> Help appreciated!
> ~Braindrool
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Braindrool <cawehk... @gmail.com>
Date: Sun, 19 Aug 2012 16:51:42 -0700 (PDT)
Local: Sun, Aug 19 2012 7:51 pm
Subject: Re: OpenGL using multiple classes
You must
Sign in before you can post messages.
You do not have the permission required to post.