Making object follow mouse

I try to make object follow mouse on 2D plane. I’m trying to convert screen coords of the mouse to word coords by using glm::unProject. When I run my code object appears but then when I move mouse it instantly moves and I can’t see it (obj doesn’t follow cursor). For debuging purposes I display obj. coords in a console and those are changing rapidly when I move cursor. Any ideas how can I make it work correctly?

#include <iostream>

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/intersect.hpp>

#include "VBO.h"
#include "VAO.h"
#include "EBO.h"
#include "Camera.h"
#include "Shader.h"

#include "Cube.h"

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;

float deltaTime = 0.0f;
float lastFrame = 0.0f;

int main()
{
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow* window = glfwCreateWindow(800, 600, "Hello Window!", NULL, NULL);
	if (window == NULL)
	{
		std::cout << "Failed to initialize GLFW" << std::endl;
		glfwTerminate();
		return -1;
	}

	glfwMakeContextCurrent(window);

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

	glViewport(0, 0, 800, 600);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
	glfwSetCursorPosCallback(window, mouse_callback);
	glfwSetScrollCallback(window, scroll_callback);

	//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

	glEnable(GL_DEPTH_TEST);

	glm::mat4 projection = glm::mat4(1.0f);
	glm::mat4 view = glm::mat4(1.0f);
	glm::mat4 model = glm::mat4(1.0f);

	Cube cube1(1.0f, "D:/Textures for OpenGL/container.jpg", &view, &projection);
	Cube cube2(1.0f, "D:/Textures for OpenGL/grass.jpg", &view, &projection);

	while (!glfwWindowShouldClose(window))
	{
		float currentFrame = glfwGetTime();
		deltaTime = currentFrame - lastFrame;
		lastFrame = currentFrame;

		processInput(window);
		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
		view = camera.GetViewMatrix();

		double xpos, ypos;
		glfwGetCursorPos(window, &xpos, &ypos);

		GLfloat depth_comp;
		glReadPixels(xpos, ypos, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth_comp);

		glm::vec4 viewport = glm::vec4(0.0f, 0.0f, 800.0f, 600.0f);
		glm::vec3 windows_coords = glm::vec3(xpos, ypos, depth_comp);
		glm::vec3 position = glm::unProject(windows_coords, model*view, projection, viewport);

		cube1.setPosition(glm::vec3(position.x, position.y, cube1.getPosition().z));
		cube1.draw();

		cube2.setPosition(glm::vec3(0.0, 0.0, -3.0));
		cube2.draw();

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwTerminate();
}

you may find this helpfull:
using-glms-unprojec

the link uses view*model , the reverse order of yours

Hello, thank you for response this stack overflow post helped me a lot! I add additional line of code and now object follow mouse perfectly but another problem has occured. I want to object to stay on it’s z position and move it on XY plane. When I set it’s xy position to calculated earlier and leave it’s z component intact object is moving vary fast. I guess it’s because this movement is calculated for bigger z value. My question is how to find the offset which I should move object “proportional” to it’s z value?

double xpos, ypos;	
glfwGetCursorPos(window, &xpos, &ypos);
ypos = 600.0f - ypos; // This line makes Y coordinate correct

I also tried to use implement this with ray plane intersection but it doesn’t work. Any ideas how to make object follow mouse perfectly on any z value distance ?