Skip to content

Commit 5872d10

Browse files
committed
Implemented grid construction for vertices and indices
1 parent d93364f commit 5872d10

File tree

2 files changed

+94
-10
lines changed

2 files changed

+94
-10
lines changed

renderer.cpp

+78-9
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,41 @@
3434
#include <glm/gtc/type_ptr.hpp>
3535
#include <glm/ext.hpp> //"glm/gtx/string_cast.hpp"
3636

37-
float vertexInfo[][3] = {
37+
float vertexInfo2[][3] = {
3838
{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f},
3939
{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f},
4040
{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f},
4141
{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}
4242
};
4343

44-
const uint16_t indices[] = {
45-
0, 1, 2, 2, 3, 0
44+
const uint16_t indices2[] = {
45+
0, 1, 2, 2, 3, 1
4646
};
4747

4848
Renderer::Renderer(VkExtent2D& extent, VkPhysicalDeviceMemoryProperties& memProps)
4949
: imageExtent(extent),
5050
memProperties(memProps)
5151
{
52+
numVertsX = 7;
53+
numVertsY = 7;
54+
55+
numVerts = numVertsX * numVertsY;
56+
numPrims = (numVertsX - 1) * (numVertsY - 1) * 2;
57+
58+
uint32_t numComponents = 3;
59+
60+
uint32_t vertInfoSize = sizeof(float)*numComponents*numVerts*2;
61+
62+
mat4Size = vertInfoSize;
63+
64+
vertexInfo = static_cast<float*>(malloc(vertInfoSize));
65+
66+
numIndices = numPrims * 3;
67+
68+
indicesBufferSize = sizeof(uint16_t)*numIndices;
69+
70+
indices = static_cast<uint16_t*>(malloc(indicesBufferSize));
71+
5272
framebuffers = static_cast<VkFramebuffer*>(malloc(sizeof(VkFramebuffer)*numFBOs));
5373
drawCommandBuffers = static_cast<VkCommandBuffer*>(malloc(sizeof(VkCommandBuffer)*numDrawCmdBuffers));
5474
attributeDescriptions = static_cast<VkVertexInputAttributeDescription*>(malloc(sizeof(VkVertexInputAttributeDescription)*numAttrDesc));
@@ -59,6 +79,8 @@ Renderer::~Renderer()
5979
free(framebuffers);
6080
free(drawCommandBuffers);
6181
free(attributeDescriptions);
82+
free(vertexInfo);
83+
free(indices);
6284
}
6385

6486
bool Renderer::Init(VkDevice& device, const VkFormat& surfaceFormat, const VkImageView* imageViews, uint32_t queueFamilyId)
@@ -70,6 +92,53 @@ bool Renderer::Init(VkDevice& device, const VkFormat& surfaceFormat, const VkIma
7092
size_t fragmentShaderFileSize;
7193
char* fragmentShader;
7294

95+
float delta = 0.5f;
96+
uint32_t index = 0;
97+
float xStartPos = - ((numVertsX - 1) * delta) / 2;
98+
float yStartPos = - ((numVertsY - 1) * delta) / 2;
99+
float xPos = xStartPos;
100+
float yPos = yStartPos;
101+
float zPos = 0.0f;
102+
103+
for (uint32_t i = 0; i < numVertsY; ++i)
104+
{
105+
for (uint32_t j = 0; j < numVertsX; ++j)
106+
{
107+
vertexInfo[index] = xPos;
108+
vertexInfo[index + 1] = yPos;
109+
vertexInfo[index + 2] = zPos;
110+
111+
vertexInfo[index + 3] = 0.0f;
112+
vertexInfo[index + 4] = 0.0f;
113+
vertexInfo[index + 5] = 1.0f;
114+
115+
index += 6;
116+
xPos += delta;
117+
}
118+
119+
xPos = xStartPos;
120+
121+
yPos += delta;
122+
}
123+
124+
index = 0;
125+
126+
for (uint32_t i = 0; i < numVertsY - 1; ++i)
127+
{
128+
for (uint32_t j = 0; j < numVertsX - 1; ++j)
129+
{
130+
indices[index] = i * numVertsX + j;
131+
indices[index + 1] = indices[index] + 1;
132+
indices[index + 2] = indices[index] + numVertsX;
133+
134+
indices[index + 3] = indices[index + 2];
135+
indices[index + 4] = indices[index + 3] + 1;
136+
indices[index + 5] = indices[index + 1];
137+
138+
index += 6;
139+
}
140+
}
141+
73142
SetupServerSideVertexBuffer(device);
74143

75144
SetupIndexBuffer(device);
@@ -172,7 +241,7 @@ bool Renderer::Init(VkDevice& device, const VkFormat& surfaceFormat, const VkIma
172241
rasterizer.rasterizerDiscardEnable = VK_FALSE;
173242
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
174243
rasterizer.lineWidth = 1.0f;
175-
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
244+
//rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
176245
rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
177246
rasterizer.depthBiasEnable = VK_FALSE;
178247
//rasterizer.depthBiasConstantFactor = 0.0f;
@@ -440,7 +509,7 @@ void Renderer::ConstructFrames()
440509
vkCmdBindVertexBuffers(drawCommandBuffers[i], 0, 1, &vertexBuffer, offsets);
441510
vkCmdBindIndexBuffer(drawCommandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16);
442511

443-
vkCmdDrawIndexed(drawCommandBuffers[i], sizeof(indices), 1, 0, 0, 0);
512+
vkCmdDrawIndexed(drawCommandBuffers[i], numIndices, 1, 0, 0, 0);
444513

445514
vkCmdEndRenderPass(drawCommandBuffers[i]);
446515

@@ -615,7 +684,7 @@ bool Renderer::SetupClientSideVertexBuffer(VkDevice& device)
615684

616685
bool Renderer::SetupServerSideVertexBuffer(VkDevice& device)
617686
{
618-
VkDeviceSize size = sizeof(vertexInfo);
687+
VkDeviceSize size = mat4Size; //sizeof(vertexInfo);
619688

620689
VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
621690
VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
@@ -679,10 +748,10 @@ VkCommandBuffer& Renderer::TransferStaticBuffers(VkDevice& device)
679748
//copyRegion.srcOffset = 0;
680749
//copyRegion.dstOffset = 0;
681750

682-
copyRegion.size = sizeof(vertexInfo);
751+
copyRegion.size = mat4Size;//sizeof(vertexInfo);
683752
vkCmdCopyBuffer(staticTransferCommandBuffer, vertexTransferBuffer, vertexBuffer, 1, &copyRegion);
684753

685-
copyRegion.size = sizeof(indices);
754+
copyRegion.size = indicesBufferSize;
686755
vkCmdCopyBuffer(staticTransferCommandBuffer, indexTransferBuffer, indexBuffer, 1, &copyRegion);
687756

688757
vkEndCommandBuffer(staticTransferCommandBuffer);
@@ -716,7 +785,7 @@ VkCommandBuffer& Renderer::TransferDynamicBuffers(VkDevice& device)
716785

717786
bool Renderer::SetupIndexBuffer(VkDevice& device)
718787
{
719-
VkDeviceSize size = sizeof(indices);
788+
VkDeviceSize size = indicesBufferSize;
720789

721790
VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
722791
VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;

renderer.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,22 @@ class Renderer
9494

9595
//float mvp[4][4] = {};
9696
glm::mat4 mvp;
97-
const uint32_t mat4Size = sizeof(float[4][4]);
97+
//const uint32_t mat4Size = sizeof(float[4][4]);
98+
99+
uint32_t mat4Size;
100+
101+
float* vertexInfo;
102+
103+
uint16_t* indices;
104+
uint16_t numIndices;
105+
uint32_t indicesBufferSize;
106+
107+
uint32_t numVertsX;
108+
uint32_t numVertsY;
109+
110+
uint32_t numVerts;
111+
112+
uint32_t numPrims;
98113
};
99114

100115
#endif

0 commit comments

Comments
 (0)