c++ - 奇怪的opengl渲染口吃

标签 c++ windows opengl

我在我的简单 opengl(通过 GLFW3)应用程序中遇到奇怪的卡顿。尽管启用了 vsync(帧速率几乎稳定在 60 fps),但旋转三角形的运动并不总是平滑的 - 几乎就像有时跳过某些帧一样。我尝试查看连续调用 glSwapBuffers() 之间的时间差,但它们看起来非常一致。

我做错了什么吗?我应该使用某种运动模糊过滤来使其看起来更平滑吗?

代码:

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cfloat>
#include <cassert>
#include <minmax.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>

#include <Windows.h>
#include <GL/glew.h>
#include <gl/GLU.h>
//#include <GL/GL.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp> 

#ifdef _WIN32
#pragma warning(disable:4996)
#endif

static int swap_interval;
static double frame_rate;


GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){

    // Create the shaders
    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    // Read the Vertex Shader code from the file
    std::string VertexShaderCode;
    std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
    if(VertexShaderStream.is_open()){
        std::string Line = "";
        while(getline(VertexShaderStream, Line))
            VertexShaderCode += "\n" + Line;
        VertexShaderStream.close();
    }else{
        printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
        return 0;
    }

    // Read the Fragment Shader code from the file
    std::string FragmentShaderCode;
    std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
    if(FragmentShaderStream.is_open()){
        std::string Line = "";
        while(getline(FragmentShaderStream, Line))
            FragmentShaderCode += "\n" + Line;
        FragmentShaderStream.close();
    }

    GLint Result = GL_FALSE;
    int InfoLogLength;

    // Compile Vertex Shader
    printf("Compiling shader : %s\n", vertex_file_path);
    char const * VertexSourcePointer = VertexShaderCode.c_str();
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
    glCompileShader(VertexShaderID);

    // Check Vertex Shader
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    if (Result != GL_TRUE)
    {
        glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
        if ( InfoLogLength > 0 ){
            std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
            glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
            printf("%s\n", &VertexShaderErrorMessage[0]);
        }
    }


    // Compile Fragment Shader
    printf("Compiling shader : %s\n", fragment_file_path);
    char const * FragmentSourcePointer = FragmentShaderCode.c_str();
    glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
    glCompileShader(FragmentShaderID);

    // Check Fragment Shader
    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
    if (Result != GL_TRUE)
    {
        glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
        if ( InfoLogLength > 0 ){
            std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
            glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
            printf("%s\n", &FragmentShaderErrorMessage[0]);
        }
    }

    // Link the program
    printf("Linking program\n");
    GLuint ProgramID = glCreateProgram();
    glAttachShader(ProgramID, VertexShaderID);
    glAttachShader(ProgramID, FragmentShaderID);
    glLinkProgram(ProgramID);

    // Check the program
    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
    if (Result != GL_TRUE)
    {
        glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
        if ( InfoLogLength > 0 ){
            std::vector<char> ProgramErrorMessage(InfoLogLength+1);
            glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
            printf("%s\n", &ProgramErrorMessage[0]);
        }
    }
#ifdef _DEBUG
    glValidateProgram(ProgramID);
#endif

    glDeleteShader(VertexShaderID);
    glDeleteShader(FragmentShaderID);

    return ProgramID;
}


static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

static void set_swap_interval(GLFWwindow* window, int interval)
{
    swap_interval = interval;
    glfwSwapInterval(swap_interval);
}

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
        set_swap_interval(window, 1 - swap_interval);
}

static bool init(GLFWwindow** win)
{
    if (!glfwInit())
        exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

    // creating a window using the monitor param will open it full screen
    const bool useFullScreen = false;
    GLFWmonitor* monitor = useFullScreen ? glfwGetPrimaryMonitor() : NULL;
    *win = glfwCreateWindow(640, 480, "", monitor, NULL);
    if (!(*win))
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    glfwMakeContextCurrent(*win);

    GLenum glewError = glewInit();
    if( glewError != GLEW_OK )
    {
        printf( "Error initializing GLEW! %s\n", glewGetErrorString( glewError ) );
        return false;
    }
    //Make sure OpenGL 2.1 is supported
    if( !GLEW_VERSION_2_1 )
    {
        printf( "OpenGL 2.1 not supported!\n" );
        return false; 
    }

    glfwMakeContextCurrent(*win);
    glfwSetFramebufferSizeCallback(*win, framebuffer_size_callback);
    glfwSetKeyCallback(*win, key_callback);

    // get version info
    const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString (GL_VERSION); // version as a string
    printf("Renderer: %s\n", renderer);
    printf("OpenGL version supported %s\n", version);

    return true;
}
std::string string_format(const std::string fmt, ...) {
    int size = 100;
    std::string str;
    va_list ap;
    while (1) {
        str.resize(size);
        va_start(ap, fmt);
        int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap);
        va_end(ap);
        if (n > -1 && n < size) {
            str.resize(n);
            return str;
        }
        if (n > -1)
            size = n + 1;
        else
            size *= 2;
    }
    return str;
}
int main(int argc, char* argv[])
{
    srand(9); // constant seed, for deterministic results

    unsigned long frame_count = 0;

    GLFWwindow* window;
    init(&window);

    // An array of 3 vectors which represents 3 vertices
    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f,  1.0f, 0.0f,
    };

    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    // acclocate GPU memory and copy data
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    unsigned int vao = 0;
    glGenVertexArrays (1, &vao);
    glBindVertexArray (vao);
    glEnableVertexAttribArray (0);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    // Create and compile our GLSL program from the shaders
    GLuint programID = LoadShaders( "1.vert", "1.frag" );

    // Use our shader
    glUseProgram(programID);

    GLint locPosition = glGetAttribLocation(programID, "vertex");
    assert(locPosition != -1);

    glm::mat4 world(1.0f);
    GLint locWorld = glGetUniformLocation(programID, "gWorld");
    assert(locWorld != -1 && "Error getting address (was it optimized out?)!");
    glUniformMatrix4fv(locWorld, 1, GL_FALSE, glm::value_ptr(world));
    GLenum err = glGetError();

    GLint loc = glGetUniformLocation(programID, "time");
    assert(loc != -1 && "Error getting uniform address (was it optimized out?)!");

    bool isRunning = true;
    while (isRunning)
    {
        static float time = 0.0f;
        static float oldTime = 0.0f;
        static float fpsLastUpdateTime = 0.0f;
        oldTime = time;
        time = (float)glfwGetTime();
        static std::string fps;

        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram (programID);
        glUniform1f(loc, time);
        glBindVertexArray (vao);
        glDrawArrays (GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
        glfwPollEvents();
        isRunning = !glfwWindowShouldClose(window);

        float dT = time-oldTime;
        if (time-fpsLastUpdateTime > 0.5)
        {
            static const char* fmt = "frame rate: %.1f frames per second";      
            glfwSetWindowTitle(window, string_format(fmt, 1.0f/(dT)).c_str());
            fpsLastUpdateTime = time;
        }
    }

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}


////////////////////////////////////////
// 1.frag
////////////////////////////////////////
#version 330 core

// Ouput data
out vec3 color;

void main()
{
    // Output color = red
    color = vec3(1,0,0);
}

//////////////////////////////////////////////
// 1.vert
//////////////////////////////////////////////
#version 330 core

// Input vertex data, different for all executions of this shader.
in vec3 vertex;

uniform mat4 gWorld;
uniform float time;

void main()
{
    gl_Position = gWorld * vec4(vertex, 1.0f);
    gl_Position.x += sin(time);
    gl_Position.y += cos(time)/2.0f;
    gl_Position.w = 1.0;
}

好的。我回到家并做了更多测试。

首先我尝试禁用垂直同步,但我做不到!我必须禁用 Windows 的桌面效果 (Aero) 才能这样做,你瞧 - 一旦 Aero 被禁用,卡顿现象就消失了(打开 V-Sync)。

然后我在关闭 V-Sync 的情况下对其进行了测试,当然,我得到了更高的帧率,但偶尔会出现预期的撕裂现象。

然后我全屏测试了。使用 Aero 和不使用 Aero 时渲染都很流畅。

我找不到其他人分享这个问题。你认为这是 GLFW3 的错误吗?驱动程序/硬件问题(我有带有最新驱动程序的 GTS450)?

谢谢大家的回答。我学到了很多,但我的问题仍然没有解决。

最佳答案

这是一个奇怪的 Windows dwm(桌面窗口管理器)组合模式和 glfwSwapBuffers() 交互问题。我还没有深入到问题的根源。但您可以通过执行以下操作之一解决卡顿问题:

  • 进入全屏
  • 禁用 dwm 窗口组合(参见我对 Linear movement stutter 的回答)
  • 启用多重采样:glfwWindowHint(GLFW_SAMPLES, 4);

关于c++ - 奇怪的opengl渲染口吃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18182016/

相关文章:

c++ - 未在范围内声明,初学者蜗牛赛车游戏

java - 如何使用 Java 或 CMD 获取 PC 硬件信息

windows - 是否可以从批处理文件启动文件的默认编辑器?

c# - 多个 FileSystemWatcher 来监视本地系统上的文件?

边界框的 OpenGL 深度计算

java - OpenGL VAO 多个 VBO 绑定(bind)问题

opengl - glVertexAttribPointer,改变顶点属性布局

C++ 初始化列表和默认值

c++ - 子类中具有不同值的静态基类属性

c++ - 为什么 AVFormatContext 指针在非全局结构对象中初始化,而在全局对象中初始化为 NULL?