Home > Blockchain >  Vulkan vertex drawing order
Vulkan vertex drawing order

Time:07-30

I am currently getting into vulkan and am now at the point where I want to draw a qube with perspective projection. But the drawing order of the faces doesnt seem to woek right.

qube

This is the depth stencil info of my pipeline

const auto depth_stencil_state_create_info = VkPipelineDepthStencilStateCreateInfo{
      .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
      .pNext = nullptr,
      .flags = 0,
      .depthTestEnable = true,
      .depthWriteEnable = true,
      .depthCompareOp = VK_COMPARE_OP_LESS,
      .depthBoundsTestEnable = false,
      .stencilTestEnable = false,
      .front = VkStencilOpState{},
      .back = VkStencilOpState{},
      .minDepthBounds = 0.0f,
      .maxDepthBounds = 1.0f
    };

And the rasterization into

const auto rasterization_stage_create_info = VkPipelineRasterizationStateCreateInfo {
      .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
      .pNext = nullptr,
      .flags = 0,
      .depthClampEnable = false,
      .rasterizerDiscardEnable = false,
      .polygonMode = VK_POLYGON_MODE_FILL,
      .cullMode = VK_CULL_MODE_NONE,
      .frontFace = VK_FRONT_FACE_CLOCKWISE,
      .depthBiasEnable = false,
      .depthBiasConstantFactor = 0.0f,
      .depthBiasClamp = 0.0f,
      .depthBiasSlopeFactor = 0.0f,
      .lineWidth = 1.0f
    };

I am using a vertex struct with a glm::vec3 for position and color.

Those are my vertices

const auto vertices = std::vector<vertex>{
      // left face (white)
      {{-.5f, -.5f, -.5f}, {.9f, .9f, .9f}},
      {{-.5f, .5f, .5f}, {.9f, .9f, .9f}},
      {{-.5f, -.5f, .5f}, {.9f, .9f, .9f}},
      {{-.5f, .5f, -.5f}, {.9f, .9f, .9f}},
 
      // right face (yellow)
      {{.5f, -.5f, -.5f}, {.8f, .8f, .1f}},
      {{.5f, .5f, .5f}, {.8f, .8f, .1f}},
      {{.5f, -.5f, .5f}, {.8f, .8f, .1f}},
      {{.5f, .5f, -.5f}, {.8f, .8f, .1f}},
 
      // top face (orange, remember y axis points down)
      {{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
      {{.5f, -.5f, .5f}, {.9f, .6f, .1f}},
      {{-.5f, -.5f, .5f}, {.9f, .6f, .1f}},
      {{.5f, -.5f, -.5f}, {.9f, .6f, .1f}},
 
      // bottom face (red)
      {{-.5f, .5f, -.5f}, {.8f, .1f, .1f}},
      {{.5f, .5f, .5f}, {.8f, .1f, .1f}},
      {{-.5f, .5f, .5f}, {.8f, .1f, .1f}},
      {{.5f, .5f, -.5f}, {.8f, .1f, .1f}},
 
      // nose face (blue)
      {{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
      {{.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
      {{-.5f, .5f, 0.5f}, {.1f, .1f, .8f}},
      {{.5f, -.5f, 0.5f}, {.1f, .1f, .8f}},
 
      // tail face (green)
      {{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}},
      {{.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
      {{-.5f, .5f, -0.5f}, {.1f, .8f, .1f}},
      {{.5f, -.5f, -0.5f}, {.1f, .8f, .1f}}
    };

And my indices

const auto indices = std::vector<sbx::uint32>{
      0,  1,  2,
      0,  3,  1,
      4,  5,  6,
      4,  7,  5,
      8,  9,  10,
      8,  11, 9,
      12, 13, 14, 
      12, 15, 13, 
      16, 17, 18,
      16, 19, 17,
      20, 21, 22,
      20, 23, 21
    };

Do you have any idears or hints where I sould look to solve this behaviour? Has this even to do with the pipeline or am I on the wrong path?

Edit: Might that be a problem caused by vulkans coordiante system? And if so, how would I fix it?

CodePudding user response:

Your problem has nothing to do with vertex order since you're not culling backfaces:

  .cullMode = VK_CULL_MODE_NONE,

If you're trying to avoid setting up a depth test by just drawing front faces, you need to change this to

  .cullMode = VK_CULL_MODE_BACK_BIT,

and then you'll either be able to see only the front faces, and won't have this overlapping issue. However, if your triangle winding is incorrect, the cube will look inverted.

  • Related