There is a crash (unhandled exception at 0x0000000000000000) on the line glGenFramebuffers(1, &framebufferName);
, and I mentioned in a comment above it:
#if 1 // I'm not sure where should I place this block of #if, because if I set that number to 0 then there is no crash.
Note: When I say, I crash on a line, I say that when I press F10 on that line then an unhandled exception, that’s because the error line will stop after that line.
Is there an alternative of Vulkan Validation Layer in OpenGL? I tried to add GLenum e = glGetError();
just after the lines with crashes (glGenFramebuffers()
or glFramebufferTexture()
) but the program cannot even evaluate glGetError()
because of an unhandled exception before it. How to detect such error in OpenGL because there is no information about the errors?
Goal:
I want to create a Render Target of a Shadow Map. But I always crash either on glGenFramebuffers()
or on glFramebufferTexture()
sometimes, and I have no idea how to fix it?
Code referenced from “imgui/example_sdl_opengl3/main.cpp”
Link: https://github.com/ocornut/imgui/blob/master/examples/example_sdl_opengl3/main.cpp
And the link I want to apply in the #if block: Tutorial 16 : Shadow mapping
int main(int, char **) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
printf("Error: %s\n", SDL_GetError());
return -1;
}
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
...
#elif defined(__APPLE__)
...
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
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, 0);
#endif
// Create window with graphics context
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
// Our state
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
#if 1 // I'm not sure where should I place this block of #if, because if I set that number to 0 then there is no crash.
{
// Originated from [Basic OpenGL > "tutorial16_shadowmaps"](https://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/)
// The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer.
GLuint framebufferName = 0;
glGenFramebuffers(1, &framebufferName); // <-- Error: Exception has occurred: W32/0xC0000005 Unhandled exception at 0x0000000000000000 in (...).exe
glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
// Depth texture. Slower than a depth buffer, but you can sample it later in your shader
GLuint depthTexture;
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT16, 1024, 1024, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0);
// No color output in the bound framebuffer, only depth.
glDrawBuffer(GL_NONE);
// Always check that our framebuffer is ok
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
printf("Error\n");
return -1;
}
}
#endif
// Main loop
bool done = false;
while (!done) {
...
}
...
return 0;
}