Sign in to follow this  

2D sprites in 3D world...

This topic is 4072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys. I'm having a slight problem. I want to create a sprite in my 3d environment that would always face the camera. I want to use this sprite for things like smoke and fire. What I do is I draw a quad thats on the y-z plane, then rotate it to face teh camera position using polar coordinates. But for some reason it never works.. all the angles seem to be messed up. Here's the code:

     float dx = camera->_xCam - _x;
     float dy = camera->_yCam - _y;
     float dz = camera->_zCam - _z;               
     float r = sqrt(dx*dx + dy*dy + dz*dz );     

//Calculate the rotation around the y and z axis
     _yrot = 90-acos(dz/r)*180/PI;
     _zrot = atan2(dy, dx)*180/PI;

     
     glPushMatrix(); 
        glRotatef(_xrot, 1.0,0.0,0.0);         
        glRotatef(_yrot, 0.0,1.0,0.0);        
        glRotatef(_zrot, 0.0, 0.0,1.0);             
        glScalef( _xscale, _yscale, _zscale);                        
        
        if( hasTexture ){
            glEnable(GL_TEXTURE_2D);
            texture->activateTexture();
        } else {
            glDisable(GL_TEXTURE_2D);
        }
             

        glBegin(GL_QUADS);
            glTexCoord2f( 0.0,0.0);
            glVertex3f(0.0,-0.5,-0.5);    

            glTexCoord2f( 0.0,1.0);            
            glVertex3f(0.0,-0.5,0.5);             

            glTexCoord2f( 1.0,1.0);                        
            glVertex3f(0.0,0.5,0.5);                          

            glTexCoord2f( 1.0,0.0);                        
            glVertex3f(0.0,0.5,-0.5);                                       
        glEnd();
        
     glPopMatrix();



If anyone knows whats wrong or has a better way to draw these sprites. Please let me know. Thank you. Gavin

Share this post


Link to post
Share on other sites
I didn't look further than this line, but here:
90-acos(dz/r)*180/PI;
You're mixing radians and degrees, which renders the results meaningless. So I'd fix that first and see if it improves things at all (it may not, since there may be other errors as well).

Share this post


Link to post
Share on other sites
I just spent a good 5 minutes running through the Open GL forum looking for a previous reply I made not too long ago, only to realise that it was in the Graphics Programming and Theory section...

Anyway, here's the stuff you're looking for.

Share this post


Link to post
Share on other sites

This topic is 4072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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