Sign in to follow this  
welOhim

Problem with FloatBuffer Object as a paramerer

Recommended Posts

Hello,

(No XPing, this is completely separate Q)

Generally I regard myself as an almost decent coder, but occasionally i get humbled by seemingly simple looking stuff and here is one. I am hoping someone can see where the proplem is.

 

Anticipating (infact fully knowing) that the following program will eventually grow exponentially, i am trying to write a function that would take a FloatBuffer type as a parameter so that i won't have to write those 5 lines over again and again bcus i have got zillions of vertices.

 

Also another reason for doing it this way is that the actual vertices amount would not be known at compile time but decoded at runtime 

so i would eventually do some dynamic memory allocation and read other stuff... but i the mean time when i call

 

...the function  setupByteBuffer( linev0, linebff0 );   

 

and by the time     gl.glDrawArrays(GL10.GL_POINTS, 0, 4 ); ,   is called,  the program stops working or crashes

 

I'm sure there is a way of doing this, does anyone know how.  

 

I always feel my code can sometimes be too cluttered, so the code below is oversimplified so the actual problem can be easily spotted

 

 Many thanks in advance

public class rampantjoiner {
   private FloatBuffer colorBuffer;   
   
   private FloatBuffer linebff;
   private FloatBuffer linebff0;
   
   
   public rampantjoiner( int n ) {
	   	   
	      setupByteBuffer( linev0,  linebff0  );
	      
	      ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
	      cbb.order(ByteOrder.nativeOrder());
	      colorBuffer = cbb.asFloatBuffer();
	      colorBuffer.put(colors);
	      colorBuffer.position(0);  
   }
   
   public void setupByteBuffer( float f[], FloatBuffer lbff  ){
	   
       linev = f;                                                 //EDit: still crashes ,  linebff is global to the class 
       linebff = lbff;                                           //EDIT:  this obvious error crept in will i was trying to isolate the error
       ByteBuffer vbbc = ByteBuffer.allocateDirect(linev.length * 4); // as you will see from the top linebff isnt even a local variale
       vbbc.order(ByteOrder.nativeOrder());                            // its always been global (check previous unedited history -2) 
       linebff = vbbc.asFloatBuffer();              
       linebff.put(linev);                       
       linebff.position(0);                          
   }
  
          
   private float[] linev0 = {
          1.0f,  -1.0f,   0.5f,
          1.0f,   1.0f,   0.5f,
          1.0f,   1.0f,  -1.5f,
          1.0f,  -1.0f,  -1.5f,
	  1.0f,  -1.0f,   0.5f,
   };

   private float[] linev = {};
  

   public void draw(GL10 gl) {
  
	  gl.glPointSize(5);
	  gl.glVertexPointer(3, GL10.GL_FLOAT, 0, linebff);      
	  gl.glDrawArrays(GL10.GL_POINTS, 0, 4 );
	      
	      
          gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
          gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
   }
   
   private float[] colors = {  

      0.0f, 0.0f, 0.0f, 1.0f, 
      0.0f, 0.0f, 0.0f, 1.0f,  
      0.0f, 0.0f, 0.0f, 1.0f,   
      0.0f, 0.0f, 0.0f, 1.0f,   
      0.0f, 0.0f, 0.0f, 1.0f,   
      0.0f, 0.0f, 0.0f, 1.0f,   
      0.0f, 0.0f, 0.0f, 1.0f,   
      0.0f, 0.0f, 0.0f, 1.0f,   
   };
}

Edited by welOhim

Share this post


Link to post
Share on other sites

Well, the main problem is your jugglery with local and member variables. Within setupByteBuffer, you have a local variable linebff that shadows the member variable linebff. The routine initializes the local variable, but those is killed when the routine is left. So the member variable is left uninitialized, but is then used within the rendering routine.

 

Honestly: That code snippet show many flaws: Violation of well-defined naming style conventions, cryptic variable names, needless copying of object references, misuse of member variables ... Such things "naturally" lead to problems like yours, and make debugging needless expensive. I suggest to clean up the code and to follow commonly accepted conventions.

 

BTW: I never programmed OpenGL in Java, so I'm not sure whether parts of regular set-up are not necessary there, whether you've forgotten them, or whether you have ripped them out in order to show us the essential parts of the code only. However, I'm missing some steps in setting up the VBO.

Edited by haegarr

Share this post


Link to post
Share on other sites

Well, the main problem is your jugglery with local and member variables. Within setupByteBuffer, you have a local variable linebff that shadows the member variable linebff. The routine initializes the local variable, but those is killed when the routine is left. So the member variable is left uninitialized, but is then used within the rendering routine.
 
Honestly: That code snippet show many flaws: Violation of well-defined naming style conventions, cryptic variable names, needless copying of object references, misuse of member variables ... Such things "naturally" lead to problems like yours, and make debugging needless expensive. I suggest to clean up the code and to follow commonly accepted conventions.
 

 

Apologies for the very obvious error

 

Please check edited code: Initially i was trying to isolate the error but i forgot to change it back. Anyway it crashed before then, proof of this is that if you check the edit history of question to the very firstrolleyes.gif , you will see that linebff was also a class global  variable. Thats what it was only what it was originally, but still crashes

 

As for cryptic variable names, i started this by modifying an example from somewhere (that worked for a very generalised situation) and only continuing with their conventions 

 

Somethings might seem needless because before i post (for clarity purpose) i simplify coderolleyes.gif

Many thanks

Edited by welOhim

Share this post


Link to post
Share on other sites
-I posted  a question which showed how much of a dummy i really am ( mainly because i fail to fully clean up my diagnosis and isolating error code. And also it also looks over the top because i had simplify for clarity purposes)
 
its just like when you make some typo errors that makes your sentence loose its meaning, sometimes you don't even know until u read ur own post back.... How much more on a coding level
 
But the main purpose of this reply post is because I OBSERVE SOME ARROGANCE HERE
 
I am very new here but i have read through some few posts on this forum and without any doubt people here are extremely brilliant and intelligent many times over (no one needs me to know that)
 
But in normal life that sometimes leads to arrogance, and unfortunately - in my observations - there is a some arrogance here too.
 
Mark of arrogance:  I am really stupid (literally now) and  doing unintelligent, gullible things. A smart person comes to around...    .... to say a lot about how flawed i am (BTW he is right), and is applauded (up voted). But this smart person would not highlight (quote) the exact flaws directly in 1 or 2 simple line's effort and show how to correct them (and thats the problem
 
I don't mind if my question remains unanswered, that means no one knows. But  what happened, to me, delves from being not just a brilliant mind but to being brilliant and arrogant, because the aim seem to be to shout it loud and clear that you are smart enough to decode how stupid my work is and leave it there.
 
Brilliant and arrogant people make you look stupid and leave it there, Brilliant people without the arrogance would still make you look stupid but in addition would highlight exactly were the problem is and how you sort it.
 
My guess is that at least one of the reasons you great, selfless people (plus all moderators) are on this forum/setup this forum  (and by the way many many thanks) is not just to help brilliant and smart people but also to give a hand to stupid people like me.
 
But Hey its your playing field guys if you love it that way (obviously it seems you do), then ride on!!!
Edited by welOhim

Share this post


Link to post
Share on other sites
But this smart person would not highlight (quote) the exact flaws directly in 1 or 2 simple line's effort and show how to correct them (and thats the problem)

 Or you could you know, do the reasonable thing "I don't understand why do you say that I'm copying references needlessly, could you specify what you mean?" You making a thread doesn't automatically makes you worthy of anyone's time. Which means that you're the arrogant here, in case the point didn't get across. 

 

And you can easily Google Java's code conventions to see what he means.

 

Anyway, enough of this, I see a problem here:

 

colorBuffer.put(colors);
colorBuffer.position(0); 

 

Don't set the position back to zero, put stuff in the buffer, then call buffer.flip(). Buffers hold internally a position and a limit, the limit is what the OGL functions will use to know how far they have to read into the buffer. What you have right there is that limit still remains zero, since you never called flip().

 

Moreover, you're probably going to start leaking memory, Google around for direct buffers in Java (Android, whatever) and how to free them. They're memory outside JVM managed heap, and they're not guaranteed to be freed at any specific time, so its best if you free them yourself manually in a timely manner.

 

EDIT: And the editor fucked up the quotes again, *sigh*

Edited by TheChubu

Share this post


Link to post
Share on other sites

But the main purpose of this reply post is because I OBSERVE SOME ARROGANCE HERE [...]

This forum is not a help desk. You can expect that you get help (because of statistics and, well, the overall great community), but you cannot demand it. You asked a question for which (at that time) a sound answer was given. The answer contained hints at how, in my opinion, the mistake could have happened, and suggested to overcome it. It even told some keywords to look for. Do I know what you are aware of and what not? I don't see that you have raised a query about how to overcome it; although it shall be your very own interest. Really, why do you mean that there is a duty for anyone here to guess your needs?

 

If you want to know something for which you have not found an answer so far, please ask. If your question is read from a person who can contribute, it is likely that s/he will do so. If you don't get an answer after a while, ask again. That would be much more efficient than affronting those persons.

Edited by haegarr

Share this post


Link to post
Share on other sites

This forum is not a help desk. You can expect that you get help (because of statistics and, well, the overall great community), but you cannot demand it.

 

You are right. And at no time did i "imply" i expected this forum to be a help desk.

 

In fact if you read it well, i appreciated the selfless time and efforts that people here spend to deal with other people's queries

 

All i was saying is if your answers just aims at making the questionnaire look stupid only, and leaving it there, then in my opinion that's arrogance 

Edited by welOhim

Share this post


Link to post
Share on other sites


All i was saying is if your answers just aims at making the questionnaire look stupid only, and leaving it there, then in my opinion that's arrogance 

Because you've misunderstood my post, I'll repeat it in other words.

 

We're voluntarily here. Maybe some people have the chance to watch the site all the day long. I cannot. If I see a post on the first overview page, and I decide to read it, and I see a question. and I understand the problem, and I have something (hopefully) constructive to contribute, then I write an answer. Anything else would be a waste of time for me and the readers. Although you had the opportunity to ask "where can I read about style conventions?" or "what does shadowing variables mean?", you did not. Hence I assume it is all clear to you, or else you are about to research it elsewhere. Instead you just wrote "well, that shadowing was obvious; however, the original problem still exists". Now, as said in my first post, I'm not familiar with OpenGL handling in Java; in fact I have no clue what the solution is. So I had nothing more to contribute.

 

Maybe I'm arrogant. So what? Makes it sense for you to affront me? There are 32 hours between your 1st (polite) post and your 2nd (affronting) post. Nobody has answered in this time. You were frustrated about that. A calm person would have bumped the thread, perhaps excusing with some "I wasn't able to find a solution in the meanwhile, so...".

 

As a last attempt here: Do you have any further question? Then please ask, and if somebody hits the post and has an answer, you will get it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this