-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcamera.cpp
More file actions
181 lines (163 loc) · 3.13 KB
/
Copy pathcamera.cpp
File metadata and controls
181 lines (163 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// camera.cpp
// OpenGLProgramming
//
// Created by Lokesh Basu on 08/02/15.
// Copyright (c) 2015 InHouse. All rights reserved.
//
#include "camera.h"
void camera::lockCamera()
{
if(camPitch>90)
camPitch=90;
if(camPitch<-90)
camPitch=-90;
if(camYaw<0.0)
camYaw+=360.0;
if(camYaw>360.0)
camYaw-=360;
}
void camera::moveCamera(float dir)
{
float rad=(camYaw+dir)*M_PI/180.0;
loc.x-=sin(rad)*movevel;
loc.z-=cos(rad)*movevel;
}
void camera::moveCameraUp(float dir)
{
float rad=(camPitch+dir)*M_PI/180.0;
loc.y+=sin(rad)*movevel;
}
camera::camera()
{
camPitch=0;
camYaw=0;
movevel=0.2;
mousevel=0.2;
mi=ismoved=false;
}
camera::camera(vector3d l)
{
loc.change(l);
camPitch=0;
camYaw=0;
movevel=0.2;
mousevel=0.2;
mi=ismoved=false;
}
camera::camera(vector3d l,float yaw,float pitch)
{
loc.change(l);
camPitch=pitch;
camYaw=yaw;
movevel=0.2;
mousevel=0.2;
mi=ismoved=false;
}
camera::camera(vector3d l,float yaw,float pitch,float mv,float mov)
{
loc.change(l);
camPitch=pitch;
camYaw=yaw;
movevel=mv;
mousevel=mov;
mi=false;
}
void camera::Control()
{
if(mi)
{
int MidX=320;
int MidY=240;
SDL_ShowCursor(SDL_DISABLE);
int tmpx,tmpy;
SDL_GetMouseState(&tmpx,&tmpy);
camYaw+=mousevel*(MidX-tmpx);
camPitch+=mousevel*(MidY-tmpy);
lockCamera();
SDL_WarpMouse(MidX,MidY);
Uint8* state=SDL_GetKeyState(NULL);
ismoved=false;
if(state[SDLK_w])
{
ismoved=true;
if(camPitch!=90 && camPitch!=-90)
moveCamera(0.0);
moveCameraUp(0.0);
}else if(state[SDLK_s])
{
ismoved=true;
if(camPitch!=90 && camPitch!=-90)
moveCamera(180.0);
moveCameraUp(180.0);
}
if(state[SDLK_a])
{
ismoved=true;
moveCamera(90.0);
}
else if(state[SDLK_d])
{
ismoved=true;
moveCamera(270);
}
}
glRotatef(-camPitch,1.0,0.0,0.0);
glRotatef(-camYaw,0.0,1.0,0.0);
}
void camera::UpdateCamera()
{
glTranslatef(-loc.x,-loc.y,-loc.z);
}
//change the spherical coordinate system to cartesian
vector3d camera::getVector()
{
//Google->spherical to cartesian
return (vector3d(-cos(camPitch*M_PI/180.0)*sin(camYaw*M_PI/180.0),sin(camPitch*M_PI/180.0),-cos(camPitch*M_PI/180.0)*cos(camYaw*M_PI/180.0)));
}
vector3d camera::getLocation()
{
return loc;
}
float camera::getPitch()
{
return camPitch;
}
float camera::getYaw()
{
return camYaw;
}
float camera::getMovevel()
{
return movevel;
}
float camera::getMousevel()
{
return mousevel;
}
bool camera::isMouseIn()
{
return mi;
}
void camera::setLocation(vector3d vec)
{
loc.change(vec);
}
void camera::lookAt(float pitch,float yaw)
{
camPitch=pitch;
camYaw=yaw;
}
void camera::mouseIn(bool b)
{
mi=b;
}
void camera::setSpeed(float mv,float mov)
{
movevel=mv;
mousevel=mov;
}
bool camera::isMoved()
{
return ismoved;
}