10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course

6,594 views
6,225 views

Published on

Main topics:
In this presentation, part of the 3D graphics and game develpment course, we discuss OpenGL & GLUT mouse interaction & camera control
http://academy.telerik.com/.../3d-game-development-opengl
Telerik Software Academy: http://www.academy.telerik.com
The website and all video materials are in Bulgarian

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
6,594
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
70
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course

  1. 1. OpenGL & GLUT Mouse Interaction & Camera Control Mouse events, Advanced camera controlGeorge GeorgievTechnical TrainerGeorge AtanasovFront-End Developerhttp://academy.telerik.com
  2. 2. Table of Contents GLUT Mouse Interaction  Mouse up/down events  Passive motion  Active motion OpenGL Camera  Default transformation  “Manual” control  Custom camera class 2
  3. 3. GLUT Mouse Interaction Mouse event callbacks
  4. 4. GLUT Mouse Interaction GLUT provides mouse detection capabilities  Advantages  Multi-platform  Similar to other GLUT routines  Easy to code  Disadvantages  Doesn’t receive device information  No scroll information  Unstable ‘leave’ and ‘enter’ events 4
  5. 5. GLUT Mouse Interaction Detecting mouse buttons  Registering callback function  glutMouseFunc(void(*func)(int button, int state, int x, int y))  func – mouse button callback function  button – mouse button raising the event  state – type of event  x, y – cursor coordinates (from upper left corner) 5
  6. 6. GLUT Mouse Interaction Detecting mouse buttons (2)  Callback parameter values  button  GLUT_LEFT_BUTTON  GLUT_MIDDLE_BUTTON  GLUT_RIGHT_BUTTON  state  GLUT_DOWN  GLUT_UP 6
  7. 7. GLUT Mouse Interaction Detecting mouse buttons (2)  Callback parameter values  button  GLUT_LEFT_BUTTON  GLUT_MIDDLE_BUTTON  GLUT_RIGHT_BUTTON  state  GLUT_DOWN  GLUT_UP 7
  8. 8. GLUT Mouse Interaction Detecting movement  Types  Passive  Mouse moves  No buttons pressed  Active  Mouse moves  One or more buttons pressed 8
  9. 9. GLUT Mouse Interaction Passive motion  Registering callback function  glutPassiveMotionFunc(void(*func)(int x, int y))  func – mouse passive motion callback  x, y – cursor coordinates (from upper left corner) 9
  10. 10. GLUT Mouse Interaction Active motion  Registering callback function  glutMotionFunc(void(*func)(int x, int y))  func – mouse active motion callback  x, y – cursor coordinates (from upper left corner) 10
  11. 11. GLUT Mouse Interaction GLUT Mouse leave and entry routines  Detect when cursor leaves and enters window  Registering callback  glutEntryFunc(void(*func)(int state))  Func – callback handling cursor leaving and entering window  State – enter or leave event  GLUT_ENTERED  GLUT_LEFT 11
  12. 12. GLUT Mouse Interaction Cursor-related routines  glutWarpPointer (int x, int y)  Sends cursor to specified coordinates  glutSetCursor (int cursor)  Sets the cursor image (style) for the window  Values:  GLUT_CURSOR_NONE, GLUT_CURSOR_INFO, GLUT_CURSOR_DESTROY, etc…  Google for more (much, much more) 12
  13. 13. Mouse Interaction Object-oriented mouse handling  Custom mouse class  Current cursor position  Last cursor position and/or drag-begin position  Current mouse button states  Last mouse button states  Cursor style  Anything else you can think of 13
  14. 14. Custom mouse class Live Demo
  15. 15. Mouse Interaction курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET http://academy.telerik.com/ програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезаниякурсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
  16. 16. OpenGL Camera ControlBasic and Advanced camera positioning
  17. 17. OpenGL Camera Control Default camera position  Set up with glLoadIdentity()  Centered at coordinate system center (0, 0, 0)  Rotation around all axes – zero (0, 0, 0)  Looks down the negative Z axis 17
  18. 18. OpenGL Camera Control “Manual” control  Applying transformations to the world  Opposite those of the camera  Applied before rendering anything else  Why not use gluLookAt ?  Does the same things without asking you  Gives you less freedom  Future – transformations won’t be deprecated  gluLookAt most probably will 18
  19. 19. OpenGL Camera Control “Manual” control (2)  Imagining it – three options  Positioning the ‘world’  Opposite to the location and orientation of the ‘camera’  Positioning the ‘camera’ away from the ‘world’  Positioning the coordinate system of the ‘paintbrush’  Either way, the result is the same 19
  20. 20. OpenGL Camera Control “Manual” control (3)  The steps  Load default view  glLoadIdentity – not really camera-related  Move away  glTranslate*  Look away  glRotate*  Move on to lighting, drawing objects, etc… 20
  21. 21. OpenGL Camera control Moving away  glTranslate*  Specifies a translation  Applies to any subsequently drawn geometry  You could imagine it moves the ‘paintbrush’  Used for all object positioning (not just camera)  Say we want the camera at C(0, 0, 20)  glTranslatef(0, 0, -20) 21
  22. 22. OpenGL Camera control Looking away  glRotate*  Specifies a rotation  Applies to any subsequently drawn geometry  You could imagine it turns the ‘paintbrush’  Used for all object positioning (not just camera)  If we want the camera to look right 90 degrees  glRotatef (-90, 0, 1.0, 0) 22
  23. 23. OpenGL Camera control Looking away (2) – explanation  glRotatef parameters  1st parameter – degrees to rotate  2nd, 3rd, 4th parameter – multiplier for resultant rotation on x, y, z axis respectively  Example: glRotatef (90, 0.5, 1.0, 0.0) = rotate by  45 degrees round X  90 degrees round Y  0 degrees round Z 23
  24. 24. OpenGL Camera control Looking away (3) – explanation  glRotatef positive rotations  Positive rotation round Y 24
  25. 25. OpenGL Camera control Looking away (4) – explanation  glRotatef positive rotations  Positive rotation round X 25
  26. 26. OpenGL Camera control Looking away (5) – explanation  glRotatef positive rotations  Positive rotation round Z 26
  27. 27. OpenGL Camera Control Basic camera control  So far we can  Set the location of the camera  Set the orientation of the camera  Limit the rotation of the camera  Hard to do that with gluLookAt 27
  28. 28. OpenGL Camera Control Advanced camera control  A good camera can  Move forward and backward  Strafe left and right  Move up and down  Look left and right  Look up and down  Tilt left and right  “LookAt” 28
  29. 29. OpenGL Camera Control Custom camera class  Fields  Location, last location  Rotation (Euler angles), last rotation  Forward vector  Right vector  Upward vector  Speed? Maximum upward (x) rotation? Etc… 29
  30. 30. OpenGL Camera Control Custom camera class  Methods  Set/Get location  Set/Get rotation  Move forward, backward, upward, downward  Strafe left, right  Look up, down, left, right  Tilt left, tilt right  “Look at”, “sweep left/right”, “fly to” ? Etc… 30
  31. 31. Custom camera class Live Demo
  32. 32. Mouse Interaction & Camera control курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET http://academy.telerik.com/ програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезаниякурсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
  33. 33. Free Trainings @ Telerik Academy 3D Graphics and Game Development  academy.telerik.com/.../3d-game- development-opengl Telerik Software Academy  academy.telerik.com Telerik Academy @ Facebook  facebook.com/TelerikAcademy Telerik Software Academy Forums  forums.academy.telerik.com

×