First Person Objects (Gun) in OpenGL

Member
Posts: 281
Joined: 2009.04
Post: #1
I'm writing a first person shoota in openGL and it's all working nicely, apart from textures.

I was wondering how to recreate a first person gun, like in HALO. I thought a 2D bitmap is not good enough, so how would I make a 3D gun?
I had a crack at doing this myself, but the gun (Cube + imagination) keeps moving where I don't want it: I can make it so when I turn it stays, or when I move it stays. Either or.

I think the problem is where drawPlayer is called, before glLoadIdentity or after?

Here's drawPlayer() (it should be drawgun)

Code:
void     drawPlayer(void)
{
        // xp , yp and zp are the translation variables.
    //glLoadIdentity();
    glPushMatrix();

    glLoadIdentity();
    //glTranslatef( xp,5.0f,zp+5);
    

    //glRotatef(-pitch,1.0f,0.0f,0.0f);
    
    glTranslatef(xp+3,6.0f,zp+3 );
    

    glRotatef(-heading,0.0f,1.0f,0.0f);
    glRotatef(pitch,0.0f ,0.0f,1.0f);
    
    

    glColor3f(0.0f,0.0f,1.0f);
    glutSolidCube(2);

    
    glPopMatrix();
    glFlush();
    //glLoadIdentity();
        
}

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
An easy way to draw geometry relative to the viewer's location is to do it without applying your camera transformation; either before it's applied or after popping/loading identity.

Learning to visualize transformations takes a while before it becomes intuitive, but it makes coding things like this much easier once you've done it. Maybe my matrix tutorial and its companion application could help?
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #3
Hmm, I don't understand what you mean, In my main display function, there is a sequence:

  1. glClear();
  2. glLoadIdentity();
  3. Camera transforms


I tried inserting the drawPlayer() function above into:
• After glClear, but before glLoadIdentity.
• After glLoadIdentitiy.
• After camera transforms.

Neither of these seem to give the desired effect.

~ Bring a Pen ~
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
You shouldn't have glFlush in there.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #5
Sorry, I saw glFlush() in somebody elses work and ehhh... 'borrowed' Smile

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
mikey Wrote:I tried inserting the drawPlayer() function above

Sorry, I wasn't very specific... You'll want to do no transformations at all in drawPlayer(), aside from positioning your cube in a visible spot relative to the viewer. Anything having to do with player orientation or position should come out, because the identity transformation is already in the space you want.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #7
Ahh, I've removed all transformations as you said, but still nothing happens. I tried moving it to all the places above, but still nothing.

Code:
void     drawPlayer(void)
{
    //glLoadIdentity();
    glPushMatrix();

    glLoadIdentity();
    //glTranslatef( xp,5.0f,zp+5);
    

    //glRotatef(-pitch,1.0f,0.0f,0.0f);
    
    //glTranslatef(0.0f,0.0f,0.0f );
    

    //glRotatef(-heading,0.0f,1.0f,0.0f);
    //glRotatef(pitch,0.0f ,0.0f,1.0f);
    
    

    glColor3f(0.0f,0.0f,1.0f);
    glutSolidCube(2);

    
    glPopMatrix();
    //glFlush();
    //glLoadIdentity();
        
}

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #8
I think you're misunderstanding how transformations work. glTranslatef(0.0f, 0.0f, 0.0f) does nothing at all, as was explained on this forum a few days ago. You'll need to put in the effort of gaining an understanding of the system before you use it, so that you can write working code based on your own understanding rather than pasting snippets together haphazardly and crossing your fingers for it to do what you want. The Red Book has a chapter on viewing that can help you.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #9
What? You mean my commenting out of glTranslate? Well, I did post it without, but I just wanted to be sure.

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #10
unknown Wrote:You shouldn't have glFlush in there.

OMG he's back Blink

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #11
(sing) Who's Back? Back Again? Who's Back? Shall I tell a friend? (/sing)

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #12
ThemsAllTook Wrote:I think you're misunderstanding how transformations work. glTranslatef(0.0f, 0.0f, 0.0f) does nothing at all, as was explained on this forum a few days ago. You'll need to put in the effort of gaining an understanding of the system before you use it, so that you can write working code based on your own understanding rather than pasting snippets together haphazardly and crossing your fingers for it to do what you want. The Red Book has a chapter on viewing that can help you.

Transformations are vital, and not entirely simple. It consists of two parts, projection part (the camera lens) and the modelview. This requires two or three chapters in any decent CG book, plus some linear algebra theory. And that is there for a reason.

The easiest way to handle it should be something like this (trying to make a long story short, sorry if I miss something):

Start your display function with glLoadIdentity and then gluLookAt to place the camera. Now you are in "world coordinates". Then add transformations (translation and rotation) for placing the objects in the world. Use the sequence glPushMatrix/transform/draw/glPopMatrix to restore world coordinates before you go to the next one.

Every transformation is relative to the previous. Translating by 0, 0, 0 means move by nothing, a no-operation, Rotating by zero also does nothing. So it isn't a "move to" but "move from where you are".

Maybe that can be a start. By all means read the Red Book but maybe there are better material to learn it from. (I have a whole pile of such books but it isn't all that easy to say which one is best; I know it a bit too well so it has nothing to say to me.)
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #13
... Yes! And that's why I posted:

Push/Pop a new matrix,
Cube @ 0,0,0.

But that's why I posted! It doesn't work for me!

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 67
Joined: 2006.06
Post: #14
Najdorf Wrote:OMG he's back Blink

Yea I nearly had a heart attack when I read that!

The machine does not run without the coin.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #15
Sorry, but my game still isn't working. Shall I just give up hope? Sad

~ Bring a Pen ~
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Third Person Control PAD Games 4 2,954 Jun 22, 2005 01:59 AM
Last Post: PAD Games
  3rd person camera JeroMiya 3 3,035 Jan 11, 2004 07:13 PM
Last Post: JeroMiya
  Releasing OpenGL Objects Jake 3 2,695 Dec 10, 2003 11:21 AM
Last Post: arekkusu
  openGL 2d drawing problems, objects appear to overlap matthew 12 5,572 Oct 5, 2003 07:53 PM
Last Post: matthew
  drawing objects in openGL, plane question. xDexx 7 5,467 Apr 28, 2003 01:06 PM
Last Post: OneSadCookie