Vulkan发布有段时间,经过搜索,暂未发现有用的引导性文章,C++ 版demo教程、例子,我也找了很长的一段时间包括官方的教程,可惜官方的教程有短板。
系统要求
建议winX64 vs2015,作者自述支持Linux android,编译器必须支持c++11,笔者只在win系统做过试验,建议更新最新的显卡驱动。近两年的显卡支持上没问题。
源代码下载
github的下载地址,建议用git 检出,以后方便升级
https://github.com/SaschaWillems/Vulkan/archive/master.zip
游戏引擎支持
unity3d 、Unreal Engine 4、OGRE(图形引擎计划),Urho3D(以后支持)
编译
打开vulkanExamples.sln,设置项目triangle为启动项,按F5,经典三角形渲染
源代码简单解析
分辨率设置
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { width = 1280; height = 720; zoom = -2.5f; title = "Vulkan Example - Basic indexed triangle"; // Values not set here are initialized in the base class constructor }
矩阵变换,和GL一样,用了GLM的数学库, MVP(模型矩阵 视图矩阵 投影矩阵)
void updateUniformBuffers() { // Update matrices uboVS.projectionMatrix = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f); uboVS.viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom)); uboVS.modelMatrix = glm::mat4(); uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); // Map uniform buffer and update it uint8_t *pData; VK_CHECK_RESULT(vkMapMemory(device, uniformDataVS.memory, 0, sizeof(uboVS), 0, (void **)&pData)); memcpy(pData, &uboVS, sizeof(uboVS)); vkUnmapMemory(device, uniformDataVS.memory); }
渲染
virtual void render() { if (!prepared) return; draw(); }