Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (582)
games submitted by our members
Games in WIP (503)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Error in billbording code  (Read 1300 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2011-12-12 04:41:46 »

Hello, I've been going through this tutorial on billboarding, but when I finished it and ran the code I couldn't find my billboarded sprite at all, can anyone tell me why I can't find it? All the calculations seem to be correct.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
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);
          GL11.glMultMatrix(fb);
           
         // draw a quad textured to match the sprite
         GL11.glBegin(GL11.GL_QUADS);
            GL11.glTexCoord2f(0,0);                              GL11.glVertex3f(-sprPos.x, (sprHeight/2), -sprPos.z);    // Top left Of The Quad (Left)
           GL11.glTexCoord2f(0, texture.getHeight());               GL11.glVertex3f(-(sprPos.x + sprWidth), (sprHeight/2), -sprPos.z);  // Top right Of The Quad (Left)
           GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());   GL11.glVertex3f(-(sprPos.x + sprWidth), -(sprHeight/2), -sprPos.z);   // Bottom right Of The Quad (Left)
           GL11.glTexCoord2f(texture.getWidth(), 0);               GL11.glVertex3f(-sprPos.x, -(sprHeight/2), -sprPos.z);   // Bottom left Of The Quad (Left)
        GL11.glEnd();
         
      // restore the model view matrix to prevent contamination
     GL11.glPopMatrix();

   }
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2011-12-12 08:05:57 »

You didn't flip your FloatBuffer Grin

1  
2  
fb.flip();
glMultMatrix(fb);

Offline theagentd
« Reply #2 - Posted 2011-12-12 09:37:12 »

Not again! We need a sticky'd thread telling people to flip their buffers!

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CyanPrime
« Reply #3 - Posted 2011-12-12 18:04:53 »

You didn't flip your FloatBuffer Grin

1  
2  
fb.flip();
glMultMatrix(fb);

Yup, that was it. Thank you very much.  Grin
Offline CyanPrime
« Reply #4 - Posted 2011-12-12 21:05:53 »

Okay, so I thought I had this all worked out, but I found another error. The farther away from 0,0,0 the sprite is the less the billboarding works. only being 20 units away the sprite goes nearly sideways so you can almost not see it.

Below is my new code, can anyone tell me whats going on?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
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();

   }
Offline theagentd
« Reply #5 - Posted 2011-12-13 14:22:40 »

Why generate your own matrix instead of just using glTranslatef(...)?

Myomyomyo.
Offline CyanPrime
« Reply #6 - Posted 2011-12-13 16:59:27 »

Why generate your own matrix instead of just using glTranslatef(...)?
Oops, I read this wrong. How would I go about using glTranslatef() for this?
Offline CyanPrime
« Reply #7 - Posted 2011-12-13 23:00:22 »

Like this? it doesn't rotate enough though, but I think I'm getting close. Any help would be greatly appreciated.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
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();

   }
Offline theagentd
« Reply #8 - Posted 2011-12-14 09:06:49 »

I don't have much experience with neither vector math nor sprites in 3D, so I don't know exactly how much I can help here. A few tips though:

glRotatef(angle, x, y, z) works by rotating around the axis supplied. As an example, if your camera is in front of the sprite and you rotate it around the x-axis (1, 0, 0), the sprite will make a front/back flip. In 2D, you always rotate along the z-axis (0, 0, 1) because it is the axis extending into the screen, which is the only possible rotation in 2D. I have no idea what values you are passing into glRotatef(...), but it seems to me that you're trying to do about the same thing as gluLookAt(...) does.

Also when finding an angle between two points, I always use Math.atan2(y, x), because it works for any angle without any if-cases.

Myomyomyo.
Offline CyanPrime
« Reply #9 - Posted 2011-12-14 16:25:25 »

Got it working, finally  Grin Grin Grin Grin Grin Grin Grin

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
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();
   }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (72 views)
2014-04-15 09:08:23

BurntPizza (68 views)
2014-04-14 18:46:01

UprightPath (79 views)
2014-04-14 08:39:50

UprightPath (65 views)
2014-04-14 08:35:47

Porlus (80 views)
2014-04-14 06:48:38

tom_mai78101 (104 views)
2014-04-09 19:04:31

BurntPizza (164 views)
2014-04-08 14:06:04

tom_mai78101 (260 views)
2014-04-05 04:34:39

trollwarrior1 (210 views)
2014-04-04 03:06:45

CJLetsGame (220 views)
2014-03-31 17:16:10
List of Learning Resources
by SHC
2014-04-17 18:17:39

List of Learning Resources
by Longarmx
2014-04-07 18:14:44

Good Examples
by matheus23
2014-04-05 04:51:37

Good Examples
by Grunnt
2014-04-03 06:48:46

Good Examples
by Grunnt
2014-04-03 06:48:37

Good Examples
by matheus23
2014-04-01 09:40:51

Good Examples
by matheus23
2014-04-01 09:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 06:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!