修改纹理背景拉伸
This commit is contained in:
@@ -77,48 +77,118 @@ void TextureLoading::request_gpu_features(vkb::PhysicalDevice& gpu)
|
||||
}
|
||||
|
||||
|
||||
// 生成简单的测试图像数据(红绿蓝三色条)
|
||||
std::vector<uint8_t> generateSimpleTestImage(int width, int height, int* outRowStride = nullptr)
|
||||
{
|
||||
int rowStride = width * 4; // RGBA 每个像素4字节
|
||||
if (outRowStride)
|
||||
{
|
||||
*outRowStride = rowStride;
|
||||
|
||||
void hslToRgb(float h, float s, float l, uint8_t& r, uint8_t& g, uint8_t& b) {
|
||||
float c = (1 - std::abs(2 * l - 1)) * s;
|
||||
float x = c * (1 - std::abs(std::fmod(h / 60.0f, 2.0f) - 1));
|
||||
float m = l - c / 2.0f;
|
||||
|
||||
float r_, g_, b_;
|
||||
|
||||
if (h < 60) {
|
||||
r_ = c; g_ = x; b_ = 0;
|
||||
}
|
||||
else if (h < 120) {
|
||||
r_ = x; g_ = c; b_ = 0;
|
||||
}
|
||||
else if (h < 180) {
|
||||
r_ = 0; g_ = c; b_ = x;
|
||||
}
|
||||
else if (h < 240) {
|
||||
r_ = 0; g_ = x; b_ = c;
|
||||
}
|
||||
else if (h < 300) {
|
||||
r_ = x; g_ = 0; b_ = c;
|
||||
}
|
||||
else {
|
||||
r_ = c; g_ = 0; b_ = x;
|
||||
}
|
||||
|
||||
size_t dataSize = rowStride * height;
|
||||
std::vector<uint8_t> imageData(dataSize, 0);
|
||||
r = static_cast<uint8_t>((r_ + m) * 255);
|
||||
g = static_cast<uint8_t>((g_ + m) * 255);
|
||||
b = static_cast<uint8_t>((b_ + m) * 255);
|
||||
}
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
int pixelOffset = y * rowStride + x * 4;
|
||||
std::vector<uint8_t> generateSimpleTestImage(int width, int height, int cell_width) {
|
||||
std::vector<uint8_t> imageData(width * height * 4);
|
||||
|
||||
// 简单分成三个区域:红、绿、蓝
|
||||
if (x < width / 3)
|
||||
int gridCols = (width + cell_width - 1) / cell_width;
|
||||
int gridRows = (height + cell_width - 1) / cell_width;
|
||||
|
||||
// 存储每个单元格的颜色
|
||||
std::vector<std::vector<std::vector<uint8_t>>> cellColors(
|
||||
gridRows,
|
||||
std::vector<std::vector<uint8_t>>(
|
||||
gridCols,
|
||||
std::vector<uint8_t>(4)
|
||||
)
|
||||
);
|
||||
|
||||
// 为每个单元格生成不同的颜色
|
||||
for (int gridY = 0; gridY < gridRows; ++gridY) {
|
||||
for (int gridX = 0; gridX < gridCols; ++gridX) {
|
||||
// 使用网格坐标生成HSL颜色
|
||||
float hue = static_cast<float>(gridX + gridY * gridCols) / (gridCols * gridRows) * 360.0f;
|
||||
float saturation = 0.7f + 0.3f * static_cast<float>(gridX % 2); // 交替饱和度
|
||||
float lightness = 0.5f + 0.2f * static_cast<float>(gridY % 2); // 交替亮度
|
||||
|
||||
uint8_t r, g, b;
|
||||
hslToRgb(hue, saturation, lightness, r, g, b);
|
||||
|
||||
cellColors[gridY][gridX][0] = r;
|
||||
cellColors[gridY][gridX][1] = g;
|
||||
cellColors[gridY][gridX][2] = b;
|
||||
cellColors[gridY][gridX][3] = 255;
|
||||
if (gridY == 0 && gridX == 0)
|
||||
{
|
||||
// 红色区域
|
||||
imageData[pixelOffset] = 2; // R
|
||||
imageData[pixelOffset + 1] = 0; // G
|
||||
imageData[pixelOffset + 2] = 0; // B
|
||||
}
|
||||
else if (x < 2 * width / 3)
|
||||
{
|
||||
// 绿色区域
|
||||
imageData[pixelOffset] = 0; // R
|
||||
imageData[pixelOffset + 1] = 2; // G
|
||||
imageData[pixelOffset + 2] = 0; // B
|
||||
}
|
||||
else
|
||||
{
|
||||
// 蓝色区域
|
||||
imageData[pixelOffset] = 0; // R
|
||||
imageData[pixelOffset + 1] = 0; // G
|
||||
imageData[pixelOffset + 2] = 2; // B
|
||||
cellColors[gridY][gridX][0] = 0;
|
||||
cellColors[gridY][gridX][1] = 0;
|
||||
cellColors[gridY][gridX][2] = 0;
|
||||
cellColors[gridY][gridX][3] = 255;
|
||||
}
|
||||
|
||||
imageData[pixelOffset + 3] = 255; // A (完全不透明)
|
||||
if (gridY == 0 && gridX == gridCols-1)
|
||||
{
|
||||
cellColors[gridY][gridX][0] = 255;
|
||||
cellColors[gridY][gridX][1] = 0;
|
||||
cellColors[gridY][gridX][2] = 0;
|
||||
cellColors[gridY][gridX][3] = 255;
|
||||
}
|
||||
|
||||
if (gridY == gridRows-1 && gridX == 0)
|
||||
{
|
||||
cellColors[gridY][gridX][0] = 0;
|
||||
cellColors[gridY][gridX][1] = 0;
|
||||
cellColors[gridY][gridX][2] = 255;
|
||||
cellColors[gridY][gridX][3] = 255;
|
||||
}
|
||||
|
||||
if (gridY == gridRows - 1 && gridX == gridCols - 1)
|
||||
{
|
||||
cellColors[gridY][gridX][0] = 255;
|
||||
cellColors[gridY][gridX][1] = 255;
|
||||
cellColors[gridY][gridX][2] = 255;
|
||||
cellColors[gridY][gridX][3] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 填充像素数据
|
||||
for (int y = 0; y < height; ++y) {
|
||||
int gridY = y / cell_width;
|
||||
|
||||
for (int x = 0; x < width; ++x) {
|
||||
int gridX = x / cell_width;
|
||||
|
||||
if (gridY < gridRows && gridX < gridCols) {
|
||||
const uint8_t* color = cellColors[gridY][gridX].data();
|
||||
int index = (y * width + x) * 4;
|
||||
|
||||
imageData[index] = color[0];
|
||||
imageData[index + 1] = color[1];
|
||||
imageData[index + 2] = color[2];
|
||||
imageData[index + 3] = color[3];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -502,14 +572,14 @@ void TextureLoading::build_command_buffers()
|
||||
vkCmdBindPipeline(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.background);
|
||||
vkCmdDraw(draw_cmd_buffers[i], 6, 1, 0, 0);
|
||||
|
||||
vkCmdBindDescriptorSets(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout, 0, 1, &descriptor_set, 0, NULL);
|
||||
vkCmdBindPipeline(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.solid);
|
||||
//vkCmdBindDescriptorSets(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout, 0, 1, &descriptor_set, 0, NULL);
|
||||
//vkCmdBindPipeline(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.solid);
|
||||
|
||||
VkDeviceSize offsets[1] = { 0 };
|
||||
vkCmdBindVertexBuffers(draw_cmd_buffers[i], 0, 1, vertex_buffer->get(), offsets);
|
||||
vkCmdBindIndexBuffer(draw_cmd_buffers[i], index_buffer->get_handle(), 0, VK_INDEX_TYPE_UINT32);
|
||||
//VkDeviceSize offsets[1] = { 0 };
|
||||
//vkCmdBindVertexBuffers(draw_cmd_buffers[i], 0, 1, vertex_buffer->get(), offsets);
|
||||
//vkCmdBindIndexBuffer(draw_cmd_buffers[i], index_buffer->get_handle(), 0, VK_INDEX_TYPE_UINT32);
|
||||
|
||||
vkCmdDrawIndexed(draw_cmd_buffers[i], index_count, 1, 0, 0, 0);
|
||||
//vkCmdDrawIndexed(draw_cmd_buffers[i], index_count, 1, 0, 0, 0);
|
||||
|
||||
draw_point_cloud(draw_cmd_buffers[i]);
|
||||
|
||||
@@ -926,8 +996,8 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options)
|
||||
// --- 加载前景纹理 (示例) ---
|
||||
int width = 640;
|
||||
int height = 480;
|
||||
int rowStride;
|
||||
auto testImage = generateSimpleTestImage(width, height, &rowStride);
|
||||
int rowStride = width*4;
|
||||
auto testImage = generateSimpleTestImage(width, height, 80);
|
||||
size_t dataSize = testImage.size();
|
||||
std::cout << "Generated test image: " << width << "x" << height << std::endl;
|
||||
std::cout << "Row stride: " << rowStride << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user