The openGL roaming function is simple, and the opengl roaming function
Recently, when I got the openGL roaming function, I found a lot of source code on the Internet and did not achieve the expected results. Then I wrote an algorithm and shared it.
The up and down keys realize shift, and the left and right keys rotate.
Algorithm idea: the prototype of the observation point function is changed:
Void gluLookAt (GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz );
This function defines a view matrix and multiply it with the current matrix. First sets of eyex, eyey, and eyez cameras in the world coordinates. Second sets of centerx, centery, and centerz cameras in the world coordinates. Third sets of upx, upy, the upz camera's upward direction is in the coordinates of the world. You think of the camera as your own head: the first set of data is the head position. The second set of data is the head position. The third set is the head orientation)
So the role of R is to calculate the desired position of the eye after the rotation with the angle s_angle.
# Define PI 3.141592653
# Define R 100
Static GLfloat s_eye [] = {0, 0.0, 1}; // position of the observation point
Static GLfloat s_at [] = {0.0, 0.0, 0.0}; // observation direction of the observation point
Static GLfloat s_angle = 0.0; // Rotation Angle
Tatic float rad = 0; // calculates the arc value based on the Rotation Angle
Float speed = 0.1; // speed of movement
.........
S_at [0] = float (s_eye [0] + R * sin (rad); // calculates the x coordinate of the position of the object to be viewed by the eye Based on the Rotation Angle
S_at [2] = float (s_eye [2]-100 * cos (rad); // calculates the y coordinate of the position of the object to be viewed by the eye Based on the Rotation Angle
S_at [1] = s_eye [1]; // The zcoordinate remains unchanged.
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 );
..............
If (g_keys-> keyDown [VK_UP])
{
Rad = float (PI * s_angle/180.0f );
S_eye [2]-= (float) cos (rad) * speed;
S_eye [0] + = (float) sin (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 (g_keys-> keyDown [VK_DOWN])
{
Rad = float (PI * s_angle/180.0f );
S_eye [2] + = (float) cos (rad) * speed;
S_eye [0]-= (float) sin (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.
}
If (g_keys-> keyDown [VK_LEFT])
{
S_angle-= 0.1;
Rad = float (PI * s_angle/180.0f); // rotate 0.1 degrees each time and calculate the corresponding radian
}
If (g_keys-> keyDown [VK_RIGHT])
{
S_angle + = 0.1;
Rad = float (PI * s_angle/180.0f );
}