example: add moai
This commit is contained in:
parent
a482b3ad0a
commit
19f7af73b6
@ -910,14 +910,28 @@ example/castle.elf: $(START_OBJ) $(CASTLE_OBJ)
|
||||
|
||||
GRADIENT_OBJ = \
|
||||
example/gradient.o \
|
||||
texture/gradient/gradient.data.o \
|
||||
example/moai.o \
|
||||
holly/core.o \
|
||||
holly/region_array.o \
|
||||
holly/background.o \
|
||||
holly/ta_fifo_polygon_converter.o \
|
||||
holly/video_output.o \
|
||||
sh7091/serial.o \
|
||||
texture/gradient/gradient.data.o \
|
||||
$(LIBGCC)
|
||||
|
||||
example/gradient.elf: LDSCRIPT = $(LIB)/main.lds
|
||||
example/gradient.elf: $(START_OBJ) $(GRADIENT_OBJ)
|
||||
|
||||
MOAI_OBJ = \
|
||||
example/moai.o \
|
||||
holly/core.o \
|
||||
holly/region_array.o \
|
||||
holly/background.o \
|
||||
holly/ta_fifo_polygon_converter.o \
|
||||
holly/video_output.o \
|
||||
sh7091/serial.o \
|
||||
$(LIBGCC)
|
||||
|
||||
example/moai.elf: LDSCRIPT = $(LIB)/main.lds
|
||||
example/moai.elf: $(START_OBJ) $(MOAI_OBJ)
|
||||
|
538
example/moai.cpp
Normal file
538
example/moai.cpp
Normal file
@ -0,0 +1,538 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#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_alloc4.hpp"
|
||||
#include "holly/video_output.hpp"
|
||||
|
||||
#include "sh7091/sh7091.hpp"
|
||||
#include "sh7091/sh7091_bits.hpp"
|
||||
#include "sh7091/serial.hpp"
|
||||
#include "sh7091/vbr.hpp"
|
||||
|
||||
#include "systembus.hpp"
|
||||
#include "systembus_bits.hpp"
|
||||
|
||||
#include "memorymap.hpp"
|
||||
|
||||
#include "math/vec2.hpp"
|
||||
#include "math/vec3.hpp"
|
||||
#include "math/vec4.hpp"
|
||||
#include "math/mat4x4.hpp"
|
||||
|
||||
using vec2 = vec<2, float>;
|
||||
using vec3 = vec<3, float>;
|
||||
using vec4 = vec<4, float>;
|
||||
using mat4x4 = mat<4, 4, float>;
|
||||
|
||||
#include "model/model.h"
|
||||
#include "model/moai/material.h"
|
||||
#include "model/moai/model.h"
|
||||
|
||||
void vbr100()
|
||||
{
|
||||
serial::string("vbr100\n");
|
||||
serial::string("expevt ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.EXPEVT);
|
||||
serial::string("intevt ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.INTEVT);
|
||||
serial::string("tra ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.TRA);
|
||||
|
||||
uint32_t spc;
|
||||
uint32_t ssr;
|
||||
asm volatile ("stc spc,%0" : "=r" (spc));
|
||||
asm volatile ("stc ssr,%0" : "=r" (ssr));
|
||||
serial::string("spc ");
|
||||
serial::integer(spc);
|
||||
serial::string("ssr ");
|
||||
serial::integer(ssr);
|
||||
|
||||
while (1);
|
||||
}
|
||||
|
||||
void vbr400()
|
||||
{
|
||||
serial::string("vbr400");
|
||||
serial::string("expevt ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.EXPEVT);
|
||||
serial::string("intevt ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.INTEVT);
|
||||
serial::string("tra ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.TRA);
|
||||
|
||||
uint32_t spc;
|
||||
uint32_t ssr;
|
||||
asm volatile ("stc spc,%0" : "=r" (spc));
|
||||
asm volatile ("stc ssr,%0" : "=r" (ssr));
|
||||
serial::string("spc ");
|
||||
serial::integer(spc);
|
||||
serial::string("ssr ");
|
||||
serial::integer(ssr);
|
||||
|
||||
while (1);
|
||||
}
|
||||
|
||||
static int render_done = 0;
|
||||
|
||||
void vbr600()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
if (istnrm & istnrm::end_of_render_tsp) {
|
||||
system.ISTNRM = istnrm::end_of_render_tsp
|
||||
| istnrm::end_of_render_isp
|
||||
| istnrm::end_of_render_video;
|
||||
render_done = 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
serial::string("vbr600");
|
||||
serial::string("expevt ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.EXPEVT);
|
||||
serial::string("intevt ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.INTEVT);
|
||||
serial::string("tra ");
|
||||
serial::integer<uint16_t>(sh7091.CCN.TRA);
|
||||
|
||||
serial::string("istnrm: ");
|
||||
serial::integer<uint32_t>(system.ISTNRM);
|
||||
serial::string("isterr: ");
|
||||
serial::integer<uint32_t>(system.ISTERR);
|
||||
|
||||
uint32_t spc;
|
||||
uint32_t ssr;
|
||||
asm volatile ("stc spc,%0" : "=r" (spc));
|
||||
asm volatile ("stc ssr,%0" : "=r" (ssr));
|
||||
serial::string("spc ");
|
||||
serial::integer(spc);
|
||||
serial::string("ssr ");
|
||||
serial::integer(ssr);
|
||||
|
||||
while (1);
|
||||
}
|
||||
|
||||
void interrupt_init()
|
||||
{
|
||||
system.IML2NRM = 0;
|
||||
system.IML2ERR = 0;
|
||||
system.IML2EXT = 0;
|
||||
|
||||
system.IML4NRM = 0;
|
||||
system.IML4ERR = 0;
|
||||
system.IML4EXT = 0;
|
||||
|
||||
system.IML6NRM = 0;
|
||||
system.IML6ERR = 0;
|
||||
system.IML6EXT = 0;
|
||||
|
||||
system.ISTERR = 0xffffffff;
|
||||
system.ISTNRM = 0xffffffff;
|
||||
|
||||
sh7091.CCN.INTEVT = 0;
|
||||
sh7091.CCN.EXPEVT = 0;
|
||||
|
||||
uint32_t vbr = reinterpret_cast<uint32_t>(&__vbr_link_start) - 0x100;
|
||||
serial::string("vbr ");
|
||||
serial::integer<uint32_t>(vbr);
|
||||
serial::string("vbr100 ");
|
||||
serial::integer<uint32_t>(reinterpret_cast<uint32_t>(&vbr100));
|
||||
|
||||
asm volatile ("ldc %0,vbr"
|
||||
:
|
||||
: "r" (vbr));
|
||||
|
||||
uint32_t sr;
|
||||
asm volatile ("stc sr,%0"
|
||||
: "=r" (sr));
|
||||
serial::string("sr ");
|
||||
serial::integer<uint32_t>(sr);
|
||||
|
||||
sr &= ~sh::sr::bl; // BL
|
||||
sr &= ~sh::sr::imask(15); // imask
|
||||
|
||||
serial::string("sr ");
|
||||
serial::integer<uint32_t>(sr);
|
||||
|
||||
asm volatile ("ldc %0,sr"
|
||||
:
|
||||
: "r" (sr));
|
||||
}
|
||||
|
||||
void global_polygon_type_1(ta_parameter_writer& writer,
|
||||
uint32_t texture_address,
|
||||
uint32_t list,
|
||||
uint32_t cull)
|
||||
{
|
||||
const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume
|
||||
| list
|
||||
| obj_control::col_type::intensity_mode_1
|
||||
| obj_control::gouraud
|
||||
;
|
||||
const uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::depth_compare_mode::greater
|
||||
| cull;
|
||||
|
||||
const uint32_t tsp_instruction_word = tsp_instruction_word::fog_control::no_fog
|
||||
| tsp_instruction_word::src_alpha_instr::one
|
||||
| tsp_instruction_word::dst_alpha_instr::zero
|
||||
;
|
||||
|
||||
const uint32_t texture_control_word = 0;
|
||||
|
||||
const float alpha = 1.0f;
|
||||
const float r = 0.6f;
|
||||
const float g = 0.6f;
|
||||
const float b = 0.6f;
|
||||
|
||||
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,
|
||||
alpha,
|
||||
r,
|
||||
g,
|
||||
b
|
||||
);
|
||||
}
|
||||
|
||||
static inline vec3 screen_transform(vec3 v)
|
||||
{
|
||||
float dim = 480 / 2.0;
|
||||
|
||||
return {
|
||||
v.x / v.z * dim + 640 / 2.0f,
|
||||
v.y / 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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
constexpr inline mat4x4 screen_rotation(float theta)
|
||||
{
|
||||
//float zt = -0.7853981633974483 + (0.2);
|
||||
float zt = -0.7853981633974483 * 0;
|
||||
float yt = -0.7853981633974483 * theta;
|
||||
float xt = 0.7853981633974483 * 4;
|
||||
//float xt = 0.7853981633974483 * 3.7;
|
||||
|
||||
mat4x4 rx = {
|
||||
1, 0, 0, 0,
|
||||
0, cos(xt), -sin(xt), 0,
|
||||
0, sin(xt), cos(xt), 0,
|
||||
0, 0, 0, 1,
|
||||
};
|
||||
|
||||
mat4x4 ry = {
|
||||
cos(yt), 0, sin(yt), 0,
|
||||
0, 1, 0, 0,
|
||||
-sin(yt), 0, cos(yt), 0,
|
||||
0, 0, 0, 1,
|
||||
};
|
||||
|
||||
mat4x4 rz = {
|
||||
cos(zt), -sin(zt), 0, 0,
|
||||
sin(zt), cos(zt), 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
};
|
||||
|
||||
mat4x4 t = {
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 5.5,
|
||||
0, 0, 1, 8.5,
|
||||
0, 0, 0, 1,
|
||||
};
|
||||
|
||||
return t * ry * rx * rz;
|
||||
}
|
||||
|
||||
#define _fsrra(n) (1.0f / (__builtin_sqrtf(n)))
|
||||
|
||||
static inline float inverse_length(vec3 v)
|
||||
{
|
||||
float f = dot(v, v);
|
||||
return _fsrra(f);
|
||||
}
|
||||
|
||||
float light_intensity(vec3 light_vec, mat4x4& trans, vec3 n0)
|
||||
{
|
||||
vec4 n1 = trans * (vec4){n0.x, n0.y, n0.z, 0.f}; // no translation component
|
||||
vec3 n = {n1.x, n1.y, n1.z};
|
||||
float n_dot_l = dot(n, light_vec);
|
||||
|
||||
float intensity = 0.5f;
|
||||
if (n_dot_l > 0) {
|
||||
intensity += 0.5f * n_dot_l * (inverse_length(n) * inverse_length(light_vec));
|
||||
if (intensity > 1.0f)
|
||||
intensity = 1.0f;
|
||||
}
|
||||
return intensity;
|
||||
}
|
||||
|
||||
void render_moai(ta_parameter_writer& writer, const mat4x4& model_trans, const mat4x4& screen)
|
||||
{
|
||||
vec3 light_vec = {20, 1, -20};
|
||||
|
||||
mat4x4 trans = screen * model_trans;
|
||||
|
||||
for (int i = 0; i < moai_ranu_raku_grey.quadrilateral_count; i++) {
|
||||
//const union quadrilateral * quad = &moai_ranu_raku_grey_quadrilateral[i];
|
||||
const union quadrilateral * quad = &moai_ranu_raku_grey_1_quadrilateral[i];
|
||||
vec3 a = trans * moai_position[quad->v[0].position];
|
||||
vec3 b = trans * moai_position[quad->v[1].position];
|
||||
vec3 c = trans * moai_position[quad->v[2].position];
|
||||
vec3 d = trans * moai_position[quad->v[3].position];
|
||||
|
||||
float ai = light_intensity(light_vec, trans, moai_normal[quad->v[0].normal]);
|
||||
float bi = light_intensity(light_vec, trans, moai_normal[quad->v[1].normal]);
|
||||
float ci = light_intensity(light_vec, trans, moai_normal[quad->v[2].normal]);
|
||||
float di = light_intensity(light_vec, trans, moai_normal[quad->v[3].normal]);
|
||||
|
||||
render_quad(writer,
|
||||
screen_transform(a),
|
||||
screen_transform(b),
|
||||
screen_transform(c),
|
||||
screen_transform(d),
|
||||
ai,
|
||||
bi,
|
||||
ci,
|
||||
di);
|
||||
}
|
||||
}
|
||||
|
||||
void transfer_scene(ta_parameter_writer& writer)
|
||||
{
|
||||
const float deg = 0.017453292519943295 / 4;
|
||||
static float theta = deg;
|
||||
const mat4x4 screen = screen_rotation(theta);
|
||||
|
||||
theta += deg;
|
||||
|
||||
// opaque
|
||||
{
|
||||
global_polygon_type_1(writer, texture_memory_alloc.texture.start,
|
||||
para_control::list_type::opaque,
|
||||
isp_tsp_instruction_word::culling_mode::cull_if_negative);
|
||||
float scale = 0.1f;
|
||||
float translate = 0.f;
|
||||
const mat4x4 model = {
|
||||
scale, 0, 0, 0,
|
||||
0, scale, 0, 0,
|
||||
0, 0, -scale, translate,
|
||||
0, 0, 0, 1,
|
||||
};
|
||||
render_moai(writer, model, screen);
|
||||
}
|
||||
|
||||
// end of opaque list
|
||||
writer.append<ta_global_parameter::end_of_list>() =
|
||||
ta_global_parameter::end_of_list(para_control::para_type::end_of_list);
|
||||
}
|
||||
|
||||
void transfer_ta_fifo_texture_memory_32byte(void * dst, void * src, int length)
|
||||
{
|
||||
uint32_t out_addr = (uint32_t)dst;
|
||||
sh7091.CCN.QACR0 = ((reinterpret_cast<uint32_t>(out_addr) >> 24) & 0b11100);
|
||||
sh7091.CCN.QACR1 = ((reinterpret_cast<uint32_t>(out_addr) >> 24) & 0b11100);
|
||||
|
||||
volatile uint32_t * base = &store_queue[(out_addr & 0x03ffffc0) / 4];
|
||||
uint32_t * src32 = reinterpret_cast<uint32_t *>(src);
|
||||
|
||||
length = (length + 31) & ~31; // round up to nearest multiple of 32
|
||||
while (length > 0) {
|
||||
base[0] = src32[0];
|
||||
base[1] = src32[1];
|
||||
base[2] = src32[2];
|
||||
base[3] = src32[3];
|
||||
base[4] = src32[4];
|
||||
base[5] = src32[5];
|
||||
base[6] = src32[6];
|
||||
base[7] = src32[7];
|
||||
asm volatile ("pref @%0"
|
||||
: // output
|
||||
: "r" (&base[0]) // input
|
||||
: "memory");
|
||||
length -= 32;
|
||||
base += 8;
|
||||
src32 += 8;
|
||||
}
|
||||
}
|
||||
|
||||
void transfer_textures()
|
||||
{
|
||||
system.LMMODE0 = 0; // 64-bit address space
|
||||
system.LMMODE1 = 0; // 64-bit address space
|
||||
|
||||
/*
|
||||
{
|
||||
uint32_t offset = texture_memory_alloc.texture.start;
|
||||
void * dst = reinterpret_cast<void *>(&ta_fifo_texture_memory[offset / 4]);
|
||||
void * src = reinterpret_cast<void *>(&_binary_model_dragon_dragon_data_start);
|
||||
uint32_t size = reinterpret_cast<uint32_t>(&_binary_model_dragon_dragon_data_size);
|
||||
transfer_ta_fifo_texture_memory_32byte(dst, src, size);
|
||||
}
|
||||
|
||||
{
|
||||
uint32_t offset = texture_memory_alloc.texture.start + 131072;
|
||||
void * dst = reinterpret_cast<void *>(&ta_fifo_texture_memory[offset / 4]);
|
||||
void * src = reinterpret_cast<void *>(&_binary_model_dragon_chrome_data_start);
|
||||
uint32_t size = reinterpret_cast<uint32_t>(&_binary_model_dragon_chrome_data_size);
|
||||
transfer_ta_fifo_texture_memory_32byte(dst, src, size);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void transfer_palette()
|
||||
{
|
||||
holly.PAL_RAM_CTRL = pal_ram_ctrl::pixel_format::rgb565;
|
||||
|
||||
/*
|
||||
uint16_t * src = reinterpret_cast<uint16_t *>(&_binary_model_dragon_dragon_data_pal_start);
|
||||
uint32_t size = reinterpret_cast<uint32_t>(&_binary_model_dragon_dragon_data_pal_size);
|
||||
|
||||
for (uint32_t i = 0; i < size / 2; i++) {
|
||||
holly.PALETTE_RAM[i] = src[i];
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
uint8_t __attribute__((aligned(32))) ta_parameter_buf[1024 * 1024];
|
||||
|
||||
void main()
|
||||
{
|
||||
serial::init(0);
|
||||
|
||||
serial::integer<uint32_t>((sizeof (moai_position)) / (sizeof (moai_position[0])));
|
||||
|
||||
interrupt_init();
|
||||
|
||||
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::no_list
|
||||
| ta_alloc_ctrl::o_opb::_16x4byte;
|
||||
|
||||
constexpr int render_passes = 1;
|
||||
constexpr struct opb_size opb_size[render_passes] = {
|
||||
{
|
||||
.opaque = 16 * 4,
|
||||
.opaque_modifier = 0,
|
||||
.translucent = 0,
|
||||
.translucent_modifier = 0,
|
||||
.punch_through = 0
|
||||
}
|
||||
};
|
||||
|
||||
holly.SOFTRESET = softreset::pipeline_soft_reset
|
||||
| softreset::ta_soft_reset;
|
||||
holly.SOFTRESET = 0;
|
||||
|
||||
core_init();
|
||||
|
||||
system.IML6NRM = istnrm::end_of_render_tsp;
|
||||
|
||||
const int framebuffer_width = 640;
|
||||
const int framebuffer_height = 480;
|
||||
const int tile_width = framebuffer_width / 32;
|
||||
const int tile_height = framebuffer_height / 32;
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
region_array_multipass(tile_width,
|
||||
tile_height,
|
||||
opb_size,
|
||||
render_passes,
|
||||
texture_memory_alloc.region_array[i].start,
|
||||
texture_memory_alloc.object_list[i].start);
|
||||
|
||||
background_parameter2(texture_memory_alloc.background[i].start,
|
||||
0xff202040);
|
||||
}
|
||||
|
||||
int ta = 0;
|
||||
int core = 0;
|
||||
|
||||
ta_parameter_writer writer = ta_parameter_writer(ta_parameter_buf);
|
||||
|
||||
transfer_textures();
|
||||
transfer_palette();
|
||||
video_output::set_mode_vga();
|
||||
|
||||
while (1) {
|
||||
ta_polygon_converter_init2(texture_memory_alloc.isp_tsp_parameters[ta].start,
|
||||
texture_memory_alloc.isp_tsp_parameters[ta].end,
|
||||
texture_memory_alloc.object_list[ta].start,
|
||||
texture_memory_alloc.object_list[ta].end,
|
||||
opb_size[0].total(),
|
||||
ta_alloc,
|
||||
tile_width,
|
||||
tile_height);
|
||||
writer.offset = 0;
|
||||
transfer_scene(writer);
|
||||
ta_polygon_converter_writeback(writer.buf, writer.offset);
|
||||
ta_polygon_converter_transfer(writer.buf, writer.offset);
|
||||
ta_wait_opaque_list();
|
||||
|
||||
render_done = 0;
|
||||
core_start_render2(texture_memory_alloc.region_array[core].start,
|
||||
texture_memory_alloc.isp_tsp_parameters[core].start,
|
||||
texture_memory_alloc.background[core].start,
|
||||
texture_memory_alloc.framebuffer[core].start,
|
||||
framebuffer_width);
|
||||
while (render_done == 0) {
|
||||
asm volatile ("nop");
|
||||
};
|
||||
|
||||
while (!spg_status::vsync(holly.SPG_STATUS));
|
||||
holly.FB_R_SOF1 = texture_memory_alloc.framebuffer[ta].start;
|
||||
while (spg_status::vsync(holly.SPG_STATUS));
|
||||
}
|
||||
}
|
@ -236,33 +236,36 @@ static inline void render_quad(ta_parameter_writer& writer,
|
||||
}
|
||||
|
||||
static inline void render_quad_type2(ta_parameter_writer& writer,
|
||||
float intensity,
|
||||
vec3 ap,
|
||||
vec3 bp,
|
||||
vec3 cp,
|
||||
vec3 dp)
|
||||
vec3 dp,
|
||||
float ai,
|
||||
float bi,
|
||||
float ci,
|
||||
float di)
|
||||
{
|
||||
#ifdef LINE_DRAWING
|
||||
#else
|
||||
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,
|
||||
intensity);
|
||||
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,
|
||||
intensity);
|
||||
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,
|
||||
intensity);
|
||||
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,
|
||||
intensity);
|
||||
ci);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,10 @@ def random_k_points(k, points):
|
||||
def distance(point, centroid):
|
||||
x0, y0, z0 = point
|
||||
x1, y1, z1 = centroid
|
||||
xd = (x1 - x0) ** 2
|
||||
try:
|
||||
xd = (x1 - x0) ** 2
|
||||
except OverflowError:
|
||||
print(point, x1, x0)
|
||||
yd = (y1 - y0) ** 2
|
||||
zd = (z1 - z0) ** 2
|
||||
return math.sqrt(xd + yd + zd)
|
||||
@ -63,7 +66,7 @@ def k_means_cluster(k, points):
|
||||
# Calculate new centroids
|
||||
# (the standard implementation uses the mean of all points in a
|
||||
# cluster to determine the new centroid)
|
||||
print(clusters)
|
||||
#print(clusters)
|
||||
new_centroids = [calculate_centroid(cluster) for cluster in clusters]
|
||||
|
||||
converged = (new_centroids == centroids)
|
||||
@ -77,13 +80,69 @@ def palettize(centroids, point):
|
||||
cluster_assignment = argmin(distances_to_each_centroid)
|
||||
return cluster_assignment
|
||||
|
||||
def pixel_sub(a, b):
|
||||
return (
|
||||
a[0] - b[0],
|
||||
a[1] - b[1],
|
||||
a[2] - b[2]
|
||||
)
|
||||
|
||||
def pixel_mul(a, s):
|
||||
return (
|
||||
a[0] * s,
|
||||
a[1] * s,
|
||||
a[2] * s
|
||||
)
|
||||
|
||||
def pixel_add(a, b):
|
||||
return (
|
||||
a[0] + b[0],
|
||||
a[1] + b[1],
|
||||
a[2] + b[2]
|
||||
)
|
||||
|
||||
def dither(width, height, palette, pixels):
|
||||
pixels = list(pixels)
|
||||
|
||||
def ix(x, y):
|
||||
return y * width + x
|
||||
|
||||
def add_error(x, y, error, scale):
|
||||
if x >= width or y >= height:
|
||||
return
|
||||
old_value = pixels[ix(x, y)]
|
||||
new_value = pixel_add(old_value, pixel_mul(error, scale))
|
||||
pixels[ix(x, y)] = new_value
|
||||
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
old_pixel = pixels[y * width + x]
|
||||
new_pixel_ix = palettize(palette, old_pixel)
|
||||
new_pixel = palette[new_pixel_ix]
|
||||
|
||||
pixels[ix(x, y)] = new_pixel_ix
|
||||
|
||||
quant_error = pixel_sub(old_pixel, new_pixel)
|
||||
|
||||
add_error(x + 1, y + 0, quant_error, 7 / 16)
|
||||
add_error(x - 1, y + 1, quant_error, 3 / 16)
|
||||
add_error(x + 0, y + 1, quant_error, 5 / 16)
|
||||
add_error(x + 1, y + 1, quant_error, 1 / 16)
|
||||
|
||||
return pixels
|
||||
|
||||
output = sys.argv[2]
|
||||
with Image.open(sys.argv[1]) as im:
|
||||
pixels = list(im.convert("RGB").getdata())
|
||||
clusters = k_means_cluster(16, pixels)
|
||||
clusters = k_means_cluster(4, pixels)
|
||||
palette = list(calculate_centroid(cluster) for cluster in clusters)
|
||||
print(palette)
|
||||
#print(palette)
|
||||
palimage = Image.new('P', im.size)
|
||||
palimage.putpalette(map(int, list(chain.from_iterable(palette))))
|
||||
palimage.putdata([palettize(palette, pixel) for pixel in pixels])
|
||||
palimage.save(sys.argv[2])
|
||||
|
||||
ditherimage = Image.new('P', im.size)
|
||||
ditherimage.putpalette(map(int, list(chain.from_iterable(palette))))
|
||||
ditherimage.putdata(dither(*im.size, palette, pixels))
|
||||
ditherimage.save(sys.argv[3])
|
||||
|
12
model/moai/material.h
Normal file
12
model/moai/material.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "model/material.h"
|
||||
|
||||
enum material {
|
||||
grey,
|
||||
};
|
||||
|
||||
const struct material_descriptor moai_material[] = {
|
||||
};
|
22
model/moai/moai.mtl
Normal file
22
model/moai/moai.mtl
Normal file
@ -0,0 +1,22 @@
|
||||
# Blender 4.3.2 MTL File: 'None'
|
||||
# www.blender.org
|
||||
|
||||
newmtl grey.001
|
||||
Ns 249.019638
|
||||
Ka 1.000000 1.000000 1.000000
|
||||
Kd 0.194617 0.194618 0.194618
|
||||
Ks 0.500000 0.500000 0.500000
|
||||
Ke 0.000000 0.000000 0.000000
|
||||
Ni 1.500000
|
||||
d 1.000000
|
||||
illum 2
|
||||
|
||||
newmtl grey.002
|
||||
Ns 250.000000
|
||||
Ka 1.000000 1.000000 1.000000
|
||||
Kd 0.194617 0.194618 0.194618
|
||||
Ks 0.500000 0.500000 0.500000
|
||||
Ke 0.000000 0.000000 0.000000
|
||||
Ni 1.500000
|
||||
d 1.000000
|
||||
illum 2
|
10163
model/moai/moai.obj
Normal file
10163
model/moai/moai.obj
Normal file
File diff suppressed because it is too large
Load Diff
12
model/moai/moai03.mtl
Normal file
12
model/moai/moai03.mtl
Normal file
@ -0,0 +1,12 @@
|
||||
# Blender 4.2.3 LTS MTL File: 'moai03.blend'
|
||||
# www.blender.org
|
||||
|
||||
newmtl grey
|
||||
Ns 250.000000
|
||||
Ka 1.000000 1.000000 1.000000
|
||||
Kd 0.194617 0.194618 0.194618
|
||||
Ks 0.500000 0.500000 0.500000
|
||||
Ke 0.000000 0.000000 0.000000
|
||||
Ni 1.500000
|
||||
d 1.000000
|
||||
illum 2
|
5083
model/moai/moai03.obj
Normal file
5083
model/moai/moai03.obj
Normal file
File diff suppressed because it is too large
Load Diff
12
model/moai/moai04.mtl
Normal file
12
model/moai/moai04.mtl
Normal file
@ -0,0 +1,12 @@
|
||||
# Blender MTL File: 'None'
|
||||
# Material Count: 1
|
||||
|
||||
newmtl grey
|
||||
Ns 249.019608
|
||||
Ka 1.000000 1.000000 1.000000
|
||||
Kd 0.194617 0.194618 0.194618
|
||||
Ks 0.500000 0.500000 0.500000
|
||||
Ke 0.000000 0.000000 0.000000
|
||||
Ni 1.500000
|
||||
d 1.000000
|
||||
illum 2
|
5083
model/moai/moai04.obj
Normal file
5083
model/moai/moai04.obj
Normal file
File diff suppressed because it is too large
Load Diff
19621
model/moai/model.h
Normal file
19621
model/moai/model.h
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user