First Person Objects (Gun) in OpenGL
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)
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 ~
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?
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?
Hmm, I don't understand what you mean, In my main display function, there is a sequence:
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.
- glClear();
- glLoadIdentity();
- 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 ~
You shouldn't have glFlush in there.
Sir, e^iπ + 1 = 0, hence God exists; reply!
Sorry, I saw glFlush() in somebody elses work and ehhh... 'borrowed'
~ Bring a Pen ~
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.
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 ~
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.
What? You mean my commenting out of glTranslate? Well, I did post it without, but I just wanted to be sure.
~ Bring a Pen ~
unknown Wrote:You shouldn't have glFlush in there.
OMG he's back
©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
(sing) Who's Back? Back Again? Who's Back? Shall I tell a friend? (/sing)
~ Bring a Pen ~
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.)
... 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!
Push/Pop a new matrix,
Cube @ 0,0,0.
But that's why I posted! It doesn't work for me!
~ Bring a Pen ~
Najdorf Wrote:OMG he's back
Yea I nearly had a heart attack when I read that!
The machine does not run without the coin.
Sorry, but my game still isn't working. Shall I just give up hope?
~ Bring a Pen ~
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Third Person Control | PAD Games | 4 | 2,609 |
Jun 22, 2005 01:59 AM Last Post: PAD Games |
|
| 3rd person camera | JeroMiya | 3 | 2,609 |
Jan 11, 2004 07:13 PM Last Post: JeroMiya |
|
| Releasing OpenGL Objects | Jake | 3 | 2,499 |
Dec 10, 2003 11:21 AM Last Post: arekkusu |
|
| openGL 2d drawing problems, objects appear to overlap | matthew | 12 | 4,770 |
Oct 5, 2003 07:53 PM Last Post: matthew |
|
| drawing objects in openGL, plane question. | xDexx | 7 | 4,521 |
Apr 28, 2003 01:06 PM Last Post: OneSadCookie |
|

