/******************************************************************************** Get mouse updates ********************************************************************************/ bool Application::GetMouseUpdate() { glfwGetCursorPos(m_window, &mouse_current_x, &mouse_current_y); mouse_current_y = m_window_height - mouse_current_y; if (MouseWithinScreen) { // Calculate the difference in positions mouse_diff_x = mouse_current_x - mouse_last_x; mouse_diff_y = mouse_current_y - mouse_last_y; //Calculate the yaw and pitch camera_yaw = (float)mouse_diff_x * 0.0174555555555556f;// * 3.142f / 180.0f; camera_pitch = mouse_diff_y * 0.0174555555555556f;// 3.142f / 180.0f ); // Do a wraparound if the mouse cursor has gone out of the deadzone if ((mouse_current_x < m_window_deadzone) || (mouse_current_x > m_window_width - m_window_deadzone)) { mouse_current_x = m_window_width >> 1; glfwSetCursorPos(m_window, mouse_current_x, mouse_current_y); } if ((mouse_current_y < m_window_deadzone) || (mouse_current_y > m_window_height - m_window_deadzone)) { mouse_current_y = m_window_height >> 1; glfwSetCursorPos(m_window, mouse_current_x, mouse_current_y); }
// update mouse pos and button status void Mouse::Update() { for (int i = LEFT_BUTTON; i < MAX_BUTTON; ++i) { mouseButtonStatus[i] = false; } glfwGetCursorPos(m_window, ¤tPosX, ¤tPosY); diffPos.x = (float)currentPosX - lastPos.x; diffPos.y = (float)currentPosY - lastPos.y; mouseYaw = (float)(diffPos.x * 0.0174555555555556f * sensitivity); // 3.142f / 180.0f ); mousePitch = (float)(diffPos.y * 0.0174555555555556f * sensitivity); // 3.142f / 180.0f ); // Do a wraparound if the mouse cursor has gone out of the deadzone if (deadzone) { if ((currentPosX < deadZoneDimension.x) || (currentPosX > Application::getWindowWidth() - deadZoneDimension.x)) { currentPosX = Application::getWindowWidth() >> 1; glfwSetCursorPos(m_window, currentPosX, currentPosY); } if ((currentPosY < deadZoneDimension.y) || (currentPosY > Application::getWindowHeight() - deadZoneDimension.y)) { currentPosY = Application::getWindowHeight() >> 1; glfwSetCursorPos(m_window, currentPosX, currentPosY); }
void Camera::onMousePos(GLFWwindow * window, int x, int y ) { if(rmb_state == RELEASE) lastPos = glm::ivec2(x, y); if(rmb_state == PRESS) { glfwSetCursorPos(window, m_windowWidth/2, m_windowHeight/2); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); m_AngleH += mouseSpeed * deltaTime * static_cast<float>((m_windowWidth/2 - x)); m_AngleV += mouseSpeed * deltaTime * static_cast<float>((m_windowHeight/2 - y)); if(m_AngleV > 90.0f) m_AngleV = 90.0f; if(m_AngleV < -90.0f) m_AngleV = -90.0f; glm::vec3 direction(cos(m_AngleV) * sin(m_AngleH), sin(m_AngleV), cos(m_AngleV) * cos(m_AngleH)); m_target = direction; glm::vec3 right(sin(m_AngleH - 3.14f/2.0f), 0, cos(m_AngleH - 3.14f/2.0f)); m_up = glm::cross(right, direction); updatePos = false; } if(rmb_state == RELEASE && updatePos == false) { updatePos = true; glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetCursorPos(window, lastPos.x, lastPos.y); //glfwEnable(GLFW_MOUSE_CURSOR); deprecated functions which need refactoring //glfwSetMousePos(lastPos.x, lastPos.y); } Update(); }
void updateState(GLFWwindow* window, Grid* fluid, Vector3* camerapos,Vector3* camera_z,Vector3* camera_y, Vector3* lightPos){ static double lastTime = glfwGetTime(); double currentTime = glfwGetTime(); glfwPollEvents(); double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); xpos -= 1366/2; ypos -= 768/2; xpos/=100; ypos/=100; Vector3 centre(0,0,2),current(xpos,ypos,sqrt(4-xpos*xpos-ypos*ypos)); Matrix4 rotMat; int state = glfwGetKey(window, GLFW_KEY_LEFT_CONTROL ); if((centre*current).mod()>0.005 ){ Vector3 rotation_Goal=centre*current*300; Matrix4 basisTransform(-*camera_z,*camera_y,Matrix4::BASIS); rotation_Goal=(basisTransform*Vector4(rotation_Goal,0)).xyz(); rotMat = Matrix4(Vector3(50,50,50),Matrix4::TRANSLATE)* Matrix4(rotation_Goal,Vector3(1.25,3.4,5.4),Matrix4::BASIS)* Matrix4(rotation_Goal ,Matrix4::ROTATION_Z)* Matrix4(rotation_Goal,Vector3(1.25,3.4,5.4))* Matrix4(Vector3(-50,-50,-50),Matrix4::TRANSLATE); fluid->Model = rotMat*fluid->Model; } glfwSetCursorPos(window, 1366/2, 768/2); }
void GameLoop::inputHandler() { float dist = starField->getDist(cam->getPosition()); float speed = 1000.0f; float time = glfwGetTime(); float elapsed_time = time - lastTime; lastTime = time; /* Poll for and process events */ glfwPollEvents(); if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) cam->moveFoward(-speed * elapsed_time); if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) cam->moveFoward(speed * elapsed_time); if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) cam->moveHorizontal(-speed * elapsed_time); if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) cam->moveHorizontal(speed * elapsed_time); double x,y; float rotSpeed = 0.05f; glfwGetCursorPos(window, &x, &y); cam->yaw(rotSpeed * (WINDOW_WIDTH/2. - x) * elapsed_time); cam->pitch(rotSpeed * (WINDOW_HEIGH/2. - y) * elapsed_time); glfwSetCursorPos(window, WINDOW_WIDTH/2., WINDOW_HEIGH/2.); }
void move() { int width, height = 0; glfwGetCursorPos(window, &xpos, &ypos); glfwGetWindowSize (window, &width, &height); glfwSetCursorPos(window, width/2, height/2); // Compute new orientation horizontalAngle += mouseSpeed * float( width/2 - xpos ); verticalAngle += mouseSpeed * float( height/2 - ypos ); // Direction : Spherical coordinates to Cartesian coordinates conversion direction = glm::vec3( cos(verticalAngle) * sin(horizontalAngle), sin(verticalAngle), cos(verticalAngle) * cos(horizontalAngle) ); // Right vector right = glm::vec3( sin(horizontalAngle - 3.14f/2.0f), tan(depthAngle), cos(horizontalAngle - 3.14f/2.0f) ); // Up vector up = glm::cross( right, direction ); keyboard(); mouse(); }
std::pair<double, double> Window::checkMouse() { double x_pos, y_pos; glfwGetCursorPos(_window, &x_pos, &y_pos); glfwSetCursorPos(_window, 1024 / 2, 768 / 2); return std::make_pair(x_pos, y_pos); }
void Window::create() { if (!glfwInit()) throw std::string("Failed to initialize GLFW\n"); glfwWindowHint(GLFW_SAMPLES, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 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, _title.c_str(), NULL, NULL); if (_window == NULL) { glfwTerminate(); throw std::string("Failed to open GLFW window\n"); } glfwMakeContextCurrent(_window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) throw std::string("Failed to initialize GLEW\n"); glfwSetInputMode(_window, GLFW_STICKY_KEYS, GL_TRUE); glfwSetCursorPos(_window, 1024 / 2, 768 / 2); glfwSetInputMode(_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); }
// -------------------------------------------------------------------------------------------------------------------- void window::cursor(double x, double y) { if (is_open()) { glfwSetCursorPos(_internal->_window, x, y); } }
Camera::Camera( glm::vec3& position, glm::vec3& target, glm::vec3& up, GLfloat near, GLfloat far, GLfloat fieldOfView, unsigned int windowWidth, unsigned int windowHeight, const char* cameraName) : m_vCameraPosition(position), m_vCameraTarget(target), m_vCameraUp(up), m_fNearClippingPlane(near), m_fFarClippingPlane(far), m_fFOV(fieldOfView) { m_fHorizontalAngle = 0.0f; m_fVerticalAngle = 0.0f; CameraName = new char[strlen(cameraName) + 1]; strcpy_s(CameraName, strlen(cameraName) + 1, cameraName); // Debug std::cout<<CameraName<<std::endl; SetView(); SetProjection(m_fFOV, WINDOW_WIDTH, WINDOW_HEIGHT, m_fNearClippingPlane, m_fFarClippingPlane); // Set mouse position to the center of the screen glfwSetCursorPos(Core::Window, (double)WINDOW_WIDTH / 2, (double)WINDOW_HEIGHT / 2); }
static void ImGui_ImplGlfw_UpdateMousePosAndButtons() { // Update buttons ImGuiIO& io = ImGui::GetIO(); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) { // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame. io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0; g_MouseJustPressed[i] = false; } // Update mouse position const ImVec2 mouse_pos_backup = io.MousePos; io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED)) { if (io.WantSetMousePos) { glfwSetCursorPos(g_Window, (double)mouse_pos_backup.x, (double)mouse_pos_backup.y); } else { double mouse_x, mouse_y; glfwGetCursorPos(g_Window, &mouse_x, &mouse_y); io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); } } }
void SimpleApp::SimpleFrameListener::WindwoFocusCB(GLFWwindow *window, int hasFocus) { if(hasFocus) { glfwSetCursorPos(window, app->w2, app->h2); } }
void vxl::OnFocus(int focused) { if (focused == GL_FALSE && !m_paused) { m_paused = !m_paused; glfwSetInputMode(m_window, GLFW_CURSOR, ((!m_paused) ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL)); // set the cursor to the middle of the screen glfwSetCursorPos(m_window, static_cast<double>(m_width) / 2.0, static_cast<double>(m_height) / 2.0); } }
void focus(GLFWwindow* window, int focused){ if (v.infocus = focused == GL_TRUE){ v.mouseWarped = true; glfwSetCursorPos(v.window, v.centerX, v.centerY); } else{ v.keyStates.clear(); } }
void updateControl(GLFWwindow *window) { glfwSetCursorPos(window, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); forwardVel += forwardAccel; forwardVel /= FRICTION; strafeVel += strafeAccel; strafeVel /= FRICTION; }
void ctx_cursor_pos(struct context *ctx, double x, double y) { if (ctx->hidpi) { x *= 2; y *= 2; } glfwSetCursorPos(ctx->win, x, ctx->winh - y); cursor_pos_callback(ctx->win, x, ctx->winh - y); }
void camera_t::orient(float dt) { static const glm::vec2 centre(window_width / 2, window_height / 2); static double xpos = 0, ypos = 0; glfwGetCursorPos(window, &xpos, &ypos); if (xpos > window_width || xpos < -window_width) xpos = window_width / 2; if (ypos > window_height || ypos < -window_height) ypos = window_height / 2; glm::vec2 crnt_pos(xpos, ypos); const glm::vec2 diff = (centre - crnt_pos); const float max_diff = 20.0f; const glm::vec2 ad = glm::abs(diff); const glm::vec2 diff_rate = glm::vec2(glm::pow(ad, glm::vec2(2))); if (crnt_pos.x > centre.x) crnt_pos.x -= diff_rate.x * dt; else if (crnt_pos.x < centre.x) crnt_pos.x += diff_rate.x * dt; if (crnt_pos.y > centre.y) crnt_pos.y -= diff_rate.y * dt; else if (crnt_pos.y < centre.y) crnt_pos.y += diff_rate.y * dt; crnt_pos = glm::clamp(crnt_pos, centre - glm::vec2(max_diff), centre + glm::vec2(max_diff)); glfwSetCursorPos(window, crnt_pos.x, crnt_pos.y); float a = diff.x; glm::vec2 b = (centre - crnt_pos); horizontal_ang += rotational_speed * dt * diff.x; if (horizontal_ang > M_PI * 2.0f) horizontal_ang = diff.x; vertical_ang += rotational_speed * dt * diff.y; if (vertical_ang > M_PI * 2.0f) vertical_ang = 0; this->dir = glm::vec3(cos(vertical_ang) * sin(horizontal_ang), sin(vertical_ang), cos(vertical_ang) * cos(horizontal_ang)); // right vector this->right = glm::vec3(sin(horizontal_ang - ((float)(M_PI) / 2.0f)), 0, cos(horizontal_ang - ((float)(M_PI) / 2.0f))); // up vector : perpendicular to both direction and right glm::vec3 up = glm::cross(this->right, this->dir); // form "the" camera matrix this->matrix = glm::lookAt(pos, pos + dir, up); }
JNIEXPORT void JNICALL Java_com_badlogic_jglfw_Glfw_glfwSetCursorPos(JNIEnv* env, jclass clazz, jlong window, jint x, jint y) { //@line:865 glfwSetCursorPos((GLFWwindow*)window, x, y); }
void Application::Init() { //Set the error callback glfwSetErrorCallback(error_callback); //Initialize GLFW if (!glfwInit()) { exit(EXIT_FAILURE); } //Set the GLFW window creation hints - these are optional glfwWindowHint(GLFW_SAMPLES, 4); //Request 4x antialiasing glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Request a specific OpenGL version glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Request a specific OpenGL version //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL //Create a window and create its OpenGL context m_window = glfwCreateWindow(1920, 1080, "Computer Graphics :D", NULL, NULL); glfwSetWindowSizeCallback(m_window, resize_callback); //If the window couldn't be created if (!m_window) { fprintf( stderr, "Failed to open GLFW window.\n" ); glfwTerminate(); exit(EXIT_FAILURE); } //This function makes the context of the specified window current on the calling thread. glfwMakeContextCurrent(m_window); //Sets the key callback //glfwSetKeyCallback(m_window, key_callback); glewExperimental = true; // Needed for core profile //Initialize GLEW GLenum err = glewInit(); //If GLEW hasn't initialized if (err != GLEW_OK) { fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); //return -1; } int sizeX = 0; int sizeY = 0; glfwGetWindowSize(m_window, &sizeX, &sizeY); glfwSetCursorPos(m_window, sizeX / 2, sizeY / 2); glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN ); }
int main() { srand(time(NULL)); aspect::GLWindow window(XRES, YRES); glfwMakeContextCurrent(window.get_window()); glfwSetKeyCallback(window.get_window(), key_callback); glfwSetInputMode(window.get_window(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetCursorPos(window.get_window(), XRES/2, YRES/2); gs.camera.set_position(glm::vec3(+0.0f, +10.0f, +0.0f)); double currentframe = glfwGetTime(); double lastframe = currentframe; aspect::GLProgram monkey_program("shaders/vshader-nm.glsl", "shaders/fshader-nm.glsl"); aspect::Mesh monkey_mesh("models/monkey.dae"); aspect::ModelAsset monkey_asset(&monkey_mesh, &monkey_program); aspect::ModelInstance monkey(&monkey_asset); monkey.offset_position(glm::vec3(40, 6, 60)); monkey.update(); gs.model_instances.push_back(&monkey); aspect::GLProgram chunk_program("shaders/vshader-tex-light.glsl", "shaders/fshader-tex-light.glsl"); for(int x = 0; x < 16; x++) { for(int y = 0; y < 1; y++) { for(int z = 0; z < 16; z++) { aspect::CubeChunk *chunk = new aspect::CubeChunk(&chunk_program, x*CCOUNTX, y*CCOUNTY, z*CCOUNTZ); chunk->update(); gs.cube_chunks.push_back(chunk); } } } glClearColor(0.20, 0.6, 0.7, 0.00); while(!glfwWindowShouldClose(window.get_window())) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for_each(begin(gs.model_instances), end(gs.model_instances), [](aspect::ModelInstance *instance) { instance->draw(gs.camera.matrix()); walk(instance); }); for_each(begin(gs.cube_chunks), end(gs.cube_chunks), [](aspect::CubeChunk *chunk) { chunk->draw(gs.camera.matrix()); }); handle_mouse(window.get_window()); update_fps_counter(window.get_window()); window.update_fps_counter(); glfwSwapBuffers(window.get_window()); glfwPollEvents(); currentframe = glfwGetTime(); gs.delta_time = currentframe - lastframe; lastframe = currentframe; } return 0; }
void Update(float secondsElapsed, GLFWwindow* window) { const GLfloat degreesPerSecond = 180.0f; //const GLfloat degreesPerSecond = 0.0f; gDegreesRotated += secondsElapsed * degreesPerSecond; while (gDegreesRotated > 360.0f) gDegreesRotated -= 360.0f; gInstances.front().transform = glm::rotate(glm::mat4(), gDegreesRotated, glm::vec3(0, 1, 0)); //move position of camera based on WASD keys const float moveSpeed = 4.0; //units per second if (glfwGetKey(window, 'S')) { gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.forward()); } else if (glfwGetKey(window, 'W')) { gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.forward()); } if (glfwGetKey(window, 'A')) { gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.right()); } else if (glfwGetKey(window, 'D')) { gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.right()); } if (glfwGetKey(window, 'Z')) { gCamera.offsetPosition(secondsElapsed * moveSpeed * -glm::vec3(0, 1, 0)); } else if (glfwGetKey(window, 'X')) { gCamera.offsetPosition(secondsElapsed * moveSpeed * glm::vec3(0, 1, 0)); } //move light if (glfwGetKey(window, '1')) gLight.position = gCamera.position(); // change light color if (glfwGetKey(window, '2')) gLight.intensities = glm::vec3(1, 0, 0); //red else if (glfwGetKey(window, '3')) gLight.intensities = glm::vec3(0, 1, 0); //green else if (glfwGetKey(window, '4')) gLight.intensities = glm::vec3(1, 1, 1); //white //rotate camera based on mouse movement const float mouseSensitivity = 0.1f; double mouseX, mouseY; glfwGetCursorPos(window, &mouseX, &mouseY); gCamera.offsetOrientation(mouseSensitivity * (float)mouseY, mouseSensitivity * (float)mouseX); glfwSetCursorPos(window, 0, 0); //reset the mouse, so it doesn't go out of the window const float zoomSensitivity = -0.2f; float fieldOfView = gCamera.fieldOfView() + zoomSensitivity * (float)gScrollY; if (fieldOfView < 5.0f) fieldOfView = 5.0f; if (fieldOfView > 130.0f) fieldOfView = 130.0f; gCamera.setFieldOfView(fieldOfView); gScrollY = 0; }
void FreeCamera::Update() { static auto enabled = false; auto &io = ImGui::GetIO(); // main render window auto &window = EngineBase::Instance()->Window(); if(io.KeyShift && ImGui::IsKeyReleased(GLFW_KEY_F)) { enabled = !enabled; } io.MouseDrawCursor = !enabled; // camera movement if (Camera::Active() && enabled) { auto &cam = Camera::Active(); auto &sceneExtent = Scene::Active()->rootNode->boundaries.Extent(); auto scaleSpeed = glm::max(sceneExtent.x, glm::max(sceneExtent.y, sceneExtent.z)) * 0.5f; auto cameraSpeed = scaleSpeed * io.DeltaTime; auto cameraRight = normalize(cross(cam->Forward(), cam->Up())); static auto yaw = 3.14f, pitch = 0.0f; if (io.KeysDown[GLFW_KEY_W]) { cam->Position(cam->Position() + cam->Forward() * cameraSpeed); } if (io.KeysDown[GLFW_KEY_S]) { cam->Position(cam->Position() - cam->Forward() * cameraSpeed); } if (io.KeysDown[GLFW_KEY_A]) { cam->Position(cam->Position() - cameraRight * cameraSpeed); } if (io.KeysDown[GLFW_KEY_D]) { cam->Position(cam->Position() + cameraRight * cameraSpeed); } auto hWidth = window.Info().displayWidth / 2; auto hHeight = window.Info().displayHeight / 2; glfwSetCursorPos(window.Handler(), hWidth, hHeight); // Compute new orientation float sensitivity = 0.1f; yaw += sensitivity * io.DeltaTime * float(hWidth - io.MousePos.x); pitch += sensitivity * io.DeltaTime * float(hHeight - io.MousePos.y); cam->Forward(glm::vec3(cos(pitch) * sin(yaw), sin(pitch), cos(pitch) * cos(yaw))); } }
// マウスのカーソル位置を指定 void AppEnv::mousePosition(const Vec2f& pos) { mouse_current_pos_ = pos; Vec2f window_pos = windowPosition(Vec2f(pos.x(), -pos.y()), current_window_size_, Vec2f(viewport_size_.x(), viewport_size_.y())); Vec2f mouse_pos = Vec2f(window_pos.x() + viewport_ofs_.x(), window_pos.y() + viewport_ofs_.y()); glfwSetCursorPos(window_(), mouse_pos.x(), mouse_pos.y()); }
RenderContext::RenderContext() { if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); exit(-1); } // Initialise GLFW glfwWindowHint(GLFW_SAMPLES, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); screen_width = 1024; screen_height = 768; aspect_ratio = float(screen_width) / float(screen_height); // Open a window and create its OpenGL context window = glfwCreateWindow(screen_width, screen_height, "mandlebroh", NULL, NULL); if (window == NULL) { fprintf(stderr, "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"); glfwTerminate(); exit(-1); } glfwMakeContextCurrent(window); // Initialize GLEW glewExperimental = true; // Needed for core profile if (glewInit() != GLEW_OK) { fprintf(stderr, "Failed to initialize GLEW\n"); exit(-1); } // Ensure we can capture the escape key being pressed below glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); glfwSetCursorPos(window, screen_width / 2, screen_height / 2); // setup the callbacks glfwSetFramebufferSizeCallback(window, framebuffer_cb); glfwSetScrollCallback(window, wheel_cb); glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); // init cursor pos glfwGetCursorPos(window, &pos_x, &pos_y); glfwGetCursorPos(window, &dragstart_x, &dragstart_y); glfwSwapInterval(1); mb.init(); ac.init(); text = TextGL("stuff", 20, glm::vec3(.75, .75, 1)); }
void ImGui_ImplGlfw_NewFrame() { if (!g_FontTexture) ImGui_ImplGlfw_CreateDeviceObjects(); ImGuiIO& io = ImGui::GetIO(); // Setup display size (every frame to accommodate for window resizing) int w, h; int display_w, display_h; glfwGetWindowSize(g_Window, &w, &h); glfwGetFramebufferSize(g_Window, &display_w, &display_h); io.DisplaySize = ImVec2((float)w, (float)h); io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0); // Setup time step double current_time = glfwGetTime(); io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); g_Time = current_time; // Setup inputs // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents()) if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED)) { if (io.WantMoveMouse) { glfwSetCursorPos(g_Window, (double)io.MousePos.x, (double)io.MousePos.y); // Set mouse position if requested by io.WantMoveMouse flag (used when io.NavMovesTrue is enabled by user and using directional navigation) } else { double mouse_x, mouse_y; glfwGetCursorPos(g_Window, &mouse_x, &mouse_y); io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Get mouse position in screen coordinates (set to -1,-1 if no mouse / on another screen, etc.) } } else { io.MousePos = ImVec2(-1,-1); } for (int i = 0; i < 3; i++) { io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame. g_MousePressed[i] = false; } io.MouseWheel = g_MouseWheel; g_MouseWheel = 0.0f; // Hide OS mouse cursor if ImGui is drawing it glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL); // Start the frame ImGui::NewFrame(); }
void ItemCamera::onMouseMotion(double xpos, double ypos) { // Start of user code onMouseMotion if(oldX == -1 && oldY == -1) { oldX = 960; oldY = 540; } //if(oldX >= 0 && oldY >= 0) else { double dx = (xpos-oldX)/100; double dy = (ypos-oldY)/100; angleZ += dx; angleY += dy; if(dy<0) angleY = max(PI/2.0,(double)angleY); else angleY = min(3*PI/2.0,(double)angleY); std::cout << angleY << std::endl; if(abs(oldX-960) > 100) { glfwSetCursorPos(Engine::getInstance()->getWindow(), 960, ypos); oldX = 960; } else oldX = xpos; if(abs(oldY-540) > 100) { glfwSetCursorPos(Engine::getInstance()->getWindow(), xpos, 540); oldY = 540; } else oldY = ypos; handle(events[0]); } // End of user code }
void WindowContext::recieveMsg(Message* m) { switch (m->type) { case Message::MsgType::SET_MOUSE: { auto setMouseMsg = dynamic_cast<SetMouseMessage*>(m); glfwSetCursorPos(window, setMouseMsg->x, setMouseMsg->y); } } }
void State::Gameplay::onLoad() { changeTo(this); glEnable(GL_BLEND); glEnable(GL_CULL_FACE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthFunc(GL_LEQUAL); glfwSetCursorPos(Game::get().getWindow().getHandle(), 400.0, 300.0); glfwSetKeyCallback(Game::get().getWindow().getHandle(), handleKeyboard); glfwSetScrollCallback(Game::get().getWindow().getHandle(), handleMouseWheel); glfwSetCursorPosCallback(Game::get().getWindow().getHandle(), handleMouseMovement); Game::get().getWindow().getContext().setClearColor(glm::vec4(0.1f, 0.1f, 0.1f, 1.0f)); _pipeline.setProjection(60.0f, 4.0f/3.0f, 0.1f, 10 * 1000.0f); _map.init(); _viewWireframe2D.init(); _viewWireframe3D.init(); _viewSector2D.init(_map.getSectors().front()); _viewSector3D.init(_map.getSectors().front()); Game::get().getWindow().setCountingFPS(true); Game::get().getWindow().setFPSRefreshRate(0.05); Game::get().getWindow().setFPSCountCallback([&, this](int fps) { std::string newTitle = std::string(" | FPS: ") + std::to_string(fps); if(this->isMode2D() == false) newTitle += std::string(" | LoD: ") + std::to_string(this->_viewSector3D.getLoD()) + std::string(" (factor: ") + std::to_string(this->_viewSector3D.getLoDFactor()) + std::string(" , auto: ") + std::string(this->_viewSector3D.isAutoLoD() ? "true" : "false") + ")" + std::string(" | Triangles: ") + std::to_string(this->_viewSector3D.getTrianglesCount()); if(this->isMode2D() == false) { if(this->_viewSector3D.isAutoLoD()) { if(fps < 100) { this->_viewSector3D.decreaseLoD(); if(fps < 60) this->_viewSector3D.decreaseLoD(); } else if(fps > 160) { this->_viewSector3D.increaseLoD(); } } } Game::get().getWindow().appendTitle(newTitle); }); }
static void cursor_pos_callback(GLFWwindow *window, double x_d, double y_d) { int width_i, height_i; glfwGetWindowSize(window, &width_i, &height_i); const glf2 size = {width_i, height_i}; const glf2 center = scale_glf2(size, 0.5f); const glf2 cursor = {x_d, size.y - y_d}; cam3_rot(cursor, center, size); glfwSetCursorPos(window, center.x, center.y); }
//Calculate the view matrix from the mouse positions void Camera::calculateViewMatrix(Window window) { //Get the mouse positions xPos, yPos; glfwGetCursorPos(window.getWindow(), &xPos, &yPos); //Reset the mouse position int width, height; glfwGetWindowSize(window.getWindow(), &width, &height); glfwSetCursorPos(window.getWindow(), width / 2, height / 2); //Calculate the change in the x and y position double deltaXPos = xPos - (width / 2); double deltaYPos = yPos - (height / 2); //Calculate the pitch and yaw values from the change in x and y pitch += deltaYPos * sensitivity; yaw += deltaXPos * sensitivity; //Create the EulerAngles object to convert the angles into a direction vector EulerAngle angles = EulerAngle(yaw, pitch, 0); //Check the pitch and yaw values and stop them from inversing angles.constrain(); //Get the direction vector from the angles direction = angles.toVector(); //Movement speed and perpendicular vector to direction vector glm::vec3 speed = glm::vec3(0.1, 0.1, 0.1); glm::vec3 right = glm::cross(direction, glm::vec3(0, 1, 0)); //Calculate the position and movement if (glfwGetKey(window.getWindow(), GLFW_KEY_W) == GLFW_PRESS) { position -= (direction * speed); } if (glfwGetKey(window.getWindow(), GLFW_KEY_S) == GLFW_PRESS) { position += (direction * speed); } if (glfwGetKey(window.getWindow(), GLFW_KEY_D) == GLFW_PRESS) { position -= (right * speed); } if (glfwGetKey(window.getWindow(), GLFW_KEY_A) == GLFW_PRESS) { position += (right * speed); } //Create the view matrix from the position and direction view = glm::lookAt( position, position - direction, glm::vec3(0, 1, 0) ); }