3D computer Grapihcs Using OpenGL-18 camera move

Source: Internet
Author: User

Keyboard keys are needed to move the camera, and the introduction of key events requires a header file

#include <Qt3DInput\qkeyevent.h>

and implement the virtual function defined in Qwidget keypressevent

We first rewrite this virtual function in Myglwindow.

Join in MyGlWindow.h

    void keypressevent (qkeyevent*);

Defined in MyGlWindow.cpp:

1 voidMyglwindow::keypressevent (Qkeyevent *e)2 {3     Switch(e->key ())4     {5      CaseQt::key::key_w:6 Camera.moveforward ();7          Break;8      Caseqt::key::key_s:9 Camera.movebackward ();Ten          Break; One      Caseqt::key::key_a: A camera.strafeleft (); -          Break; -      CaseQt::key::key_d: the camera.straferight (); -          Break; -      Caseqt::key::key_q: - camera.moveup (); +          Break; -      Caseqt::key::key_e: + Camera.movedown (); A          Break; at  -     default: -          Break; -     } - repaint (); -}

The code is simple, from which we can see that pressing the W and S keys will move forward and backward, and pressing the A and D keys will translate left to right, and pressing the Q and E keys will be shifted up and down respectively.

But for now we haven't implemented these 6 moving functions in the camera class.

Here's how to implement these functions:

To add a member in CAMERA.H:

 1  void   Moveforward ();  2  void   Movebackward ();  3  void   Strafeleft ();  4  void   Straferight ();  5  void   MoveUp ();  6  void   MoveDown ();  7  8  float  movespeed = 0.1f ; 

These methods are defined in Camera.cpp:

1 voidCamera::moveforward ()2 {3Position + = Viewdirection *Movespeed;4 }5 6 voidCamera::movebackward ()7 {8Position-= Viewdirection *Movespeed;9 }Ten  One voidCamera::strafeleft () A { -GLM::VEC3 Pitchaxis =Glm::cross (viewdirection, up); -Position + = Pitchaxis *Movespeed; the } -  - voidcamera::straferight () - { +GLM::VEC3 Pitchaxis =Glm::cross (viewdirection, up); -Position-= Pitchaxis *Movespeed; +  A } at  - voidcamera::moveup () - { -Position + = up *Movespeed; - } -  in voidCamera::movedown () - { toPosition-= up *Movespeed; +}

After compiling the run, we found that the camera can move forward and backward to pan up or down.

Ps:

This is not up and down the camera's local coordinates, but the world coordinates up and down. It's a little cumbersome to change to local coordinates.

I made some attempts to define a new member, CAMERAUP, to represent the top of the camera, re-use the vector spread after each rotation to recalculate the cameraup. This really can be moved up and down the camera, but brings some other problems, the camera in the world is no longer "positive", after several rotations, will accumulate some deviations, causing the camera tilt. It may take four of dollars to solve the problem here, but it's not going to go into it for the moment.

3D computer Grapihcs Using OpenGL-18 camera move

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.