Skip to content

Commit

Permalink
修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
luxiaodong committed Sep 9, 2022
1 parent 2c88984 commit e4f3ecc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
54 changes: 42 additions & 12 deletions Vulkan/Vulkan_Sample/Vulkan/common/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Text::Text(VkFormat colorformat, VkFormat depthformat, uint32_t framebufferWidth

Text::~Text()
{

}

void Text::init()
Expand Down Expand Up @@ -45,9 +46,9 @@ void Text::clear()

void Text::prepareResources()
{
const uint32_t fontWidth = STB_FONT_consolas_24_latin1_BITMAP_WIDTH;
const uint32_t fontHeight = STB_FONT_consolas_24_latin1_BITMAP_WIDTH;
static unsigned char font24pixels[fontWidth][fontHeight];
const uint32_t fontWidth = STB_FONT_consolas_24_latin1_BITMAP_WIDTH;
const uint32_t fontHeight = STB_FONT_consolas_24_latin1_BITMAP_HEIGHT;
static unsigned char font24pixels[fontHeight][fontWidth];
stb_font_consolas_24_latin1(m_stbFontData, font24pixels, fontHeight);

unsigned char* fontData = &font24pixels[0][0];
Expand Down Expand Up @@ -239,13 +240,13 @@ void Text::createRenderPass()

void Text::createPipeline()
{
std::array<VkVertexInputBindingDescription, 2> vertexInputBindings = {};
std::array<VkVertexInputBindingDescription, 1> vertexInputBindings = {};
vertexInputBindings[0] = Tools::getVertexInputBindingDescription(0, sizeof(glm::vec4));
vertexInputBindings[1] = Tools::getVertexInputBindingDescription(1, sizeof(glm::vec4));
// vertexInputBindings[1] = Tools::getVertexInputBindingDescription(0, sizeof(glm::vec4));

std::array<VkVertexInputAttributeDescription, 2> vertexInputAttributes = {};
vertexInputAttributes[0] = Tools::getVertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32_SFLOAT, 0);
vertexInputAttributes[1] = Tools::getVertexInputAttributeDescription(1, 1, VK_FORMAT_R32G32_SFLOAT, sizeof(glm::vec2));
vertexInputAttributes[1] = Tools::getVertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32_SFLOAT, sizeof(glm::vec2));

VkPipelineVertexInputStateCreateInfo vertexInput = {};
vertexInput.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Expand All @@ -270,7 +271,7 @@ void Text::createPipeline()
};

VkPipelineDynamicStateCreateInfo dynamic = Tools::getPipelineDynamicStateCreateInfo(dynamicStates);
VkPipelineRasterizationStateCreateInfo rasterization = Tools::getPipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE);
VkPipelineRasterizationStateCreateInfo rasterization = Tools::getPipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE);
VkPipelineMultisampleStateCreateInfo multisample = Tools::getPipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT);
VkPipelineDepthStencilStateCreateInfo depthStencil = Tools::getPipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);

Expand Down Expand Up @@ -307,12 +308,12 @@ void Text::begin()
m_numLetter = 0;
}

void Text::addString(std::string str, float x, float y, TextAlign align)
void Text::addString(std::string str, float x, float y, float sx, float sy, TextAlign align)
{
const uint32_t firstChar = STB_FONT_consolas_24_latin1_FIRST_CHAR;

const float charW = 1.5f / m_framebufferWidth;
const float charH = 1.5f / m_framebufferHeight;
const float charW = sx / m_framebufferWidth;
const float charH = sy / m_framebufferHeight;

x = (x / m_framebufferWidth * 2.0f) - 1.0f;
y = (y / m_framebufferHeight * 2.0f) - 1.0f;
Expand All @@ -327,11 +328,11 @@ void Text::addString(std::string str, float x, float y, TextAlign align)

if(align == TextAlign::Center)
{
x -= textWidth;
x -= textWidth/2.0f;
}
else if(align == TextAlign::Right)
{
x -= textWidth/2.0f;
x -= textWidth;
}

// Generate a uv mapped quad per char in the new text
Expand Down Expand Up @@ -375,6 +376,35 @@ void Text::end()
mapped = nullptr;
}

void Text::test()
{
mapped->x = -1.0f;
mapped->y = -1.0f;
mapped->z = 0.0f;
mapped->w = 0.0f;
mapped++;

mapped->x = 1.0f;
mapped->y = -1.0f;
mapped->z = 1.0f;
mapped->w = 0.0f;
mapped++;

mapped->x = -1.0f;
mapped->y = 1.0f;
mapped->z = 0.0f;
mapped->w = 1.0f;
mapped++;

mapped->x = 1.0f;
mapped->y = 1.0f;
mapped->z = 1.0f;
mapped->w = 1.0f;
mapped++;

m_numLetter = 1;
}

void Text::updateCommandBuffers(const VkFramebuffer& frameBuffer)
{
VkCommandBufferBeginInfo beginInfo = {};
Expand Down
3 changes: 2 additions & 1 deletion Vulkan/Vulkan_Sample/Vulkan/common/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Text
void clear();

void begin();
void addString(std::string str, float x, float y, TextAlign align);
void addString(std::string str, float x, float y, float sx = 1.0f, float sy = 1.0f, TextAlign align = TextAlign::Center);
void test();
void end();
void updateCommandBuffers(const VkFramebuffer& frameBuffer);
void recordRenderCommand(const VkCommandBuffer& commandBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

Textoverlay::Textoverlay(std::string title) : Application(title)
{

}

Textoverlay::~Textoverlay()
Expand Down Expand Up @@ -194,12 +193,12 @@ void Textoverlay::prepareText()
glm::vec3 projected = glm::project(glm::vec3(0.0f), m_camera.m_viewMat, m_camera.m_projMat, glm::vec4(0, 0, (float)m_swapchainExtent.width, (float)m_swapchainExtent.height));

m_pText->begin();
m_pText->addString("A Cube", 5.0f, 5.0f, TextAlign::Center);
m_pText->addString("A Cube", projected.x, projected.y, 4, 4, TextAlign::Center);
m_pText->end();
}

void Textoverlay::createOtherRenderPass(const VkFramebuffer& frameBuffer)
{
// m_pText->updateCommandBuffers(frameBuffer);
m_pText->updateCommandBuffers(frameBuffer);
}

0 comments on commit e4f3ecc

Please sign in to comment.