如何为GLFW窗口设置(0,0)坐标位置

时间:2017-02-20 22:46:02

标签: c++ macos opengl window glfw

我正在创建一个GLFW窗口,我希望左下角是原点。出于某种原因,左上角是原点。

我还应该提到我正在使用OSX。

这是我的窗口类:

#include "window.h"

namespace graphics {
    void window_size_callback(GLFWwindow* window, int width, int height);
    void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
    void mouse_button_callback(GLFWwindow* window, int button, int action, int mode);
    void cursor_position_callback(GLFWwindow* window, double mouse_x, double mouse_y);
    Window::Window(const char* title, int width, int height) {
        m_title = title;
        m_width = width;
        m_height = height;
        if (!init())
            glfwTerminate();
    }
    Window::~Window() {
        glfwTerminate();
    }
    bool Window::init() {
        if (!glfwInit()) {
            std::cout << "GLFW failed to initialize!" << std::endl;
            return false;
        }
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
        m_window = glfwCreateWindow(m_width, m_height, m_title, NULL, NULL);
        if (!m_window) {
            glfwTerminate();
            std::cout << "GLFW failed to create a window!" << std::endl;
            return false;
        }
        glfwMakeContextCurrent(m_window);
        glfwSetWindowUserPointer(m_window, this);
        glfwSetWindowSizeCallback(m_window, window_size_callback);
        glfwSetKeyCallback(m_window, key_callback);
        glfwSetMouseButtonCallback(m_window, mouse_button_callback);
        glfwSetCursorPosCallback(m_window, cursor_position_callback);
        if (glewInit() != GLEW_OK) {
            std::cout << "GLEW failed to initialize!" << std::endl;
            return false;
        }
        std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
        std::cout << "OpenGL Shading Language Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
        return true;
    }
    void Window::update() {
        glfwPollEvents();
        glfwSwapBuffers(m_window);
    }
    void Window::clear() const {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    bool Window::isClosed() const {
        return glfwWindowShouldClose(m_window);
    }
    bool Window::isKeyPressed(unsigned int keycode) const {
        if (keycode >= MAX_KEYS)
            return false;
        return m_keys[keycode];
    }
    bool Window::isMouseButtonPressed(unsigned int button) const {
        if (button >= MAX_BUTTONS)
            return false;
        return m_mouse_buttons[button];
    }
    void Window::getMousePosition(double& x, double& y) const {
        x = m_mouse_x;
        y = m_mouse_y;
    }
    void window_size_callback(GLFWwindow* window, int width, int height) {
        Window* win = (Window*)glfwGetWindowUserPointer(window);
        win->m_width = width;
        win->m_height = height;
    }
    void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
        Window* win = (Window*)glfwGetWindowUserPointer(window);
        win->m_keys[key] = action != GLFW_RELEASE;
    }
    void mouse_button_callback(GLFWwindow* window, int button, int action, int mode) {
        Window* win = (Window*)glfwGetWindowUserPointer(window);
        win->m_mouse_buttons[button] = action != GLFW_RELEASE;
    }
    void cursor_position_callback(GLFWwindow* window, double mouse_x, double mouse_y) {
        Window* win = (Window*)glfwGetWindowUserPointer(window);
        win->m_mouse_x = mouse_x;
        win->m_mouse_y = mouse_y;
    }
}

1 个答案:

答案 0 :(得分:-1)

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/

  

但是这里有一个更好的可视化方法:使用右手规则

     
      
  • X是你的拇指
  •   
  • Y是您的索引
  •   
  • Z是你的中指。如果你把
  •   
     

你的拇指向右,你的指数指向天空,它会指向   你也回来了。让Z朝这个方向很奇怪,为什么呢?   那么?简短的回答:因为100年的右手规则数学会   给你很多有用的工具。唯一的缺点是不直观的Z。

     

这是您无法更改的内容,它内置于您的显卡中。   所以(-1,-1)是屏幕的左下角。 (1,-1)是   右下角,(0,1)是中间的顶部。所以这个三角形应该   占据屏幕的大部分。