Fixed model 'vanilla' pipeline and shaders to work with updated uniform buffer

This commit is contained in:
Conál 2024-02-16 20:38:33 -05:00
parent c9a6f11a7a
commit dfd39257c0
15 changed files with 318 additions and 24 deletions

View File

@ -1,6 +1,7 @@
# Pléascach # Pléascach
###### Check out the raymarching branch (pléascach-dronuilleog)! ###### Check out the raymarching branch (pléascach-dronuilleog)!
Small Vulkan 3D renderer. Small Vulkan 3D renderer.
![really cool wireframe tessellated terrain)](capture.png "Terrain Capture") ![really cool wireframe tessellated terrain)](capture.png "Terrain Capture")
## Features ## Features

View File

@ -222,7 +222,6 @@ Renderer::Renderer(Window& win) : win(win) {
pipeline->update(0, *uniform_buffer); pipeline->update(0, *uniform_buffer);
pipeline->update(1, textures[1]); pipeline->update(1, textures[1]);
/* create Terrain */ /* create Terrain */
terrain = std::make_unique<Terrain>(phys_dev, dev, textures[1]); terrain = std::make_unique<Terrain>(phys_dev, dev, textures[1]);
@ -364,6 +363,11 @@ void Renderer::draw() {
command_buffer->bind(terrain.get()); command_buffer->bind(terrain.get());
command_buffer->command_buffer.drawIndexed(terrain->indices.size(), 1, 0, 0, 0); command_buffer->command_buffer.drawIndexed(terrain->indices.size(), 1, 0, 0, 0);
command_buffer->bind(*pipeline);
command_buffer->bind(pipeline->layout, pipeline->desc_set);
command_buffer->bind(models[0]);
command_buffer->command_buffer.drawIndexed(models[0]->indices.size(), 10, 0, 0, 0);
/* draw User Interface stuff */ /* draw User Interface stuff */
ui->newFrame(); ui->newFrame();

62
Scene/BSP.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <Scene/BSP.hpp>
#include <util/file.hpp>
#include <util/log.hpp>
#include <cstring>
using namespace Q3BSP;
static inline void copy_data(void* file_data, std::string& dst, Lump& lump) {
dst.resize(lump.len);
std::memcpy(dst.data(), (u8*)file_data + (size_t)lump.offset, lump.len);
}
template<typename T>
static inline void copy_data(void* file_data, std::vector<T>& dst, Lump& lump) {
Log::debug("Size: %zu (%zu)\n", lump.len/sizeof(T), lump.len);
dst.resize(lump.len / sizeof(T));
puts("ALLOC'D");
//Log::debug("%p %p\n", dst.data(), (u8*)file_data + lump.offset);
std::memcpy(dst.data(), ((u8*)file_data) + lump.offset, lump.len);
}
BSP::BSP(const std::string& fname) : filename(fname) {
file_data = file::slurpb(fname);
Log::debug("File size: %zu\n", file_data.size());
header = reinterpret_cast<Header*>(file_data.data());
Log::info("Loading BSP: %s\n", fname.c_str());
if(header->magic != BSP_MAGIC) {
Log::error("BSP file missing magic!\n");
}
for (size_t i = 0; i < std::size(header->lumps); i++) {
Log::debug("%i: Offset: %u | Length: %u\n", i, header->lumps[i].offset, header->lumps[i].len);
Log::debug("\tPointer: 0x%p\n", (u8*)file_data.data() + (size_t)header->lumps[i].offset);
}
copy_data(file_data.data(), entities, header->entities);
copy_data(file_data.data(), textures, header->textures);
copy_data(file_data.data(), planes, header->planes);
copy_data(file_data.data(), nodes, header->nodes);
copy_data(file_data.data(), leafs, header->leafs);
copy_data(file_data.data(), leaf_faces, header->leaf_faces);
copy_data(file_data.data(), leaf_brushes, header->leaf_brushes);
copy_data(file_data.data(), models, header->models);
copy_data(file_data.data(), brushes, header->brushes);
copy_data(file_data.data(), brush_sides, header->brush_sides);
copy_data(file_data.data(), vertices, header->vertices);
copy_data(file_data.data(), mesh_vertices, header->mesh_vertices);
copy_data(file_data.data(), effects, header->effects);
copy_data(file_data.data(), faces, header->faces);
copy_data(file_data.data(), lightmaps, header->lightmaps);
copy_data(file_data.data(), lightvols, header->lightvols);
vis_info.sz_vectors = reinterpret_cast<u32*>(file_data.data() + header->vis_info.offset)[1];
auto sz = header->vis_info.len;
Log::debug("Size: %u\n", sz);
vis_info.vectors.resize(sz);
std::memcpy(vis_info.vectors.data(), file_data.data() + header->vis_info.offset + 2*sizeof(u32), sz);
}

194
Scene/BSP.hpp Normal file
View File

@ -0,0 +1,194 @@
#pragma once
#include <util/int.hpp>
#include <glm/glm.hpp>
#include <vector>
#include <string>
/* contains loading functions for Quake III-style BSPs */
namespace Q3BSP {
struct Lump {
u32 offset;
u32 len;
};
using rgb = glm::u8vec3;
using rgba = glm::u8vec4;
/* "IBSP" */
const uint32_t BSP_MAGIC = 0x50534249U;
struct Header {
u32 magic;
u32 version;
union {
Lump lumps[17];
struct {
Lump entities,
textures,
planes,
nodes,
leafs,
leaf_faces,
leaf_brushes,
models,
brushes,
brush_sides,
vertices,
mesh_vertices,
effects,
faces,
lightmaps,
lightvols,
vis_info;
};
};
};
struct Texture {
char name[64];
/* values of unknown meaning - TODO: check darkplaces or some other 3rd party loader */
i32 flags;
i32 contents;
};
struct Plane {
glm::vec3 norm;
float dist;
};
struct Node {
u32 plane;
/* negative numbers are leaf indices */
i32 children[2];
/* bounding box coords (integer) */
glm::ivec3 bb_mins;
glm::ivec3 bb_maxes;
};
struct Leaf {
i32 cluster_idx;
u32 area;
glm::ivec3 bb_mins;
glm::ivec3 bb_maxes;
i32 first_leaf_face_idx;
u32 n_leaf_faces;
i32 first_leaf_brush_idx;
u32 n_leaf_brushes;
};
struct LeafFaces {
/* list of face indices (one list per leaf) */
i32 face_idx;
};
struct LeafBrush {
/* list of brush indices (one list leaf) */
i32 brush_idx;
};
struct Model {
glm::vec3 bb_mins;
glm::vec3 bb_maxes;
i32 first_face_idx;
u32 n_faces;
i32 first_brush_idx;
u32 n_brushes;
};
struct Brush {
i32 first_brushside_idx;
u32 n_brushsides;
i32 texture_idx;
};
struct BrushSide {
i32 plane_idx;
i32 texture_idx;
};
struct Vertex {
glm::vec3 position;
glm::vec2 tex_coords;
glm::vec2 lightmap_coords;
glm::vec3 normal;
glm::u8vec4 color;
};
struct MeshVertex {
i32 idx;
};
struct Effect {
char name[64];
i32 brush_idx;
/* almost always 5 for some reason */
i32 unknown;
};
struct Face {
i32 texture_idx;
/* -1 if no effect */
i32 effect_idx;
enum FaceType {
ePOLYGON = 1,
ePATCH = 2,
eMESH = 3,
eBILLBOARD = 4,
} type;
i32 first_vertex_idx;
u32 n_vertices;
i32 first_mesh_vertex_idx;
u32 n_mesh_vertices;
i32 lightmap_idx;
glm::vec2 lightmap_start;
glm::vec2 lightmap_end;
glm::vec3 lightmap_origin;
glm::vec3 lightmap_unit_vectors[2];
glm::vec3 norm;
glm::ivec2 patch_dimensions;
};
struct Lightmap {
u8 map[128][128][3];
};
struct Lightvol {
rgb ambient;
rgb directional;
/* spherical coordinates */
glm::u8vec2 direction;
};
struct VisibilityInfo {
u32 sz_vectors;
std::vector<u8> vectors;
};
struct BSP {
Header* header;
BSP(const std::string& fname);
std::string filename;
std::vector<u8> file_data;
std::string entities;
std::vector<Texture> textures;
std::vector<Plane> planes;
std::vector<Node> nodes;
std::vector<Leaf> leafs;
std::vector<LeafBrush> leaf_brushes;
std::vector<LeafFaces> leaf_faces;
std::vector<Model> models;
std::vector<Brush> brushes;
std::vector<BrushSide> brush_sides;
std::vector<Vertex> vertices;
std::vector<MeshVertex> mesh_vertices;
std::vector<Effect> effects;
std::vector<Face> faces;
std::vector<Lightmap> lightmaps;
std::vector<Lightvol> lightvols;
VisibilityInfo vis_info;
};
}

View File

@ -3,7 +3,6 @@
#include <util/int.hpp> #include <util/int.hpp>
float Terrain::getHeight(int32_t x, int32_t y) { float Terrain::getHeight(int32_t x, int32_t y) {
if (x < 0) if (x < 0)
x += 64; x += 64;
@ -20,7 +19,7 @@ float Terrain::getHeight(int32_t x, int32_t y) {
yf /= 64.0; yf /= 64.0;
yf *= heightmap_tex->extent.height; yf *= heightmap_tex->extent.height;
return static_cast<float>(heightmap_tex->image_data[static_cast<int>(static_cast<int>(yf) * heightmap_tex->extent.width + xf) * 4]) / 256.0f; return static_cast<float>(heightmap_tex->image_data[static_cast<int>(static_cast<int>(yf) * heightmap_tex->extent.width + xf) * 4]) / 256.0f * 15.0f;
} }
Terrain::Terrain(vk::PhysicalDevice phys_dev, vk::Device dev, Texture& tex) : phys_dev(phys_dev), dev(dev) { Terrain::Terrain(vk::PhysicalDevice phys_dev, vk::Device dev, Texture& tex) : phys_dev(phys_dev), dev(dev) {
@ -70,20 +69,26 @@ Terrain::Terrain(vk::PhysicalDevice phys_dev, vk::Device dev, Texture& tex) : ph
{ getHeight(x + 1, y - 1), getHeight(x + 1, y), getHeight(x + 1, y + 1) }, { getHeight(x + 1, y - 1), getHeight(x + 1, y), getHeight(x + 1, y + 1) },
}; };
auto normal = glm::vec3( // auto normal = glm::vec3(
/* x gets X sobel filter */ // /* x gets X sobel filter */
moores_heights[0][0] + 2.0f * moores_heights[0][1] + moores_heights[0][2] // moores_heights[0][0] + 2.0f * moores_heights[0][1] + moores_heights[0][2]
- moores_heights[2][0] - 2.0f * moores_heights[2][1] - moores_heights[2][2], // - moores_heights[2][0] - 2.0f * moores_heights[2][1] - moores_heights[2][2],
0.0, // 0.0,
/* z gets Y sobel filter */ // /* z gets Y sobel filter */
moores_heights[0][0] + 2.0f * moores_heights[1][0] + moores_heights[2][0] // moores_heights[0][0] + 2.0f * moores_heights[1][0] + moores_heights[2][0]
- moores_heights[0][2] - 2.0f * moores_heights[1][2] - moores_heights[2][2] // - moores_heights[0][2] - 2.0f * moores_heights[1][2] - moores_heights[2][2]
); // );
auto relx = glm::vec3(2.0, getHeight(x + 1, y)-getHeight(x, y), 0.0);
auto relz = glm::vec3(0.0, getHeight(x, y + 1)-getHeight(x, y), 2.0);
auto normal = glm::normalize(glm::cross(relz, relx));
/* fill in missing component, first scalar scales bump */ /* fill in missing component, first scalar scales bump */
normal.y = 0.25 * glm::sqrt(glm::abs(1.0 - normal.x*normal.x - normal.z*normal.z)); //normal.y = 0.25 * glm::sqrt(glm::abs(1.0 - normal.x*normal.x - normal.z*normal.z));
//vertices[x + y * patch_size].norm = glm::vec3(getHeight(x, y)); //vertices[x + y * patch_size].norm = glm::vec3(getHeight(x, y));
vertices[x + y * patch_size].norm = glm::normalize(normal * glm::vec3(2.0f, 1.0f, 2.0f)); vertices[x + y * patch_size].norm = normal;
} }
vertex_buffer = std::make_unique<VertexBuffer>(phys_dev, dev, vertices.size()); vertex_buffer = std::make_unique<VertexBuffer>(phys_dev, dev, vertices.size());

View File

@ -11,8 +11,15 @@ layout (location = 0) out vec3 _norm;
layout (location = 1) out vec2 _texCoord; layout (location = 1) out vec2 _texCoord;
layout (set = 0, binding = 0) uniform Matrices { layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp; mat4 view;
mat4 proj;
float time; float time;
vec3 cam_pos;
vec3 cam_dir;
vec4 frustum[6];
vec2 viewport;
float tess_factor;
float tess_edge_size;
}; };
vec4 explode(vec4 pos, vec3 n) { vec4 explode(vec4 pos, vec3 n) {
@ -24,7 +31,7 @@ vec4 explode(vec4 pos, vec3 n) {
void main(void) { void main(void) {
if (time < 3.0) { if (time < 3.0) {
for(int i = 0; i < gl_in.length(); i++) { for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * gl_in[i].gl_Position; gl_Position = proj * view * gl_in[i].gl_Position;
_norm = norm[i]; _norm = norm[i];
_texCoord = texCoord[i]; _texCoord = texCoord[i];
EmitVertex(); EmitVertex();
@ -35,7 +42,7 @@ void main(void) {
vec3 n = norm[0] + norm[1] + norm[2]; vec3 n = norm[0] + norm[1] + norm[2];
n/=3; n/=3;
for(int i = 0; i < gl_in.length(); i++) { for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * explode(gl_in[i].gl_Position, n); gl_Position = proj * view * explode(gl_in[i].gl_Position, n);
_texCoord = texCoord[i]; _texCoord = texCoord[i];
_norm = n; _norm = n;
EmitVertex(); EmitVertex();

Binary file not shown.

View File

@ -13,14 +13,21 @@ layout (location = 1) out vec2 _texCoord;
layout (location = 2) out vec3 _pos; layout (location = 2) out vec3 _pos;
layout (set = 0, binding = 0) uniform Matrices { layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp; mat4 view;
mat4 proj;
float time; float time;
vec3 cam_pos;
vec3 cam_dir;
vec4 frustum[6];
vec2 viewport;
float tess_factor;
float tess_edge_size;
}; };
void main(void) { void main(void) {
for(int i = 0; i < gl_in.length(); i++) { for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * gl_in[i].gl_Position; gl_Position = proj * view * gl_in[i].gl_Position;
_norm = norm[i]; _norm = norm[i];
_texCoord = texCoord[i]; _texCoord = texCoord[i];
_pos = pos[i]; _pos = pos[i];

Binary file not shown.

View File

@ -8,8 +8,15 @@ layout (location = 1) out vec2 texCoord;
layout (location = 2) out vec3 pos; layout (location = 2) out vec3 pos;
layout (set = 0, binding = 0) uniform Matrices { layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp; mat4 view;
mat4 proj;
float time; float time;
vec3 cam_pos;
vec3 cam_dir;
vec4 frustum[6];
vec2 viewport;
float tess_factor;
float tess_edge_size;
}; };
void main() { void main() {

Binary file not shown.

View File

@ -11,8 +11,15 @@ layout (location = 0) out vec3 _norm;
layout (location = 1) out vec2 _texCoord; layout (location = 1) out vec2 _texCoord;
layout (set = 0, binding = 0) uniform Matrices { layout (set = 0, binding = 0) uniform Matrices {
mat4 mvp; mat4 view;
mat4 proj;
float time; float time;
vec3 cam_pos;
vec3 cam_dir;
vec4 frustum[6];
vec2 viewport;
float tess_factor;
float tess_edge_size;
}; };
vec4 explode(vec4 pos, vec3 n) { vec4 explode(vec4 pos, vec3 n) {
@ -26,7 +33,7 @@ void main(void) {
if (time < 3.0) { if (time < 3.0) {
for(int i = 0; i < gl_in.length(); i++) { for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * gl_in[i].gl_Position; gl_Position = proj * view * gl_in[i].gl_Position;
_norm = norm[i]; _norm = norm[i];
_texCoord = texCoord[i]; _texCoord = texCoord[i];
EmitVertex(); EmitVertex();
@ -35,7 +42,7 @@ void main(void) {
vec3 n = norm[0] + norm[1] + norm[2]; vec3 n = norm[0] + norm[1] + norm[2];
n/=3; n/=3;
for(int i = 0; i < gl_in.length(); i++) { for(int i = 0; i < gl_in.length(); i++) {
gl_Position = mvp * explode(gl_in[i].gl_Position*abs(cos(time)), n); gl_Position = proj * view * explode(gl_in[i].gl_Position*abs(cos(time)), n);
_texCoord = texCoord[i]; _texCoord = texCoord[i];
_norm = n; _norm = n;
EmitVertex(); EmitVertex();

Binary file not shown.

View File

@ -42,7 +42,7 @@ void main() {
vec4 pos2 = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x); vec4 pos2 = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
vec4 fpos = mix(pos1, pos2, gl_TessCoord.y); vec4 fpos = mix(pos1, pos2, gl_TessCoord.y);
fpos.y += 15.0 * texture(heightmap, _texCoord).r; fpos.y = 15.0 * texture(heightmap, _texCoord).r;
_pos = fpos.xyz; _pos = fpos.xyz;
gl_Position = proj * view * fpos; gl_Position = proj * view * fpos;

Binary file not shown.