Billboarding woes

Started by
0 comments, last by CyanPrime 12 years, 4 months ago
I'm trying to get Billboarding in my game, but I can't seem to get it to work. I've tried both Lighthouse's and NeHe's tutorials, but no matter what there are problems. My current code uses gl rotate, and translate, but the sprites seem to rotate too slowly, and rotate also based on the distance of the player/camera from the sprite.

Here is the code, can anyone help me?


public void drawBillBoardSprite(Texture texture, Vector3f sprPos, float r, float g, float b, int sprWidth, int sprHeight){
// store the current model matrix
GL11.glPushMatrix();
GL11.glTranslatef(sprPos.x, 0, sprPos.z);
// bind to the appropriate texture for this sprite
texture.bind();

Vector3f objToCam = new Vector3f(player.pos.x - sprPos.x, 0, player.pos.z - sprPos.x);
Vector3f look = new Vector3f(0,0,1);

objToCam.normalise();

float angleCosine = Vector3f.dot(look, objToCam);

Vector3f upAux = new Vector3f(0,0,0);
Vector3f.cross(look, objToCam, upAux);

if ((angleCosine < 0.9999999) && (angleCosine > -0.9999999)) {
float angle = (float) (Math.toDegrees(Math.acos(angleCosine)));
GL11.glRotatef(angle, upAux.x, upAux.y, upAux.z);
}

// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0); GL11.glVertex3f( - (sprWidth/2), (sprHeight/2), 0); // Top left Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), 0); GL11.glVertex3f( + (sprWidth/2), (sprHeight/2), 0); // Top right Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight()); GL11.glVertex3f( + (sprWidth/2), -(sprHeight/2), 0); // Bottom right Of The Quad (Left)
GL11.glTexCoord2f(0, texture.getHeight()); GL11.glVertex3f( - (sprWidth/2), -(sprHeight/2), 0); // Bottom left Of The Quad (Left)
GL11.glEnd();

// restore the model view matrix to prevent contamination
GL11.glPopMatrix();

}
Advertisement
Got it working, finally ;D ;D ;D ;D ;D ;D ;D


public void drawBillBoardSprite(Texture texture, Vector3f sprPos, float r, float g, float b, int sprWidth, int sprHeight){
// store the current model matrix
GL11.glPushMatrix();
GL11.glTranslatef(sprPos.x, 0, sprPos.z);
// bind to the appropriate texture for this sprite
texture.bind();

FloatBuffer modelview = BufferUtils.createFloatBuffer(16);

// get the current modelview matrix
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);

// Note that a row in the C convention is a column
// in OpenGL convention (see the red book, pg.106 in version 1.2)
// right vector is [1,0,0] (1st column)
// lookAt vector is [0,0,1] (3d column)
// leave the up vector unchanged (2nd column)
// notice the increment in i in the first cycle (i+=2)
for(int i=0; i<3; i+=2 )
for(int j=0; j<3; j++ ) {
if ( i==j )
modelview.put(i*4+j, 1.0f);
else
modelview.put(i*4+j, 0.0f);
}

// set the modelview matrix
GL11.glLoadMatrix(modelview);

// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0); GL11.glVertex3f( - (sprWidth/2), (sprHeight/2), 0); // Top left Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), 0); GL11.glVertex3f( + (sprWidth/2), (sprHeight/2), 0); // Top right Of The Quad (Left)
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight()); GL11.glVertex3f( + (sprWidth/2), -(sprHeight/2), 0); // Bottom right Of The Quad (Left)
GL11.glTexCoord2f(0, texture.getHeight()); GL11.glVertex3f( - (sprWidth/2), -(sprHeight/2), 0); // Bottom left Of The Quad (Left)
GL11.glEnd();

// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}

This topic is closed to new replies.

Advertisement