Use glulookat in OpenGL for roaming

Source: Internet
Author: User

Http://blog.csdn.net/zywuying/archive/2009/04/03/4045906.aspx

Http://www.cnblogs.com/songjie2010/archive/2011/01/05/1926086.html

 

In general, there are two ways to implement a first-person perspective game: Mobile scene and mobile eye coordinate. the mobile scenario method is relatively simple. You can use gltranslatef and glrotatef in combination, but it is generally only used in simple scenarios and single roles, and various role calculations (such as real-time coordinates and collisions) it is not easy to implement, so it is not recommended to use it. The method of moving eye coordinates is very flexible, and it does not perform any operations on the scene and role status, generally, you only need to set it to follow the main role to move and rotate to achieve the first-person visual effect.
Void airroam (void)
{
// Track the direction of the observation point
Static glfloat s_eye [] = {0, 8.0, 0 };
Static glfloat s_at [] = {0.0, 0.0, 0.0 };
Static glfloat s_angle =-90.0; // if the initial angle is set to 0, it is initially oriented to the positive direction of the X axis and is set to-90.
// The negative orientation of the Z axis, which meets the default design habits.
Float Pi = 3.14159f;
// Rotate the request
If (key_down (dik_left ))
{
S_angle-= 2.0; // each time you press the left button, the rotation is 2 degrees.
}
If (key_down (dik_right ))
{
S_angle + = 2.0; // right-click each time and rotate for 2 degrees.
}
Float rad = float (pI * s_angle/180.0f); // calculate parameters required by sin and cos functions.
// Forward and backward requests
If (key_down (dik_up ))
{
S_eye [2] + = (float) sin (RAD) * speed;
S_eye [0] + = (float) Cos (RAD) * speed;
// If you press the up arrow key and move forward along the direction after the conversion angle, speed is the step of every forward, and the sin and cos functions are used to achieve
// Advance with an angle.
}
If (key_down (dik_down ))
{
S_eye [2]-= (float) sin (RAD) * speed;
S_eye [0]-= (float) Cos (RAD) * speed;
// If you press the direction key and move backward along the direction after the conversion angle, speed is the step forward each time. The sin and cos functions are used to implement
// Advance with an angle.
}
// Observation point
S_at [0] = float (s_eye [0] + 100 * Cos (RAD ));
S_at [2] = float (s_eye [2] + 100 * sin (RAD ));
S_at [1] = s_eye [1];
// The observation point can be further set. If the value is smaller, you may not be able to see objects that are far away. It is also implemented through the sin and cos functions.
// Set the observation point in the forward direction.
// Set the observation point
Glloadidentity ();
Glulookat (s_eye [0], s_eye [1], s_eye [2],
S_at [0], s_at [1], s_at [2],
0.0, 1.0, 0.0 );
}

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.