OpenGL不会随机渲染任何三角形

k4aesqcs  于 2022-10-18  发布在  其他
关注(0)|答案(1)|浏览(222)

所以我有一个奇怪的错误,我的程序有时不会渲染场景中的任何东西,有时会渲染完美的Find。它呈现完美的每四次或第五次尝试,有时甚至需要更多的尝试。此问题出现在具有不同显卡的多台计算机上。在网上搜索时,我似乎找不到任何解决方案。
这是当它不工作的时候:

它应该一直是这样的:

我试过使用Sanitize,但不起作用。启动程序只花了更长的时间,结果发现它仍然不能渲染。
我在C++中使用glfw,这是我的初始化代码:

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

我怎么才能解决这个问题呢?

7ajki6be

7ajki6be1#

根据我的经验,这似乎不太可能是OpenGL的问题,而更有可能是内存问题。你是否正确、完全地初始化了矩阵和向量之类的东西?例如,我遇到了类似的问题,这是因为我没有完全初始化正交矩阵;我只设置了计算值,而没有将其他单元格设置为0.0。问题以同样的方式表现出来--有时候,正射矩阵的内存还可以,我会看到场景渲染,但其他时候就不会了。

相关问题