我想运行模板测试以概述多维数据集。它来自learnopengl https://learnopengl.com/Advanced-OpenGL/Stencil-testing。
进行概述的多维数据集应该丢弃带纹理的多维数据集上的碎片。不会发生,轮廓立方体覆盖整个区域。
static const char* cubeVtx = R"(
#version 460 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_uv;
out vec2 v_uv;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
v_uv = a_uv;
gl_Position = projection * view * model * vec4(a_position, 1.0f);
}
)";
static const char* cubeFrag = R"(
#version 460 core
layout (location = 0) out vec4 f_color;
in vec2 v_uv;
uniform sampler2D u_texture1;
void main()
{
f_color = texture(u_texture1, v_uv);
}
)";
static const char* singleColorVtx = R"(
#version 460 core
layout (location = 0) in vec3 a_position;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(a_position, 1.0f);
}
)";
static const char* singleColorFrag = R"(
#version 460 core
layout (location = 0) out vec4 f_color;
void main()
{
f_color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}
)";
void Cube::InitAPI()
{
shader = Shader::Create();
ShaderProgram program[2] = {
shader->ShaderFromSource(cubeVtx, ShaderType::Vertex),
shader->ShaderFromSource(cubeFrag, ShaderType::Fragment)
};
shader->End(program, 2);
singleColorShader = Shader::Create();
ShaderProgram singleColorProgram[2] = {
singleColorShader->ShaderFromSource(singleColorVtx, ShaderType::Vertex),
singleColorShader->ShaderFromSource(singleColorFrag, ShaderType::Fragment)
};
singleColorShader->End(singleColorProgram, 2);
glCreateVertexArrays(1, &vao);
glBindVertexArray(vao);
glCreateBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glNamedBufferData(vbo, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 5 * sizeof(float), 0);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glCreateVertexArrays(1, &planeVao);
glBindVertexArray(planeVao);
glCreateBuffers(1, &planeVbo);
glBindBuffer(GL_ARRAY_BUFFER, planeVbo);
glNamedBufferData(planeVbo, sizeof(planeVertices), planeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 5 * sizeof(float), 0);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 5 * sizeof(float), (void*)(3 * sizeof(float)));
texture = Texture::LoadPNG("Media/container2");
planeTexture = Texture::LoadPNG("Media/matrix");
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
shader->UseProgram();
shader->Uniform1i("u_texture1", 0);
}
void Cube::Render()
{
singleColorShader->UseProgram();
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)1280.0f / (float)720.0f, 0.1f, 100.0f);
singleColorShader->UniformMatrix4fv("view", view);
singleColorShader->UniformMatrix4fv("projection", projection);
shader->UseProgram();
shader->UniformMatrix4fv("view", view);
shader->UniformMatrix4fv("projection", projection);
glStencilMask(0x00);
glBindVertexArray(planeVao);
planeTexture->Active();
planeTexture->Bind();
glDrawArrays(GL_TRIANGLES, 0, 6);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilMask(0xFF);
glBindVertexArray(vao);
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
shader->UniformMatrix4fv("model", model);
texture->Active();
texture->Bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
shader->UniformMatrix4fv("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
singleColorShader->UseProgram();
float scale = 1.1;
glBindVertexArray(vao);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
model = glm::scale(model, glm::vec3(scale, scale, scale));
shader->UniformMatrix4fv("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
model = glm::scale(model, glm::vec3(scale, scale, scale));
shader->UniformMatrix4fv("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glStencilMask(0xFF);
glEnable(GL_DEPTH_TEST);
}
Test::Test()
{
//tried many patterns and stencil values
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
window = SDL_CreateWindow("window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
glewExperimental = true;
glewInit();
cube.InitAPI();
}
void Test::MainLoop()
{
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
cube.Render();
SDL_GL_SwapWindow(window);
}
}
我还是没有得到深度测试的东西,当我开始以为我要进行模板测试时,一切都搞砸了,因为它不起作用。
是的,在Render()
方法之前,我在其他地方清除了颜色,深度和模板缓冲区。
我期望的是红色轮廓,而不是整个立方体
请您解释一下,就像我是个白痴一样,为什么它不丢弃1
的片段
答案 0 :(得分:1)
在调用任何其他SDL函数之前,先调用SDL_Init
SDL_Init(SDL_INIT_EVERYTHING);
请参阅SDL_Init()
的文档:
使用此函数初始化SDL库。在使用大多数其他SDL函数之前,必须先调用它。
要使模板缓冲区起作用,必须设置SDL_GL_STENCIL_SIZE
属性。默认情况下,此属性为0。通常,模板缓冲区具有8位。例如:
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute
必须在SDL_CreateWindow
之前被调用,否则不会起作用。
请参阅SDL_GL_SetAttribute
的文档:
使用此功能可以在创建窗口之前设置OpenGL窗口属性。
答案 1 :(得分:0)
原始代码在SDL_Init
之后调用SDL_GL_SetAttribute()
上面的代码甚至没有调用SDL_Init
。初始化SDL可以修复所有问题。