我对GLFW和拖动鼠标有问题。该程序无法获得Linux上鼠标的位移。我需要你的帮助,因为在windows上它是有效的。这是软件问题吗?
代码如下:
#include <Seed/Graphics/engine/engine.hpp>
Engine::Engine()
{
initLog();
}
Engine::~Engine()
{
glfwTerminate();
TwTerminate();
}
void Engine::mainRender(std::shared_ptr<Scene> scene)
{
double currentTime = 0, lastTime = 0;
float deltaTime = 0;
glfwSetCursorPos(window, WIDTH / 2, HEIGHT / 2);
std::stack<ObjectNode*> n;
std::vector<ObjectNode*> nodes;
n.push(scene->getRootObjectNode());
while (!n.empty())
{
ObjectNode *n2 = n.top();
n.pop();
for (int i = 0; i < n2->getChildren()->size(); i++)
{
n.push(n2->getChildren()->at(i));
}
if (n2->getModel())
{
nodes.push_back(n2);
}
}
do
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glfwPollEvents();
currentTime = glfwGetTime();
this->controller->updateControl(this->window, scene->getCamera(), deltaTime);
scene->render(nodes);
TwDraw();
lastTime = glfwGetTime();
deltaTime = float(lastTime - currentTime);
glfwSwapBuffers(this->window);
} while (glfwGetKey(this->window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(this->window) == 0);
}
bool Engine::initSystem()
{
if (!glfwInit())
{
std::cout << "Failed to initialize GLFW" << std::endl;
return false;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(WIDTH, HEIGHT, "Moteur3d", NULL, NULL);
if (window == NULL){
std::cout << "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" << std::endl;
glfwTerminate();
return false;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW, version of opengl must be greater or equal than opengl 3.2\n" << std::endl;
return false;
}
return true;
}
bool Engine::initController()
{
this->controller = new Controller(this->window);
return true;
}
void TW_CALL CallbackButtonReset(void *clientData)
{
SPH::reset = true;
}
void TW_CALL CallbackButtonNextFrame(void *clientData)
{
SPH::nextFrame = true;
}
void TW_CALL CallbackButtonPlay(void *clientData)
{
SPH::play = true;
}
void TW_CALL CallbackButtonPause(void *clientData)
{
SPH::play = false;
}
控制器:
int Controller::context = 0;
Controller::Controller(GLFWwindow *window)
{
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glfwSetMouseButtonCallback(window, mouse_buttonID_callback);
this->context = 0;
glfwSetCursorPos(window, WIDTH / 2, HEIGHT / 2);
}
Controller::~Controller()
{
}
void Controller::updateControl(GLFWwindow* window, Camera *cam, float deltaTime)
{
glm::vec3 position = cam->getPosition();
float WAngle = cam->getWAngle();
float HAngle = cam->getHAngle();
const float initFoV = cam->getInitFoV();
float near = cam->getNear();
float far = cam->getFar();
float speed = cam->getSpeed();
float mouseSpeed = cam->getMouseSpeed();
float FoV = cam->getInitFoV();
glm::vec3 direction;
glm::vec3 up;
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
if (Controller::context == 0)
{
glfwSetCursorPos(window, WIDTH / 2, HEIGHT / 2);
WAngle += mouseSpeed * float(WIDTH / 2 - xpos);
HAngle += mouseSpeed * float(HEIGHT / 2 - ypos);
direction = glm::vec3(cos(HAngle) * sin(WAngle), sin(HAngle), cos(HAngle) * cos(WAngle));
glm::vec3 right(sin(WAngle - 3.14f / 2.0f), 0, cos(WAngle - 3.14f / 2.0f));
up = glm::normalize(glm::cross(right, direction));
if (glfwGetKey(window, GLFW_KEY_KP_3) == GLFW_PRESS)
{
position += direction * speed;
}
else if (glfwGetKey(window, GLFW_KEY_KP_1) == GLFW_PRESS)
{
position -= direction * speed;
}
else if (glfwGetKey(window, GLFW_KEY_KP_6) == GLFW_PRESS)
{
position += right * speed;
}
else if (glfwGetKey(window, GLFW_KEY_KP_4) == GLFW_PRESS)
{
position -= right * speed;
}
else if (glfwGetKey(window, GLFW_KEY_KP_8) == GLFW_PRESS)
{
position += up * speed;
}
else if (glfwGetKey(window, GLFW_KEY_KP_2) == GLFW_PRESS)
{
position -= up * speed;
}
cam->setHAngle(HAngle);
cam->setWAngle(WAngle);
cam->setViewMatrix(position, direction, up);
cam->setProjectionMatrix(FoV, WIDTH, HEIGHT, near, far);
}
else
{
TwMouseMotion(xpos, ypos);
if (glfwGetKey(window, GLFW_KEY_KP_0) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_0, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_1) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_1, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_2) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_2, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_3) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_3, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_4) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_4, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_5) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_5, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_6) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_6, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_7) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_7, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_8) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_8, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_9) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_9, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_KP_ENTER) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_KP_ENTER, GLFW_PRESS);
}
else if (glfwGetKey(window, GLFW_KEY_DELETE) == GLFW_PRESS)
{
TwEventKeyGLFW(GLFW_KEY_DELETE, GLFW_PRESS);
}
}
}
void mouse_buttonID_callback(GLFWwindow* window, int button, int action, int mods)
{
if (action == GLFW_PRESS)
{
switch (button)
{
case GLFW_MOUSE_BUTTON_LEFT:
TwMouseButton(TW_MOUSE_PRESSED, TW_MOUSE_LEFT);
break;
case GLFW_MOUSE_BUTTON_RIGHT:
Controller::context += 1;
Controller::context %= 2;
TwMouseButton(TW_MOUSE_PRESSED, TW_MOUSE_RIGHT);
break;
}
}
else if (action == GLFW_RELEASE)
{
switch (button)
{
case GLFW_MOUSE_BUTTON_LEFT:
TwMouseButton(TW_MOUSE_RELEASED, TW_MOUSE_LEFT);
break;
case GLFW_MOUSE_BUTTON_RIGHT:
TwMouseButton(TW_MOUSE_RELEASED, TW_MOUSE_RIGHT);
break;
}
}
}
void Controller::initAntWeakBar(std::string name)
{
TwInit(TW_OPENGL_CORE, NULL);
TwWindowSize(WIDTH, HEIGHT);
this->bar = TwNewBar(name.c_str());
}
TwBar* Controller::getBar()
{
return this->bar;
}
你有解决方案吗?