23. OpenGL Camera Part 2
In this opengl camera tutorial you will learn how to incorporate
the mouse using the glut mouse functions, to move our opengl camera like in a first person shooter, along with how to implement the strafe (sideways movement) you see in these games also.
To add the strafe we use:
if (key==’d’)
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos += float(cos(yrotrad)) * 0.2;
zpos += float(sin(yrotrad)) * 0.2;
}
if (key==’a’)
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= float(cos(yrotrad)) * 0.2;
zpos -= float(sin(yrotrad)) * 0.2;
}
You can see that I have simply flipped the cos and sin functions
and set both the xpos and zpos to either a negative or a positive
Now the mouse is a little easier as it uses no trigonometry.
We have to add a function which I have called: mouseMovement
void mouseMovement(int x, int y) {
int diffx=x-lastx; //check the difference between the current x and the last x position
int diffy=y-lasty; //check the difference between the current y and the last y position
lastx=x; //set lastx to the current x position
lasty=y; //set lasty to the current y position
xrot += (float) diffy; //set the xrot to xrot with the addition of the difference in the y position
yrot += (float) diffx;// set the xrot to yrot with the addition of the difference in the x position
}
To get GLUT to register the mouse movements, we call the line:
glutPassiveMotionFunc(mouseMovement);
to see where the mouse is when no mouse button is pressed and call
any functions within our mouseMovement function.
And that does it…
You now know how to add a strafe movement and mouse view.
The next camera tutorial will have lean, jump and duck effects.
If you have any questions, email me at swiftless@gmail.com
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. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. |
#include <GL/gl.h> #include <GL/glut.h> #include <stdlib.h> #include <math.h> //angle of rotation float lastx, lasty; //positions of the cubes void cubepositions (void) { //set the positions of the cubes //draw the cube void init (void) { void enable (void) { void camera (void) { void display (void) { void reshape (int w, int h) { void keyboard (unsigned char key, int x, int y) { if (key==‘z’) if (key==‘w’) if (key==‘s’) if (key==‘d’) if (key==‘a’) if (key==27) void mouseMovement(int x, int y) { int main (int argc, char **argv) { glutPassiveMotionFunc(mouseMovement); //check for mouse glutKeyboardFunc (keyboard);
|
Please run you damn code before you post it online, it is full of glitches.
Hey KanTheMan2134,
You sound frustrated, how is everything? The code has been run and virtually all of my code is run before it goes online. This is an older tutorial and some of the formatting may not be up to scratch but I can assure you that it will work. You may just have to type it out instead of copying and pasting.
Thanks,
Swiftless
thanks for the code, I was looking for a fps example
In Code::Blocks, you need to replace the single quotation marks. But good overall code!!!
Hi drew,
That’s a problem with how WordPress converts the characters.
Cheers,
Swiftless
This implementation has gimbal lock
Hi Joshua,
It doesn’t technically have gimbal lock when you first start, it can however suffer from gimbal lock. This is only a starter, as we don’t want to scare people away with quaternions straight away.
Cheers,
Swiftless
Hi, two suggestions:
– if you include math.h you can use the M_PI define for the pi angle.
– you are moving the modelview around, it’s incorrect, you want to move the camera, so in the display you should traslate and rotate the GL_PROJECTION, and then before the return point, set the GL_MODELVIEW again. Obviously, if you do this you have to change all the functions, but it’s the correct way.
Anyway very good tutorial keep up the good work!
Hi Enrico,
That’s definitely true about math.h and M_PI!
As for saying what I’m doing is incorrect, I hate to disappoint, but there is absolutely nothing wrong with it. In fact, it is a lot riskier to mess around with your GL_PROJECTION matrix and debugging your camera code can be a massive pain. The GL_PROJECTION matrix defines the projection from 3D to 2D on our screen, not where we are in our 3D scene.
As a camera moves around a 3D scene, it makes a lot more sense to move the objects around the point of projection, to me that makes more sense anyway.
I recommend you take a look at the following article on GL_PROJECTION abuse:
http://www.sjbaker.org/steve/omniv/projection_abuse.html
Cheers,
Swiftless
Really good. It helped me a lot. I’m working OpenGL with SDL instead GLUT, but that strafe movement was what I wanted to learn how to implement.
I was using gluLookAt() to try to do that, but I understood the way you did. Thanks!
Hello,
I am working on a project that involves being able to walk around a scene using the asdw keys and looking around using the arrow keys. I just recently added lighting effects to the mix. I also have a variable ‘mode’ that determines whether the scene is viewed in orthogonal mode or perspective mode. In orthogonal mode, I not able to change the position of the camera or eye (however you wanna think of it). You can only walk around in the perspective mode. When in orthogonal mode, the lighting effects work. But as soon as I switch to perspective mode, it is as if I never applied the lighting effects. The global ambiance is back and light intensities do not change. Would you have any insight as to why this would be happening. I was wondering if it had anything to do with the specific method you used to create the first person perspective as opposed to using something like gluLookAt(). Thanks
Bonjour,
D’abord je vous remercie sur votre code qui’est vraiment intéressant, et qui m’a aidé vraiment.
Pourriez vous faire un autre tutorial sur linteraction multitouch avce glut et opengl.
et merci