added BSP support, depth seems broken

This commit is contained in:
Hopeless Tyromancy 2026-04-27 14:18:12 +02:00
parent 74a1ae3dee
commit 85699bba23
46 changed files with 1340172 additions and 22 deletions

View File

@ -4,7 +4,7 @@ SHADER_DIR := shaders
BUILD_DIR := build BUILD_DIR := build
CXX := g++ CXX := g++
CXXFLAGS := -g -std=c++23 -Wall -Wextra -Wpedantic -Wno-missing-field-initializers -Wno-unused-function CXXFLAGS := -g -std=c++23 -Wall -Wextra -Wpedantic -Wno-missing-field-initializers -Wno-unused-function # -fsanitize=address
LDFLAGS := -lSDL3 LDFLAGS := -lSDL3
GLSLC := glslc GLSLC := glslc

12842
assets/conv_strife/MAP01.obj Normal file

File diff suppressed because it is too large Load Diff

60675
assets/conv_strife/MAP02.obj Normal file

File diff suppressed because it is too large Load Diff

14892
assets/conv_strife/MAP03.obj Normal file

File diff suppressed because it is too large Load Diff

59263
assets/conv_strife/MAP04.obj Normal file

File diff suppressed because it is too large Load Diff

41190
assets/conv_strife/MAP05.obj Normal file

File diff suppressed because it is too large Load Diff

52778
assets/conv_strife/MAP06.obj Normal file

File diff suppressed because it is too large Load Diff

51378
assets/conv_strife/MAP07.obj Normal file

File diff suppressed because it is too large Load Diff

15302
assets/conv_strife/MAP08.obj Normal file

File diff suppressed because it is too large Load Diff

31196
assets/conv_strife/MAP09.obj Normal file

File diff suppressed because it is too large Load Diff

59796
assets/conv_strife/MAP10.obj Normal file

File diff suppressed because it is too large Load Diff

59096
assets/conv_strife/MAP11.obj Normal file

File diff suppressed because it is too large Load Diff

54745
assets/conv_strife/MAP12.obj Normal file

File diff suppressed because it is too large Load Diff

50243
assets/conv_strife/MAP13.obj Normal file

File diff suppressed because it is too large Load Diff

40935
assets/conv_strife/MAP14.obj Normal file

File diff suppressed because it is too large Load Diff

62780
assets/conv_strife/MAP15.obj Normal file

File diff suppressed because it is too large Load Diff

59301
assets/conv_strife/MAP16.obj Normal file

File diff suppressed because it is too large Load Diff

57859
assets/conv_strife/MAP17.obj Normal file

File diff suppressed because it is too large Load Diff

38870
assets/conv_strife/MAP18.obj Normal file

File diff suppressed because it is too large Load Diff

46480
assets/conv_strife/MAP19.obj Normal file

File diff suppressed because it is too large Load Diff

51581
assets/conv_strife/MAP20.obj Normal file

File diff suppressed because it is too large Load Diff

55321
assets/conv_strife/MAP21.obj Normal file

File diff suppressed because it is too large Load Diff

24655
assets/conv_strife/MAP22.obj Normal file

File diff suppressed because it is too large Load Diff

46239
assets/conv_strife/MAP23.obj Normal file

File diff suppressed because it is too large Load Diff

54626
assets/conv_strife/MAP24.obj Normal file

File diff suppressed because it is too large Load Diff

15129
assets/conv_strife/MAP25.obj Normal file

File diff suppressed because it is too large Load Diff

49778
assets/conv_strife/MAP26.obj Normal file

File diff suppressed because it is too large Load Diff

20631
assets/conv_strife/MAP27.obj Normal file

File diff suppressed because it is too large Load Diff

19790
assets/conv_strife/MAP28.obj Normal file

File diff suppressed because it is too large Load Diff

10866
assets/conv_strife/MAP29.obj Normal file

File diff suppressed because it is too large Load Diff

13547
assets/conv_strife/MAP30.obj Normal file

File diff suppressed because it is too large Load Diff

20594
assets/conv_strife/MAP31.obj Normal file

File diff suppressed because it is too large Load Diff

13090
assets/conv_strife/MAP32.obj Normal file

File diff suppressed because it is too large Load Diff

49487
assets/conv_strife/MAP33.obj Normal file

File diff suppressed because it is too large Load Diff

12940
assets/conv_strife/MAP34.obj Normal file

File diff suppressed because it is too large Load Diff

BIN
assets/hl1.bsp Normal file

Binary file not shown.

7813
assets/strife/MAP01.obj Normal file

File diff suppressed because it is too large Load Diff

3901
assets/vox.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -70,7 +70,7 @@ void main() {
const mat3 sobely = mat3(-1.0, 0.0, 1.0, -2.0, 0.0, 2.0, -1.0, 0.0, 1.0); const mat3 sobely = mat3(-1.0, 0.0, 1.0, -2.0, 0.0, 2.0, -1.0, 0.0, 1.0);
int time = int(fract(time_padded.x/1000.0) * 3.0); int time = 2;//int(fract(time_padded.x/100.0) * 3.0);
switch(time) { switch(time) {
case 0: case 0:
out_color = texture(screen, uv); out_color = texture(screen, uv);
@ -84,6 +84,6 @@ void main() {
} }
out_color = out_color * 0.9 + out_color = out_color * 0.9 +
0.1*((t(uv.x, uv.y) > 0.01)? 0.8*((t(uv.x, uv.y) > 0.01)?
vec4(abs(f(sobelx) + f(sobely)), 0.0, 0.0, 1.0) : vec4(0.0)); vec4(abs(f(sobelx) + f(sobely)), 0.0, 0.0, 1.0) : vec4(0.0));
} }

View File

@ -20,10 +20,19 @@ const float EPS_DIST = 1e-6;
float sdf(vec3 point){ float sdf(vec3 point){
float t = 1.0 + 5.0*abs(sin(time_padded.x)); vec3 p = point - cam_pos.xyz;
vec3 s1_local = point; //mod(point, vec3(t)) - vec3(t/2.0); const float speed = 1.0 + exp(sin(time_padded.x));
float s1 = length(s1_local) - t; float t = time_padded.x * speed;
return s1; const int N = 5;
float d = 1.0/EPS_DIST;
float orbit_radius = 0.4;
float radius = 0.03;
for(int i = 0; i < N; i++) {
float phase = ((3.14159 * 2.0) / float(N)) * float(i);
d = min(d, length(p - orbit_radius*vec3(sin(t + phase), 0.0, cos(t + phase))) - radius);
}
return d;
} }
vec3 getnormal(vec3 p) { vec3 getnormal(vec3 p) {
@ -41,14 +50,17 @@ vec4 march(vec3 point, vec3 ray, float max_depth) {
int i; int i;
for (i = 0; i < MAX_MARCH_STEPS && totaldist < max_depth; i += 1) { for (i = 0; i < MAX_MARCH_STEPS && totaldist < max_depth; i += 1) {
float h = sdf(point); float h = sdf(point);
point += h * ray;
if (h + totaldist > max_depth)
break;
if (h < EPS_DIST) { if (h < EPS_DIST) {
return vec4(point, totaldist + h); totaldist += h;
return vec4(point, totaldist);
} }
totaldist += h; totaldist += h;
if (totaldist > max_depth)
break;
point += h * ray;
} }
return vec4(-1.0); return vec4(-1.0);
@ -72,7 +84,7 @@ vec4 kernel(float max_depth) {
float linearize_depth(vec2 uv) { float linearize_depth(vec2 uv) {
float d = texture(depth_tex, uv).r; float d = texture(depth_tex, uv).r;
if (d >= 1.0) return 1000.0; if (d >= 1.0) return 1000.0;
vec4 ndc = vec4((uv * 2.0 - 1.0), d * 2.0 - 1.0, 1.0); vec4 ndc = vec4(uv * 2.0 - 1.0, d * 2.0 - 1.0, 1.0);
vec4 view_pos = inv_projection * ndc; vec4 view_pos = inv_projection * ndc;
view_pos /= view_pos.w; view_pos /= view_pos.w;
return -view_pos.z; // view space, positive distance return -view_pos.z; // view space, positive distance
@ -82,8 +94,9 @@ float linearize_depth(vec2 uv) {
void main() { void main() {
float max_depth = linearize_depth(uv); float max_depth = linearize_depth(uv);
vec4 hit = kernel(max_depth); vec4 hit = kernel(max_depth);
if (hit.w > 0.0) { if (hit.w > 0.0) {
out_color = vec4(getnormal(hit.xyz), 1.0); out_color = vec4(1.0);//out_color = vec4(getnormal(hit.xyz), 1.0);
} else { } else {
out_color = texture(screen, uv); out_color = texture(screen, uv);
} }

View File

@ -5,6 +5,7 @@ layout(location = 2) in vec3 v_normal_vs;
layout(location = 0) out vec4 out_color; layout(location = 0) out vec4 out_color;
void main() { void main() {
vec3 light_dir = normalize(vec3(0.5, 1.0, 0.8)); vec3 light_dir = normalize(vec3(0.5, 1.0, 0.8));
@ -12,5 +13,9 @@ void main() {
float diffuse = max(dot(v_normal_vs, light_dir), 0.0); float diffuse = max(dot(v_normal_vs, light_dir), 0.0);
float lighting = ambient + (1.0 - ambient) * diffuse; float lighting = ambient + (1.0 - ambient) * diffuse;
out_color = vec4(v_color.rgb * lighting, v_color.a); out_color = v_color * vec4(v_normal_vs,1.0);
//out_color = vec4(v_color.rgb * lighting, v_color.a);
//out_color = vec4(1.0);
//out_color = vec4(viz(v_normal_vs), 1.0);
//out_color = vec4(lighting);
} }

285
src/BSP.cpp Normal file
View File

@ -0,0 +1,285 @@
#include "bsp/BSP.h"
#include <SDL3/SDL_iostream.h>
#include <SDL3/SDL_log.h>
#include <set>
#include <cstring>
/* changes handedness by swapping z and y */
template<typename T>
static inline void change_swizzle(T& v) {
auto tmp = v.y;
v.y = v.z;
v.z = tmp;
}
using namespace HLBSP;
static inline void copy_data(void* file_data, std::string& dst, Lump& lump) {
dst.resize(lump.len);
std::memcpy(dst.data(), (uint8_t*)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) {
dst.resize(lump.len / sizeof(T));
std::memcpy(dst.data(), ((uint8_t*)file_data) + lump.offset, lump.len);
}
static inline glm::vec2 calc_tex_coords(glm::vec3 v, const TexInfo& t) {
change_swizzle(v);
return glm::vec2(
t.shift_s + glm::dot(v, t.shift_s_dir),
t.shift_t + glm::dot(v, t.shift_t_dir)
);
}
void BSP::load_vertices() {
SDL_Log("Loading vertices");
std::set<int> present_faces;
std::vector<Face> visible_faces;
// if (visibility_test) {
// auto leaf_idx = determine_leaf(cam_pos);
// auto fr_planes = frustum(view);
// if (leaf_idx == last_leaf)
// return;
// last_leaf = leaf_idx;
// auto& cam_leaf = leaves[leaf_idx];
// std::vector<Leaf> visible_leafs;
// for (auto& leaf : leaves) {
// const auto min = leaf.bb_mins;
// const auto max = leaf.bb_maxes;
// const glm::vec3 bounding_planes[8] = {
// { min.x, min.y, min.z },
// { max.x, min.y, min.z },
// { max.x, max.y, min.z },
// { min.x, max.y, min.z },
// { min.x, min.y, max.z },
// { max.x, min.y, max.z },
// { max.x, max.y, max.z },
// { min.x, max.y, max.z },
// };
// if (determine_visibility(cam_leaf, leaf, fr_planes, bounding_planes))
// visible_leafs.push_back(leaf);
// }
// for (const auto& leaf : visible_leafs) {
// for (size_t i = 0; i < leaf.n_mark_surfaces; i++) {
// auto idx = mark_surfaces[leaf.first_mark_surface_idx + i];
// if (present_faces.contains(idx))
// continue;
// present_faces.insert(idx);
// visible_faces.push_back(faces[idx]);
// }
// }
// } else {
visible_faces = faces;
// }
textured_vertices.clear();
for (auto& face : visible_faces) {
auto& tex_info = tex_infos[face.tex_info_idx];
for (int16_t i = 1, j = 2; j < face.n_surf_edges; i++, j++) {
vec3 pos[3] = {
processed_vertices[face.first_surf_edge_idx],
processed_vertices[face.first_surf_edge_idx+i],
processed_vertices[face.first_surf_edge_idx+j],
};
vec3 face_normal = glm::normalize(glm::cross(pos[1] - pos[0], pos[2] - pos[0]));
textured_vertices.push_back(Vertex{
.pos = pos[0],
.uv = calc_tex_coords(processed_vertices[face.first_surf_edge_idx], tex_info),
.norm = face_normal
});
textured_vertices.push_back(Vertex{
.pos = pos[1],
.uv = calc_tex_coords(processed_vertices[face.first_surf_edge_idx+i], tex_info),
.norm = face_normal
});
textured_vertices.push_back(Vertex{
.pos = pos[2],
.uv = calc_tex_coords(processed_vertices[face.first_surf_edge_idx+j], tex_info),
.norm = face_normal
});
}
}
SDL_Log("Done loading vertices");
}
int BSP::get_index_from_surfedge(int surfedge) {
int surf = surfedges[surfedge];
if(surf >= 0) {
return edges[surf].vertex_indices[0];
}
else {
return edges[-surf].vertex_indices[1];
}
}
int BSP::determine_leaf(glm::vec3 cam_pos) {
/* use SDF of planes to determine relative position with respect to partitioning planes */
int idx = 0;
/* positive values are node indices, negative values are leaf indices */
while (idx >= 0) {
const auto& plane = planes[nodes[idx].plane];
const auto dist = glm::dot(plane.norm, cam_pos) - plane.dist;
if (dist >= 0)
idx = nodes[idx].children[0];
else
idx = nodes[idx].children[1];
}
return -idx - 1;
}
bool BSP::determine_visibility(const Leaf& cam_leaf, const Leaf& leaf, const std::array<glm::vec4, 6>& frustum, const glm::vec3 box_verts[8]) {
/* perform fustrum culling */
//return box_in_frustum(frustum, box_verts);
return true;
}
static std::vector<std::map<std::string, std::string>> load_entities(const std::string& in) {
/* TODO */
return {
{{"test", "this"}},
};
}
static std::vector<MipTexture> load_mip_textures(const u8* data, u32 offset) {
const TextureLump* lump = reinterpret_cast<const TextureLump*>(data + offset);
std::vector<MipTexture> ret;
ret.resize(lump->n_mip_textures);
for(size_t i = 0; i < ret.size(); i++) {
ret[i] = *reinterpret_cast<const MipTexture*>(data + lump->offsets[i]);
}
return ret;
}
BSP::BSP(const std::string& fname) : filename(fname) {
size_t fsize;
uint8_t* fdata = (uint8_t*)SDL_LoadFile(fname.c_str(), &fsize);
if(!fdata) {
SDL_Log("Can't load bsp file: %s", fname.c_str());
return;
}
file_data.resize(fsize);
memcpy(file_data.data(), fdata, fsize);
header = reinterpret_cast<Header*>(file_data.data());
if(header->version != 30) {
SDL_Log("BSP file not expected version (Half Life has version 30)!");
}
SDL_Log("Loading entities\n");
std::string entities_buff;
copy_data(file_data.data(), entities_buff, header->entities);
entities = load_entities(entities_buff);
SDL_Log("Loading planes\n");
copy_data(file_data.data(), planes, header->planes);
/* change swizzle */
for (auto& plane : planes) {
change_swizzle(plane.norm);
}
//SDL_Log("Loading textures\n");
//textures = load_mip_textures(file_data.data(), header->textures.offset);
SDL_Log("Loading vertices\n");
copy_data(file_data.data(), vertices, header->vertices);
for (auto& vertex : vertices) {
change_swizzle(vertex);
}
SDL_Log("Loading nodes\n");
copy_data(file_data.data(), nodes, header->nodes);
for (auto& node : nodes) {
change_swizzle(node.bb_mins);
change_swizzle(node.bb_maxes);
}
SDL_Log("Loading texinfo\n");
copy_data(file_data.data(), tex_infos, header->texinfo);
SDL_Log("Loading faces\n");
copy_data(file_data.data(), faces, header->faces);
SDL_Log("Loading lightmap\n");
lightmap.lights = reinterpret_cast<rgb*>(file_data.data()+header->lighting.offset);
SDL_Log("Loading clip nodes\n");
copy_data(file_data.data(), clip_nodes, header->clip_nodes);
SDL_Log("Loading leaves\n");
copy_data(file_data.data(), leaves, header->leaves);
for (auto& leaf : leaves) {
change_swizzle(leaf.bb_mins);
change_swizzle(leaf.bb_maxes);
}
SDL_Log("Loading mark surfaces\n");
copy_data(file_data.data(), mark_surfaces, header->mark_surfaces);
SDL_Log("Loading edges\n");
copy_data(file_data.data(), edges, header->edges);
SDL_Log("Loading surfedges\n");
copy_data(file_data.data(), surfedges, header->surf_edges);
processed_vertices.reserve(surfedges.size());
/* use this to build our processed_vertices, idea thanks to gzalo's HalfMapper */
for(const auto& s : surfedges) {
processed_vertices.push_back(vertices[edges[s > 0? s : -s].vertex_indices[s<=0]]);
}
SDL_Log("Loading models\n");
copy_data(file_data.data(), models, header->models);
for (auto& model : models) {
change_swizzle(model.bb_mins);
change_swizzle(model.bb_maxes);
}
size_t max_vertex_count = 0;
for (const auto& face : faces) {
max_vertex_count += (face.n_surf_edges - 2) * 3;
}
SDL_Log("Creating vertex buffer of size %zu\n", max_vertex_count);
textured_vertices.reserve(max_vertex_count);
}
std::vector<::Vertex> BSP::get_vertices() {
std::vector<::Vertex> ret;
ret.reserve(textured_vertices.size());
for(const auto& tex_vert : textured_vertices) {
ret.push_back({
.pos = tex_vert.pos,
.color = vec4(tex_vert.uv.x, tex_vert.uv.y, tex_vert.uv.x * tex_vert.uv.y, 1.0),
.normal = tex_vert.pos,
});
}
return ret;
}

212
src/bsp/BSP.h Normal file
View File

@ -0,0 +1,212 @@
#pragma once
#include "../types.h"
#include <glm/glm.hpp>
#include <vector>
#include <string>
#include <map>
#define MAX_TEXTURE_NAME 16
#define MIP_LEVELS 4
#define MAX_MAP_HULLS 4
/* contains loading functions for Half Life BSPs */
namespace HLBSP {
struct Lump {
uint32_t offset;
uint32_t len;
};
using rgb = glm::uvec3;
using rgba = glm::uvec4;
using vec3 = glm::vec3;
using vec2 = glm::vec2;
using ivec3 = glm::vec<3, int16_t>;
struct Header {
uint32_t version;
union {
Lump lumps[15];
struct {
Lump entities,
planes,
textures,
vertices,
visibility,
nodes,
texinfo,
faces,
lighting,
clip_nodes,
leaves,
mark_surfaces,
edges,
surf_edges,
models;
};
};
};
struct Plane {
vec3 norm;
float dist;
/* exists for certain optimizations (swaped y and z) */
enum PlaneType {
eX,
eZ,
eY,
eAnyX,
eAnyZ,
eAnyY,
} type;
};
struct TextureLump {
uint32_t n_mip_textures;
int32_t offsets[];
};
struct MipTexture {
char name[MAX_TEXTURE_NAME];
uint32_t width, height;
/* is 0 if stored in WAD, otherwise, offset is from beginning of this struct */
uint32_t mip_offsets[MIP_LEVELS];
};
struct Vertex {
vec3 pos;
vec2 uv;
vec3 norm;
};
struct Vis {};
struct Node {
int32_t plane;
/* negative numbers are leaf indices */
int16_t children[2];
/* bounding box coords (integer) */
ivec3 bb_mins;
ivec3 bb_maxes;
int16_t first_face_idx;
int16_t n_faces;
};
struct TexInfo {
vec3 shift_s_dir;
float shift_s;
vec3 shift_t_dir;
float shift_t;
uint32_t mip_tex_idx;
/* seems to always be 0 */
uint32_t flags;
};
struct Face {
uint16_t plane_idx;
/* set if different normals orientation */
uint16_t plane_side;
uint32_t first_surf_edge_idx;
int16_t n_surf_edges;
int16_t tex_info_idx;
uint8_t lighting_styles[4];
uint32_t lightmap_offset;
};
struct Lightmap {
rgb* lights;
};
struct ClipNode {
int32_t plane_idx;
/* negative numbers are contents */
int16_t children[2];
};
struct Leaf {
enum {
eEmpty = -1,
eSolid = -2,
eWater = -3,
eSlime = -4,
eLava = -5,
eSky = -6,
eOrigin = -7,
eClip = -8,
eCurrent0 = -9,
eCurrent90 = -10,
eCurrent180 = -11,
eCurrent270 = -12,
eCurrentUp = -13,
eCurrentDown = -14,
eTranslucent = -15,
} contents;
/* if this is -1, no VIS data */
int32_t vis_offset;
ivec3 bb_mins;
ivec3 bb_maxes;
uint16_t first_mark_surface_idx;
uint16_t n_mark_surfaces;
uint8_t ambient_sound_levels[4];
};
typedef uint16_t MarkSurface;
struct Edge {
uint16_t vertex_indices[2];
};
typedef int32_t Surfedge;
struct Model {
vec3 bb_mins;
vec3 bb_maxes;
vec3 origin;
int32_t head_node_indices[MAX_MAP_HULLS];
int32_t vis_leafs;
int32_t first_face_idx;
int32_t n_faces;
};
struct BSP {
BSP(const std::string& fname);
void load_vertices();
int determine_leaf(vec3 cam_pos);
bool determine_visibility(const Leaf& cam_leaf, const Leaf& leaf, const std::array<glm::vec4, 6>& frustum, const vec3 box_verts[8]);
int get_index_from_surfedge(int surfedge);
Header* header;
std::string filename;
std::vector<uint8_t> file_data;
std::vector<::std::map<::std::string, std::string>> entities;
std::vector<Plane> planes;
std::vector<MipTexture> textures;
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> processed_vertices;
/* skipping vis for now */
std::vector<Node> nodes;
std::vector<TexInfo> tex_infos;
std::vector<Face> faces;
Lightmap lightmap;
std::vector<ClipNode> clip_nodes;
std::vector<Leaf> leaves;
std::vector<MarkSurface> mark_surfaces;
std::vector<Edge> edges;
std::vector<Surfedge> surfedges;
std::vector<Model> models;
std::vector<::Vertex> get_vertices();
std::vector<Vertex> textured_vertices;
/* to eliminate needless re-loading*/
int last_leaf = -0x1337;
};
}

View File

@ -8,7 +8,7 @@ struct Camera {
vec3 target = vec3(0.0f, 0.0f, 0.0f); vec3 target = vec3(0.0f, 0.0f, 0.0f);
vec3 up = vec3(0.0f, 1.0f, 0.0f); vec3 up = vec3(0.0f, 1.0f, 0.0f);
float fov = 60.0f; float fov = 60.0f;
float move_speed = 0.05f; float move_speed = 0.5f;
float look_speed = 1.0f; float look_speed = 1.0f;
float pitch = 0.0f; float pitch = 0.0f;
float yaw = -90.0f; float yaw = -90.0f;

View File

@ -1,9 +1,13 @@
#include "renderer.h" #include "renderer.h"
#include "gpu.h" #include "gpu.h"
#include <SDL3/SDL_gamepad.h> #include <SDL3/SDL_gamepad.h>
#include <SDL3/SDL_oldnames.h>
#include <SDL3/SDL_timer.h>
#include <iomanip>
#include <iostream> #include <iostream>
#define TINYOBJLOADER_IMPLEMENTATION #define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h" #include "tiny_obj_loader.h"
#include "bsp/BSP.h"
bool Renderer::init(SDL_Window* w) { bool Renderer::init(SDL_Window* w) {
@ -13,7 +17,7 @@ bool Renderer::init(SDL_Window* w) {
SDL_ClaimWindowForGPUDevice(dev, win); SDL_ClaimWindowForGPUDevice(dev, win);
load_obj(); load_bsp();
render_sampler = create_linear_sampler(dev); render_sampler = create_linear_sampler(dev);
SDL_GPUSamplerCreateInfo depth_sampler_info { SDL_GPUSamplerCreateInfo depth_sampler_info {
@ -50,8 +54,23 @@ void Renderer::destroy() {
SDL_DestroyGPUDevice(dev); SDL_DestroyGPUDevice(dev);
} }
bool Renderer::load_bsp() {
const std::string& path = "assets/hl1.bsp";
HLBSP::BSP bsp(path);
bsp.load_vertices();
vertices = bsp.get_vertices();
SDL_GPUBufferCreateInfo info {
.usage = SDL_GPU_BUFFERUSAGE_VERTEX,
.size = static_cast<uint32_t>(vertices.size() * sizeof(Vertex)),
};
vert_buff = SDL_CreateGPUBuffer(dev, &info);
upload_buffer(dev, vert_buff, vertices.data(), info.size);
return true;
}
bool Renderer::load_obj() { bool Renderer::load_obj() {
const std::string& path = "assets/homer.obj"; const std::string& path = "assets/vox.obj";
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
@ -96,12 +115,13 @@ bool Renderer::load_obj() {
for(int v = 0; v < 3; v++) { for(int v = 0; v < 3; v++) {
vertices.push_back(Vertex { vertices.push_back(Vertex {
.pos = pos[v], .pos = pos[v],
.color = vec4(abs(sin(pos[v].x)), abs(cos(pos[v].y)), abs(sin(pos[v].z) * cos(pos[v].y)), 1.0f), .color = vec4(1.0),//vec4(abs(sin(pos[v].x)), abs(cos(pos[v].y)), abs(sin(pos[v].z) * cos(pos[v].y)), 1.0f),
.normal = nor[v], .normal = nor[v],
}); });
} }
} }
} }
std::cerr << "Vertices: " << vertices.size() << std::endl;
SDL_GPUBufferCreateInfo info { SDL_GPUBufferCreateInfo info {
.usage = SDL_GPU_BUFFERUSAGE_VERTEX, .usage = SDL_GPU_BUFFERUSAGE_VERTEX,
@ -292,10 +312,15 @@ void Renderer::add_gol_layer() {
upload_buffer(dev, vert_buff, vertices.data(), info.size); upload_buffer(dev, vert_buff, vertices.data(), info.size);
} }
void Renderer::draw(const CameraUBO& ubo) { void Renderer::draw(const CameraUBO& ubo) {
if(frame % 256 == 0 && (frame >> 8) < 20) { static float last_time = (float)SDL_GetPerformanceCounter()/(float)SDL_GetPerformanceFrequency();
add_gol_layer(); // if(frame % 256 == 0 && (frame >> 8) < 20) {
// add_gol_layer();
// }
if(frame % 60 == 59) {
float now = (float)SDL_GetPerformanceCounter()/(float)SDL_GetPerformanceFrequency();
std::cerr << std::setprecision(3) << 60.0f/(now-last_time) << " fps\r";
last_time = now;
} }
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(dev); SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(dev);

View File

@ -32,6 +32,7 @@ struct Renderer {
private: private:
bool load_obj(); bool load_obj();
bool load_bsp();
void add_gol_layer(); void add_gol_layer();
void resize_render_texture(uint32_t w, uint32_t h); void resize_render_texture(uint32_t w, uint32_t h);
void pass_scene(SDL_GPUCommandBuffer* cmd, const CameraUBO& ubo); void pass_scene(SDL_GPUCommandBuffer* cmd, const CameraUBO& ubo);