example: add shadow_volume
This commit is contained in:
parent
9f4d743dbb
commit
b39abc0b85
307
blender_shadow_volume.py
Normal file
307
blender_shadow_volume.py
Normal file
@ -0,0 +1,307 @@
|
||||
import bpy
|
||||
import bmesh
|
||||
from mathutils import Vector
|
||||
from collections import defaultdict
|
||||
from itertools import combinations, chain
|
||||
|
||||
def sprint(*text):
|
||||
screen = bpy.data.screens['Scripting']
|
||||
for area in screen.areas:
|
||||
if area.type != "CONSOLE":
|
||||
continue
|
||||
override = {'screen': screen, 'area': area}
|
||||
with bpy.context.temp_override(**override):
|
||||
bpy.ops.console.scrollback_append(text=" ".join(map(str, text)))
|
||||
|
||||
print = sprint
|
||||
|
||||
def create_shadow_volume_mesh(light, o: bpy.types.Object, vertex_indices=None):
|
||||
mesh = bpy.data.meshes.new("test")
|
||||
|
||||
if vertex_indices is None:
|
||||
vertex_indices = range(len(o.data.vertices))
|
||||
|
||||
length = len(vertices)
|
||||
mesh.vertices.add(1 + length)
|
||||
origin = mesh.vertices[0]
|
||||
origin.co = Vector(light.location)
|
||||
|
||||
mesh.edges.add(length)
|
||||
|
||||
for i, v in enumerate(vertices):
|
||||
v = o.data.vertices[ix]
|
||||
world_v = Vector(o.matrix_world @ v.co)
|
||||
mesh.vertices[1 + i].co = world_v
|
||||
mesh.edges[i].vertices = [0, 1 + i]
|
||||
|
||||
object = bpy.data.objects.new("test", mesh)
|
||||
|
||||
bpy.context.scene.collection.objects.link(object)
|
||||
|
||||
def cast_ray(light, o, ix):
|
||||
v = o.data.vertices[ix]
|
||||
start = Vector(o.matrix_world @ v.co)
|
||||
ray = start - light.location
|
||||
#ray = Vector((0, 0, 0)) - light.location
|
||||
ray.normalize()
|
||||
end = start + (ray * 10)
|
||||
return start, end
|
||||
|
||||
def shadow_volume_mesh_rays(light, o: bpy.types.Object, loop):
|
||||
length = len(loop)
|
||||
mesh = bpy.data.meshes.new("test")
|
||||
#mesh.vertices.add(length * 2)
|
||||
#mesh.edges.add(length)
|
||||
|
||||
vertices = [0] * length * 2
|
||||
|
||||
for i, ix in enumerate(loop):
|
||||
start, end = cast_ray(light, o, ix)
|
||||
vertices[i * 2 + 0] = start
|
||||
vertices[i * 2 + 1] = end
|
||||
|
||||
bm = bmesh.new()
|
||||
#bm.from_mesh(mesh)
|
||||
for i in range(len(loop)):
|
||||
i1 = i
|
||||
i2 = (i + 1) % len(loop)
|
||||
bm.faces.new([
|
||||
bm.verts.new(vertices[i1 * 2 + 0]), # start
|
||||
bm.verts.new(vertices[i2 * 2 + 0]), # start
|
||||
bm.verts.new(vertices[i2 * 2 + 1]), # end
|
||||
bm.verts.new(vertices[i1 * 2 + 1]), # end
|
||||
])
|
||||
bm.to_mesh(mesh)
|
||||
bm.free()
|
||||
object = bpy.data.objects.new("test", mesh)
|
||||
|
||||
bpy.context.scene.collection.objects.link(object)
|
||||
|
||||
def polygon_edges(l):
|
||||
for i in range(len(l)):
|
||||
j = (i + 1) % len(l)
|
||||
yield frozenset((l[i], l[j]))
|
||||
|
||||
def polygons_by_edge_pairs(polygons):
|
||||
pairs = defaultdict(list)
|
||||
for i, polygon in enumerate(polygons):
|
||||
for edge in polygon_edges(polygon.vertices):
|
||||
pairs[edge].append(i)
|
||||
return list(pairs.items())
|
||||
|
||||
def face_indicators(light, o: bpy.types.Object):
|
||||
indicators = []
|
||||
for i, normal in enumerate(o.data.polygon_normals):
|
||||
n = o.matrix_world.to_3x3() @ normal.vector
|
||||
n.normalize()
|
||||
a = o.data.polygons[i].vertices[0]
|
||||
v = o.matrix_world @ o.data.vertices[a].co
|
||||
#d = v.dot(n)
|
||||
#indicator = n.dot(light.location) + d
|
||||
indicator = n.dot(light.location - v)
|
||||
indicators.append(indicator)
|
||||
return indicators
|
||||
|
||||
def edge_indices(o):
|
||||
return {
|
||||
frozenset(edge.vertices): i
|
||||
for i, edge in enumerate(o.data.edges)
|
||||
}
|
||||
|
||||
def object_silhouette(light, o: bpy.types.Object):
|
||||
indicators = face_indicators(light, o)
|
||||
edges = []
|
||||
for edge, polygons in polygons_by_edge_pairs(o.data.polygons):
|
||||
assert len(polygons) == 2, polygons
|
||||
a, b = polygons
|
||||
if (indicators[a] > 0) != (indicators[b] > 0):
|
||||
edges.append(edge)
|
||||
assert len(set(edges)) == len(edges)
|
||||
return edges, indicators
|
||||
|
||||
def delete_test_objects(collection):
|
||||
for o in collection.objects:
|
||||
if o.name.startswith("test"):
|
||||
collection.objects.unlink(o)
|
||||
|
||||
def edge_loop(edges):
|
||||
loop = list(edges.pop())
|
||||
while True:
|
||||
for i, (a, b) in enumerate(edges):
|
||||
if a == loop[-1]:
|
||||
if b in loop:
|
||||
return loop
|
||||
loop.append(b)
|
||||
elif b == loop[-1]:
|
||||
if a in loop:
|
||||
return loop
|
||||
loop.append(a)
|
||||
else:
|
||||
continue
|
||||
del edges[i]
|
||||
break
|
||||
else:
|
||||
return None
|
||||
|
||||
def append(l, a):
|
||||
if l[0] == -1:
|
||||
assert l[1] == -1
|
||||
l[0] = a
|
||||
else:
|
||||
assert l[1] == -1
|
||||
l[1] = a
|
||||
|
||||
def make_list(length):
|
||||
l = []
|
||||
for i in range(length):
|
||||
ll = [-1, -1]
|
||||
l.append(ll)
|
||||
return l
|
||||
|
||||
def edge_loop_graph(edges, num_vertices):
|
||||
edges_by_vertices = make_list(num_vertices)
|
||||
for i, (a, b) in enumerate(edges):
|
||||
append(edges_by_vertices[a], i)
|
||||
append(edges_by_vertices[b], i)
|
||||
return edges_by_vertices
|
||||
|
||||
def neq(a, b, y):
|
||||
assert a != -1
|
||||
assert b != -1
|
||||
if a == y:
|
||||
assert b != y
|
||||
return b
|
||||
else:
|
||||
assert a != y
|
||||
return a
|
||||
|
||||
def edge_loop2_inner(edges, graph, ix, visited_edges):
|
||||
loop = []
|
||||
while True:
|
||||
loop.append(ix)
|
||||
visited_edges[ix] = True
|
||||
|
||||
a, b = edges[ix]
|
||||
next_ix_a = neq(*graph[a], ix)
|
||||
next_ix_b = neq(*graph[b], ix)
|
||||
if not visited_edges[next_ix_a]:
|
||||
ix = next_ix_a
|
||||
continue
|
||||
elif not visited_edges[next_ix_b]:
|
||||
ix = next_ix_b
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
print("inner", loop)
|
||||
return loop
|
||||
|
||||
def next_unvisited(visited):
|
||||
for i, v in enumerate(visited):
|
||||
if v == False:
|
||||
return i
|
||||
return -1
|
||||
|
||||
def edge_loop2(edges, graph):
|
||||
visited_edges = [False] * len(edges)
|
||||
loops = []
|
||||
while True:
|
||||
start = next_unvisited(visited_edges)
|
||||
if start == -1:
|
||||
break
|
||||
loops.append(edge_loop2_inner(edges, graph, start, visited_edges))
|
||||
return loops
|
||||
|
||||
def edge_loops(edges):
|
||||
edges = list(edges)
|
||||
loops = []
|
||||
while edges:
|
||||
loop = edge_loop(edges)
|
||||
if loop is None:
|
||||
break
|
||||
loops.append(loop)
|
||||
return loops
|
||||
|
||||
def object_end_caps(light, o: bpy.types.Object, indicators):
|
||||
front = bpy.data.meshes.new("front")
|
||||
back = bpy.data.meshes.new("back")
|
||||
|
||||
bm_front = bmesh.new()
|
||||
bm_back = bmesh.new()
|
||||
|
||||
for i, polygon in enumerate(o.data.polygons):
|
||||
assert len(polygon.vertices) == 4
|
||||
a = o.matrix_world @ o.data.vertices[polygon.vertices[0]].co
|
||||
b = o.matrix_world @ o.data.vertices[polygon.vertices[1]].co
|
||||
c = o.matrix_world @ o.data.vertices[polygon.vertices[2]].co
|
||||
d = o.matrix_world @ o.data.vertices[polygon.vertices[3]].co
|
||||
if indicators[i] > 0:
|
||||
face = [
|
||||
bm_front.verts.new(a),
|
||||
bm_front.verts.new(b),
|
||||
bm_front.verts.new(c),
|
||||
bm_front.verts.new(d),
|
||||
]
|
||||
bm_front.faces.new(face)
|
||||
else:
|
||||
#ray = Vector((0, 0, 0)) - light.location
|
||||
ray_a = a - light.location
|
||||
ray_a.normalize()
|
||||
ray_b = b - light.location
|
||||
ray_b.normalize()
|
||||
ray_c = c - light.location
|
||||
ray_c.normalize()
|
||||
ray_d = d - light.location
|
||||
ray_d.normalize()
|
||||
face = [
|
||||
bm_back.verts.new(a + (ray_a * 10)),
|
||||
bm_back.verts.new(b + (ray_b * 10)),
|
||||
bm_back.verts.new(c + (ray_c * 10)),
|
||||
bm_back.verts.new(d + (ray_d * 10)),
|
||||
]
|
||||
bm_back.faces.new(face)
|
||||
|
||||
bm_front.to_mesh(front)
|
||||
bm_front.free()
|
||||
object_front = bpy.data.objects.new("test_front", front)
|
||||
bpy.context.scene.collection.objects.link(object_front)
|
||||
|
||||
bm_back.to_mesh(back)
|
||||
bm_back.free()
|
||||
object_back = bpy.data.objects.new("test_back", back)
|
||||
bpy.context.scene.collection.objects.link(object_back)
|
||||
|
||||
light = bpy.context.scene.objects['Light']
|
||||
cube = bpy.context.scene.objects['Torus']
|
||||
|
||||
delete_test_objects(bpy.context.scene.collection)
|
||||
edges, indicators = object_silhouette(light, cube)
|
||||
|
||||
object_end_caps(light, cube, indicators)
|
||||
|
||||
for loop in edge_loops(edges):
|
||||
print("loop", len(loop))
|
||||
shadow_volume_mesh_rays(light, cube, loop)
|
||||
|
||||
graph = edge_loop_graph(edges, len(cube.data.vertices))
|
||||
|
||||
"""
|
||||
print(graph)
|
||||
loops = edge_loop2(edges, graph)
|
||||
obj = bpy.context.edit_object
|
||||
bm = bmesh.from_edit_mesh(obj.data)
|
||||
for loop in loops:
|
||||
for edge_ix in loop:
|
||||
edge = edges[edge_ix]
|
||||
for e in bm.edges:
|
||||
if frozenset((e.verts[0].index, e.verts[1].index)) == frozenset(edge):
|
||||
e.select = True
|
||||
bmesh.update_edit_mesh(obj.data)
|
||||
"""
|
||||
|
||||
"""
|
||||
obj = bpy.context.edit_object
|
||||
bm = bmesh.from_edit_mesh(obj.data)
|
||||
bm.faces[4].select = True
|
||||
bmesh.update_edit_mesh(obj.data)
|
||||
"""
|
@ -1147,3 +1147,22 @@ MODIFIER_VOLUME_HOLE_OBJ = \
|
||||
|
||||
example/modifier_volume_hole.elf: LDSCRIPT = $(LIB)/main.lds
|
||||
example/modifier_volume_hole.elf: $(START_OBJ) $(MODIFIER_VOLUME_HOLE_OBJ)
|
||||
|
||||
SHADOW_VOLUME_OBJ = \
|
||||
example/shadow_volume.o \
|
||||
holly/core.o \
|
||||
holly/region_array.o \
|
||||
holly/background.o \
|
||||
holly/ta_fifo_polygon_converter.o \
|
||||
holly/video_output.o \
|
||||
sh7091/serial.o \
|
||||
maple/maple.o \
|
||||
sh7091/c_serial.o \
|
||||
printf/printf.o \
|
||||
printf/unparse.o \
|
||||
printf/parse.o \
|
||||
shadow_volume.o \
|
||||
$(LIBGCC)
|
||||
|
||||
example/shadow_volume.elf: LDSCRIPT = $(LIB)/main.lds
|
||||
example/shadow_volume.elf: $(START_OBJ) $(SHADOW_VOLUME_OBJ)
|
||||
|
599
example/shadow_volume.cpp
Normal file
599
example/shadow_volume.cpp
Normal file
@ -0,0 +1,599 @@
|
||||
#include <bit>
|
||||
|
||||
#include "holly/background.hpp"
|
||||
#include "holly/core.hpp"
|
||||
#include "holly/core_bits.hpp"
|
||||
#include "holly/holly.hpp"
|
||||
#include "holly/isp_tsp.hpp"
|
||||
#include "holly/region_array.hpp"
|
||||
#include "holly/ta_bits.hpp"
|
||||
#include "holly/ta_fifo_polygon_converter.hpp"
|
||||
#include "holly/ta_global_parameter.hpp"
|
||||
#include "holly/ta_parameter.hpp"
|
||||
#include "holly/ta_vertex_parameter.hpp"
|
||||
#include "holly/texture_memory_alloc5.hpp"
|
||||
#include "holly/video_output.hpp"
|
||||
|
||||
#include "systembus.hpp"
|
||||
#include "systembus_bits.hpp"
|
||||
|
||||
#include "maple/maple.hpp"
|
||||
#include "maple/maple_host_command_writer.hpp"
|
||||
#include "maple/maple_bus_bits.hpp"
|
||||
#include "maple/maple_bus_commands.hpp"
|
||||
#include "maple/maple_bus_ft0.hpp"
|
||||
|
||||
#include "memorymap.hpp"
|
||||
|
||||
#include "sh7091/sh7091.hpp"
|
||||
#include "sh7091/sh7091_bits.hpp"
|
||||
#include "sh7091/serial.hpp"
|
||||
#include "printf/printf.h"
|
||||
|
||||
#include "math/float_types.hpp"
|
||||
#include "math/transform.hpp"
|
||||
|
||||
#include "interrupt.hpp"
|
||||
#include "assert.h"
|
||||
|
||||
#include "model/blender_export.h"
|
||||
#include "model/torus.h"
|
||||
|
||||
#include "shadow_volume.hpp"
|
||||
|
||||
static ft0::data_transfer::data_format data[4];
|
||||
|
||||
uint8_t send_buf[1024] __attribute__((aligned(32)));
|
||||
uint8_t recv_buf[1024] __attribute__((aligned(32)));
|
||||
|
||||
void do_get_condition()
|
||||
{
|
||||
auto writer = maple::host_command_writer(send_buf, recv_buf);
|
||||
|
||||
using command_type = maple::get_condition;
|
||||
using response_type = maple::data_transfer<ft0::data_transfer::data_format>;
|
||||
|
||||
auto [host_command, host_response]
|
||||
= writer.append_command_all_ports<command_type, response_type>();
|
||||
|
||||
for (int port = 0; port < 4; port++) {
|
||||
auto& data_fields = host_command[port].bus_data.data_fields;
|
||||
data_fields.function_type = std::byteswap(function_type::controller);
|
||||
}
|
||||
maple::dma_start(send_buf, writer.send_offset,
|
||||
recv_buf, writer.recv_offset);
|
||||
|
||||
for (uint8_t port = 0; port < 4; port++) {
|
||||
auto& bus_data = host_response[port].bus_data;
|
||||
if (bus_data.command_code != response_type::command_code) {
|
||||
return;
|
||||
}
|
||||
auto& data_fields = bus_data.data_fields;
|
||||
if ((std::byteswap(data_fields.function_type) & function_type::controller) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
data[port].digital_button = data_fields.data.digital_button;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
data[port].analog_coordinate_axis[i]
|
||||
= data_fields.data.analog_coordinate_axis[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vbr100()
|
||||
{
|
||||
serial::string("vbr100\n");
|
||||
interrupt_exception();
|
||||
}
|
||||
|
||||
void vbr400()
|
||||
{
|
||||
serial::string("vbr400\n");
|
||||
interrupt_exception();
|
||||
}
|
||||
|
||||
const int framebuffer_width = 640;
|
||||
const int framebuffer_height = 480;
|
||||
const int tile_width = framebuffer_width / 32;
|
||||
const int tile_height = framebuffer_height / 32;
|
||||
|
||||
constexpr uint32_t ta_alloc = 0
|
||||
| ta_alloc_ctrl::pt_opb::no_list
|
||||
| ta_alloc_ctrl::tm_opb::no_list
|
||||
| ta_alloc_ctrl::t_opb::no_list
|
||||
| ta_alloc_ctrl::om_opb::_32x4byte
|
||||
| ta_alloc_ctrl::o_opb::_32x4byte;
|
||||
|
||||
constexpr int ta_cont_count = 1;
|
||||
constexpr struct opb_size opb_size[ta_cont_count] = {
|
||||
{
|
||||
.opaque = 32 * 4,
|
||||
.opaque_modifier = 32 * 4,
|
||||
.translucent = 0,
|
||||
.translucent_modifier = 0,
|
||||
.punch_through = 0
|
||||
}
|
||||
};
|
||||
|
||||
static volatile int ta_in_use = 0;
|
||||
static volatile int core_in_use = 0;
|
||||
static volatile int next_frame = 0;
|
||||
static volatile int framebuffer_ix = 0;
|
||||
static volatile int next_frame_ix = 0;
|
||||
|
||||
static inline void pump_events(uint32_t istnrm)
|
||||
{
|
||||
if (istnrm & istnrm::v_blank_in) {
|
||||
system.ISTNRM = istnrm::v_blank_in;
|
||||
|
||||
next_frame = 1;
|
||||
holly.FB_R_SOF1 = texture_memory_alloc.framebuffer[next_frame_ix].start;
|
||||
}
|
||||
|
||||
if (istnrm & istnrm::end_of_render_tsp) {
|
||||
system.ISTNRM = istnrm::end_of_render_tsp
|
||||
| istnrm::end_of_render_isp
|
||||
| istnrm::end_of_render_video;
|
||||
|
||||
next_frame_ix = framebuffer_ix;
|
||||
framebuffer_ix += 1;
|
||||
if (framebuffer_ix >= 3) framebuffer_ix = 0;
|
||||
|
||||
core_in_use = 0;
|
||||
}
|
||||
|
||||
if (istnrm & istnrm::end_of_transferring_opaque_list) {
|
||||
system.ISTNRM = istnrm::end_of_transferring_opaque_list;
|
||||
|
||||
core_in_use = 1;
|
||||
core_start_render2(texture_memory_alloc.region_array.start,
|
||||
texture_memory_alloc.isp_tsp_parameters.start,
|
||||
texture_memory_alloc.background[0].start,
|
||||
texture_memory_alloc.framebuffer[framebuffer_ix].start,
|
||||
framebuffer_width);
|
||||
|
||||
ta_in_use = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void vbr600()
|
||||
{
|
||||
uint32_t sr;
|
||||
asm volatile ("stc sr,%0" : "=r" (sr));
|
||||
sr |= sh::sr::imask(15);
|
||||
asm volatile ("ldc %0,sr" : : "r" (sr));
|
||||
|
||||
if (sh7091.CCN.EXPEVT == 0 && sh7091.CCN.INTEVT == 0x320) {
|
||||
uint32_t istnrm = system.ISTNRM;
|
||||
uint32_t isterr = system.ISTERR;
|
||||
|
||||
if (isterr) {
|
||||
serial::string("isterr: ");
|
||||
serial::integer<uint32_t>(system.ISTERR);
|
||||
}
|
||||
|
||||
pump_events(istnrm);
|
||||
|
||||
sr &= ~sh::sr::imask(15);
|
||||
asm volatile ("ldc %0,sr" : : "r" (sr));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
serial::string("vbr600\n");
|
||||
interrupt_exception();
|
||||
}
|
||||
|
||||
void global_polygon_type_1(ta_parameter_writer& writer,
|
||||
uint32_t para_control_obj_control,
|
||||
bool always,
|
||||
const float a = 1.0f,
|
||||
const float r = 1.0f,
|
||||
const float g = 1.0f,
|
||||
const float b = 1.0f
|
||||
)
|
||||
{
|
||||
const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume
|
||||
| obj_control::col_type::intensity_mode_1
|
||||
| obj_control::gouraud
|
||||
| para_control_obj_control
|
||||
;
|
||||
|
||||
const uint32_t depth_compare_mode = always
|
||||
? isp_tsp_instruction_word::depth_compare_mode::always
|
||||
: isp_tsp_instruction_word::depth_compare_mode::greater_or_equal
|
||||
;
|
||||
const uint32_t isp_tsp_instruction_word = depth_compare_mode
|
||||
| isp_tsp_instruction_word::culling_mode::no_culling
|
||||
;
|
||||
|
||||
const uint32_t tsp_instruction_word = tsp_instruction_word::fog_control::no_fog
|
||||
| tsp_instruction_word::texture_shading_instruction::decal
|
||||
| tsp_instruction_word::src_alpha_instr::one
|
||||
| tsp_instruction_word::dst_alpha_instr::zero
|
||||
;
|
||||
|
||||
const uint32_t texture_control_word = 0;
|
||||
|
||||
writer.append<ta_global_parameter::polygon_type_1>() =
|
||||
ta_global_parameter::polygon_type_1(parameter_control_word,
|
||||
isp_tsp_instruction_word,
|
||||
tsp_instruction_word,
|
||||
texture_control_word,
|
||||
a,
|
||||
r,
|
||||
g,
|
||||
b
|
||||
);
|
||||
}
|
||||
|
||||
void global_polygon_modifier_volume(ta_parameter_writer * writer)
|
||||
{
|
||||
uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume
|
||||
| para_control::list_type::opaque_modifier_volume
|
||||
;
|
||||
|
||||
uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::volume_instruction::normal_polygon
|
||||
| isp_tsp_instruction_word::culling_mode::no_culling;
|
||||
|
||||
writer->append<ta_global_parameter::modifier_volume>() =
|
||||
ta_global_parameter::modifier_volume(parameter_control_word,
|
||||
isp_tsp_instruction_word);
|
||||
}
|
||||
|
||||
void global_polygon_modifier_volume_last_in_volume(ta_parameter_writer * writer)
|
||||
{
|
||||
const uint32_t last_parameter_control_word = para_control::para_type::polygon_or_modifier_volume
|
||||
| para_control::list_type::opaque_modifier_volume
|
||||
| obj_control::volume::modifier_volume::last_in_volume;
|
||||
|
||||
const uint32_t last_isp_tsp_instruction_word = isp_tsp_instruction_word::volume_instruction::inside_last_polygon
|
||||
| isp_tsp_instruction_word::culling_mode::no_culling;
|
||||
|
||||
writer->append<ta_global_parameter::modifier_volume>() =
|
||||
ta_global_parameter::modifier_volume(last_parameter_control_word,
|
||||
last_isp_tsp_instruction_word);
|
||||
}
|
||||
|
||||
static inline vec3 screen_transform(vec3 v)
|
||||
{
|
||||
float dim = 480 / 2.0;
|
||||
|
||||
return {
|
||||
v.x / (1.f * v.z) * dim + 640 / 2.0f,
|
||||
v.y / (1.f * v.z) * dim + 480 / 2.0f,
|
||||
1 / v.z,
|
||||
};
|
||||
}
|
||||
|
||||
static inline void render_quad(ta_parameter_writer& writer,
|
||||
vec3 ap,
|
||||
vec3 bp,
|
||||
vec3 cp,
|
||||
vec3 dp,
|
||||
float ai,
|
||||
float bi,
|
||||
float ci,
|
||||
float di)
|
||||
{
|
||||
if (ap.z < 0 || bp.z < 0 || cp.z < 0 || dp.z < 0)
|
||||
return;
|
||||
|
||||
writer.append<ta_vertex_parameter::polygon_type_2>() =
|
||||
ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false),
|
||||
ap.x, ap.y, ap.z,
|
||||
ai);
|
||||
|
||||
writer.append<ta_vertex_parameter::polygon_type_2>() =
|
||||
ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false),
|
||||
bp.x, bp.y, bp.z,
|
||||
bi);
|
||||
|
||||
writer.append<ta_vertex_parameter::polygon_type_2>() =
|
||||
ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(false),
|
||||
dp.x, dp.y, dp.z,
|
||||
di);
|
||||
|
||||
writer.append<ta_vertex_parameter::polygon_type_2>() =
|
||||
ta_vertex_parameter::polygon_type_2(polygon_vertex_parameter_control_word(true),
|
||||
cp.x, cp.y, cp.z,
|
||||
ci);
|
||||
}
|
||||
|
||||
#define fsrra(n) (1.0f / (sqrt(n)))
|
||||
|
||||
void transfer_line(ta_parameter_writer& writer, vec3 p1, vec3 p2)
|
||||
{
|
||||
float dy = p2.y - p1.y;
|
||||
float dx = p2.x - p1.x;
|
||||
float d = fsrra(dx * dx + dy * dy) * 0.7f;
|
||||
float dy1 = dy * d;
|
||||
float dx1 = dx * d;
|
||||
|
||||
vec3 ap = { p1.x + dy1, p1.y + -dx1, p1.z };
|
||||
vec3 bp = { p1.x + -dy1, p1.y + dx1, p1.z };
|
||||
vec3 cp = { p2.x + -dy1, p2.y + dx1, p2.z };
|
||||
vec3 dp = { p2.x + dy1, p2.y + -dx1, p2.z };
|
||||
|
||||
float li = 1.0f;
|
||||
|
||||
render_quad(writer, ap, bp, cp, dp, li, li, li, li);
|
||||
}
|
||||
|
||||
static ta_parameter_writer * _writer;
|
||||
static ta_parameter_writer * _sv_writer;
|
||||
|
||||
void render_quad_sv(vec3 a,
|
||||
vec3 b,
|
||||
vec3 c,
|
||||
vec3 d,
|
||||
bool last_in_volume)
|
||||
{
|
||||
float ai = 1.0f;
|
||||
float bi = 1.0f;
|
||||
float ci = 1.0f;
|
||||
float di = 1.0f;
|
||||
|
||||
vec3 ap = screen_transform(a);
|
||||
vec3 bp = screen_transform(b);
|
||||
vec3 cp = screen_transform(c);
|
||||
vec3 dp = screen_transform(d);
|
||||
|
||||
/*
|
||||
render_quad(*_writer,
|
||||
ap,
|
||||
bp,
|
||||
cp,
|
||||
dp,
|
||||
ai,
|
||||
bi,
|
||||
ci,
|
||||
di);
|
||||
*/
|
||||
|
||||
/*
|
||||
transfer_line(*_writer, ap, bp);
|
||||
transfer_line(*_writer, bp, cp);
|
||||
transfer_line(*_writer, cp, dp);
|
||||
transfer_line(*_writer, dp, ap);
|
||||
*/
|
||||
|
||||
/*
|
||||
A B A B B
|
||||
D C D D C
|
||||
*/
|
||||
|
||||
_sv_writer->append<ta_vertex_parameter::modifier_volume>() =
|
||||
ta_vertex_parameter::modifier_volume(modifier_volume_vertex_parameter_control_word(),
|
||||
ap.x, ap.y, ap.z,
|
||||
bp.x, bp.y, bp.z,
|
||||
dp.x, dp.y, dp.z);
|
||||
|
||||
if (last_in_volume) {
|
||||
global_polygon_modifier_volume_last_in_volume(_sv_writer);
|
||||
}
|
||||
|
||||
_sv_writer->append<ta_vertex_parameter::modifier_volume>() =
|
||||
ta_vertex_parameter::modifier_volume(modifier_volume_vertex_parameter_control_word(),
|
||||
bp.x, bp.y, bp.z,
|
||||
cp.x, cp.y, cp.z,
|
||||
dp.x, dp.y, dp.z);
|
||||
}
|
||||
|
||||
float _rotate_x = 0;
|
||||
|
||||
void transfer_mesh(ta_parameter_writer& writer,
|
||||
const mat4x4& screen_trans,
|
||||
const object * object,
|
||||
const vec3 light,
|
||||
const bool cast_shadow,
|
||||
const bool receive_shadow,
|
||||
const bool diffuse,
|
||||
vec3 color)
|
||||
{
|
||||
const mesh * mesh = object->mesh;
|
||||
|
||||
vec3 position[mesh->position_length];
|
||||
vec3 polygon_normal[mesh->polygon_normal_length];
|
||||
assert(mesh->polygon_normal_length == mesh->polygons_length);
|
||||
|
||||
mat4x4 trans = screen_trans
|
||||
* translate(object->location)
|
||||
* rotate_x(_rotate_x)
|
||||
* rotate_quaternion(object->rotation)
|
||||
* scale(object->scale);
|
||||
|
||||
for (int i = 0; i < mesh->position_length; i++) {
|
||||
position[i] = trans * mesh->position[i];
|
||||
}
|
||||
for (int i = 0; i < mesh->polygon_normal_length; i++) {
|
||||
polygon_normal[i] = normalize(normal_multiply(trans, mesh->polygon_normal[i]));
|
||||
}
|
||||
|
||||
bool always = false;
|
||||
uint32_t shadow = receive_shadow ? obj_control::shadow : 0;
|
||||
uint32_t control = para_control::list_type::opaque | shadow;
|
||||
global_polygon_type_1(writer,
|
||||
control,
|
||||
always,
|
||||
1.0f,
|
||||
color.x, color.y, color.z);
|
||||
|
||||
for (int i = 0; i < mesh->polygons_length; i++) {
|
||||
const polygon * p = &mesh->polygons[i];
|
||||
|
||||
vec3 ap = screen_transform(position[p->a]);
|
||||
vec3 bp = screen_transform(position[p->b]);
|
||||
vec3 cp = screen_transform(position[p->c]);
|
||||
vec3 dp = screen_transform(position[p->d]);
|
||||
|
||||
float li = 1.0f;
|
||||
if (diffuse) {
|
||||
vec3 light_dir = normalize(light - position[p->a]);
|
||||
float diffuse = max(dot(polygon_normal[i], light_dir), 0.0f);
|
||||
li = 0.5 + 0.6 * diffuse;
|
||||
}
|
||||
|
||||
render_quad(writer, ap, bp, cp, dp, li, li, li, li);
|
||||
}
|
||||
|
||||
if (cast_shadow) {
|
||||
global_polygon_modifier_volume(_sv_writer);
|
||||
|
||||
global_polygon_type_1(writer,
|
||||
control,
|
||||
always,
|
||||
1, 1, 0.5, 0.5);
|
||||
|
||||
shadow_volume_mesh(light, position, polygon_normal, mesh, render_quad_sv);
|
||||
}
|
||||
}
|
||||
|
||||
mat4x4 light_trans = mat4x4();
|
||||
float _torus_rx = 0;
|
||||
|
||||
void transfer_scene(ta_parameter_writer& writer, const mat4x4& screen_trans)
|
||||
{
|
||||
light_trans = rotate_z(0.01f) * light_trans;
|
||||
vec3 light = screen_trans * light_trans * objects[0].location;
|
||||
|
||||
// opaque list
|
||||
{
|
||||
_rotate_x = 0;
|
||||
transfer_mesh(writer, screen_trans * light_trans, &objects[0], light,
|
||||
false, // cast shadow
|
||||
false, // receive shadow
|
||||
false, // diffuse
|
||||
(vec3){0.9, 0.9, 0.9}
|
||||
);
|
||||
|
||||
_rotate_x = 0;
|
||||
transfer_mesh(writer, screen_trans, &objects[1], light,
|
||||
false, // cast shadow
|
||||
true, // receive shadow
|
||||
true, // diffuse
|
||||
(vec3){0.5, 0.9, 0.5}
|
||||
);
|
||||
|
||||
_rotate_x = _torus_rx;
|
||||
_torus_rx += 0.001f;
|
||||
transfer_mesh(writer, screen_trans, &objects[2], light,
|
||||
true, // cast shadow
|
||||
false, // receive shadow
|
||||
true, // diffuse
|
||||
(vec3){1, 0, 1}
|
||||
);
|
||||
|
||||
writer.append<ta_global_parameter::end_of_list>() =
|
||||
ta_global_parameter::end_of_list(para_control::para_type::end_of_list);
|
||||
|
||||
_sv_writer->append<ta_global_parameter::end_of_list>() =
|
||||
ta_global_parameter::end_of_list(para_control::para_type::end_of_list);
|
||||
}
|
||||
}
|
||||
|
||||
mat4x4 update_analog(mat4x4& screen_trans)
|
||||
{
|
||||
const float l_ = static_cast<float>(data[0].analog_coordinate_axis[0]) * (1.f / 255.f);
|
||||
const float r_ = static_cast<float>(data[0].analog_coordinate_axis[1]) * (1.f / 255.f);
|
||||
|
||||
const float x_ = static_cast<float>(data[0].analog_coordinate_axis[2] - 0x80) / 127.f;
|
||||
const float y_ = static_cast<float>(data[0].analog_coordinate_axis[3] - 0x80) / 127.f;
|
||||
|
||||
float y = -0.05f * x_;
|
||||
float x = 0.05f * y_;
|
||||
|
||||
float z = -0.05f * r_ + 0.05f * l_;
|
||||
|
||||
return translate((vec3){0, 0, z}) *
|
||||
screen_trans *
|
||||
rotate_x(x) *
|
||||
rotate_z(y);
|
||||
}
|
||||
|
||||
uint8_t __attribute__((aligned(32))) ta_parameter_buf1[1024 * 1024];
|
||||
uint8_t __attribute__((aligned(32))) ta_parameter_buf2[1024 * 1024];
|
||||
|
||||
int main()
|
||||
{
|
||||
sh7091.TMU.TSTR = 0; // stop all timers
|
||||
sh7091.TMU.TOCR = tmu::tocr::tcoe::tclk_is_external_clock_or_input_capture;
|
||||
sh7091.TMU.TCR0 = tmu::tcr0::tpsc::p_phi_256; // 256 / 50MHz = 5.12 μs ; underflows in ~1 hour
|
||||
sh7091.TMU.TCOR0 = 0xffff'ffff;
|
||||
sh7091.TMU.TCNT0 = 0xffff'ffff;
|
||||
sh7091.TMU.TSTR = tmu::tstr::str0::counter_start;
|
||||
|
||||
serial::init(0);
|
||||
|
||||
interrupt_init();
|
||||
|
||||
holly.SOFTRESET = softreset::pipeline_soft_reset
|
||||
| softreset::ta_soft_reset;
|
||||
holly.SOFTRESET = 0;
|
||||
|
||||
core_init();
|
||||
|
||||
holly.FPU_SHAD_SCALE = fpu_shad_scale::simple_shadow_enable::intensity_volume_mode
|
||||
| fpu_shad_scale::scale_factor_for_shadows(128);
|
||||
|
||||
system.IML6NRM = istnrm::end_of_render_tsp
|
||||
| istnrm::v_blank_in
|
||||
| istnrm::end_of_transferring_opaque_list;
|
||||
|
||||
region_array_multipass(tile_width,
|
||||
tile_height,
|
||||
opb_size,
|
||||
ta_cont_count,
|
||||
texture_memory_alloc.region_array.start,
|
||||
texture_memory_alloc.object_list.start);
|
||||
|
||||
background_parameter2(texture_memory_alloc.background[0].start,
|
||||
0xff202040);
|
||||
|
||||
ta_parameter_writer writer = ta_parameter_writer(ta_parameter_buf1, (sizeof (ta_parameter_buf1)));
|
||||
ta_parameter_writer sv_writer = ta_parameter_writer(ta_parameter_buf2, (sizeof (ta_parameter_buf2)));
|
||||
_writer = &writer;
|
||||
_sv_writer = &sv_writer;
|
||||
|
||||
video_output::set_mode_vga();
|
||||
|
||||
mat4x4 screen_trans = {
|
||||
1, 0, 0, 0,
|
||||
0, 0, -1, 0,
|
||||
0, 1, 0, 7,
|
||||
0, 0, 0, 1,
|
||||
};
|
||||
|
||||
do_get_condition();
|
||||
while (1) {
|
||||
maple::dma_wait_complete();
|
||||
do_get_condition();
|
||||
|
||||
screen_trans = update_analog(screen_trans);
|
||||
|
||||
writer.offset = 0;
|
||||
sv_writer.offset = 0;
|
||||
transfer_scene(writer, screen_trans);
|
||||
|
||||
while (ta_in_use);
|
||||
while (core_in_use);
|
||||
ta_in_use = 1;
|
||||
ta_polygon_converter_init2(texture_memory_alloc.isp_tsp_parameters.start,
|
||||
texture_memory_alloc.isp_tsp_parameters.end,
|
||||
texture_memory_alloc.object_list.start,
|
||||
texture_memory_alloc.object_list.end,
|
||||
opb_size[0].total(),
|
||||
ta_alloc,
|
||||
tile_width,
|
||||
tile_height);
|
||||
ta_polygon_converter_writeback(sv_writer.buf, sv_writer.offset);
|
||||
ta_polygon_converter_transfer(sv_writer.buf, sv_writer.offset);
|
||||
ta_wait_opaque_modifier_volume_list();
|
||||
ta_polygon_converter_writeback(writer.buf, writer.offset);
|
||||
ta_polygon_converter_transfer(writer.buf, writer.offset);
|
||||
|
||||
while (next_frame == 0);
|
||||
next_frame = 0;
|
||||
}
|
||||
}
|
15
math/float_types.hpp
Normal file
15
math/float_types.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "math/vec2.hpp"
|
||||
#include "math/vec3.hpp"
|
||||
#include "math/vec4.hpp"
|
||||
#include "math/mat2x2.hpp"
|
||||
#include "math/mat3x3.hpp"
|
||||
#include "math/mat4x4.hpp"
|
||||
|
||||
using vec2 = vec<2, float>;
|
||||
using vec3 = vec<3, float>;
|
||||
using vec4 = vec<4, float>;
|
||||
using mat2x2 = mat<2, 2, float>;
|
||||
using mat3x3 = mat<3, 3, float>;
|
||||
using mat4x4 = mat<4, 4, float>;
|
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
struct polygon {
|
||||
int a, b, c, d;
|
||||
int material_index;
|
||||
@ -10,6 +12,19 @@ struct mesh_material {
|
||||
int offset;
|
||||
};
|
||||
|
||||
struct edge {
|
||||
int a; // vertices index
|
||||
int b; // vertices index
|
||||
};
|
||||
|
||||
struct edge_polygon {
|
||||
struct edge edge;
|
||||
struct {
|
||||
int a;
|
||||
int b;
|
||||
} polygon_index; // polygon indices
|
||||
};
|
||||
|
||||
struct mesh {
|
||||
const vec3 * position;
|
||||
const int position_length;
|
||||
@ -23,6 +38,8 @@ struct mesh {
|
||||
const int uv_layers_length;
|
||||
const mesh_material * materials;
|
||||
const int materials_length;
|
||||
const edge_polygon * edge_polygons;
|
||||
const int edge_polygons_length;
|
||||
};
|
||||
|
||||
struct object {
|
||||
|
BIN
model/torus.blend
Normal file
BIN
model/torus.blend
Normal file
Binary file not shown.
14294
model/torus.h
Normal file
14294
model/torus.h
Normal file
File diff suppressed because it is too large
Load Diff
288
shadow_volume.cpp
Normal file
288
shadow_volume.cpp
Normal file
@ -0,0 +1,288 @@
|
||||
#include "shadow_volume.hpp"
|
||||
|
||||
#include "math/float_types.hpp"
|
||||
#include "assert.h"
|
||||
#include "printf/printf.h"
|
||||
|
||||
static inline void face_indicators(const vec3 light,
|
||||
const vec3 * position,
|
||||
const vec3 * polygon_normal,
|
||||
const mesh * mesh,
|
||||
float * indicators)
|
||||
{
|
||||
for (int i = 0; i < mesh->polygons_length; i++) {
|
||||
vec3 n = polygon_normal[i];
|
||||
vec3 p = position[mesh->polygons[i].a];
|
||||
|
||||
float indicator = dot(n, (light - p));
|
||||
indicators[i] = indicator;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int object_silhouette(const float * indicators,
|
||||
const mesh * mesh,
|
||||
int * edge_indices)
|
||||
{
|
||||
int ix = 0;
|
||||
for (int i = 0; i < mesh->edge_polygons_length; i++) {
|
||||
const edge_polygon * ep = &mesh->edge_polygons[i];
|
||||
if ((indicators[ep->polygon_index.a] > 0) != (indicators[ep->polygon_index.b] > 0)) {
|
||||
edge_indices[ix] = i;
|
||||
ix += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ix;
|
||||
}
|
||||
|
||||
struct graph {
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
|
||||
void graph_append(graph * g, int v)
|
||||
{
|
||||
if (!(g->a == -1 || g->b == -1)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (g->a == -1) {
|
||||
g->a = v;
|
||||
} else {
|
||||
g->b = v;
|
||||
}
|
||||
}
|
||||
|
||||
void edge_loop_graph(const mesh * mesh,
|
||||
const int * edge_indices,
|
||||
const int edge_indices_length,
|
||||
graph * graph)
|
||||
{
|
||||
for (int i = 0; i < mesh->position_length; i++) {
|
||||
graph[i].a = -1;
|
||||
graph[i].b = -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < edge_indices_length; i++) {
|
||||
int edge_index = edge_indices[i];
|
||||
const edge& edge = mesh->edge_polygons[edge_index].edge;
|
||||
graph_append(&graph[edge.a], i);
|
||||
graph_append(&graph[edge.b], i);
|
||||
}
|
||||
}
|
||||
|
||||
int next_neighbor(const graph& graph, int ix)
|
||||
{
|
||||
if (graph.a == ix)
|
||||
return graph.b;
|
||||
else
|
||||
return graph.a;
|
||||
}
|
||||
|
||||
int edge_loop_inner(const mesh * mesh,
|
||||
const int * edge_indices,
|
||||
const graph * graph,
|
||||
bool * visited_edge_indices,
|
||||
int ix,
|
||||
int * edge_loop)
|
||||
{
|
||||
int edge_loop_ix = 0;
|
||||
|
||||
const edge& e = mesh->edge_polygons[edge_indices[ix]].edge;
|
||||
edge_loop[edge_loop_ix] = e.b;
|
||||
edge_loop_ix += 1;
|
||||
|
||||
while (true) {
|
||||
visited_edge_indices[ix] = true;
|
||||
|
||||
int edge_index = edge_indices[ix];
|
||||
const edge& e = mesh->edge_polygons[edge_index].edge;
|
||||
int next_ix_a = next_neighbor(graph[e.a], ix);
|
||||
int next_ix_b = next_neighbor(graph[e.b], ix);
|
||||
if (visited_edge_indices[next_ix_a] == false) {
|
||||
edge_loop[edge_loop_ix] = e.a;
|
||||
edge_loop_ix += 1;
|
||||
|
||||
ix = next_ix_a;
|
||||
} else if (visited_edge_indices[next_ix_b] == false) {
|
||||
edge_loop[edge_loop_ix] = e.b;
|
||||
edge_loop_ix += 1;
|
||||
|
||||
ix = next_ix_b;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return edge_loop_ix;
|
||||
}
|
||||
|
||||
int next_unvisited(const bool * visited_edge_indices,
|
||||
const int edge_indices_length)
|
||||
{
|
||||
for (int i = 0; i < edge_indices_length; i++) {
|
||||
if (visited_edge_indices[i] == false)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int edge_loop(const mesh * mesh,
|
||||
const int * edge_indices,
|
||||
const int edge_indices_length,
|
||||
const graph * graph,
|
||||
int * edge_loops,
|
||||
int * edge_loop_lengths,
|
||||
int max_edge_loops)
|
||||
{
|
||||
bool visited_edge_indices[edge_indices_length];
|
||||
for (int i = 0; i < edge_indices_length; i++) {
|
||||
visited_edge_indices[i] = false;
|
||||
}
|
||||
|
||||
int edge_loop_ix = 0;
|
||||
int i;
|
||||
for (i = 0; i < max_edge_loops; i++) {
|
||||
int start = next_unvisited(visited_edge_indices, edge_indices_length);
|
||||
if (start == -1)
|
||||
break;
|
||||
int length = edge_loop_inner(mesh, edge_indices, graph, visited_edge_indices, start,
|
||||
&edge_loops[edge_loop_ix]);
|
||||
edge_loop_lengths[i] = length;
|
||||
edge_loop_ix += length;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static inline vec3 cast_ray(const vec3 light,
|
||||
const vec3 start)
|
||||
{
|
||||
vec3 ray = start - light;
|
||||
return start + (normalize(ray) * 7.f);
|
||||
}
|
||||
|
||||
void shadow_volume_mesh_rays(const vec3 light,
|
||||
const vec3 * position,
|
||||
const vec3 * cast_position,
|
||||
const int * edge_loop,
|
||||
const int edge_loop_length,
|
||||
void(*render_quad)(vec3 a, vec3 b, vec3 c, vec3 d, bool l))
|
||||
{
|
||||
for (int i = 0; i < edge_loop_length; i++) {
|
||||
int j = i + 1;
|
||||
if (j >= edge_loop_length) j = 0;
|
||||
|
||||
int i1 = edge_loop[i];
|
||||
int i2 = edge_loop[j];
|
||||
|
||||
vec3 a = position[i1];
|
||||
vec3 b = position[i2];
|
||||
vec3 c = cast_position[i2];
|
||||
vec3 d = cast_position[i1];
|
||||
|
||||
bool last_in_volume = (i == (edge_loop_length - 1));
|
||||
render_quad(a, b, c, d, last_in_volume);
|
||||
}
|
||||
}
|
||||
|
||||
void shadow_volume_end_caps(const vec3 light,
|
||||
const vec3 * position,
|
||||
const vec3 * cast_position,
|
||||
const mesh * mesh,
|
||||
const float * indicators,
|
||||
void(*render_quad)(vec3 a, vec3 b, vec3 c, vec3 d, bool l))
|
||||
{
|
||||
for (int i = 0; i < mesh->polygons_length; i++) {
|
||||
const polygon * p = &mesh->polygons[i];
|
||||
|
||||
if (indicators[i] > 0) {
|
||||
vec3 a = position[p->a];
|
||||
vec3 b = position[p->b];
|
||||
vec3 c = position[p->c];
|
||||
vec3 d = position[p->d];
|
||||
render_quad(a, b, c, d, false);
|
||||
} else {
|
||||
vec3 a = cast_position[p->a];
|
||||
vec3 b = cast_position[p->b];
|
||||
vec3 c = cast_position[p->c];
|
||||
vec3 d = cast_position[p->d];
|
||||
render_quad(a, b, c, d, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void shadow_volume_mesh(const vec3 light,
|
||||
const vec3 * position,
|
||||
const vec3 * polygon_normal,
|
||||
const mesh * mesh,
|
||||
void(*render_quad)(vec3 a, vec3 b, vec3 c, vec3 d, bool l))
|
||||
{
|
||||
// light in world space
|
||||
|
||||
float indicators[mesh->polygon_normal_length];
|
||||
face_indicators(light, position, polygon_normal, mesh, indicators);
|
||||
|
||||
// edge_indicies: mesh->edge_polygons indices
|
||||
int edge_indices[mesh->edge_polygons_length];
|
||||
int edge_indices_length = object_silhouette(indicators, mesh, edge_indices);
|
||||
|
||||
// graph contains indexes to edge_indices (not mesh edge indices)
|
||||
graph graph[mesh->position_length];
|
||||
edge_loop_graph(mesh, edge_indices, edge_indices_length, graph);
|
||||
|
||||
const int max_edge_loops = 2;
|
||||
int edge_loops[edge_indices_length];
|
||||
int edge_loop_lengths[max_edge_loops];
|
||||
int loop_count = edge_loop(mesh,
|
||||
edge_indices,
|
||||
edge_indices_length,
|
||||
graph,
|
||||
edge_loops,
|
||||
edge_loop_lengths,
|
||||
max_edge_loops);
|
||||
|
||||
|
||||
vec3 cast_position[mesh->position_length];
|
||||
for (int i = 0; i < mesh->position_length; i++) {
|
||||
cast_position[i] = cast_ray(light, position[i]);
|
||||
}
|
||||
|
||||
shadow_volume_end_caps(light,
|
||||
position,
|
||||
cast_position,
|
||||
mesh,
|
||||
indicators,
|
||||
render_quad);
|
||||
|
||||
// edge_loops contains position indices
|
||||
int edge_loop_ix = 0;
|
||||
for (int i = 0; i < loop_count; i++) {
|
||||
int edge_loop_length = edge_loop_lengths[i];
|
||||
int * edge_loop = &edge_loops[edge_loop_ix];
|
||||
shadow_volume_mesh_rays(light,
|
||||
position,
|
||||
cast_position,
|
||||
edge_loop,
|
||||
edge_loop_length,
|
||||
render_quad);
|
||||
edge_loop_ix += edge_loop_length;
|
||||
}
|
||||
|
||||
if (0) {
|
||||
int edge_loop_ix = 0;
|
||||
for (int i = 0; i < loop_count; i++) {
|
||||
int length = edge_loop_lengths[i];
|
||||
printf("loop %d: %d\n", i, length);
|
||||
for (int j = 0; j < length; j++) {
|
||||
printf(" %d", edge_loops[j + edge_loop_ix]);
|
||||
}
|
||||
printf("\n\n");
|
||||
edge_loop_ix += length;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
for (int i = 0; i < silhouette_length; i++) {
|
||||
out_edges[edges[i]] = 1;
|
||||
}
|
||||
*/
|
||||
}
|
10
shadow_volume.hpp
Normal file
10
shadow_volume.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "math/float_types.hpp"
|
||||
#include "model/blender_export.h"
|
||||
|
||||
void shadow_volume_mesh(const vec3 light,
|
||||
const vec3 * position,
|
||||
const vec3 * polygon_normal,
|
||||
const mesh * mesh,
|
||||
void(*render_quad)(vec3 a, vec3 b, vec3 c, vec3 d, bool l));
|
Loading…
x
Reference in New Issue
Block a user