Need help with FloatBuffers, Vector3fs and other goodies...

Started by smitty1276, June 30, 2004, 07:07:45

Previous topic - Next topic

smitty1276

Alright guys, I'm still new to this java opengl stuff... here goes.

I'm trying to implement a billboarded particle class... pretty straightforward.  But using the vector classes and Floatbuffers and whatnot is confusing me, so let me cut to the chase (warning, it is slapped together and isn't pretty)...

Vector3f right = null;
Vector3f up = null;
Vector3f centerPt = null;

Vector3f topLeft = null;
Vector3f bottomLeft = null;
Vector3f bottomRight = null;
Vector3f topRight = null;
FloatBuffer viewMatrix = ByteBuffer.allocateDirect(16*4).asFloatBuffer();
        
//get the up and right vectors from modelview matrix for billboarding...
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, viewMatrix);
right = new Vector3f(viewMatrix.get(0), viewMatrix.get(4), viewMatrix.get(8));
up = new Vector3f(viewMatrix.get(1), viewMatrix.get(5), viewMatrix.get(9));
                
//topleft = center + (up-right)*(width/2)
Vector3f.sub(up, right, topLeft);  
topLeft.scale(width/2);                  
Vector3f.add(centerPt, topLeft, topLeft);  
        
//bottomleft = center + (up+right)*(-width/2)
Vector3f.add(right, up, bottomLeft);
bottomLeft.scale(-width/2);
Vector3f.add(centerPt, bottomLeft, bottomLeft);
        
//etc...
Vector3f.sub(right, up, bottomRight);
bottomRight.scale(width/2);
Vector3f.add(centerPt, bottomRight, bottomRight);
        
Vector3f.add(right, up, topRight);
topRight.scale(width/2);
Vector3f.add(centerPt, topRight, topRight);       

GL11.glBegin(GL11.GL_QUADS);
  GL11.glTexCoord2f(0.0f, 1.0f);
  GL11.glVertex3f(topLeft.x, topLeft.y, topLeft.z);
  GL11.glTexCoord2f(0.0f, 0.0f);
  GL11.glVertex3f(bottomLeft.x, bottomLeft.y, bottomLeft.z);
  GL11.glTexCoord2f(1.0f, 0.0f);
  GL11.glVertex3f(bottomRight.x, bottomRight.y, bottomRight.z);
  GL11.glTexCoord2f(1.0f, 1.0f);
  GL11.glVertex3f(topRight.x, topRight.y, topRight.z);
GL11.glEnd();



It's appears as though the Vector3f.add() and sub() calls are leaving the destination vectors null, and I'm getting a null pointer exception.

Any idea what I might be doing wrong?  

If this is an absolutely retarded question, I apologize.

smitty1276

Well... it did turn out to be a dumb problem.  I assumed that the methods assigned a new reference to the variables.  Apparantly that wasn't the case...

I explicitly created a new instance of Vector3f for each of the declared Vector3fs and it seems to accept that now.

Now I just have to figure out how to get the thing to render correctly.  :?

Peter M Ashford

Quote from: "smitty1276"Well... it did turn out to be a dumb problem.  I assumed that the methods assigned a new reference to the variables.  Apparantly that wasn't the case...
:?

It will assign a new reference if you do:

Vector3f myVector = Vector3f.sub(up, right, null);  

The format you were using:

Vector3f myVector
Vector3f.sub(up, right, myVector);  


...assumes that myVector is already allocated (I'd say that it's like this so you don't allocate space for return values - only when you need to)