10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course
-
Upload
telerik-software-academy -
Category
Education
-
view
4.649 -
download
6
description
Transcript of 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course
![Page 1: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/1.jpg)
OpenGL & GLUTMouse Interaction &
Camera ControlMouse events, Advanced camera control
George Georgiev
http://academy.telerik.com
Technical Trainer
academy.telerik.com
/.../3d-game-developme
nt-opengl
George AtanasovFront-End Developer
![Page 2: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/2.jpg)
Table of Contents
GLUT Mouse Interaction Mouse up/down events
Passive motion
Active motion
OpenGL Camera Default transformation
“Manual” control
Custom camera class
2
![Page 3: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/3.jpg)
GLUT Mouse Interaction
Mouse event callbacks
![Page 4: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/4.jpg)
GLUT Mouse Interaction
4
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
![Page 5: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/5.jpg)
GLUT Mouse Interaction
5
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)
![Page 6: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/6.jpg)
GLUT Mouse Interaction
6
Detecting mouse buttons (2) Callback parameter values
button
GLUT_LEFT_BUTTON
GLUT_MIDDLE_BUTTON
GLUT_RIGHT_BUTTON
state
GLUT_DOWN
GLUT_UP
![Page 7: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/7.jpg)
GLUT Mouse Interaction
7
Detecting mouse buttons (2) Callback parameter values
button
GLUT_LEFT_BUTTON
GLUT_MIDDLE_BUTTON
GLUT_RIGHT_BUTTON
state
GLUT_DOWN
GLUT_UP
![Page 8: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/8.jpg)
GLUT Mouse Interaction
8
Detecting movement Types
Passive
Mouse moves
No buttons pressed
Active
Mouse moves
One or more buttons pressed
![Page 9: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/9.jpg)
GLUT Mouse Interaction
9
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)
![Page 10: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/10.jpg)
GLUT Mouse Interaction
10
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)
![Page 11: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/11.jpg)
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_LEFT11
![Page 12: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/12.jpg)
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
![Page 13: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/13.jpg)
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
![Page 14: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/14.jpg)
Custom mouse classLive Demo
![Page 15: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/15.jpg)
форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно
програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop
уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
безплатен курс "Разработка на софтуер в cloud среда"
BG Coder - онлайн състезателна система - online judge
курсове и уроци по програмиране, книги – безплатно от Наков
безплатен курс "Качествен програмен код"
алго академия – състезателно програмиране, състезания
ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия
курс мобилни приложения с iPhone, Android, WP7, PhoneGap
free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно
?
? ? ??
?? ?
?
?
?
??
?
?
? ?
Questions?
?
Mouse Interaction
http://academy.telerik.com/
![Page 16: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/16.jpg)
OpenGL Camera ControlBasic and Advanced camera positioning
![Page 17: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/17.jpg)
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
![Page 18: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/18.jpg)
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
![Page 19: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/19.jpg)
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 same19
![Page 20: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/20.jpg)
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
![Page 21: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/21.jpg)
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
![Page 22: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/22.jpg)
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
![Page 23: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/23.jpg)
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
![Page 24: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/24.jpg)
OpenGL Camera control Looking away (3) – explanation
glRotatef positive rotations Positive rotation round Y
24
![Page 25: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/25.jpg)
OpenGL Camera control Looking away (4) – explanation
glRotatef positive rotations Positive rotation round X
25
![Page 26: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/26.jpg)
OpenGL Camera control Looking away (5) – explanation
glRotatef positive rotations Positive rotation round Z
26
![Page 27: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/27.jpg)
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
![Page 28: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/28.jpg)
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
![Page 29: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/29.jpg)
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
![Page 30: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/30.jpg)
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
![Page 31: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/31.jpg)
Custom camera classLive Demo
![Page 32: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/32.jpg)
форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно
програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop
уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
безплатен курс "Разработка на софтуер в cloud среда"
BG Coder - онлайн състезателна система - online judge
курсове и уроци по програмиране, книги – безплатно от Наков
безплатен курс "Качествен програмен код"
алго академия – състезателно програмиране, състезания
ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия
курс мобилни приложения с iPhone, Android, WP7, PhoneGap
free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно
?
? ? ??
?? ?
?
?
?
??
?
?
? ?
Questions?
?
Mouse Interaction & Camera control
http://academy.telerik.com/
![Page 33: 10. Open GL & Glut Mouse & Camera - 3D Graphics and Game Development Course](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b502554a79592d4c8b456b/html5/thumbnails/33.jpg)
Free Trainings @ Telerik Academy
3D Graphics and Game Development academy.telerik.com/.../3d-game-de
velopment-opengl
Telerik Software Academy academy.telerik.com
Telerik Academy @ Facebook facebook.com/TelerikAcademy
Telerik Software Academy Forums forums.academy.telerik.com