2017-02-20 2 views
0

Ich erstelle ein GLFW-Fenster und möchte, dass die linke untere Ecke der Ursprung ist. Aus irgendeinem Grund ist die obere linke Ecke der Ursprung.Wie setze ich (0, 0) Koordinatenposition für das GLFW-Fenster

Ich sollte auch erwähnen, dass ich OSX verwende.

Hier ist meine Fensterklasse:

#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; 
    } 
} 
+0

Der Ursprung für was? Mauskoordinaten? – genpfault

+0

Das und opengl. @genpfault –

Antwort

-1

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

Aber hier ist ein besserer Weg, dies zu visualisieren: Verwenden Sie die rechte Hand-Regel

  • X Daumen
  • Y ist Ihr Index
  • Z ist dein Mittelfinger. Wenn Sie

Ihren Daumen nach rechts und Ihren Index in den Himmel setzen, wird es auf Ihren Rücken auch zeigen. Das Z in dieser Richtung ist komisch, also warum ist es so? Kurze Antwort: Weil 100 Jahre Right Hand Rule Math Ihnen viele nützliche Werkzeuge geben. Der einzige Nachteil ist ein unintuitive Z.

Das ist etwas, was Sie nicht ändern können, es ist in Ihrer Grafikkarte eingebaut. Also (-1, -1) ist die untere linke Ecke des Bildschirms. (1, -1) ist die unten rechts und (0,1) ist die mittlere Spitze. Also sollte dieses Dreieck den größten Teil des Bildschirms einnehmen.

+0

Der Ursprung ist nur in NDC fest codiert. Wenn Sie zum Beispiel nach der Projektion eine Spiegelung der y-Achse anwenden, verhält sich alles so, als wäre der Ursprung die obere linke Ecke. Denken Sie daran, dass das Spiegeln die Reihenfolge der Polygonwindung ändert. – BDL

+0

Worüber reden Sie? OpenGL verwendet das gewünschte Koordinatensystem. Sie sprechen davon, den Fensterursprung auf * unten links * zu setzen, so wie es in OpenGL ist. Stattdessen haben die Fensterkoordinaten den Ursprung in * oben links *, so dass die Ecke 0,0 ist. – JamEngulfer

Verwandte Themen