Reputation: 109
Basically what I'm trying to do is rotate a camera around an object in the center when I hold down "c" and use the arrow keys.
My first question doesn't have much to do with the camera, but with having a key callback recognize 2 keys at the same time.
My function DOES work if I have separate if statements for L/R keys and no "c", but I can't get it to work when I only want the camera to rotate when I'm holding down "c". I have tried using switch(key) and if statements within if statements. Both implementations I've tried are in the code below:
float R = 0.0;
float U = 0.0;
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(key == GLFW_KEY_C && action == GLFW_PRESS) {
switch(key) {
case GLFW_KEY_RIGHT:
R+=0.05;
camera(R, U);
break;
case GLFW_KEY_LEFT:
R-=0.05;
camera(R, U);
break;
case GLFW_KEY_UP:
break;
case GLFW_KEY_DOWN:
break;
default:
break;
}
}
//OR --
if(key == GLFW_KEY_C && action == GLFW_PRESS) {
if(key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
R+=0.05;
camera(R, U);
}
}
}
What am I doing wrong? Is there something else I can try?
My second question has more to do with the camera. It rotates fine for a half circle around the object with my current code, but then once it reaches a certain point, the camera just pans farther and farther away from the object, rather than rotating. This is the code for my camera function:
GLfloat ox = 10.0;
GLfloat oy = 10.0;
static void camera(float RL, float UD) {
ox+=cos(glm::radians(RL));
oy+=sin(glm::radians(RL));
gViewMatrix = glm::lookAt(glm::vec3(ox, oy, 10.0f), // eye
glm::vec3(0.0, 0.0, 0.0), // center
glm::vec3(0.0, 1.0, 0.0));
}
Upvotes: 0
Views: 512
Reputation: 2065
Your 'C' detecting code won't work because you're only matching when key equals C and an arrow. It can't be both. You'll have to keep another global variable, isCPressed. When C is pressed you set it to true, when C is released you set it to false, and then when an arrow is pressed you check if(isCPressed).
as for the camera code, your algorithm orbits the point 0,0,10 while looking at 0,0,0. That doesn't seem to be what you want, you should have something like oxdist, oydist, 0 for the eye position to orbit 0,0,0 at a distance of 'dist'
Upvotes: 1