我正在使用 Derelict3,它使用特定的 openGL3 标准,如果调用 glEnable(GL_DEPTH_TEST),我会遇到屏幕冻结(不更新)的问题,如果不是,深度缓冲区不起作用。我注意到设置清除颜色和启用深度测试的调用如果在设置 SDL_GL 上下文和重新加载 Derelict3 之前进行,则会被忽略。我必须按以下顺序制作它们:
win=SDL_CreateWindow("3Doodle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
if(!win){
writefln("Error creating SDL window");
SDL_Quit();
}
context=SDL_GL_CreateContext(win);
SDL_GL_SetSwapInterval(1);
glVersion=DerelictGL3.reload();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearDepth(1.0);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
虽然这在写入标准双缓冲区时解决了深度剔除,但当我使用 renderBuffer 作为新的深度缓冲区实现帧缓冲区对象时,我得到了冻结...程序编译并运行时没有警告,但渲染图像到屏幕对齐的四边形保持固定。如果我注释掉 glEnable(GL_DEPTH_TEST); 相机自由移动,但渲染图像不考虑深度测试,只是按顺序绘制对象。
帧缓冲初始化代码为:
//fbo
glEnable (GL_FRAMEBUFFER_SRGB);
glGenFramebuffers(1, &fbo);
assert(fbo > 0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenRenderbuffers(1, &rbo);
assert(rbo > 0);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, projMat.w, projMat.h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenTextures(1, &fbon);
assert(fbon > 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbon);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, projMat.w, projMat.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbon, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &fboc);
assert(fboc > 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, fboc);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, projMat.w, projMat.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, fboc, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &fbop);
assert(fbop > 0);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, fbop);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, projMat.w, projMat.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, fbop, 0);
glBindTexture(GL_TEXTURE_2D, 0);
int status=glCheckFramebufferStatus(GL_FRAMEBUFFER);
assert(status == GL_FRAMEBUFFER_COMPLETE);
buffs=[GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2];
glDrawBuffers(3, buffs.ptr);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
在 draw() 中调用 fbo 的代码是:
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shad);
glUniformMatrix4fv(viewLoc, 1, GL_TRUE, player.mat.ptr);
current.draw();
glUseProgram(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
rend.draw();
我很茫然,因为相同的代码在 c# 中正常工作。