модификация обработчика мыши
This commit is contained in:
		
							parent
							
								
									be2917546a
								
							
						
					
					
						commit
						68a8f4d0d0
					
				
							
								
								
									
										58
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										58
									
								
								src/main.cpp
									
									
									
									
									
								
							@ -61,23 +61,34 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
 | 
			
		||||
    Camera::current().setPerspective(CAMERA_FOVy, (float)width/height);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
bool firstMouse = true;
 | 
			
		||||
float lastX, lastY;
 | 
			
		||||
// Данные о мыши
 | 
			
		||||
struct Mouse
 | 
			
		||||
{
 | 
			
		||||
    float x = 0, y = 0; // Координаты курсора
 | 
			
		||||
    float prev_x = 0, prev_y = 0; // Координаты курсора на предыдущем кадре
 | 
			
		||||
    uint16_t left = 040100, right = 040100; // Состояние кнопок
 | 
			
		||||
} mouse; 
 | 
			
		||||
 | 
			
		||||
void process_mouse_button(uint16_t& button)
 | 
			
		||||
{
 | 
			
		||||
    if ((++button & 037777) == 037777)
 | 
			
		||||
        button &= 0140100;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
 | 
			
		||||
{ 
 | 
			
		||||
    if (firstMouse)
 | 
			
		||||
    {
 | 
			
		||||
        lastX = xpos;
 | 
			
		||||
        lastY = ypos;
 | 
			
		||||
        firstMouse = false;
 | 
			
		||||
    }
 | 
			
		||||
    mouse.x = xpos;
 | 
			
		||||
    mouse.y = ypos;
 | 
			
		||||
}  
 | 
			
		||||
 | 
			
		||||
    glm::vec2 offset(xpos - lastX, lastY - ypos); 
 | 
			
		||||
    lastX = xpos;
 | 
			
		||||
    lastY = ypos;
 | 
			
		||||
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
 | 
			
		||||
{
 | 
			
		||||
    uint16_t& mouse_button = (button == GLFW_MOUSE_BUTTON_LEFT)?mouse.left:mouse.right;
 | 
			
		||||
    
 | 
			
		||||
    Camera::current().rotate(offset);
 | 
			
		||||
    if (action == GLFW_PRESS && !(mouse_button & 0100000))
 | 
			
		||||
        mouse_button = 0100000;
 | 
			
		||||
    else if (action == GLFW_RELEASE)
 | 
			
		||||
        mouse_button = 040000;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(void)
 | 
			
		||||
@ -115,6 +126,7 @@ int main(void)
 | 
			
		||||
 | 
			
		||||
    // Установка callback-функции для мыши и камеры
 | 
			
		||||
    glfwSetCursorPosCallback(window, mouse_callback);
 | 
			
		||||
    glfwSetMouseButtonCallback(window, mouse_button_callback);
 | 
			
		||||
 | 
			
		||||
    // Загрузка функций OpenGL с помощью GLAD
 | 
			
		||||
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
 | 
			
		||||
@ -442,9 +454,13 @@ int main(void)
 | 
			
		||||
        scene.render(gShader, material_data);
 | 
			
		||||
        rectangle.render(gShader, material_data);
 | 
			
		||||
 | 
			
		||||
        glReadBuffer(GL_COLOR_ATTACHMENT4);
 | 
			
		||||
        glReadPixels(lastX, WINDOW_HEIGHT-lastY, 1, 1, GL_RGB_INTEGER, GL_UNSIGNED_INT, &selected);
 | 
			
		||||
        std::cout << (void*) selected.value << ' ' << selected.etc << '\n';
 | 
			
		||||
        // Выбор объекта
 | 
			
		||||
        if (mouse.left == 0100000)
 | 
			
		||||
        {
 | 
			
		||||
            glReadBuffer(GL_COLOR_ATTACHMENT4);
 | 
			
		||||
            glReadPixels(mouse.x, WINDOW_HEIGHT-mouse.y, 1, 1, GL_RGB_INTEGER, GL_UNSIGNED_INT, &selected);
 | 
			
		||||
            std::cout << (void*) selected.value << ' ' << selected.etc << '\n';
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Активируем буфер SSAO
 | 
			
		||||
        ssaoBuffer.use();
 | 
			
		||||
@ -543,10 +559,22 @@ int main(void)
 | 
			
		||||
        bulbShader.use();
 | 
			
		||||
        Light::render(bulbShader, material_data);
 | 
			
		||||
 | 
			
		||||
        // Дополнительная обработка мыши
 | 
			
		||||
        process_mouse_button(mouse.left);
 | 
			
		||||
        process_mouse_button(mouse.right);
 | 
			
		||||
        mouse.prev_x = mouse.x;
 | 
			
		||||
        mouse.prev_y = mouse.y;
 | 
			
		||||
 | 
			
		||||
        // Представление содержимого буфера цепочки показа на окно
 | 
			
		||||
        glfwSwapBuffers(window);
 | 
			
		||||
        // Обработка системных событий
 | 
			
		||||
        glfwPollEvents();
 | 
			
		||||
 | 
			
		||||
        // Поворот камеры
 | 
			
		||||
        if (mouse.right & 0100000
 | 
			
		||||
        &&  mouse.x != mouse.prev_x 
 | 
			
		||||
        &&  mouse.y != mouse.prev_y)
 | 
			
		||||
            Camera::current().rotate(glm::vec2(mouse.x - mouse.prev_x, mouse.prev_y - mouse.y));
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user