1. 程式人生 > >opengl在高DPI螢幕下顯示不正常的解決方法.

opengl在高DPI螢幕下顯示不正常的解決方法.

#include <iostream>
#include <gl/glew.h>
#include <GLFW/glfw3.h>
int width = 800;
int height = 600;
char *title = "GLFW window";

int main(int argc,char* argv[]){
    if (glfwInit() != GL_TRUE) {
        printf("glfwInit fault");
        return 1;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL);
    if (window == nullptr){
        glfwTerminate();
        printf("window fault");
        return 2;
    }
    glfwMakeContextCurrent(window);
    
    printf("OpenGL version supported:%s \n",glGetString(GL_VERSION));
    printf("Renderer:%s \n",glGetString(GL_RENDERER));
    printf("GLSL:%s \n",glGetString(GL_SHADING_LANGUAGE_VERSION));
    
    glewExperimental = true;
    if (glewInit() != GLEW_OK){
        printf("glewInit fault");
        return 3;
    }
    
    //解決高DPI螢幕顯示不正常
    int tw,th;
    glfwGetFramebufferSize(window, &tw, &th);
    glViewport(0, 0, tw, th);
    
    glfwSwapInterval(1);
    glClearColor( 0.0f, 0.0f, 0.4f, 1.0f );

    while (!glfwWindowShouldClose(window)) {
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        
        glfwPollEvents();
        glfwSwapBuffers(window);
    }
    
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}