简体   繁体   中英

OpenGL randomly renders no triangles

So I have this weird error where my program won't render anything in the scene sometimes, and sometimes renders perfectly find. It renders perfectly fine every forth or fifth try and sometimes even more tries are needed. This problem occurs on multiple computers with different graphics card. I cannot seem to find any solution to this when searching online.

This is when it does not work: 不工作

This is how it should look all the time: 在此处输入图像描述

I have tried to use sanitize but that did not work. It only took longer to start the program only to see that it still doesn't render.

I am using glfw with c++, this is my init code:

glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

int major, minor, rev;
glfwGetVersion(&major, &minor, &rev);
std::cout << "GLFW v" << major << '.' << minor << '.' << rev << std::endl;
std::cout << glfwGetVersionString() << std::endl;

// create a window object
GLFWwindow* window = glfwCreateWindow(800, 600, "FPS", glfwGetPrimaryMonitor(), NULL);
if (window == NULL) {
    std::cout << "Failed to create GLFW window" << std::endl;
    glfwTerminate();
    return -1;
}
glfwMakeContextCurrent(window); // assign it to main thread

// GLAD manages function pointers for OpenGL so we want to initialize GLAD before we call any OpenGL function:
// We pass GLAD the function to load the address of the OpenGL function pointers which is OS-specific. GLFW gives us glfwGetProcAddress that defines the correct function based on which OS we're compiling for.
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
    std::cout << "Failed to initialize GLAD" << std::endl;
    return -1;
}

glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);  
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_DISABLED);

// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST); // this removes fragments that are behind other fragments

How can I resolve this?

From my experience this seems less likely to be an OpenGL issue and more likely to be an issue with memory. Are you correctly & fully initializing things like matrices and vectors? For example, I had a similar issue and it is because I was not fully initializing an ortho matrix; I was setting only the computed values and was not setting the other cells to 0.0. The problem manifested in the same way - occasionally the memory for the ortho matrix would be okay and I would see my scene render, but other times it would not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM