модификация обработчика мыши
This commit is contained in:
		
							parent
							
								
									71cb0acbd6
								
							
						
					
					
						commit
						4ff13f4448
					
				
							
								
								
									
										58
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										58
									
								
								src/main.cpp
									
									
									
									
									
								
							| @ -23,23 +23,34 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) | ||||
|     glViewport(0, 0, 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) | ||||
| @ -77,6 +88,7 @@ int main(void) | ||||
| 
 | ||||
|     // Установка callback-функции для мыши и камеры
 | ||||
|     glfwSetCursorPosCallback(window, mouse_callback); | ||||
|     glfwSetMouseButtonCallback(window, mouse_button_callback); | ||||
| 
 | ||||
|     // Загрузка функций OpenGL с помощью GLAD
 | ||||
|     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) | ||||
| @ -394,9 +406,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(); | ||||
| @ -495,9 +511,21 @@ 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)); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user