OpenGL: flickering/artifacts on integrated Intel HD Graphics

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 820 Views - Last Post: 01 August 2019 - 02:26 AM Rate Topic: -----

#1 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 138
  • Joined: 26-September 16

OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 12 July 2019 - 07:31 AM

Hello,

I'm not that great when it comes to OpenGL, but I basically want to update vertex/index data in response to certain inputs/events. However, after resizing the window, I end up having the shapes being drawn flicker or have some visual artifacts, like seemingly random lines here and there. The problem seems to go away once I switch from my built-in Intel HD Graphics to the dedicated GPU (Nvidia). Either way, here's my test code. I'm wondering if there's something I could do to prevent those side-effects.

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include <vector>

void processInput(GLFWwindow* window);
void onFramebufferSize(GLFWwindow* window, int width, int height);

const char* vertexShaderSource =
    "#version 330 core\n"
    "\n"
    "layout(location = 0) in vec2 aPos;\n"
    "\n"
    "void main()\n"
    "{\n"
    "    gl_Position = vec4(aPos.x, aPos.y, 0.0f, 1.0f);\n"
    "};\n";

const char* fragmentShaderSource =
    "#version 330 core\n"
    "\n"
    "out vec4 FragColor;\n"
    "\n"
    "void main()\n"
    "{\n"
    "    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
    "};\n";

std::vector<GLfloat> vertices;
std::vector<GLuint> indices;

float phaseShift = 0.f;

int crntWidth = 720;

GLuint vertexBufferObject, vertexArrayObject, elementBufferObject;

void initializeAndConfigureGlfw()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}

GLFWwindow* createGlfwWindow()
{
    GLFWwindow* window = glfwCreateWindow(crntWidth, 560, "OpenGL Test", NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return NULL;
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    glfwSetFramebufferSizeCallback(window, onFramebufferSize);
    return window;
}

void generateSineWave(std::vector<float>& vertices, int samplingFreq, int numPoints)
{
    vertices.resize(4 * size_t(numPoints));
    constexpr float waveFreq = 2.f;
    constexpr float coeff = 0.95f;
    float t0 = -1.f;
    float timeStep = 1.f / samplingFreq;
    float omega = 2 * 3.1415927f * waveFreq;
    for (size_t i = 0; i < size_t(numPoints); ++i) {
        float t = t0 + i * timeStep;
        float val = coeff * sinf(omega * (t + phaseShift));
        vertices[4 * i] = t;
        vertices[4 * i + 1] = 0.f;
        vertices[4 * i + 2] = t;
        vertices[4 * i + 3] = val;
    }
}

void generateIndices(std::vector<GLuint>& indices, int numPoints)
{
    indices.resize(2 * size_t(numPoints));
    for (size_t i = 0; i < 2 * size_t(numPoints); i += 2) {
        indices[i] = GLuint(i);
        indices[i + 1] = GLuint(i + 1);
    }
}

void updateBufferData()
{
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_DYNAMIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_DYNAMIC_DRAW);
}

int main()
{
    initializeAndConfigureGlfw();
    GLFWwindow* window = createGlfwWindow();

    if (!gladLoadGLLoader(GLADloadproc(glfwGetProcAddress))) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    glUseProgram(shaderProgram);

    generateSineWave(vertices, crntWidth / 2, crntWidth);
    generateIndices(indices, crntWidth);

    glGenVertexArrays(1, &vertexArrayObject);
    glGenBuffers(1, &vertexBufferObject);
    glGenBuffers(1, &elementBufferObject);

    glBindVertexArray(vertexArrayObject);

    updateBufferData();

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);

    while (!glfwWindowShouldClose(window)) {
        processInput(window);

        glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glBindVertexArray(vertexArrayObject);
        glDrawElements(GL_LINES, indices.size() * sizeof(GLuint), GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteVertexArrays(1, &vertexArrayObject);
    glDeleteBuffers(1, &vertexBufferObject);
    glDeleteBuffers(1, &elementBufferObject);

    glUseProgram(0);
    glDeleteProgram(shaderProgram);

    glfwTerminate();
    return 0;
}

void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, true);
    } else if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
        phaseShift -= 0.01f;
        generateSineWave(vertices, crntWidth / 2, crntWidth);
        updateBufferData();
    } else if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
        phaseShift += 0.01f;
        generateSineWave(vertices, crntWidth / 2, crntWidth);
        updateBufferData();
    }
}

void onFramebufferSize(GLFWwindow*, int width, int height)
{
    crntWidth = width;
    generateSineWave(vertices, width / 2, width);
    generateIndices(indices, width);
    updateBufferData();
    glViewport(0, 0, width, height);
}

Thanks for your help in advance.

This post has been edited by GazinAtCode: 12 July 2019 - 09:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: OpenGL: flickering/artifacts on integrated Intel HD Graphics

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7029
  • View blog
  • Posts: 23,882
  • Joined: 05-May 12

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 12 July 2019 - 08:06 AM

Does your profiler tell you where the time is being spent when resizing?
Was This Post Helpful? 0
  • +
  • -

#3 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 138
  • Joined: 26-September 16

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 12 July 2019 - 09:28 AM

Well, when I maximize the window, my GPU usage goes up to around 14%, but the CPU stays more or less the same. Of course, the overall process memory increases.

The usual scenario is as follows:

  • I start the program.
  • I maximize the window.
  • I bring it back to the original size.
  • Now when I press and hold Left or Right to move the plot, a vertical flickering line appears as the plot is moving, but it normally disappears when I realease the key.


If I don't maximize it first, the line doesn't show, and everything is rendered as expected.

This post has been edited by GazinAtCode: 12 July 2019 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7029
  • View blog
  • Posts: 23,882
  • Joined: 05-May 12

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 14 July 2019 - 05:43 AM

Watching the task manager isn't going to give you the precision of narrowing down which functions are taking the most time.

That vertical breaking is kind of unusual. It is usually horizontal tearing that happens and is indicative of mistimed buffer swaps.

Out of curiosity, why not just compute one idealized sine wave once, and then translate and scale as needed using transforms, instead of recomputing all the points on the wave?
Was This Post Helpful? 0
  • +
  • -

#5 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 138
  • Joined: 26-September 16

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 14 July 2019 - 11:24 AM

What kind of profiler do you mean? I've tried Graphics Frame Analyzer, but I'm getting an internal error. With RenderDoc, I can only get a very general picture of how much time is spent on each call:


EID  | Event                                                       | Draw # | Duration (Microseconds)
---------------------------------------------------------------------------------------------
7     | - glClear(Color = <0.200000, 0.300000, 0.400000, 1.000000>) | 1      | 49.152
9     | - glDrawElements(5760)                                      | 2      | 297.984
10    | - SwapBuffers()                                             | 3      | 1.024

 EID  | Event                                                       | Draw # | Duration (Microseconds)
---------------------------------------------------------------------------------------------
3     | - glClear(Color = <0.200000, 0.300000, 0.400000, 1.000000>) | 1      | 138.24
5     | - glDrawElements(10928)                                     | 2      | 728.00
6     | - SwapBuffers()                                             | 3      | 1.024

The second table shows data captured when resizing.

View PostSkydiver, on 14 July 2019 - 05:43 AM, said:

Out of curiosity, why not just compute one idealized sine wave once, and then translate and scale as needed using transforms, instead of recomputing all the points on the wave?

It may not be the optimal way to do this, but it was basically a makeshift minimal working example to illustrate the problem. In the actual program, the input may be any signal that is to be displayed in the form of vertical lines, the number of which should correspond to the current pixel width of the window.

This post has been edited by GazinAtCode: 14 July 2019 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7029
  • View blog
  • Posts: 23,882
  • Joined: 05-May 12

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 14 July 2019 - 12:47 PM

Ah, I see. So you are doing your own scaling to the screensize instead of letting OpenGL scale from a theoretical "game world" to the "view port".

Just shooting in the dark at this point: Can you try commenting out the line that says:
glfwSwapInterval(1);


and see if it make a difference. My understanding from the documentation is that is already the default -- or at least with current versions. From the other Google hits, it seems like it used to be a good practice to explicitly set this because it wasn't the default(?) a few years ago.
Was This Post Helpful? 0
  • +
  • -

#7 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 138
  • Joined: 26-September 16

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 14 July 2019 - 01:29 PM

Commenting that out makes the app run at full throttle (~500 FPS on my laptop) for the integrated Intel GPU. glfwSwapInterval(1); helps to keep it at approximately 60 FPS. It is not necessary when I switch to my Nvidia card, which stays at ~60 FPS by default. Either way, removing that line doesn't help. I can still see those flickering vertical lines on my Intel card. As I said, everything looks fine on my Nvidia GPU.

I don't think it has much to do with the windowing system as such. Moving the code over to SDL doesn't really help, either.

#include <GL/glew.h>
#include <SDL.h>

#include <iostream>
#include <vector>

SDL_GLContext mainContext;
SDL_Window* mainWindow;

bool setOpenGLAttributes();
void runEventLoop();
void cleanUp();

const char* vertexShaderSource =
    "#version 330 core\n"
    "\n"
    "layout(location = 0) in vec2 aPos;\n"
    "\n"
    "void main()\n"
    "{\n"
    "    gl_Position = vec4(aPos.x, aPos.y, 0.0f, 1.0f);\n"
    "};\n";

const char* fragmentShaderSource =
    "#version 330 core\n"
    "\n"
    "out vec4 FragColor;\n"
    "\n"
    "void main()\n"
    "{\n"
    "    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
    "};\n";

std::vector<GLfloat> vertices;
std::vector<GLuint> indices;

float phaseShift = 0.f;

int crntWidth = 720;

GLuint vertexBufferObject, vertexArrayObject, elementBufferObject;

bool initialize()
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cout << "Failed to initialize SDL" << std::endl;
        return false;
    }

    mainWindow = SDL_CreateWindow("OpenGL Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        crntWidth, 560, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

    if (!mainWindow) {
        std::cout << "Unable to create window" << std::endl;
        return false;
    }

    mainContext = SDL_GL_CreateContext(mainWindow);

    setOpenGLAttributes();
    SDL_GL_SetSwapInterval(1);
    glewInit();

    return true;
}

bool setOpenGLAttributes()
{
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    return true;
}

void generateSineWave(std::vector<float>& vertices, int samplingFreq, int numPoints)
{
    vertices.resize(4 * size_t(numPoints));
    constexpr float waveFreq = 2.f;
    constexpr float coeff = 0.95f;
    float t0 = -1.f;
    float timeStep = 1.f / samplingFreq;
    float omega = 2 * 3.1415927f * waveFreq;
    for (size_t i = 0; i < size_t(numPoints); ++i) {
        float t = t0 + i * timeStep;
        float val = coeff * sinf(omega * (t + phaseShift));
        vertices[4 * i] = t;
        vertices[4 * i + 1] = 0.f;
        vertices[4 * i + 2] = t;
        vertices[4 * i + 3] = val;
    }
}

void generateIndices(std::vector<GLuint>& indices, int numPoints)
{
    indices.resize(2 * size_t(numPoints));
    for (size_t i = 0; i < 2 * size_t(numPoints); i += 2) {
        indices[i] = GLuint(i);
        indices[i + 1] = GLuint(i + 1);
    }
}

void updateBufferData()
{
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_DYNAMIC_DRAW);
}

int WinMain()
{
    if (!initialize()) {
        return -1;
    }

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    glUseProgram(shaderProgram);

    generateSineWave(vertices, crntWidth / 2, crntWidth);
    generateIndices(indices, crntWidth);

    glGenVertexArrays(1, &vertexArrayObject);
    glGenBuffers(1, &vertexBufferObject);
    glGenBuffers(1, &elementBufferObject);

    glBindVertexArray(vertexArrayObject);

    updateBufferData();

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    runEventLoop();

    cleanUp();

    return 0;
}

void runEventLoop()
{
    bool shouldClose = false;
    while (!shouldClose) {
        SDL_Event evt;
        while (SDL_PollEvent(&evt)) {
            if (evt.type == SDL_QUIT) {
                shouldClose = true;
            }
            if (evt.type == SDL_WINDOWEVENT) {
                switch (evt.window.event) {
                case SDL_WINDOWEVENT_RESIZED:
                    crntWidth = evt.window.data1;
                    generateSineWave(vertices, crntWidth / 2, crntWidth);
                    generateIndices(indices, crntWidth);
                    updateBufferData();
                    glViewport(0, 0, crntWidth, evt.window.data2);
                    break;
                default:
                    break;
                }
            }
            if (evt.type == SDL_KEYDOWN) {
                switch (evt.key.keysym.sym) {
                case SDLK_ESCAPE:
                    shouldClose = true;
                    break;
                case SDLK_LEFT:
                    phaseShift -= 0.01f;
                    generateSineWave(vertices, crntWidth / 2, crntWidth);
                    updateBufferData();
                    break;
                case SDLK_RIGHT:
                    phaseShift += 0.01f;
                    generateSineWave(vertices, crntWidth / 2, crntWidth);
                    updateBufferData();
                    break;
                default:
                    break;
                }
            }
        }
        glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glBindVertexArray(vertexArrayObject);
        glDrawElements(GL_LINES, indices.size() * sizeof(GLuint), GL_UNSIGNED_INT, 0);
        SDL_GL_SwapWindow(mainWindow);
    }
}

void cleanUp()
{
    SDL_GL_DeleteContext(mainContext);
    SDL_DestroyWindow(mainWindow);
    SDL_Quit();
}

I'm more inclined to believe that there's a mistake in the OpenGL part of the code or that the driver is at fault.

This post has been edited by GazinAtCode: 14 July 2019 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7029
  • View blog
  • Posts: 23,882
  • Joined: 05-May 12

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 14 July 2019 - 06:59 PM

View PostGazinAtCode, on 14 July 2019 - 04:29 PM, said:

Commenting that out makes the app run at full throttle (~500 FPS on my laptop) for the integrated Intel GPU. glfwSwapInterval(1); helps to keep it at approximately 60 FPS. It is not necessary when I switch to my Nvidia card, which stays at ~60 FPS by default.

Well, so much for the documentation being accurate regarding that API. Probably should file a documentation bug against GLFW. :)

Do you really need all the calls to glBindBuffer() each time through the loop? My reading of the documentation is that the binding only needs to happen once. On the other hand, as shown above, the documentation could be wrong.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7029
  • View blog
  • Posts: 23,882
  • Joined: 05-May 12

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 14 July 2019 - 07:07 PM

From the documentation of glBufferSubData():

Quote

When replacing the entire data store, consider using glBufferSubData rather than completely recreating the data store with glBufferData. This avoids the cost of reallocating the data store.


So probably try just doing the glBindBuffer() and glBufferData() calls the first time, and then after that always use glBufferSubData(), expect when there is a screen resize. When there's a resize you'll likely need the glBufferData() call to reallocate the size of the buffer.
Was This Post Helpful? 1
  • +
  • -

#10 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 138
  • Joined: 26-September 16

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 15 July 2019 - 03:24 PM

View PostSkydiver, on 14 July 2019 - 06:59 PM, said:

Well, so much for the documentation being accurate regarding that API. Probably should file a documentation bug against GLFW. :)

I didn't find any hint as to what the default value was, but it looks like it's 0. :)

View PostSkydiver, on 14 July 2019 - 07:07 PM, said:

So probably try just doing the glBindBuffer() and glBufferData() calls the first time, and then after that always use glBufferSubData(), expect when there is a screen resize. When there's a resize you'll likely need the glBufferData() call to reallocate the size of the buffer.

I've tried now, but it doesn't solve the problem completely. Maybe the lines don't appear as often, but when they do, they tend to remain on-screen even when the plot is not moving.

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include <vector>

void processInput(GLFWwindow* window);
void onFramebufferSize(GLFWwindow* window, int width, int height);

const char* vertexShaderSource =
    "#version 330 core\n"
    "\n"
    "layout(location = 0) in vec2 aPos;\n"
    "\n"
    "void main()\n"
    "{\n"
    "    gl_Position = vec4(aPos.x, aPos.y, 0.0f, 1.0f);\n"
    "};\n";

const char* fragmentShaderSource =
    "#version 330 core\n"
    "\n"
    "out vec4 FragColor;\n"
    "\n"
    "void main()\n"
    "{\n"
    "    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
    "};\n";

std::vector<GLfloat> vertices;
std::vector<GLuint> indices;

float phaseShift = 0.f;

int crntWidth = 720;

GLuint vertexBufferObject, vertexArrayObject, elementBufferObject;

void initializeAndConfigureGlfw()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}

GLFWwindow* createGlfwWindow()
{
    GLFWwindow* window = glfwCreateWindow(crntWidth, 560, "OpenGL Test", NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return NULL;
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    glfwSetFramebufferSizeCallback(window, onFramebufferSize);
    return window;
}

void generateSineWave(std::vector<float>& vertices, int samplingFreq, int numPoints)
{
    vertices.resize(4 * size_t(numPoints));
    constexpr float waveFreq = 2.f;
    constexpr float coeff = 0.95f;
    float t0 = -1.f;
    float timeStep = 1.f / samplingFreq;
    float omega = 2 * 3.1415927f * waveFreq;
    for (size_t i = 0; i < size_t(numPoints); ++i) {
        float t = t0 + i * timeStep;
        float val = coeff * sinf(omega * (t + phaseShift));
        vertices[4 * i] = t;
        vertices[4 * i + 1] = 0.f;
        vertices[4 * i + 2] = t;
        vertices[4 * i + 3] = val;
    }
}

void generateIndices(std::vector<GLuint>& indices, int numPoints)
{
    indices.resize(2 * size_t(numPoints));
    for (size_t i = 0; i < 2 * size_t(numPoints); i += 2) {
        indices[i] = GLuint(i);
        indices[i + 1] = GLuint(i + 1);
    }
}

void updateBufferData()
{
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_DYNAMIC_DRAW);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_DYNAMIC_DRAW);
}

int main()
{
    initializeAndConfigureGlfw();
    GLFWwindow* window = createGlfwWindow();

    if (!gladLoadGLLoader(GLADloadproc(glfwGetProcAddress))) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    glUseProgram(shaderProgram);

    generateSineWave(vertices, crntWidth / 2, crntWidth);
    generateIndices(indices, crntWidth);

    glGenVertexArrays(1, &vertexArrayObject);
    glGenBuffers(1, &vertexBufferObject);
    glGenBuffers(1, &elementBufferObject);

    glBindVertexArray(vertexArrayObject);

    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);

    updateBufferData();

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void*)0);
    glEnableVertexAttribArray(0);

    while (!glfwWindowShouldClose(window)) {
        processInput(window);

        glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glBindVertexArray(vertexArrayObject);
        glDrawElements(GL_LINES, indices.size() * sizeof(GLuint), GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteVertexArrays(1, &vertexArrayObject);
    glDeleteBuffers(1, &vertexBufferObject);
    glDeleteBuffers(1, &elementBufferObject);

    glUseProgram(0);
    glDeleteProgram(shaderProgram);

    glfwTerminate();
    return 0;
}

void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, true);
    } else if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
        phaseShift -= 0.01f;
        generateSineWave(vertices, crntWidth / 2, crntWidth);
        glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(GLfloat), vertices.data());
    } else if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
        phaseShift += 0.01f;
        generateSineWave(vertices, crntWidth / 2, crntWidth);
        glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(GLfloat), vertices.data());
    }
}

void onFramebufferSize(GLFWwindow*, int width, int height)
{
    crntWidth = width;
    generateSineWave(vertices, width / 2, width);
    generateIndices(indices, width);
    updateBufferData();
    glViewport(0, 0, width, height);
}

I suppose it's really the driver, or perhaps something is wrong with the settings.

This post has been edited by GazinAtCode: 15 July 2019 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7029
  • View blog
  • Posts: 23,882
  • Joined: 05-May 12

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 15 July 2019 - 04:15 PM

Did you try glBufferSubData()?

(As an aside, I tried starting at about midnight to at least compile and run your code, but I was utterly blocked trying to compile the code not because of GLFW, but rather because I couldn't bind OpenGL entry points. I'd learned OpenGL the old school way of just including gl/gl.h, but the glad/glad.h you had utterly baffled me. It was around half past by the time I figured out that it was some kind of generated code. I had to give up at that point since I'd been ill all of last week and just barely starting to feel better yesterday. I didn't want to get sick again. When I have a chance again tonight, I'll try to picking up where I left off learning about GLAD.)
Was This Post Helpful? 0
  • +
  • -

#12 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 138
  • Joined: 26-September 16

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 15 July 2019 - 07:50 PM

Yes, I did try glBufferSubData(). The updated version of the code is included in my previous post.

And don't worry! It's not a life-and-death matter. I hope you'll get better soon.

I can try to put a solution together in Visual Studio that will take care of the dependencies. If it works, I might post it later, because now it's still kind of night where I live.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7029
  • View blog
  • Posts: 23,882
  • Joined: 05-May 12

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 15 July 2019 - 08:11 PM

Thanks. I'll give it a try when I can. Work wiped me out today. Gonna call it an early night tonight.
Was This Post Helpful? 0
  • +
  • -

#14 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 138
  • Joined: 26-September 16

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 16 July 2019 - 03:15 AM

Here it goes. It was hard to include a GLFW project that would compile from scratch, so I gave up and just included static libraries. I hope it works out of the box.

Attached File(s)


Was This Post Helpful? 1
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7029
  • View blog
  • Posts: 23,882
  • Joined: 05-May 12

Re: OpenGL: flickering/artifacts on integrated Intel HD Graphics

Posted 16 July 2019 - 04:21 AM

Thanks!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2