我正试图通过int id实现拾取,但看起来我的着色器什么都没写,尽管我可以正确读取清晰的颜色。
vs,我完全跳过了任何矩阵进行调试:
#version 330
#include semantic.glsl
layout (location = POSITION) in vec3 position;
uniform Transform0
{
mat4 view;
mat4 proj;
mat4 viewProj;
};
uniform Transform1
{
mat4[TRANSFORM1_SIZE] models;
};
uniform Parameters
{
// x = mesh baseVertex
// y = selected
// z = active
// w = id
ivec4[INDICES_SIZE] params;
};
out Block
{
flat int id;
} outBlock;
int getIndex()
{
int iBegin = 0;
int iEnd = params.length() - 1;
int l = iBegin;
int r = iEnd;
int i = 0;
if(params.length > 1)
{
do
{
i = int(((l + r) / 2.0f));
if (l == (r - 1))
if (l == 0 && gl_VertexID <= params[l].x || gl_VertexID <= params[l].x && gl_VertexID > params[l - 1].x)
return l;
else if(gl_VertexID > params[l].x && gl_VertexID <= params[r].x)
return r;
else
return 0;
else if (gl_VertexID == params[i].x)
return i;
else if (gl_VertexID < params[i].x)
r = i;
else if (gl_VertexID > params[i].x)
l = i;
} while (l < r);
}
return 0;
}
void main()
{
int index = getIndex();
mat4 model = models[index];
//gl_Position = proj * (view * (model * vec4(position, 1)));
gl_Position = vec4(4.0 * float(gl_VertexID % 2) - 1.0, 4.0 * float(gl_VertexID / 2) - 1.0, 0.0, 1.0);
outBlock.id = params[index].w;
}
fs,目前的硬编码值,输出为0(FRAG_COLOR
),也尝试了1,但没有:
#version 330
#include semantic.glsl
// Outgoing final color.
layout (location = FRAG_COLOR) out int outputColor;
in Block
{
flat int id;
} inBlock;
void main()
{
//outputColor = inBlock.id;
outputColor = 9;
}
在初始化阶段,我有一个fbo,称为RESOLVE
,有3个附件,一个深度,一个浮动颜色在0上,一个整数在1上拾取。Fbo已完成:
gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(Texture.RESOLVE_ID));
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl3.glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, EC.viewer.size.x, EC.viewer.size.y, 0, GL_RED_INTEGER, GL_INT, null);
gl3.glGenFramebuffers(Framebuffer.MAX - (samples == 1 ? 1 : 0), framebufferName);
gl3.glBindFramebuffer(GL_FRAMEBUFFER, framebufferName.get(Framebuffer.RESOLVE));
gl3.glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, textureName.get(Texture.RESOLVE_DEPTH), 0);
gl3.glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureName.get(Texture.RESOLVE_COLOR), 0);
gl3.glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, textureName.get(Texture.RESOLVE_ID), 0);
if (gl3.glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
System.err.println("incomplete");
return false;
}
并渲染&读取,还添加了glFlush
、glFinish
和glPixelStorei
,但没有添加:
gl3.glBindFramebuffer(GL_FRAMEBUFFER, framebufferName.get(Framebuffer.RESOLVE));
gl3.glDrawBuffer(GL_COLOR_ATTACHMENT1);
gl3.glClearBufferiv(GL_COLOR, 0, clearId.put(0, 0)); // we care clearing only red
gl3.glBindVertexArray(EC.meshManager.vertexArrayName.get(0));
gl3.glEnable(GL_DEPTH_TEST);
gl3.glUseProgram(program.name);
gl3.glDrawElements(GL_TRIANGLES, EC_Gl3MeshManager.ELEMENT_OPAQUE_COUNT, GL_UNSIGNED_INT, 0);
gl3.glDisable(GL_DEPTH_TEST);
gl3.glReadBuffer(GL_COLOR_ATTACHMENT1);
glm.vec._2.i.Vec2i window = new glm.vec._2.i.Vec2i(
EC.inputListener.mousePressed.getX(),
EC.viewer.size.y - EC.inputListener.mousePressed.getY() - 1);
System.out.println("window (" + window.x + ", " + window.y + ")");
gl3.glFlush();
gl3.glFinish();
gl3.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Get the red coordinate reading the pixel click from the color 1 buffer, we can use the clearId buffer
gl3.glReadPixels(window.x, window.y, 1, 1, GL_RED_INTEGER, GL_INT, clearId);
gl3.glReadBuffer(GL_COLOR_ATTACHMENT0);
正如我所说,如果我用10清除,我会读10,所以这是有效的,唯一有问题的是我的着色器。。
伙计们,你们能发现错误吗?
编辑:试图调试,我正在检查颜色1 上的附件类型
gl3.glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, clearId);
System.out.println("GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE: " + clearId.get(0));
gl3.glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, clearId);
System.out.println("GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME: " + clearId.get(0)+", tex: "+textureName.get(Texture.RESOLVE_ID));
我得到:
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE: 5890
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME: 14, tex: 14
5890是GL_TEXTURE
,名称看起来正确
尝试使用glFramebufferTexture2D
,但我仍然得到GL_TEXTURE
类型而不是GL_TEXTURE_2D
编辑2:尝试读取深度分量,总是0
真遗憾,问题出在深度测试上,因为我忘记清理缓冲区,所以它从未通过