////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#define GLAD_VULKAN_IMPLEMENTATION
#include <vulkan.h>
// Include graphics because we use sf::Image for loading images
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <vector>
#include <limits>
#include <cstring>
#include <cmath>
////////////////////////////////////////////////////////////
// Helper functions
////////////////////////////////////////////////////////////
namespace
{
typedef float Vec3[3];
typedef float Matrix[4][4];
// Multiply 2 matrices
void matrixMultiply(Matrix& result, const Matrix& left, const Matrix& right)
{
Matrix temp;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
temp[i][j] = left[0][j] * right[i][0] + left[1][j] * right[i][1] + left[2][j] * right[i][2] + left[3][j] * right[i][3];
}
std::memcpy(result, temp, sizeof(Matrix));
}
// Rotate a matrix around the x-axis
void matrixRotateX(Matrix& result, float angle)
{
Matrix matrix = {
{1.f, 0.f, 0.f, 0.f},
{0.f, std::cos(angle), std::sin(angle), 0.f},
{0.f, -std::sin(angle), std::cos(angle), 0.f},
{0.f, 0.f, 0.f, 1.f}
};
matrixMultiply(result, result, matrix);
}
// Rotate a matrix around the y-axis
void matrixRotateY(Matrix& result, float angle)
{
Matrix matrix = {
{ std::cos(angle), 0.f, std::sin(angle), 0.f},
{ 0.f, 1.f, 0.f, 0.f},
{-std::sin(angle), 0.f, std::cos(angle), 0.f},
{ 0.f, 0.f, 0.f, 1.f}
};
matrixMultiply(result, result, matrix);
}
// Rotate a matrix around the z-axis
void matrixRotateZ(Matrix& result, float angle)
{
Matrix matrix = {
{ std::cos(angle), std::sin(angle), 0.f, 0.f},
{-std::sin(angle), std::cos(angle), 0.f, 0.f},
{ 0.f, 0.f, 1.f, 0.f},
{ 0.f, 0.f, 0.f, 1.f}
};
matrixMultiply(result, result, matrix);
}
// Construct a lookat view matrix
void matrixLookAt(Matrix& result, const Vec3& eye, const Vec3& center, const Vec3& up)
{
// Forward-looking vector
Vec3 forward = {
center[0] - eye[0],
center[1] - eye[1],
center[2] - eye[2]
};
// Normalize
float factor = 1.0f / std::sqrt(forward[0] * forward[0] + forward[1] * forward[1] + forward[2] * forward[2]);
for(int i = 0; i < 3; i++)
forward[i] = forward[i] * factor;
// Side vector (Forward cross product Up)
Vec3 side = {
forward[1] * up[2] - forward[2] * up[1],
forward[2] * up[0] - forward[0] * up[2],
forward[0] * up[1] - forward[1] * up[0]
};
// Normalize
factor = 1.0f / std::sqrt(side[0] * side[0] + side[1] * side[1] + side[2] * side[2]);
for(int i = 0; i < 3; i++)
side[i] = side[i] * factor;
result[0][0] = side[0];
result[0][1] = side[1] * forward[2] - side[2] * forward[1];
result[0][2] = -forward[0];
result[0][3] = 0.f;
result[1][0] = side[1];
result[1][1] = side[2] * forward[0] - side[0] * forward[2];
result[1][2] = -forward[1];
result[1][3] = 0.f;
result[2][0] = side[2];
result[2][1] = side[0] * forward[1] - side[1] * forward[0];
result[2][2] = -forward[2];
result[2][3] = 0.f;
result[3][0] = (-eye[0]) * result[0][0] + (-eye[1]) * result[1][0] + (-eye[2]) * result[2][0];
result[3][1] = (-eye[0]) * result[0][1] + (-eye[1]) * result[1][1] + (-eye[2]) * result[2][1];
result[3][2] = (-eye[0]) * result[0][2] + (-eye[1]) * result[1][2] + (-eye[2]) * result[2][2];
result[3][3] = (-eye[0]) * result[0][3] + (-eye[1]) * result[1][3] + (-eye[2]) * result[2][3] + 1.0f;
}
// Construct a perspective projection matrix
void matrixPerspective(Matrix& result, float fov, float aspect, float nearPlane, float farPlane)
{
const float a = 1.f / std::tan(fov / 2.f);
result[0][0] = a / aspect;
result[0][1] = 0.f;
result[0][2] = 0.f;
result[0][3] = 0.f;
result[1][0] = 0.f;
result[1][1] = -a;
result[1][2] = 0.f;
result[1][3] = 0.f;
result[2][0] = 0.f;
result[2][1] = 0.f;
result[2][2] = -((farPlane + nearPlane) / (farPlane - nearPlane));
result[2][3] = -1.f;
result[3][0] = 0.f;
result[3][1] = 0.f;
result[3][2] = -((2.f * farPlane * nearPlane) / (farPlane - nearPlane));
result[3][3] = 0.f;
}
// Clamp a value between low and high values
template<typename T>
T clamp(T value, T low, T high)
{
return (value <= low) ? low : ((value >= high) ? high : value);
}
// Helper function we pass to GLAD to load Vulkan functions via SFML
GLADapiproc getVulkanFunction(const char* name)
{
return sf::Vulkan::getFunction(name);
}
// Debug we pass to Vulkan to call when it detects warnings or errors
VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugReportFlagsEXT, VkDebugReportObjectTypeEXT, uint64_t, size_t, int32_t, const char*, const char* pMessage, void*)
{
sf::err() << pMessage << std::endl;
return VK_FALSE;
}
}
////////////////////////////////////////////////////////////
// VulkanExample class
////////////////////////////////////////////////////////////
class VulkanExample
{
public:
// Constructor
VulkanExample() :
window(sf::VideoMode(800, 600), "SFML window with Vulkan", sf::Style::Default),
vulkanAvailable(sf::Vulkan::isAvailable()),
maxFramesInFlight(2),
currentFrame(0),
swapchainOutOfDate(false),
instance(0),
debugReportCallback(0),
surface(0),
gpu(0),
queueFamilyIndex(-1),
device(0),
queue(0),
swapchainFormat(),
swapchainExtent(),
swapchain(0),
depthFormat(VK_FORMAT_UNDEFINED),
depthImage(0),
depthImageMemory(0),
depthImageView(0),
vertexShaderModule(0),
fragmentShaderModule(0),
descriptorSetLayout(0),
pipelineLayout(0),
renderPass(0),
graphicsPipeline(0),
commandPool(0),
vertexBuffer(0),
vertexBufferMemory(0),
indexBuffer(0),
indexBufferMemory(0),
textureImage(0),
textureImageMemory(0),
textureImageView(0),
textureSampler(0),
descriptorPool(0)
{
// Vulkan setup procedure
if (vulkanAvailable) setupInstance();
if (vulkanAvailable) setupDebugReportCallback();
if (vulkanAvailable) setupSurface();
if (vulkanAvailable) setupPhysicalDevice();
if (vulkanAvailable) setupLogicalDevice();
if (vulkanAvailable) setupSwapchain();
if (vulkanAvailable) setupSwapchainImages();
if (vulkanAvailable) setupShaders();
if (vulkanAvailable) setupRenderpass();
if (vulkanAvailable) setupDescriptorSetLayout();
if (vulkanAvailable) setupPipelineLayout();
if (vulkanAvailable) setupPipeline();
if (vulkanAvailable) setupCommandPool();
if (vulkanAvailable) setupVertexBuffer();
if (vulkanAvailable) setupIndexBuffer();
if (vulkanAvailable) setupUniformBuffers();
if (vulkanAvailable) setupDepthImage();
if (vulkanAvailable) setupDepthImageView();
if (vulkanAvailable) setupTextureImage();
if (vulkanAvailable) setupTextureImageView();
if (vulkanAvailable) setupTextureSampler();
if (vulkanAvailable) setupFramebuffers();
if (vulkanAvailable) setupDescriptorPool();
if (vulkanA
没有合适的资源?快使用搜索试试~ 我知道了~
C++实现蜂群涌现效果(flocking)
共1055个文件
html:419个
js:155个
hpp:101个
2 下载量 195 浏览量
2023-09-02
21:33:55
上传
评论
收藏 37.84MB ZIP 举报
温馨提示
C++实现蜂群涌现效果(flocking)
资源推荐
资源详情
资源评论
收起资源包目录
C++实现蜂群涌现效果(flocking) (1055个子文件)
Flocking.vcxprojResolveAssemblyReference.cache 713B
SFMLConfig.cmake 6KB
SFMLStaticTargets.cmake 6KB
SFMLSharedTargets.cmake 6KB
SFMLConfigDependencies.cmake 4KB
SFMLSharedTargets-release.cmake 3KB
SFMLSharedTargets-debug.cmake 3KB
SFMLStaticTargets-release.cmake 3KB
SFMLStaticTargets-debug.cmake 3KB
SFMLConfigVersion.cmake 3KB
Vulkan.cpp 98KB
Island.cpp 21KB
Shader.cpp 15KB
Tennis.cpp 11KB
OpenGL.cpp 10KB
Boid.cpp 10KB
Joystick.cpp 7KB
AviFile.cpp 7KB
Ftp.cpp 7KB
Server.cpp 7KB
main.cpp 6KB
Window.cpp 5KB
Win32.cpp 4KB
Client.cpp 4KB
Sound.cpp 3KB
SoundCapture.cpp 3KB
TCP.cpp 3KB
Flock.cpp 3KB
UDP.cpp 2KB
NewVector.cpp 1KB
Sockets.cpp 1KB
VoIP.cpp 1KB
Path.cpp 1KB
doxygen.css 24KB
search.css 5KB
tabs.css 1KB
searchOverrides.css 898B
CMPM265-Flocking.VC.db 31.04MB
sfml-graphics-d-2.dll 1.91MB
sfml-graphics-d-2.dll 1.91MB
sfml-audio-d-2.dll 1.59MB
sfml-audio-d-2.dll 1.59MB
sfml-audio-2.dll 1MB
sfml-audio-2.dll 1MB
sfml-graphics-2.dll 892KB
sfml-graphics-2.dll 892KB
openal32.dll 654KB
openal32.dll 654KB
sfml-window-d-2.dll 510KB
sfml-window-d-2.dll 510KB
sfml-network-d-2.dll 430KB
sfml-network-d-2.dll 430KB
sfml-system-d-2.dll 234KB
sfml-system-d-2.dll 234KB
sfml-window-2.dll 145KB
sfml-window-2.dll 145KB
sfml-network-2.dll 131KB
sfml-network-2.dll 131KB
sfml-system-2.dll 50KB
sfml-system-2.dll 50KB
tennis.exe 1.78MB
sound.exe 1007KB
sound-capture.exe 1006KB
shader.exe 904KB
opengl.exe 887KB
island.exe 805KB
joystick.exe 792KB
win32.exe 325KB
CMPM265-Flocking.exe 220KB
vulkan.exe 212KB
CMPM265-Flocking.exe 199KB
window.exe 184KB
ftp.exe 79KB
voip.exe 57KB
sockets.exe 39KB
CMPM265-Flocking.vcxproj.filters 2KB
Flocking.vcxproj.filters 1KB
ding.flac 55KB
edge.frag 1KB
blur.frag 892B
terrain.frag 397B
shader.frag 362B
pixelate.frag 247B
billboard.frag 175B
blink.frag 151B
billboard.geom 1KB
vulkan.h 167KB
AviFile.h 3KB
GLToMovie.h 2KB
Boid.h 1KB
Flock.h 907B
Path.h 732B
NewVector.h 711B
Shader.hpp 37KB
Utf.hpp 35KB
Texture.hpp 29KB
String.hpp 25KB
Ftp.hpp 25KB
Keyboard.hpp 22KB
RenderTarget.hpp 20KB
共 1055 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
aspiretop
- 粉丝: 597
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功