Skip to content

Commit

Permalink
mousemove
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Dec 27, 2023
1 parent 811c0ef commit e46a351
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion assets/orange.vert
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#version 330 core
in vec3 position;
out vec3 vertPosition;
uniform vec2 uniMouse;
void main() {
vertPosition = position;
vertPosition = position + vec3(uniMouse.x, uniMouse.y, 0);
gl_Position = vec4(vertPosition, 1);
}
1 change: 1 addition & 0 deletions include/InputCtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct InputCtl {

void register_callbacks(GLFWwindow *window);

glm::vec2 get_cursor_pos();
glm::mat4x4 get_view_matrix();
glm::mat4x4 get_projection_matrix();
};
12 changes: 7 additions & 5 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ void Game::render() {
auto view = m_inputCtl.get_view_matrix();
glm::mat4x4 model(1.0f); // 等会用更现代的方式指定这些矩阵

/* int prog; */
/* CHECK_GL(glGetIntegerv(GL_CURRENT_PROGRAM, &prog)); */
/* CHECK_GL(glUniformMatrix4fv(glGetUniformLocation(prog, "model"), 1, GL_FALSE, glm::value_ptr(model))); */
/* CHECK_GL(glUniformMatrix4fv(glGetUniformLocation(prog, "view"), 1, GL_FALSE, glm::value_ptr(view))); */
/* CHECK_GL(glUniformMatrix4fv(glGetUniformLocation(prog, "projection"), 1, GL_FALSE, glm::value_ptr(projection))); */
/* CHECK_GL(glUniformMatrix4fv(glGetUniformLocation(m_private->program, "model"), 1, GL_FALSE, glm::value_ptr(model))); */
/* CHECK_GL(glUniformMatrix4fv(glGetUniformLocation(m_private->program, "view"), 1, GL_FALSE, glm::value_ptr(view))); */
/* CHECK_GL(glUniformMatrix4fv(glGetUniformLocation(m_private->program, "projection"), 1, GL_FALSE, glm::value_ptr(projection))); */

glm::vec2 mousePos = m_inputCtl.get_cursor_pos();
int location = glGetUniformLocation(m_private->program, "uniMouse");
CHECK_GL(glUniform2f(location, mousePos.x, mousePos.y));
m_private->obj.draw_obj();
}
21 changes: 12 additions & 9 deletions src/InputCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ InputCtl::InputCtl() : m_private(std::make_unique<Private>()) {}

InputCtl::~InputCtl() = default;

glm::vec2 InputCtl::get_cursor_pos() {
double xpos, ypos;
glfwGetCursorPos(m_private->window, &xpos, &ypos);
int width, height;
glfwGetWindowSize(m_private->window, &width, &height);

float x = (float)(2 * xpos / width - 1);
float y = (float)(2 * (height - ypos) / height - 1);
return glm::vec2(x, y);
}

glm::mat4x4 InputCtl::get_view_matrix() {
return m_private->camState.view_matrix();
}
Expand Down Expand Up @@ -187,15 +198,7 @@ void InputCtl::cursor_pos_callback(double xpos, double ypos) {
}

void InputCtl::mouse_button_callback(int button, int action, int mods) {
double xpos, ypos;
glfwGetCursorPos(m_private->window, &xpos, &ypos);
int width, height;
glfwGetWindowSize(m_private->window, &width, &height);

float x = (float)(2 * xpos / width - 1);
float y = (float)(2 * (height - ypos) / height - 1);
glm::vec2 pos(x, y);

auto pos = get_cursor_pos();
m_private->lastpos = pos;

m_private->moving = m_inputPref.orbit_binding.check_is_pressed(m_private->window)
Expand Down

0 comments on commit e46a351

Please sign in to comment.