Skip to content

Commit

Permalink
Sample6_3: 四种纹理拉伸方式
Browse files Browse the repository at this point in the history
  • Loading branch information
JYLongKong committed May 25, 2022
1 parent 290c96e commit 11c34a9
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 36 deletions.
Binary file added apk/Sample6_3.apk
Binary file not shown.
Binary file added app/src/main/assets/texture/robot0.bntex
Binary file not shown.
Binary file added app/src/main/assets/texture/robot1.bntex
Binary file not shown.
Binary file added app/src/main/assets/texture/robot2.bntex
Binary file not shown.
Binary file added app/src/main/assets/texture/robot3.bntex
Binary file not shown.
71 changes: 62 additions & 9 deletions app/src/main/cpp/bndev/MyVulkanManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ DrawableObjectCommon *MyVulkanManager::ballForDraw;
DrawableObjectCommon *MyVulkanManager::texTri;
float MyVulkanManager::zAngle = 0;

/// Sample6_3
DrawableObjectCommon *MyVulkanManager::texTri1;
DrawableObjectCommon *MyVulkanManager::texTri2;
int MyVulkanManager::samplerType = 0;
int MyVulkanManager::texType = 0;

/**
* 创建Vulkan实例的方法
*/
Expand Down Expand Up @@ -837,13 +843,31 @@ void MyVulkanManager::createDrawableObject() {
/// Sample5_9 **************************************************** end

/// Sample6_1 ************************************************** start
float *vdataIn = new float[15]{ // 顶点数据数组
0, 10, 0, 0.5, 0, // 第1个顶点的位置和纹理(x, y, z, s, t)
-9, -5, 0, 0, 1, // 第2个顶点的数据
9, -5, 0, 1, 1 // 第3个顶点的数据
};
texTri = new DrawableObjectCommon(vdataIn, 15 * 4, 3, device, memoryroperties); // 创建三角形绘制物体
// float *vdataIn = new float[15]{ // 顶点数据数组
// 0, 10, 0, 0.5, 0, // 第1个顶点的位置和纹理(x, y, z, s, t)
// -9, -5, 0, 0, 1, // 第2个顶点的数据
// 9, -5, 0, 1, 1 // 第3个顶点的数据
// };
// texTri = new DrawableObjectCommon(vdataIn, 15 * 4, 3, device, memoryroperties); // 创建三角形绘制物体
/// Sample6_1 **************************************************** end

/// Sample6_3 ************************************************** start
float *vdataIn = new float[30]{ // 顶点数据数组(x, y, z, s, t)
9, 9, 0, 4, 0, -9, 9, 0, 0, 0, -9, -9, 0, 0, 4, // 第1个三角形的数据
9, 9, 0, 4, 0, -9, -9, 0, 0, 4, 9, -9, 0, 4, 4 // 第2个三角形的数据
};
texTri = new DrawableObjectCommon(vdataIn, 30 * 4, 6, device, memoryroperties); // 创建绘制物体1
vdataIn = new float[30]{
9, 9, 0, 4, 0, -9, 9, 0, 0, 0, -9, -9, 0, 0, 2,
9, 9, 0, 4, 0, -9, -9, 0, 0, 2, 9, -9, 0, 4, 2
};
texTri1 = new DrawableObjectCommon(vdataIn, 30 * 4, 6, device, memoryroperties); // 创建绘制物体2
vdataIn = new float[30]{
9, 9, 0, 1, 0, -9, 9, 0, 0, 0, -9, -9, 0, 0, 1,
9, 9, 0, 1, 0, -9, -9, 0, 0, 1, 9, -9, 0, 1, 1
};
texTri2 = new DrawableObjectCommon(vdataIn, 30 * 4, 6, device, memoryroperties); // 创建绘制物体3
/// Sample6_3 **************************************************** end
}

/**
Expand Down Expand Up @@ -1303,13 +1327,42 @@ void MyVulkanManager::drawObject() {
/// Sample5_9 **************************************************** end

/// Sample6_1 ************************************************** start
// MatrixState3D::pushMatrix();
// MatrixState3D::rotate(yAngle, 0, 1, 0);
// MatrixState3D::rotate(zAngle, 0, 0, 1);
// texTri->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, // 绘制纹理三角形
// &(sqsCL->descSet[TextureManager::getVkDescriptorSetIndex("texture/wall.bntex")]));
// MatrixState3D::popMatrix();
/// Sample6_1 **************************************************** end

/// Sample6_3 ************************************************** start
MatrixState3D::pushMatrix();
MatrixState3D::rotate(yAngle, 0, 1, 0);
MatrixState3D::rotate(zAngle, 0, 0, 1);
texTri->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, // 绘制纹理三角形
&(sqsCL->descSet[TextureManager::getVkDescriptorSetIndex("texture/wall.bntex")]));
string textureName; // 当前纹理名称
switch (samplerType) {
case 0:textureName = "texture/robot0.bntex";
break;
case 1:textureName = "texture/robot1.bntex";
break;
case 2:textureName = "texture/robot2.bntex";
break;
case 3:textureName = "texture/robot3.bntex";
break;
default:break;
}
if (texType == 0) { // 采用4×4纹理坐标范围
texTri->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, // 绘制物体0
&(sqsCL->descSet[TextureManager::getVkDescriptorSetIndex(textureName)]));
} else if (texType == 1) { // 采用4×2纹理坐标范围
texTri1->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, // 绘制物体1
&(sqsCL->descSet[TextureManager::getVkDescriptorSetIndex(textureName)]));
} else if (texType == 2) { // 采用1×1纹理坐标范围
texTri2->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, // 绘制物体2
&(sqsCL->descSet[TextureManager::getVkDescriptorSetIndex(textureName)]));
}
MatrixState3D::popMatrix();
/// Sample6_1 **************************************************** end
/// Sample6_3 **************************************************** end

// triForDraw->drawSelf( // 绘制三色三角形、Sample4_14-卷绕和背面剪裁
// cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, &(sqsCL->descSet[0]));
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/cpp/bndev/MyVulkanManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ class MyVulkanManager {
/// Sample5_1 球
static DrawableObjectCommon *ballForDraw;

/// Sample6_1 纹理
static DrawableObjectCommon * texTri;
/// Sample6_1 纹理三角形
static DrawableObjectCommon *texTri;

/// Sample6_3 纹理拉伸
static DrawableObjectCommon *texTri1;
static DrawableObjectCommon *texTri2;
static int samplerType; // 当前使用的采样器索引(0~3)
static int texType; // 当前使用的纹理坐标范围索引(0~2)

static void init_vulkan_instance(); // 创建Vulkan实例
static void enumerate_vulkan_phy_devices(); // 初始化物理设备
Expand Down
31 changes: 22 additions & 9 deletions app/src/main/cpp/bndev/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ static int32_t engine_handle_input(struct android_app *app, AInputEvent *event)
/// Sample4_1 **************************************************** end

/// Sample4_2 ************************************************** start
xPre = x;
yPre = y;
/// Sample4_2 **************************************************** end

/// Sample4_7 ************************************************** start
// isClick = true;
// xPre = x;
// yPre = y;
/// Sample4_7 **************************************************** end
/// Sample4_2 **************************************************** end

/// Sample4_7、6_3 ********************************************** start
isClick = true;
xPre = x;
yPre = y;
/// Sample4_7、6_3 ************************************************ end

break;
case AMOTION_EVENT_ACTION_MOVE: // 触摸点移动
Expand Down Expand Up @@ -146,14 +146,17 @@ static int32_t engine_handle_input(struct android_app *app, AInputEvent *event)
// yPre = y;
/// Sample5_6 **************************************************** end

/// Sample6_1 ************************************************** start
/// Sample6_1、6_3 ********************************************** start
xDis = x - xPre;
yDis = y - yPre;
MyVulkanManager::yAngle += xDis * 180.0 / 600;
MyVulkanManager::zAngle += yDis * 180.0 / 600;
xPre = x;
yPre = y;
/// Sample6_1 **************************************************** end
if (abs((int) xDis) > 10 || abs((int) yDis) > 10) {
isClick = false;
}
/// Sample6_1、6_3 *********************************************** end

break;
case AMOTION_EVENT_ACTION_UP: // 触摸点抬起
Expand Down Expand Up @@ -185,6 +188,16 @@ static int32_t engine_handle_input(struct android_app *app, AInputEvent *event)
// }
/// Sample4_13 *************************************************** end

/// Sample6_3 ************************************************** start
if (isClick) {
if (x < MyVulkanManager::screenWidth / 2) { // 触控位置在屏幕左侧
MyVulkanManager::samplerType = (++MyVulkanManager::samplerType % 4);
} else { // 触控位置在屏幕右侧
MyVulkanManager::texType = (++MyVulkanManager::texType % 3);
}
}
/// Sample6_3 **************************************************** end

break;
default:break;
}
Expand Down
57 changes: 42 additions & 15 deletions app/src/main/cpp/util/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
#include "HelpFunction.h"
#include "FileUtil.h"

std::vector<std::string> TextureManager::texNames = {"texture/wall.bntex"};
//std::vector<std::string> TextureManager::texNames = {"texture/wall.bntex"};
std::vector<VkSampler> TextureManager::samplerList;
std::map<std::string, VkImage> TextureManager::textureImageList;
std::map<std::string, VkDeviceMemory> TextureManager::textureMemoryList;
std::map<std::string, VkImageView> TextureManager::viewTextureList;
std::map<std::string, VkDescriptorImageInfo> TextureManager::texImageInfoList;

/// Sample6_3
std::vector<std::string>TextureManager::texNames =
{"texture/robot0.bntex", "texture/robot1.bntex", "texture/robot2.bntex", "texture/robot3.bntex"};
std::map<std::string, int> TextureManager::imageSampler;

void setImageLayout(VkCommandBuffer cmd,
VkImage image,
VkImageAspectFlags aspectMask,
Expand Down Expand Up @@ -81,19 +86,39 @@ void TextureManager::initSampler(VkDevice &device, VkPhysicalDevice &gpu) {
samplerCreateInfo.magFilter = VK_FILTER_LINEAR; // 放大时的纹理采样方式
samplerCreateInfo.minFilter = VK_FILTER_NEAREST; // 缩小时的纹理采样方式
samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; // mipmap模式
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // 纹理S轴的拉伸方式
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // 纹理T轴的拉伸方式
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // 纹理W轴的拉伸方式
samplerCreateInfo.mipLodBias = 0.0; // mipmap时的Lod调整值
samplerCreateInfo.minLod = 0.0; // 最小Lod值
samplerCreateInfo.maxLod = 0.0; // 最大Lod值
samplerCreateInfo.anisotropyEnable = VK_FALSE; // 是否启用各向异性过滤
samplerCreateInfo.maxAnisotropy = 1; // 各向异性最大过滤值
samplerCreateInfo.compareEnable = VK_FALSE; // 是否开启比较功能
samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER; // 纹素数据比较操作
samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; // 要使用的预定义边框颜色

for (int i = 0; i < SAMPLER_COUNT; ++i) { // 循环创建指定数量的采样器
// samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // 纹理S轴的拉伸方式
// samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // 纹理T轴的拉伸方式
// samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // 纹理W轴的拉伸方式
/// Sample6_3 ************************************************** start
for (int i = 0; i < SAMPLER_COUNT; ++i) { // 循环设置各种拉伸方式
if (i == 0) { // 设置为重复拉伸方式
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
} else if (i == 1) { // 设置为截取拉伸方式
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
} else if (i == 2) { // 设置为镜像重复拉伸方式
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
} else if (i == 3) { // 设置为边框拉伸方式
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
}
/// Sample6_3 **************************************************** end
samplerCreateInfo.mipLodBias = 0.0; // mipmap时的Lod调整值
samplerCreateInfo.minLod = 0.0; // 最小Lod值
samplerCreateInfo.maxLod = 0.0; // 最大Lod值
samplerCreateInfo.anisotropyEnable = VK_FALSE; // 是否启用各向异性过滤
samplerCreateInfo.maxAnisotropy = 1; // 各向异性最大过滤值
samplerCreateInfo.compareEnable = VK_FALSE; // 是否开启比较功能
samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER; // 纹素数据比较操作
samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; // 要使用的预定义边框颜色

// for (int i = 0; i < SAMPLER_COUNT; ++i) { // 循环创建指定数量的采样器
VkSampler samplerTexture; // 声明采样器对象
VkResult result = vk::vkCreateSampler( // 创建采样器
device, &samplerCreateInfo, nullptr, &samplerTexture);
Expand Down Expand Up @@ -323,7 +348,8 @@ void TextureManager::init_SPEC_2D_Textures(

VkDescriptorImageInfo texImageInfo; // 构建图像描述信息实例
texImageInfo.imageView = viewTexture; // 采用的图像视图
texImageInfo.sampler = samplerList[0]; // 采用的采样器
// texImageInfo.sampler = samplerList[0]; // 采用的采样器
texImageInfo.sampler = samplerList[imageSampler[texName]]; // Sample6_3
texImageInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL; // 图像布局
texImageInfoList[texName] = texImageInfo; // 添加到纹理图像描述信息列表

Expand All @@ -337,6 +363,7 @@ void TextureManager::initTextures(VkDevice &device,
VkQueue &queueGraphics) {
initSampler(device, gpu); // 初始化采样器
for (int i = 0; i < texNames.size(); ++i) { // 遍历纹理文件名称列表
imageSampler[texNames[i]] = i; // Sample6_3-设置对应纹理的采样器索引
TexDataObject *ctdo = FileUtil::loadCommonTexData(texNames[i]); // 加载纹理文件数据
LOGI("%s: width=%d height=%d", texNames[i].c_str(), ctdo->width, ctdo->height); // 打印纹理数据信息
init_SPEC_2D_Textures( // 加载2D纹理
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/cpp/util/TextureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include "../vksysutil/vulkan_wrapper.h"
#include "TexDataObject.h"

#define SAMPLER_COUNT 1 // 采样器数量
//#define SAMPLER_COUNT 1 // 采样器数量
#define SAMPLER_COUNT 4 // Sample6_3-四种不同拉伸方式的采样器

class TextureManager {
public:
Expand All @@ -18,6 +19,7 @@ class TextureManager {
static std::map<std::string, VkDeviceMemory> textureMemoryList; // 纹理图像内存列表
static std::map<std::string, VkImageView> viewTextureList; // 纹理图像视图列表
static std::map<std::string, VkDescriptorImageInfo> texImageInfoList; // 纹理图像描述信息列表
static std::map<std::string, int> imageSampler; // Sample6_3-对应纹理的采样器索引

/**
* 加载所有纹理
Expand Down

0 comments on commit 11c34a9

Please sign in to comment.