Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:06:33+00:00 2026-05-27T11:06:33+00:00

I got this code for billboarding I created (With the help of others, and

  • 0

I got this code for billboarding I created (With the help of others, and a nehe tutorial), but it seems that when I move my sprites from 0,0,0 it stops working correctly, for example the closer I get to a sprite that’s at 50,0,50 the more sideways it gets.

Any idea how to make the sprites billboard properly when not on 0,0,0?

Here is the code:

public void drawBillBoardSprite(Texture texture, Vector3f sprPos, float r, float g, float b, int sprWidth, int sprHeight){
         // store the current model matrix
        GL11.glPushMatrix();

            // bind to the appropriate texture for this sprite
            texture.bind();
            Vector3f look = new Vector3f(0,0,0);
            Vector3f.sub(player.pos, sprPos, look);
            look.normalise();

            Vector3f right = new Vector3f(0,0,0);
            Vector3f.cross(new Vector3f(0,1,0) /* <-- default up vector */, look, right);
            right.normalise();

            Vector3f up = new Vector3f(0,0,0);
            Vector3f.cross(look, right, up);
            up.normalise();


            Matrix4f m = new Matrix4f();
            m.m00 = right.x;
            m.m01 = right.y;
            m.m02 = right.z;
            m.m03 = 0;

            m.m10 = up.x;
            m.m11 = up.y;
            m.m12 = up.z;
            m.m13 = 0;

            m.m20 = look.x;
            m.m21 = look.y;
            m.m22 = look.z;
            m.m23 = 0;

            m.m30 = sprPos.x;
            m.m31 = sprPos.y;
            m.m32 = sprPos.z;
            m.m33 = 1;

            java.nio.FloatBuffer fb = BufferUtils.createFloatBuffer(32);
            m.store(fb);
            fb.flip();
            GL11.glMultMatrix(fb);

            // 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();

    }

Edit: tried this code based on Knetic’s answer, but it didn’t even rotate the sprites towards the camera. Here is the code I used:

public void drawBillBoardSprite(Texture texture, Vector3f sprPos, float r, float g, float b, int sprWidth, int sprHeight){
         // store the current model matrix
        GL11.glPushMatrix();
            FloatBuffer tempViewBuf = BufferUtils.createFloatBuffer(16);

            GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, tempViewBuf);

            float[] tempView = new float[16];

            for(int i = 0; i < 16; i++){
                tempView[i] = tempViewBuf.get(i);
            }

            float[] modelView = new float[6];

            modelView[0] = tempView[0] - tempView[1];
            modelView[1] = tempView[0] + tempView[1];

            modelView[2] = tempView[4] - tempView[5];
            modelView[3] = tempView[4] + tempView[5];

            modelView[4] = tempView[8] - tempView[9];
            modelView[5] = tempView[8] + tempView[9];

            // bind to the appropriate texture for this sprite
            texture.bind();

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

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

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:06:34+00:00Added an answer on May 27, 2026 at 11:06 am

    Just had to use a different way of billboarding:

    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();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

got this code from a website that helped me in creating buttons and stuff.
I got this code that should help me save an image to the servers
I got this code, but I just want it to grab p-tags that aren't
I got this code below from a tutorial I'm using to learn PHP. I
I got this code from this site: http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx But I'm just a beginner so
I got this code from a WPF (very simple) application that illustrate my problem
Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;
I got this code from our frontend guy for headings: <h2 class=headline><span>Foobar</span></h2> The span
I got this code from the wordpress <head profile=http://gmpg.org/xfn/11> What does this means? what
I got this code from someone, it's almost perfect to create a dynamic breadcrumb,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.