add non_block, view transform cache

This commit is contained in:
Zack Buhman 2026-03-11 19:35:22 -05:00
parent 48a85671c8
commit e273a61277
19 changed files with 642 additions and 123 deletions

View File

@ -25,7 +25,9 @@ OBJS = \
src/bresenham.o \ src/bresenham.o \
src/file.o \ src/file.o \
src/world.o \ src/world.o \
src/inthash.o src/inthash.o \
src/non_block.o \
src/view.o
all: test.so all: test.so

13
include/non_block.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
namespace non_block {
extern unsigned int index_buffer;
extern unsigned int per_vertex_buffer;
extern unsigned int vertex_array_object;
void load_index_buffer();
void load_per_vertex_buffer();
void load_vertex_attributes();
void load_program();
void draw();
}

21
include/view.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
namespace view {
struct view_state {
XMVECTOR up;
XMVECTOR eye;
XMVECTOR forward;
XMVECTOR direction;
float fov;
float pitch;
XMMATRIX projection_transform;
XMMATRIX view_transform;
XMMATRIX transform;
XMFLOAT4X4 float_transform;
};
extern view_state state;
void update_transforms();
}

View File

@ -1,13 +1,14 @@
#pragma once #pragma once
namespace window {
extern float width;
extern float height;
}
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern float g_window_width;
extern float g_window_height;
void update_window(int width, int height); void update_window(int width, int height);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -98,7 +98,7 @@ function love.run()
draw() draw()
local x, y = love.mouse.getPosition() local x, y = love.mouse.getPosition()
test.update_mouse(x, y) --test.update_mouse(x, y)
love.graphics.present() love.graphics.present()
love.timer.sleep(0.001) love.timer.sleep(0.001)

View File

@ -4,11 +4,11 @@ import obj
def append_triangles(state, vertex_buffer, index_buffer, index_lookup): def append_triangles(state, vertex_buffer, index_buffer, index_lookup):
for triangle in state.triangle: for triangle in state.triangle:
for p_ix, t_ix, n_ix in triangle: for p_ix, t_ix, n_ix in triangle:
key = (p_ix, n_ix, t_ix) position = state.position[p_ix]
normal = state.normal[n_ix]
texture = state.texture[t_ix]
key = (position, normal, texture)
if key not in index_lookup: if key not in index_lookup:
position = state.position[p_ix]
normal = state.normal[n_ix]
texture = state.texture[t_ix]
index = len(vertex_buffer) index = len(vertex_buffer)
index_lookup[key] = index index_lookup[key] = index
vertex_buffer.append((position, normal, texture)) vertex_buffer.append((position, normal, texture))

View File

@ -0,0 +1,20 @@
import struct
import vec3
import obj
import obj_state
def write_vertex_buffer(f, vertex_buffer):
for position, normal, texture in vertex_buffer:
position = vec3.mul(position, 0.5)
f.write(struct.pack("<eeeeeeee", *position, *normal, *texture))
def write_indices(f, index_format, index_buffer):
for i in range(len(index_buffer)):
f.write(struct.pack(index_format, index_buffer[i]))
def write_obj(vertex_buffer, index_buffer, index_lookup, path):
index_start = len(index_buffer)
state = obj.parse_obj_from_filename(path)
obj_state.append_triangles(state, vertex_buffer, index_buffer, index_lookup)
index_count = len(index_buffer) - index_start
print(f"{index_start}, {index_count}, // {path}")

View File

@ -0,0 +1,260 @@
# Blender 5.0.0
# www.blender.org
o Cube
v -0.632524 -0.657513 0.632524
v -0.937324 -0.250000 0.245001
v -0.240001 -0.200011 0.957810
v -0.240001 -0.989044 0.245001
v -0.706074 0.241672 0.711008
v -0.711819 1.250000 0.712629
v -0.699990 2.250000 0.701330
v -0.959316 0.750000 0.245001
v -0.948567 1.750000 0.245001
v -0.716633 2.739618 0.245001
v -0.240001 0.750000 0.959316
v -0.240001 1.750000 0.951878
v -0.240001 2.739607 0.716580
v -0.716731 -0.739720 -0.245001
v -0.699990 -0.250000 -0.701328
v -0.240001 -0.739663 -0.716578
v -0.947463 0.241672 -0.245001
v -0.954191 1.250000 -0.245001
v -0.937225 2.250000 -0.245001
v -0.706694 0.750000 -0.707539
v -0.704970 1.750000 -0.709902
v -0.632524 2.657511 -0.632523
v -0.240001 2.986101 -0.245001
v -0.240001 0.241672 -0.950809
v -0.240001 1.250000 -0.954188
v -0.240001 2.250000 -0.947161
v 0.240001 -0.739720 0.716731
v 0.707073 -0.250000 0.705735
v 0.716578 -0.739609 0.245001
v 0.240001 0.241672 0.967454
v 0.240001 1.250000 0.962683
v 0.240001 2.250000 0.937718
v 0.240001 2.986314 0.245001
v 0.710859 0.750000 0.711819
v 0.711872 1.750000 0.706939
v 0.635037 2.657752 0.635038
v 0.962626 0.241672 0.245001
v 0.960914 1.250000 0.245001
v 0.947653 2.250000 0.245001
v 0.240001 -0.989011 -0.245001
v 0.240001 -0.250000 -0.937551
v 0.947490 -0.250000 -0.245001
v 0.634819 -0.657682 -0.634819
v 0.240001 0.750000 -0.957538
v 0.240001 1.750000 -0.958191
v 0.240001 2.739618 -0.716630
v 0.959462 0.750000 -0.245001
v 0.961540 1.750000 -0.245001
v 0.716577 2.739661 -0.245001
v 0.710749 0.241672 -0.705815
v 0.709463 1.250000 -0.706692
v 0.707054 2.250000 -0.705714
vn -0.5492 -0.1171 0.8275
vn 0.2450 -0.9394 0.2399
vn -0.2392 0.1951 0.9512
vn -0.9523 -0.1900 -0.2388
vn -0.4667 -0.0041 0.8844
vn -0.9387 0.2519 0.2353
vn -0.4652 0.0155 0.8851
vn -0.2426 0.9407 0.2372
vn -0.2356 -0.2553 -0.9377
vn -0.2443 -0.9377 -0.2471
vn -0.8492 0.5120 -0.1293
vn -0.5928 0.5382 0.5991
vn -0.6747 -0.2958 -0.6763
vn -0.9431 0.2191 -0.2501
vn -0.5464 0.1144 -0.8297
vn 0.2338 -0.2744 0.9328
vn -0.2480 -0.2177 0.9440
vn 0.9350 -0.2644 0.2364
vn 0.2422 -0.2411 -0.9398
vn 0.2447 -0.2424 -0.9388
vn 0.2445 -0.2410 0.9392
vn 0.2438 -0.2072 0.9474
vn -0.5927 -0.5381 -0.5993
vn 0.2416 0.2093 -0.9475
vn -0.4662 -0.0013 -0.8847
vn 0.5884 0.5480 -0.5946
vn -0.9404 0.2429 0.2380
vn -0.9393 0.2454 -0.2396
vn 0.9398 -0.2341 -0.2490
vn 0.1287 0.8188 0.5594
vn -0.2483 -0.2321 -0.9404
vn 0.2419 0.9390 -0.2445
vn 0.1273 -0.5089 -0.8513
vn 0.5885 -0.5480 0.5944
vn 0.9443 -0.2165 0.2477
vn 0.9391 -0.2419 0.2440
vn 0.5565 0.8210 0.1280
vn 0.9376 -0.2440 -0.2477
vn 0.9484 0.2074 -0.2400
vn 0.8295 -0.1132 -0.5469
vn 0.8809 0.0014 0.4734
vn -0.2480 -0.2173 -0.9441
vn -0.1304 0.5241 -0.8416
vn 0.8797 0.0007 -0.4756
vn 0.8773 -0.0062 0.4799
vn 0.8420 -0.5243 -0.1271
vn 0.8298 0.1135 0.5464
vn 0.8765 0.0026 -0.4814
vn -0.9461 0.2186 0.2389
vn -0.5614 -0.8172 0.1304
vn -0.1313 -0.8150 0.5643
vn -0.4690 -0.0014 0.8832
vn -0.8360 -0.1152 0.5364
vn 0.2445 -0.9377 0.2468
vn -0.2359 0.2531 0.9382
vn -0.9382 -0.2538 -0.2353
vn -0.8813 -0.0058 0.4726
vn -0.9523 0.1901 0.2387
vn -0.8852 0.0157 0.4650
vn -0.2421 0.9389 0.2447
vn -0.2390 -0.1949 -0.9512
vn -0.2448 -0.9394 -0.2399
vn -0.5577 0.8198 -0.1302
vn -0.6762 0.2887 -0.6778
vn -0.9396 -0.2346 -0.2492
vn -0.8352 0.1126 -0.5383
vn 0.2412 -0.2088 0.9477
vn -0.2472 0.2310 0.9410
vn 0.9484 -0.2073 0.2398
vn 0.2438 0.2125 -0.9462
vn 0.2448 0.2413 -0.9391
vn 0.2442 0.2456 0.9381
vn 0.2412 0.2518 0.9373
vn 0.2392 0.2503 -0.9382
vn -0.8815 0.0004 -0.4721
vn -0.9449 -0.2237 0.2391
vn -0.9463 -0.2151 -0.2414
vn 0.9430 0.2199 -0.2498
vn 0.1269 0.5090 0.8514
vn -0.2489 0.2226 -0.9426
vn 0.2423 0.9406 -0.2378
vn 0.1291 -0.8163 -0.5630
vn 0.9390 0.2402 0.2463
vn 0.9393 0.2411 0.2441
vn 0.8421 0.5243 0.1268
vn 0.9385 0.2403 -0.2480
vn 0.9365 0.2583 -0.2370
vn 0.5299 -0.1111 -0.8407
vn 0.4734 0.0038 0.8809
vn -0.2473 0.2299 -0.9413
vn -0.1310 0.8176 -0.5606
vn 0.4714 -0.0002 -0.8819
vn 0.4831 -0.0072 0.8756
vn 0.5602 -0.8184 -0.1283
vn 0.5294 0.1114 0.8410
vn 0.4739 0.0020 -0.8806
vn -0.9387 -0.2504 0.2370
vn -0.8492 -0.5120 0.1295
vn -0.1297 -0.4983 0.8573
vn -0.0186 -0.0016 0.9998
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 1.000000
s 0
f 5/1/1 1/2/1 3/3/1
f 29/3/2 4/4/2 40/2/2
f 7/3/3 32/4/3 13/2/3
f 2/1/4 15/2/4 14/3/4
f 6/1/5 5/2/5 11/3/5
f 7/1/6 19/2/6 9/3/6
f 7/1/7 6/2/7 12/3/7
f 10/3/8 33/4/8 23/2/8
f 41/3/9 15/4/9 24/2/9
f 40/1/10 14/2/10 16/3/10
f 10/1/11 22/2/11 19/3/11
f 7/3/12 13/1/12 10/4/12
f 24/3/13 17/4/13 20/2/13
f 8/1/14 20/2/14 17/3/14
f 21/1/15 22/2/15 26/3/15
f 3/3/16 28/4/16 30/2/16
f 6/3/17 31/4/17 12/2/17
f 28/1/18 42/2/18 37/3/18
f 50/3/19 24/4/19 44/2/19
f 51/3/20 25/4/20 45/2/20
f 11/3/21 34/4/21 31/2/21
f 12/3/22 35/4/22 32/2/22
f 15/3/23 16/1/23 14/4/23
f 52/3/24 26/4/24 46/2/24
f 20/1/25 21/2/25 25/3/25
f 46/3/26 49/1/26 52/4/26
f 6/1/27 18/2/27 8/3/27
f 9/1/28 21/2/28 18/3/28
f 37/1/29 50/2/29 47/3/29
f 13/3/30 36/4/30 33/2/30
f 44/3/31 20/4/31 25/2/31
f 23/3/32 49/4/32 46/2/32
f 43/3/33 16/4/33 41/2/33
f 27/3/34 29/1/34 28/4/34
f 35/1/35 48/2/35 39/3/35
f 34/1/36 47/2/36 38/3/36
f 36/1/37 49/2/37 33/3/37
f 38/1/38 51/2/38 48/3/38
f 39/1/39 52/2/39 49/3/39
f 43/1/40 50/2/40 42/3/40
f 35/3/41 34/4/41 38/2/41
f 45/3/42 21/4/42 26/2/42
f 22/1/43 46/2/43 26/3/43
f 50/1/44 51/2/44 47/3/44
f 34/3/45 28/4/45 37/2/45
f 29/1/46 43/2/46 42/3/46
f 36/3/47 35/4/47 39/2/47
f 51/1/48 52/2/48 48/3/48
f 5/1/49 17/2/49 2/3/49
f 1/1/50 14/2/50 4/3/50
f 27/1/51 1/2/51 4/3/51
f 3/1/52 11/2/52 5/3/52
f 5/1/53 2/4/53 1/2/53
f 29/3/54 27/1/54 4/4/54
f 7/3/55 12/1/55 32/4/55
f 2/1/56 17/4/56 15/2/56
f 6/1/57 8/4/57 5/2/57
f 7/1/58 10/4/58 19/2/58
f 7/1/59 9/4/59 6/2/59
f 10/3/60 13/1/60 33/4/60
f 41/3/61 16/1/61 15/4/61
f 40/1/62 4/4/62 14/2/62
f 10/1/63 23/4/63 22/2/63
f 24/3/64 15/1/64 17/4/64
f 8/1/65 18/4/65 20/2/65
f 21/1/66 19/4/66 22/2/66
f 3/3/67 27/1/67 28/4/67
f 6/3/68 11/1/68 31/4/68
f 28/1/69 29/4/69 42/2/69
f 50/3/70 41/1/70 24/4/70
f 51/3/71 44/1/71 25/4/71
f 11/3/72 30/1/72 34/4/72
f 12/3/73 31/1/73 35/4/73
f 52/3/74 45/1/74 26/4/74
f 20/1/75 18/4/75 21/2/75
f 6/1/76 9/4/76 18/2/76
f 9/1/77 19/4/77 21/2/77
f 37/1/78 42/4/78 50/2/78
f 13/3/79 32/1/79 36/4/79
f 44/3/80 24/1/80 20/4/80
f 23/3/81 33/1/81 49/4/81
f 43/3/82 40/1/82 16/4/82
f 35/1/83 38/4/83 48/2/83
f 34/1/84 37/4/84 47/2/84
f 36/1/85 39/4/85 49/2/85
f 38/1/86 47/4/86 51/2/86
f 39/1/87 48/4/87 52/2/87
f 43/1/88 41/4/88 50/2/88
f 35/3/89 31/1/89 34/4/89
f 45/3/90 25/1/90 21/4/90
f 22/1/91 23/4/91 46/2/91
f 50/1/92 44/4/92 51/2/92
f 34/3/93 30/1/93 28/4/93
f 29/1/94 40/4/94 43/2/94
f 36/3/95 32/1/95 35/4/95
f 51/1/96 45/4/96 52/2/96
f 5/1/97 8/4/97 17/2/97
f 1/1/98 2/4/98 14/2/98
f 27/1/99 3/4/99 1/2/99
f 3/1/100 30/4/100 11/2/100

View File

@ -2,6 +2,7 @@ import struct
import vec3 import vec3
import obj import obj
import obj_state import obj_state
import obj_write
import sys import sys
normals = [ normals = [
@ -35,49 +36,28 @@ def build_configuration_index_buffers(f, faces_by_normal, index_buffer):
if i % 8 == 7: if i % 8 == 7:
print() print()
def build_vertex_buffer(f, vertex_buffer):
for position, normal, texture in vertex_buffer:
position = vec3.mul(position, 0.5)
f.write(struct.pack("<eeeeeeee", *position, *normal, *texture))
def write_indices(f, index_buffer, start, count):
for i in range(count):
f.write(struct.pack("<B", index_buffer[start + i]))
def write_custom_obj(f, vertex_buffer, index_buffer, index_lookup, path):
index_start = len(index_buffer)
state = obj.parse_obj_from_filename(path)
obj_state.append_triangles(state, vertex_buffer, index_buffer, index_lookup)
index_count = len(index_buffer) - index_start
write_indices(f, index_buffer, index_start, index_count)
print(f"{index_start}, {index_count}, // {path}")
def main(): def main():
cube_index_buffer = []
cube_index_lookup = {}
vertex_buffer = [] vertex_buffer = []
index_buffer = [] index_buffer = []
index_lookup = {} index_lookup = {}
cube_state = obj.parse_obj_from_filename("cube.obj") cube_state = obj.parse_obj_from_filename("cube.obj")
obj_state.append_triangles(cube_state, vertex_buffer, cube_index_buffer, cube_index_lookup) tmp_index_buffer = [] # discarded
cube_faces_by_normal = obj_state.build_faces_by_normal(vertex_buffer, cube_index_buffer) obj_state.append_triangles(cube_state, vertex_buffer, tmp_index_buffer, index_lookup)
cube_faces_by_normal = obj_state.build_faces_by_normal(vertex_buffer, tmp_index_buffer)
build_configuration_index_buffers(f, cube_faces_by_normal, index_buffer)
obj_write.write_obj(vertex_buffer, index_buffer, index_lookup, "tallgrass.obj")
obj_write.write_obj(vertex_buffer, index_buffer, index_lookup, "fence.obj")
obj_write.write_obj(vertex_buffer, index_buffer, index_lookup, "torch.obj")
obj_write.write_obj(vertex_buffer, index_buffer, index_lookup, "wheat.obj")
with open("../configuration.idx", "wb") as f: with open("../configuration.idx", "wb") as f:
build_configuration_index_buffers(f, cube_faces_by_normal, index_buffer) obj_write.write_indices(f, "<B", index_buffer)
index_lookup = {}
write_custom_obj(f, vertex_buffer, index_buffer, index_lookup, "tallgrass.obj")
index_lookup = {}
write_custom_obj(f, vertex_buffer, index_buffer, index_lookup, "fence.obj")
index_lookup = {}
write_custom_obj(f, vertex_buffer, index_buffer, index_lookup, "torch.obj")
index_lookup = {}
write_custom_obj(f, vertex_buffer, index_buffer, index_lookup, "wheat.obj")
with open("../per_vertex.vtx", "wb") as f: with open("../per_vertex.vtx", "wb") as f:
build_vertex_buffer(f, vertex_buffer) obj_write.write_vertex_buffer(f, vertex_buffer)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -0,0 +1,17 @@
import obj_write
def main():
vertex_buffer = []
index_buffer = []
index_lookup = {}
obj_write.write_obj(vertex_buffer, index_buffer, index_lookup, "rounded-rectangle.obj")
with open("../non_block.idx", "wb") as f:
obj_write.write_indices(f, "<H", index_buffer)
with open("../non_block.vtx", "wb") as f:
obj_write.write_vertex_buffer(f, vertex_buffer)
if __name__ == "__main__":
main()

BIN
minecraft/non_block.idx Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 B

BIN
minecraft/non_block.vtx Normal file

Binary file not shown.

18
shader/non_block.frag Normal file
View File

@ -0,0 +1,18 @@
#version 330 core
in VS_OUT {
vec3 Position; // world coordinates
vec3 Normal;
vec2 Texture;
} fs_in;
layout (location = 0) out vec3 Position;
layout (location = 1) out vec3 Normal;
layout (location = 2) out vec3 Color;
void main()
{
Position = fs_in.Position;
Normal = normalize(fs_in.Normal);
Color = vec3(0, 1, 1);
}

24
shader/non_block.vert Normal file
View File

@ -0,0 +1,24 @@
#version 330 core
// per-vertex:
in vec3 Position;
in vec3 Normal;
in vec2 Texture;
out VS_OUT {
vec3 Position;
vec3 Normal;
vec2 Texture;
} vs_out;
uniform mat4 WorldTransform;
uniform mat4 Transform;
void main()
{
vs_out.Position = (WorldTransform * vec4(Position.xzy, 1.0)).xyz;
vs_out.Normal = (WorldTransform * vec4(Normal.xzy, 0.0)).xyz;
vs_out.Texture = Texture;
gl_Position = Transform * vec4(Position.xzy, 1.0);
}

View File

@ -85,7 +85,7 @@ namespace font {
int best_font(font_desc const * const descs, int length) int best_font(font_desc const * const descs, int length)
{ {
int dimension = min(g_window_width, g_window_height); int dimension = min(window::width, window::height);
int ideal_height = (16 * dimension) / 1024; int ideal_height = (16 * dimension) / 1024;
//printf("ideal_height: %d\n", ideal_height); //printf("ideal_height: %d\n", ideal_height);
int nearest = dimension; int nearest = dimension;
@ -126,7 +126,7 @@ namespace font {
XMMATRIX transform = XMMATRIX transform =
XMMatrixScaling(font.desc->glyph_width, font.desc->glyph_height, 0) XMMatrixScaling(font.desc->glyph_width, font.desc->glyph_height, 0)
* XMMatrixTranslation(x, -y, 0) * XMMatrixTranslation(x, -y, 0)
* XMMatrixScaling(2.0f / g_window_width, 2.0f / g_window_height, 0) * XMMatrixScaling(2.0f / window::width, 2.0f / window::height, 0)
* XMMatrixTranslation(-1, 1, 0); * XMMatrixTranslation(-1, 1, 0);
XMFLOAT4X4 transformf; XMFLOAT4X4 transformf;
XMStoreFloat4x4(&transformf, transform); XMStoreFloat4x4(&transformf, transform);

133
src/non_block.cpp Normal file
View File

@ -0,0 +1,133 @@
#include <stdlib.h>
#include <stdio.h>
#include "glad/gl.h"
#include "directxmath/directxmath.h"
#include "opengl.h"
#include "file.h"
#include "non_block.h"
#include "view.h"
namespace non_block {
unsigned int vertex_array_object;
unsigned int index_buffer;
unsigned int per_vertex_buffer;
const int per_vertex_size = (3 + 3 + 2) * 2;
struct location {
struct {
unsigned int position;
unsigned int normal;
unsigned int texture;
} attrib;
struct {
unsigned int transform;
unsigned int world_transform;
} uniform;
};
location location;
unsigned int program;
void load_index_buffer()
{
glGenBuffers(1, &index_buffer);
int index_buffer_data_size;
void * index_buffer_data = read_file("minecraft/non_block.idx", &index_buffer_data_size);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_data_size, index_buffer_data, GL_STATIC_DRAW);
free(index_buffer_data);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void load_per_vertex_buffer()
{
glGenBuffers(1, &per_vertex_buffer);
int vertex_buffer_data_size;
void * vertex_buffer_data = read_file("minecraft/non_block.vtx", &vertex_buffer_data_size);
glBindBuffer(GL_ARRAY_BUFFER, per_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data_size, vertex_buffer_data, GL_STATIC_DRAW);
free(vertex_buffer_data);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void load_vertex_attributes()
{
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
glVertexBindingDivisor(0, 0);
glEnableVertexAttribArray(location.attrib.position);
glVertexAttribFormat(location.attrib.position, 3, GL_HALF_FLOAT, GL_FALSE, 0);
glVertexAttribBinding(location.attrib.position, 0);
glEnableVertexAttribArray(location.attrib.normal);
glVertexAttribFormat(location.attrib.normal, 3, GL_HALF_FLOAT, GL_FALSE, 6);
glVertexAttribBinding(location.attrib.normal, 0);
glEnableVertexAttribArray(location.attrib.texture);
glVertexAttribFormat(location.attrib.texture, 2, GL_HALF_FLOAT, GL_FALSE, 12);
glVertexAttribBinding(location.attrib.texture, 0);
glBindVertexArray(0);
}
void load_program()
{
program = compile_from_files("shader/non_block.vert",
NULL,
"shader/non_block.frag");
location.attrib.position = glGetAttribLocation(program, "Position");
location.attrib.normal = glGetAttribLocation(program, "Normal");
location.attrib.texture = glGetAttribLocation(program, "Texture");
printf("non_block program:\n");
printf(" attributes:\n position %u\n normal %u\n texture %u\n",
location.attrib.position,
location.attrib.normal,
location.attrib.texture);
location.uniform.world_transform = glGetUniformLocation(program, "WorldTransform");
location.uniform.transform = glGetUniformLocation(program, "Transform");
printf(" uniforms:\n world_transform %u\n transform %u\n",
location.uniform.world_transform,
location.uniform.transform);
}
void draw()
{
glUseProgram(program);
glBlendFunc(GL_ONE, GL_ZERO);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GREATER);
XMVECTOR offset = view::state.eye + view::state.forward * 4.0f;
XMMATRIX world_transform = XMMatrixTranslationFromVector(offset);
XMMATRIX transform = world_transform * view::state.transform;
XMFLOAT4X4 float_world_transform;
XMStoreFloat4x4(&float_world_transform, world_transform);
XMFLOAT4X4 float_transform;
XMStoreFloat4x4(&float_transform, transform);
glUniformMatrix4fv(location.uniform.transform, 1, false, (float *)&float_transform);
glUniformMatrix4fv(location.uniform.world_transform, 1, false, (float *)&float_world_transform);
glBindVertexArray(vertex_array_object);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBindVertexBuffer(0, per_vertex_buffer, 0, per_vertex_size);
void const * indices = (void *)(0);
int element_count = 300;
glDrawElements(GL_TRIANGLES, element_count, GL_UNSIGNED_SHORT, indices);
}
}

View File

@ -11,6 +11,8 @@
#include "bresenham.h" #include "bresenham.h"
#include "file.h" #include "file.h"
#include "world.h" #include "world.h"
#include "view.h"
#include "non_block.h"
#include "data.inc" #include "data.inc"
@ -342,8 +344,8 @@ static target_type const geometry_buffer_pnc_types[3] = {
template <int render_target_count> template <int render_target_count>
void init_geometry_buffer(geometry_buffer<render_target_count>& geometry_buffer, target_type const * const types) void init_geometry_buffer(geometry_buffer<render_target_count>& geometry_buffer, target_type const * const types)
{ {
int width = g_window_width; int width = window::width;
int height = g_window_height; int height = window::height;
if ((geometry_buffer.initialized == 1) && (width == geometry_buffer.width) && (height == geometry_buffer.height)) { if ((geometry_buffer.initialized == 1) && (width == geometry_buffer.width) && (height == geometry_buffer.height)) {
return; return;
@ -463,17 +465,6 @@ extern "C" {
void * SDL_GL_GetProcAddress(const char *proc); void * SDL_GL_GetProcAddress(const char *proc);
} }
struct view_state {
XMVECTOR up;
XMVECTOR eye;
XMVECTOR forward;
XMVECTOR direction;
float fov;
float pitch;
};
view_state view_state;
font::font * terminus_fonts; font::font * terminus_fonts;
struct short_point { struct short_point {
@ -582,9 +573,9 @@ void load_line()
void load_line_point_from_eye(int point_ix) void load_line_point_from_eye(int point_ix)
{ {
int x = XMVectorGetX(view_state.eye); int x = XMVectorGetX(view::state.eye);
int y = XMVectorGetY(view_state.eye); int y = XMVectorGetY(view::state.eye);
int z = XMVectorGetZ(view_state.eye); int z = XMVectorGetZ(view::state.eye);
line_state.point[point_ix].x = x; line_state.point[point_ix].x = x;
line_state.point[point_ix].y = z; line_state.point[point_ix].y = z;
@ -607,13 +598,13 @@ void load(const char * source_path)
load_textures(); load_textures();
load_texture_id_uniform_buffer(); load_texture_id_uniform_buffer();
view_state.up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); view::state.up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
view_state.eye = XMVectorSet(-55.5f, 48.25f, 50.0f, 1); view::state.eye = XMVectorSet(-55.5f, 48.25f, 50.0f, 1);
view_state.forward = XMVectorSet(-0.63, 0.78, 0, 0); view::state.forward = XMVectorSet(-0.63, 0.78, 0, 0);
view_state.direction = view_state.forward; view::state.direction = view::state.forward;
view_state.pitch = -0.11; view::state.pitch = -0.11;
view_state.fov = 1.5; view::state.fov = 1.5;
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// font // font
@ -652,6 +643,15 @@ void load(const char * source_path)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
load_world(); load_world();
//////////////////////////////////////////////////////////////////////
// non_block
//////////////////////////////////////////////////////////////////////
non_block::load_program();
non_block::load_index_buffer();
non_block::load_per_vertex_buffer();
non_block::load_vertex_attributes();
} }
float _ry = 0.0; float _ry = 0.0;
@ -667,9 +667,9 @@ light_parameters lighting = {
void update_keyboard(int up, int down, int left, int right) void update_keyboard(int up, int down, int left, int right)
{ {
XMVECTOR normal = XMVector3NormalizeEst(XMVector3Cross(view_state.forward, view_state.up)); XMVECTOR normal = XMVector3NormalizeEst(XMVector3Cross(view::state.forward, view::state.up));
view_state.eye += view_state.forward * (0.1f * up + -0.1f * down); view::state.eye += view::state.forward * (0.1f * up + -0.1f * down);
view_state.eye += normal * (-0.1f * left + 0.1f * right); view::state.eye += normal * (-0.1f * left + 0.1f * right);
} }
const int max_joysticks = 8; const int max_joysticks = 8;
@ -682,24 +682,24 @@ void update_joystick(int joystick_index,
int leftshoulder, int rightshoulder, int leftshoulder, int rightshoulder,
int start) int start)
{ {
//view_state.yaw += rx; //view::state.yaw += rx;
XMMATRIX mrz = XMMatrixRotationZ(rx * -0.035); XMMATRIX mrz = XMMatrixRotationZ(rx * -0.035);
view_state.forward = XMVector3Transform(XMVector3NormalizeEst(view_state.forward), mrz); view::state.forward = XMVector3Transform(XMVector3NormalizeEst(view::state.forward), mrz);
XMVECTOR normal = XMVector3NormalizeEst(XMVector3Cross(view_state.forward, view_state.up)); XMVECTOR normal = XMVector3NormalizeEst(XMVector3Cross(view::state.forward, view::state.up));
view_state.pitch += ry * -0.035; view::state.pitch += ry * -0.035;
if (view_state.pitch > 1.57f) view_state.pitch = 1.57f; if (view::state.pitch > 1.57f) view::state.pitch = 1.57f;
if (view_state.pitch < -1.57f) view_state.pitch = -1.57f; if (view::state.pitch < -1.57f) view::state.pitch = -1.57f;
XMMATRIX mrn = XMMatrixRotationAxis(normal, view_state.pitch); XMMATRIX mrn = XMMatrixRotationAxis(normal, view::state.pitch);
view_state.direction = XMVector3Transform(view_state.forward, mrn); view::state.direction = XMVector3Transform(view::state.forward, mrn);
view_state.eye += view_state.forward * -ly + normal * lx + view_state.up * (tl - tr); view::state.eye += view::state.forward * -ly + normal * lx + view::state.up * (tl - tr);
float new_fov = view_state.fov + 0.01 * up + -0.01 * down; float new_fov = view::state.fov + 0.01 * up + -0.01 * down;
if (new_fov > 0.00001f) { if (new_fov > 0.00001f) {
view_state.fov = new_fov; view::state.fov = new_fov;
} }
lighting.quadratic += 0.01 * a + -0.01 * b; lighting.quadratic += 0.01 * a + -0.01 * b;
if (lighting.quadratic < 0.0f) if (lighting.quadratic < 0.0f)
@ -724,6 +724,8 @@ void update_joystick(int joystick_index,
void update(float time) void update(float time)
{ {
current_time = time; current_time = time;
view::update_transforms();
} }
static inline int popcount(int x) static inline int popcount(int x)
@ -796,7 +798,7 @@ void draw_hud()
font::draw_start(ter_best, empty_vertex_array_object, quad_index_buffer); font::draw_start(ter_best, empty_vertex_array_object, quad_index_buffer);
labeled_value<float>(buf, "fov: ", "%.3f", view_state.fov); labeled_value<float>(buf, "fov: ", "%.3f", view::state.fov);
font::draw_string(ter_best, buf, 10, y); font::draw_string(ter_best, buf, 10, y);
y += ter_best.desc->glyph_height; y += ter_best.desc->glyph_height;
@ -812,10 +814,10 @@ void draw_hud()
font::draw_string(ter_best, buf, 10, y); font::draw_string(ter_best, buf, 10, y);
y += ter_best.desc->glyph_height; y += ter_best.desc->glyph_height;
y = draw_vector(ter_best, buf, y, "eye", view_state.eye); y = draw_vector(ter_best, buf, y, "eye", view::state.eye);
y = draw_vector(ter_best, buf, y, "forward", view_state.forward); y = draw_vector(ter_best, buf, y, "forward", view::state.forward);
labeled_value<float>(buf, "pitch: ", "%.9f", view_state.pitch); labeled_value<float>(buf, "pitch: ", "%.9f", view::state.pitch);
font::draw_string(ter_best, buf, 10, y); font::draw_string(ter_best, buf, 10, y);
y += ter_best.desc->glyph_height; y += ter_best.desc->glyph_height;
@ -829,33 +831,8 @@ void draw_hud()
y += ter_best.desc->glyph_height; y += ter_best.desc->glyph_height;
} }
static inline XMMATRIX current_projection()
{
float fov_angle_y = XMConvertToRadians(45 * view_state.fov);
float aspect_ratio = g_window_width / g_window_height;
float near_z = 1.0;
float far_z = 0.1;
XMMATRIX projection = XMMatrixPerspectiveFovRH(fov_angle_y, aspect_ratio, near_z, far_z);
return projection;
}
static inline XMMATRIX current_view()
{
XMVECTOR at = XMVectorAdd(view_state.eye, view_state.direction);
XMMATRIX view = XMMatrixLookAtRH(view_state.eye, at, view_state.up);
return view;
}
static inline XMMATRIX current_view_projection()
{
return current_view() * current_projection();
}
void draw_minecraft() void draw_minecraft()
{ {
// possibly re-initialize geometry buffer if window width/height changes
init_geometry_buffer(geometry_buffer_pnc, geometry_buffer_pnc_types);
glUseProgram(test_program); glUseProgram(test_program);
glBlendFunc(GL_ONE, GL_ZERO); glBlendFunc(GL_ONE, GL_ZERO);
@ -865,8 +842,7 @@ void draw_minecraft()
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
XMMATRIX transform = current_view_projection(); glUniformMatrix4fv(test_location.uniform.transform, 1, false, (float *)&view::state.float_transform);
glUniformMatrix4fv(test_location.uniform.transform, 1, false, (float *)&transform);
glUniform1i(test_location.uniform.terrain_sampler, 0); glUniform1i(test_location.uniform.terrain_sampler, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, test_location.binding.texture_id, texture_id_uniform_buffer); glBindBufferBase(GL_UNIFORM_BUFFER, test_location.binding.texture_id, texture_id_uniform_buffer);
@ -960,7 +936,7 @@ void draw_lighting()
XMFLOAT3 eye; XMFLOAT3 eye;
XMStoreFloat3(&eye, view_state.eye); XMStoreFloat3(&eye, view::state.eye);
glUniform3fv(lighting_location.uniform.eye, 1, (float*)&eye); glUniform3fv(lighting_location.uniform.eye, 1, (float*)&eye);
glUniform3fv(lighting_location.uniform.mouse_position, 1, (float*)&mouse_position); glUniform3fv(lighting_location.uniform.mouse_position, 1, (float*)&mouse_position);
/* /*
@ -988,8 +964,7 @@ void draw_line()
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS); glDepthFunc(GL_ALWAYS);
XMMATRIX transform = current_view_projection(); glUniformMatrix4fv(line_location.uniform.transform, 1, false, (float *)&view::state.float_transform);
glUniformMatrix4fv(line_location.uniform.transform, 1, false, (float *)&transform);
//glEnable(GL_CULL_FACE); //glEnable(GL_CULL_FACE);
//glCullFace(GL_FRONT); //glCullFace(GL_FRONT);
@ -1028,6 +1003,7 @@ void update_mouse(int x, int y)
glBindFramebuffer(GL_READ_FRAMEBUFFER, geometry_buffer_pnc.framebuffer); glBindFramebuffer(GL_READ_FRAMEBUFFER, geometry_buffer_pnc.framebuffer);
glReadBuffer(geometry_buffer_pnc_types[target_name::POSITION].attachment); glReadBuffer(geometry_buffer_pnc_types[target_name::POSITION].attachment);
/*
x = clamp(x, geometry_buffer_pnc.width); x = clamp(x, geometry_buffer_pnc.width);
y = clamp(y, geometry_buffer_pnc.height); y = clamp(y, geometry_buffer_pnc.height);
glReadPixels(x, glReadPixels(x,
@ -1037,23 +1013,24 @@ void update_mouse(int x, int y)
GL_RGB, GL_RGB,
GL_FLOAT, GL_FLOAT,
(void*)&mouse_position); (void*)&mouse_position);
*/
{ {
float mx = (2.0f * (float)x) / geometry_buffer_pnc.width - 1.0f; float mx = (2.0f * (float)x) / geometry_buffer_pnc.width - 1.0f;
float my = 1.0f - (2.0f * (float)y) / geometry_buffer_pnc.height; float my = 1.0f - (2.0f * (float)y) / geometry_buffer_pnc.height;
/* /*
XMVECTOR mouse_world = XMVector3Transform(mouse_clip, inverse); XMVECTOR mouse_world = XMVector3Transform(mouse_clip, inverse);
XMVECTOR ray = XMVector3Normalize(mouse_world - view_state.eye); XMVECTOR ray = XMVector3Normalize(mouse_world - view::state.eye);
mouse_ray_position = ray; mouse_ray_position = ray;
XMVECTOR ray_start = view_state.eye; XMVECTOR ray_start = view::state.eye;
XMVECTOR ray_end = ray_start + ray * 20.0f; XMVECTOR ray_end = ray_start + ray * 20.0f;
*/ */
XMVECTOR mouse_clip = XMVectorSet(mx, my, -1, 0); XMVECTOR mouse_clip = XMVectorSet(mx, my, -1, 0);
XMMATRIX projection_inverse = XMMatrixInverse(NULL, current_projection()); XMMATRIX projection_inverse = XMMatrixInverse(NULL, view::state.projection_transform);
XMMATRIX view_inverse = XMMatrixInverse(NULL, current_view()); XMMATRIX view_inverse = XMMatrixInverse(NULL, view::state.view_transform);
XMVECTOR mouse_view = XMVector3Transform(mouse_clip, projection_inverse); XMVECTOR mouse_view = XMVector3Transform(mouse_clip, projection_inverse);
mouse_ray_position = mouse_view; mouse_ray_position = mouse_view;
@ -1061,7 +1038,7 @@ void update_mouse(int x, int y)
mouse_view = XMVectorSetZ(mouse_view, -1); mouse_view = XMVectorSetZ(mouse_view, -1);
XMVECTOR ray = XMVector3Normalize(XMVector3TransformNormal(mouse_view, view_inverse)); XMVECTOR ray = XMVector3Normalize(XMVector3TransformNormal(mouse_view, view_inverse));
XMVECTOR ray_start = view_state.eye; XMVECTOR ray_start = view::state.eye;
XMVECTOR ray_end = ray_start + ray * 20.0f; XMVECTOR ray_end = ray_start + ray * 20.0f;
line_state.point[0].x = roundf(XMVectorGetX(ray_start)); line_state.point[0].x = roundf(XMVectorGetX(ray_start));
@ -1080,11 +1057,15 @@ void draw()
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(-1.0f); glClearDepth(-1.0f);
// possibly re-initialize geometry buffer if window width/height changes
init_geometry_buffer(geometry_buffer_pnc, geometry_buffer_pnc_types);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, geometry_buffer_pnc.framebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, geometry_buffer_pnc.framebuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_minecraft(); draw_minecraft();
//draw_line(); //draw_line();
non_block::draw();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

46
src/view.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "directxmath/directxmath.h"
#include "window.h"
namespace view {
struct view_state {
XMVECTOR up;
XMVECTOR eye;
XMVECTOR forward;
XMVECTOR direction;
float fov;
float pitch;
XMMATRIX projection_transform;
XMMATRIX view_transform;
XMMATRIX transform;
XMFLOAT4X4 float_transform;
};
view_state state;
static inline XMMATRIX current_projection()
{
float fov_angle_y = XMConvertToRadians(45 * state.fov);
float aspect_ratio = window::width / window::height;
float near_z = 1.0;
float far_z = 0.1;
XMMATRIX projection = XMMatrixPerspectiveFovRH(fov_angle_y, aspect_ratio, near_z, far_z);
return projection;
}
static inline XMMATRIX current_view()
{
XMVECTOR at = XMVectorAdd(state.eye, state.direction);
XMMATRIX view = XMMatrixLookAtRH(state.eye, at, state.up);
return view;
}
void update_transforms()
{
state.projection_transform = current_projection();
state.view_transform = current_view();
state.transform = state.view_transform * state.projection_transform;
XMStoreFloat4x4(&state.float_transform, state.transform);
}
}

View File

@ -1,10 +1,13 @@
#include "window.h" #include "window.h"
float g_window_width = 1; namespace window {
float g_window_height = 1; float width = 1;
float height = 1;
}
// C ABI called from Lua
void update_window(int width, int height) void update_window(int width, int height)
{ {
g_window_width = width; window::width = width;
g_window_height = height; window::height = height;
} }