0
\$\begingroup\$

I had a cube that had 8 vertices and 12 triangles and the visibility of the cube was okay. When I rotated, I was able to see all visible sides of it. Here is my old cube:

mesh.addVertex({-1.0, 1.0, -1.0,   0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0});
mesh.addVertex({1.0, 1.0, -1.0,   0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0});
mesh.addVertex({1.0, 1.0, 1.0,  0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0});
mesh.addVertex({-1.0, 1.0, 1.0,  0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0});
mesh.addVertex({-1.0, -1.0, 1.0,  0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0});
mesh.addVertex({1.0, -1.0, 1.0,   0.0, 1.0, 0.0,   1.0, 0.0, 0.0, 0.0, 1.0});
mesh.addVertex({1.0, -1.0, -1.0,   0.0, 1.0, 0.0,   1.0, 0.0, 0.0, 1.0, 0.0});
mesh.addVertex({-1.0, -1.0, -1.0,   0.0, 1.0, 0.0,   1.0, 0.0, 0.0, 1.0, 1.0});

mesh.addTriangle(0, 1, 2);
mesh.addTriangle(0, 2, 3);
mesh.addTriangle(4, 5, 6);
mesh.addTriangle(4, 6, 7);
mesh.addTriangle(3, 2, 5);
mesh.addTriangle(3, 5, 4);
mesh.addTriangle(2, 1, 6);
mesh.addTriangle(2, 6, 5);
mesh.addTriangle(1, 7, 6);
mesh.addTriangle(1, 0, 7);
mesh.addTriangle(0, 3, 4);
mesh.addTriangle(0, 4, 7);

Now, I updated my cube to have 24 vertices and 12 triangles (I need proper UV coordinates and normals). However, for some reason the cube's sides are culled as the cube rotates:

// front face (debug = pink)
mesh.addVertex({ -1, -1, -1, 0, 0, -1.0, 1.0, 0.0, 1.0 }); // 0
mesh.addVertex({ 1, -1, -1, 0, 0, -1.0, 1.0, 0.0, 1.0 }); // 1
mesh.addVertex({ -1, 1, -1, 0, 0, -1.0, 1.0, 0.0, 1.0 }); // 2
mesh.addVertex({ 1, 1, -1, 0, 0, -1.0, 1.0, 0.0, 1.0 }); // 3

mesh.addTriangle(0, 1, 2);
mesh.addTriangle(3, 2, 1);

// back face (debug = red)
mesh.addVertex({ -1, -1, 1, 0, 0, 1.0, 1.0, 0.0, 0.0 }); // 4
mesh.addVertex({ 1, -1, 1, 0, 0, 1.0, 1.0, 0.0, 0.0 }); // 5
mesh.addVertex({ -1, 1, 1, 0, 0, 1.0, 1.0, 0.0, 0.0 }); // 6
mesh.addVertex({ 1, 1, 1, 0, 0, 1.0, 1.0, 0.0, 0.0 }); // 7

mesh.addTriangle(4, 5, 6);
mesh.addTriangle(7, 6, 5);

// left face (debug = green)
mesh.addVertex({ -1, -1, 1, -1.0, 0, 0, 0.0, 1.0, 0.0 }); // 8
mesh.addVertex({ -1, -1, -1, -1.0, 0, 0, 0.0, 1.0, 0.0 }); // 9
mesh.addVertex({ -1, 1, 1, 0, -1.0, 0, 0.0, 1.0, 0.0 }); // 10
mesh.addVertex({ -1, 1, -1, 0, -1.0, 0, 0.0, 1.0, 0.0 }); // 11

mesh.addTriangle(8, 9, 10);
mesh.addTriangle(11, 10, 9);

// right face (debug = yellow)
mesh.addVertex({ 1, -1, -1, 1.0, 0, 0, 1.0, 1.0, 0.0 }); // 12
mesh.addVertex({ 1, -1, 1, 1.0, 0, 0, 1.0, 1.0, 0.0 }); // 13
mesh.addVertex({ 1, 1, -1, 1.0, 0, 0, 1.0, 1.0, 0.0 }); // 14
mesh.addVertex({ 1, 1, 1, 1.0, 0, 0, 1.0, 1.0, 0.0 }); // 15

mesh.addTriangle(12, 13, 14);
mesh.addTriangle(15, 14, 13);

// bottom face = cyan
mesh.addVertex({ -1, 1, 1, 0, 1.0, 0, 0.0, 1.0, 1.0 }); // 16
mesh.addVertex({ 1, 1, 1, 0, 1.0, 0, 0.0, 1.0, 1.0 }); // 17
mesh.addVertex({ -1, 1, -1, 0, 1.0, 0, 0.0, 1.0, 1.0 }); // 18
mesh.addVertex({ 1, 1, -1, 0, 1.0, 0, 0.0, 1.0, 1.0 }); // 19

mesh.addTriangle(16, 17, 18);
mesh.addTriangle(19, 18, 17);

// top face = blue
mesh.addVertex({ -1, -1, 1, 0, -1.0, 0, 0.0, 0.0, 1.0 }); // 20
mesh.addVertex({ 1, -1, 1, 0, -1.0, 0, 0.0, 0.0, 1.0 }); // 21
mesh.addVertex({ -1, -1, -1, 0, -1.0, 0, 0.0, 0.0, 1.0 }); // 22
mesh.addVertex({ 1, -1, -1, 0, -1.0, 0, 0.0, 0.0, 1.0 }); // 23

mesh.addTriangle(20, 21, 22);
mesh.addTriangle(23, 22, 21);

Here is a video of what happens:

Broken cube

I thought that maybe it is related to depth buffer that I have but everything seems logical here:

VkAttachmentDescription depthAttachment{};
depthAttachment.flags = 0;
depthAttachment.format = swapchain.getDepthFormat();
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depthAttachment.finalLayout =
  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;

VkAttachmentReference depthAttachmentRef{};
depthAttachmentRef.attachment = 1;
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;

Here are depth attachments attached to render pass and I am clearning the depth stencil on every render:

VkClearValue clearValue;
clearValue.color = {{0.0f, 0.0f, 0.0f, 1.0f}};
VkClearValue depthClear;
depthClear.depthStencil.depth = 1.0f;

VkClearValue clearValues[] = {clearValue, depthClear};

VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = renderPass;
renderPassInfo.framebuffer = framebuffers[imageIdx];
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = swapchain.getExtent();
renderPassInfo.clearValueCount = 2;
renderPassInfo.pClearValues = clearValues;

I do not understand why this is happening to the geometry with more vertices but not to the old geometry with less vertices. Is there some type of culling happening that I am not aware of?

EDIT: Found the problem. Some faces in my vertices had to be reordered.

\$\endgroup\$
4
  • \$\begingroup\$ You're far more likely to get a fast and accurate answer on the Khronos Vulkan forums than you are here. \$\endgroup\$
    – Engineer
    Jun 27 at 15:07
  • \$\begingroup\$ I have found the reason, which posed another question for me. Do you think I should edit the question or create a new port? \$\endgroup\$
    – Gasim
    Jun 27 at 16:18
  • \$\begingroup\$ Edit the question if you no longer need answers to this one. \$\endgroup\$
    – DMGregory
    Jun 27 at 17:01
  • \$\begingroup\$ They aren't being culled. It looks like your vertex indices are just wrong. \$\endgroup\$
    – user253751
    Jul 2 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.