We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Passing arrays to OPENGL methods (float buffers)
Page Index Toggle Pages: 1
Passing arrays to OPENGL methods (float buffers?) (Read 1575 times)
Passing arrays to OPENGL methods (float buffers?)
Apr 19th, 2008, 12:10am
 
I've got some code i've been trying to speed up by using vertex arrays but i've been running into the problem of passing arrays as arguments into some of the OPENGL methods.  I've been having the same issue with alot of other opengl methods and was hoping someone would shed some light on the subject.  here's some pseudo code to illustrate the issue:

float[] test= {1,2,1,3,1,21,3}; // array of vertices
gl.glVertexPointer(3,GL.GL_FLOAT,0, test);

this spits out the following error:

Semantic Error: No applicable overload for a method with signature "glVertexPointer(int,int,int, float[]) was found perhaps you want the overloaded version glVertexPointer(int $1, int $2, int $3, long $4);

I've looked through some of JohnG's code (thanks for posting) and it looks like i need to use a float buffer.  Is this really necessary? are there any other workarounds?
I've tried using:

FloatBuffer ftest;
ftest.wrap(test);

and then passing this as an argument, this seems to work but now  i get Java VM exception access violation.  Is it the way i'm using the floatbuffer?


TIA
Stephen.
Re: Passing arrays as arguments in OPENGL methods
Reply #1 - Apr 19th, 2008, 1:14am
 
It depends which methods you're trying to use, some take arrays, and then you need to also pass a "first value" argument, which in 99% of cases is 0, as you want the 0th item in the array to be the first one used, so you pass an array, and then 0.

VertexPointer however needs the data in a native format, so you do have to use a FloatBuffer. But it's just an extra few lines.

You have to remember to rewind() your FloatBfuffer before using it. You may also need to make sure you do the native format stuff as well.
Re: Passing arrays to OPENGL methods (float buffer
Reply #2 - Apr 19th, 2008, 1:18am
 
yeah i noticed this worked fine for lighting issues but didn't work for vertex arrays.

i tried the following

FloatBuffer ftest;
ftest.wrap(test);

but now  i get Java VM exception access violation.  Is it the way i'm using the floatbuffer?
Re: Passing arrays to OPENGL methods (float buffer
Reply #3 - Apr 19th, 2008, 1:29am
 
alright disregard, i obviously need to read up on float buffer usage, i keep getting null pointer exceptions, probably because of the way i'm declaring things.  Thanks again for the help john, i'm sure i'll have more questions later.  At least now i know i'm heading in the right direction.
Re: Passing arrays to OPENGL methods (float buffer
Reply #4 - Apr 19th, 2008, 10:42am
 
I think you really should be using:
Code:
FloatBuffer f=ByteBuffer.allocateDirect(4 * test.length).order(ByteOrder.nativeOrder()).asFloatBuffer();
f.put(test);
f.rewind();
Re: Passing arrays to OPENGL methods (float buffer
Reply #5 - Apr 19th, 2008, 4:21pm
 
I see, i was trying to use a buffer util for allocation.  I'm guessing you're backing the float buffer with a byte buffer for direct allocation, is this for performance? and why are you multiplying the test.length by 4?
Re: Passing arrays to OPENGL methods (float buffer
Reply #6 - Apr 19th, 2008, 6:06pm
 
ok i think you're multiplying it by 4 because a byte is 8 bits and it needs to be 32 bits.  Lord.... i should have finished my CS degree instead of switching to graphic design Smiley
Re: Passing arrays to OPENGL methods (float buffer
Reply #7 - Apr 19th, 2008, 6:33pm
 
@#$@! i'm having problems with gl.glDrawElements i keep getting access violations.  I'm trying to convert some of the code off the opengl bible (pg 439) to run in processing before trying to implement it with the rest of my code.


import processing.opengl.*;
import javax.media.opengl.*;
import java.nio.*;

PGraphicsOpenGL pgl;
GL gl;

float [] corners;
int [] indexes;
FloatBuffer fcorners;
IntBuffer findexes;

void setup(){
 size (600,600,OPENGL);
 pgl         = (PGraphicsOpenGL) g;
 gl          = pgl.gl;
 float[] corners= {
   -25.0f, 25.0f, 25.0f, // 0 // Front of cube
   25.0f, 25.0f, 25.0f, // 1
   25.0f, -25.0f, 25.0f,// 2
   -25.0f, -25.0f, 25.0f,// 3
   -25.0f, 25.0f, -25.0f,// 4 // Back of cube
   25.0f, 25.0f, -25.0f,// 5
   25.0f, -25.0f, -25.0f,// 6
   -25.0f, -25.0f, -25.0f   };// 7
 int[] indexes= {
   0, 1, 2, 3, // Front Face
   4, 5, 1, 0, // Top Face
   3, 2, 6, 7, // Bottom Face
   5, 4, 7, 6, // Back Face
   1, 5, 6, 2, // Right Face
   4, 0, 3, 7 }; // Left Face*/

 FloatBuffer fcorners = ByteBuffer.allocateDirect(4*corners.length).order(ByteOrder.nativeOrder()).asFlo
atBuffer();
 fcorners.put(corners);
 fcorners.rewind();

 IntBuffer findexes = ByteBuffer.allocateDirect(4*indexes.length).order(ByteOrder.nativeOrder()).asInt
Buffer();
 findexes.put(indexes);
 findexes.rewind();
}

void draw(){
 background(100);
 pgl.beginGL();
 gl.glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
 gl.glVertexPointer(3,GL.GL_FLOAT, 0, fcorners);
 gl.glDrawElements(GL.GL_QUADS, 24, GL.GL_UNSIGNED_INT, findexes);
 pgl.endGL();
}

I know i'm close.. i can feel it... help... please Tongue
Re: Passing arrays to OPENGL methods (float buffer
Reply #8 - Apr 20th, 2008, 5:38am
 
What you have is correct as far as what you are trying to do but you have some scope issues. Basically you are redefining the variables "corners", "indexes", "fcorners", "findexes" inside of the setup function and so they are being compiled as local variables. The global variables of the same name are never being updated. Here's the fixed setup function:

Code:

void setup(){
 size (600,600,OPENGL);
 pgl    = (PGraphicsOpenGL) g;
 gl     = pgl.gl;
 corners = new float[] {  
   -25.0f, 25.0f, 25.0f, // 0 // Front of cube
   25.0f, 25.0f, 25.0f, // 1
   25.0f, -25.0f, 25.0f,// 2
   -25.0f, -25.0f, 25.0f,// 3
   -25.0f, 25.0f, -25.0f,// 4 // Back of cube
   25.0f, 25.0f, -25.0f,// 5
   25.0f, -25.0f, -25.0f,// 6
   -25.0f, -25.0f, -25.0f   };// 7  
 indexes = new int[] {  
   0, 1, 2, 3, // Front Face
   4, 5, 1, 0, // Top Face
   3, 2, 6, 7, // Bottom Face
   5, 4, 7, 6, // Back Face
   1, 5, 6, 2, // Right Face
   4, 0, 3, 7 }; // Left Face*/

 fcorners = ByteBuffer.allocateDirect(4*corners.length).order(ByteOrder.nativeOrder( )).asFloatBuffer();
 fcorners.put(corners);  
 fcorners.rewind();

 findexes = ByteBuffer.allocateDirect(4*indexes.length).order(ByteOrder.nativeOrder( )).asIntBuffer();
 findexes.put(indexes);
 findexes.rewind();
}


Re: Passing arrays to OPENGL methods (float buffer
Reply #9 - Apr 20th, 2008, 1:44pm
 
I was playing around with your sketch and got it working. Saito and I are trying to get an openGl fast draw method up and running for the obj loader. But with the semester on time is very fleeting. Hope this helps

Quote:
import processing.opengl.*;
import javax.media.opengl.*;
import java.nio.*;

PGraphicsOpenGL pgl;
GL gl;

float [] corners;
int [] indexes;
FloatBuffer fcorners;
IntBuffer findexes;

void setup(){
 size (600, 600, OPENGL);
 pgl = (PGraphicsOpenGL) g;
 gl = pgl.gl;
 corners = new float[] {  
   -25.0f,  25.0f,  25.0f, // 0 // Front of cube
   25.0f,  25.0f,  25.0f, // 1
   25.0f, -25.0f,  25.0f, // 2
   -25.0f, -25.0f,  25.0f, // 3
   -25.0f,  25.0f, -25.0f, // 4 // Back of cube
   25.0f,  25.0f, -25.0f, // 5
   25.0f, -25.0f, -25.0f, // 6
   -25.0f, -25.0f, -25.0f  };// 7  
 indexes = new int[] {  
   0, 1, 2, 3, // Front Face
   4, 5, 1, 0, // Top Face
   3, 2, 6, 7, // Bottom Face
   5, 4, 7, 6, // Back Face
   1, 5, 6, 2, // Right Face
   4, 0, 3, 7   }; // Left Face*/

 fcorners = ByteBuffer.allocateDirect(4*corners.length).order(ByteOrder.nativeOrder( )).asFloatBuffer();
 fcorners.put(corners);  
 fcorners.rewind();

 findexes = ByteBuffer.allocateDirect(4*indexes.length).order(ByteOrder.nativeOrder( )).asIntBuffer();
 findexes.put(indexes);
 findexes.rewind();
 
 gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
 
 //gl.glLineWidth(2);
 gl.glEnable(GL.GL_LINE_SMOOTH);
 
 gl.glVertexPointer(3,GL.GL_FLOAT, 0, fcorners);
 
 gl.glEnableClientState(GL.GL_VERTEX_ARRAY);

}

void draw(){
 background(100);
 
 pushMatrix();
 translate(width/2, height/2);
 scale(4);
 rotateX(radians(frameCount)/4);
 rotateY(radians(frameCount)/4);
 
 pgl.beginGL();

 gl.glDrawElements(GL.GL_QUADS, 24, GL.GL_UNSIGNED_INT, findexes);
 
 pgl.endGL();
 popMatrix();
}
Re: Passing arrays to OPENGL methods (float buffer
Reply #10 - Apr 20th, 2008, 4:32pm
 
thank you thank you thank you!  That makes total sense I can't believe i declared everything twice. and it makes sense to put all those opengl calls in the setup instead of the draw.  No wonder i was getting access violations, i'm glad i posted the whole code.

Thanks John, Ness and Matt.
Re: Passing arrays to OPENGL methods (float buffer
Reply #11 - Apr 20th, 2008, 9:25pm
 
Sweeeeet! i got my obj loader working with vertex arrays. I'm currently working on importing normals and texture info which hopefully i'll finish today.  

Thanks again guys!
Re: Passing arrays to OPENGL methods (float buffer
Reply #12 - Apr 21st, 2008, 5:21am
 
I was just talking to Saito last night about getting vertex buffers into the objLoader. Everyone is beating us to the punch. Wink
Re: Passing arrays to OPENGL methods (float buffer
Reply #13 - Apr 21st, 2008, 6:36pm
 
So far i'm using indexed arrays, i haven't started using vertex buffers yet.  I'm busy trying to get my loader to accept both quads and triangles.  After i'm done i'll post my code for review.  I've only been coding since Feb. I've taken a crash course in processing/java/opengl and i'm sure there's tons of stuff that i could optimize.
Page Index Toggle Pages: 1