Mar 10
25
Next: 24. OpenGL Camera Part 3
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. 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);
|
Related posts:
- 24. OpenGL Camera Part 3
- 22. OpenGL Camera
- 19. OpenGL Fullscreen Mode
- 2. Terrain Loading
- 4. Terrain Triangle Strips
Next: 24. OpenGL Camera Part 3