Add directional light type
This commit is contained in:
parent
30fbd04678
commit
9ca2e3073c
4 changed files with 24 additions and 11 deletions
|
@ -2,8 +2,14 @@
|
|||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum class LightType {
|
||||
Point,
|
||||
Directional
|
||||
};
|
||||
|
||||
class Light {
|
||||
public:
|
||||
glm::vec3 position;
|
||||
LightType type = LightType::Point;
|
||||
glm::vec3 position = glm::vec3(0), direction = glm::vec3(0);
|
||||
glm::vec3 color = glm::vec3(1);
|
||||
};
|
||||
|
|
|
@ -7,7 +7,7 @@ layout(location = 2) in vec2 inUV;
|
|||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
struct Light {
|
||||
vec4 position;
|
||||
vec4 position, direction;
|
||||
vec3 color;
|
||||
};
|
||||
|
||||
|
@ -21,7 +21,12 @@ void main() {
|
|||
vec3 diffuse = vec3(0);
|
||||
for(int i = 0; i < 32; i++) {
|
||||
const vec3 norm = normalize(inNormal);
|
||||
const vec3 lightDir = normalize(lights[i].position.xyz - inFragPos);
|
||||
|
||||
vec3 lightDir = vec3(0);
|
||||
if(lights[i].position.w == 0)
|
||||
lightDir = normalize(lights[i].position.xyz - inFragPos);
|
||||
else
|
||||
lightDir = normalize(-lights[i].direction.xyz);
|
||||
|
||||
const float diff = max(dot(norm, lightDir), 0.0);
|
||||
diffuse += vec3(diff) * lights[i].color;
|
||||
|
|
|
@ -238,7 +238,8 @@ int main(int argc, char* argv[]) {
|
|||
World world;
|
||||
|
||||
auto light = new Light();
|
||||
light->position.y = 5;
|
||||
light->type = LightType::Directional;
|
||||
light->direction = glm::vec3(-0.2, -0.8, 0);
|
||||
|
||||
world.lights.push_back(light);
|
||||
|
||||
|
|
|
@ -32,15 +32,16 @@ WorldPass::~WorldPass() {
|
|||
|
||||
void WorldPass::render(VkCommandBuffer commandBuffer, World& world, Camera& camera, RenderTarget* target) {
|
||||
struct ShaderLight {
|
||||
glm::vec4 position;
|
||||
glm::vec4 position, direction;
|
||||
glm::vec3 color;
|
||||
};
|
||||
|
||||
ShaderLight* data;
|
||||
vkMapMemory(renderer_.getDevice(), lightMemory_, 0, sizeof(float) * (4 + 3) * 32, 0, reinterpret_cast<void**>(&data));
|
||||
vkMapMemory(renderer_.getDevice(), lightMemory_, 0, sizeof(float) * (4 + 4 + 3) * 32, 0, reinterpret_cast<void**>(&data));
|
||||
|
||||
for(const auto& light : world.lights) {
|
||||
data->position = glm::vec4(light->position, 0.0);
|
||||
data->position = glm::vec4(light->position, (int)light->type);
|
||||
data->direction = glm::vec4(light->direction, 0.0);
|
||||
data->color = light->color;
|
||||
|
||||
data++;
|
||||
|
@ -300,7 +301,7 @@ void WorldPass::createPipeline() {
|
|||
void WorldPass::createUniformBuffer() {
|
||||
VkBufferCreateInfo bufferInfo = {};
|
||||
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
bufferInfo.size = sizeof(float) * (4 + 3) * 32;
|
||||
bufferInfo.size = sizeof(float) * (4 + 4 + 3) * 32;
|
||||
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
||||
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
|
||||
|
@ -329,7 +330,7 @@ void WorldPass::createDescriptorSet() {
|
|||
|
||||
VkDescriptorBufferInfo bufferInfo = {};
|
||||
bufferInfo.buffer = lightBuffer_;
|
||||
bufferInfo.range = sizeof(float) * (4 + 3) * 32;
|
||||
bufferInfo.range = sizeof(float) * (4 + 4 + 3) * 32;
|
||||
|
||||
VkWriteDescriptorSet descriptorWrite = {};
|
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
|
@ -341,9 +342,9 @@ void WorldPass::createDescriptorSet() {
|
|||
vkUpdateDescriptorSets(renderer_.getDevice(), 1, &descriptorWrite, 0, nullptr);
|
||||
|
||||
float* data;
|
||||
vkMapMemory(renderer_.getDevice(), lightMemory_, 0, sizeof(float) * (4 + 3) * 32, 0, reinterpret_cast<void**>(&data));
|
||||
vkMapMemory(renderer_.getDevice(), lightMemory_, 0, sizeof(float) * (4 + 4 + 3) * 32, 0, reinterpret_cast<void**>(&data));
|
||||
|
||||
for(uint32_t i = 0; i < (4 + 3) * 32; i++)
|
||||
for(uint32_t i = 0; i < (4 + 4 + 3) * 32; i++)
|
||||
data[i] = 0.0f;
|
||||
|
||||
vkUnmapMemory(renderer_.getDevice(), lightMemory_);
|
||||
|
|
Reference in a new issue