From a6830aea7890c61ec118c3956633f88559bb3ba0 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Sat, 28 Dec 2024 11:07:26 -0600 Subject: [PATCH] draw a rotating cube --- c/classpath.h | 5 +++ c/classpath.inc.c | 5 +++ classpath.mk | 5 +++ example/DreamcastVideo2.java | 82 ++++++++++++++++++++++++++++++------ generate.sh | 2 + generate_classpath.sh | 12 +++--- model/FacePNT.class.h | 15 +++++++ model/FacePNT.java | 13 ++++++ model/ModelObject.class.h | 15 +++++++ model/ModelObject.java | 5 +++ model/UntitledModel.class.h | 15 +++++++ model/UntitledModel.java | 72 +++++++++++++++++++++++++++++++ model/Vec2.class.h | 15 +++++++ model/Vec2.java | 11 +++++ model/Vec3.class.h | 15 +++++++ model/Vec3.java | 13 ++++++ 16 files changed, 280 insertions(+), 20 deletions(-) create mode 100644 model/FacePNT.class.h create mode 100644 model/FacePNT.java create mode 100644 model/ModelObject.class.h create mode 100644 model/ModelObject.java create mode 100644 model/UntitledModel.class.h create mode 100644 model/UntitledModel.java create mode 100644 model/Vec2.class.h create mode 100644 model/Vec2.java create mode 100644 model/Vec3.class.h create mode 100644 model/Vec3.java diff --git a/c/classpath.h b/c/classpath.h index 5570f63..555f114 100644 --- a/c/classpath.h +++ b/c/classpath.h @@ -8,6 +8,11 @@ #include "java/lang/String.class.h" #include "java/lang/System.class.h" #include "java/misc/Memory.class.h" +#include "model/FacePNT.class.h" +#include "model/ModelObject.class.h" +#include "model/UntitledModel.class.h" +#include "model/Vec2.class.h" +#include "model/Vec3.class.h" #include "sega/dreamcast/holly/Background.class.h" #include "sega/dreamcast/holly/CoreBits.class.h" #include "sega/dreamcast/holly/Core.class.h" diff --git a/c/classpath.inc.c b/c/classpath.inc.c index 44bb591..046fef4 100644 --- a/c/classpath.inc.c +++ b/c/classpath.inc.c @@ -8,6 +8,11 @@ (const uint8_t *)&_binary_java_lang_String_class_start, (const uint8_t *)&_binary_java_lang_System_class_start, (const uint8_t *)&_binary_java_misc_Memory_class_start, +(const uint8_t *)&_binary_model_FacePNT_class_start, +(const uint8_t *)&_binary_model_ModelObject_class_start, +(const uint8_t *)&_binary_model_UntitledModel_class_start, +(const uint8_t *)&_binary_model_Vec2_class_start, +(const uint8_t *)&_binary_model_Vec3_class_start, (const uint8_t *)&_binary_sega_dreamcast_holly_Background_class_start, (const uint8_t *)&_binary_sega_dreamcast_holly_CoreBits_class_start, (const uint8_t *)&_binary_sega_dreamcast_holly_Core_class_start, diff --git a/classpath.mk b/classpath.mk index 4888219..ca82771 100644 --- a/classpath.mk +++ b/classpath.mk @@ -9,6 +9,11 @@ CLASS_PATH = \ java/lang/String.class.o \ java/lang/System.class.o \ java/misc/Memory.class.o \ + model/FacePNT.class.o \ + model/ModelObject.class.o \ + model/UntitledModel.class.o \ + model/Vec2.class.o \ + model/Vec3.class.o \ sega/dreamcast/holly/Background.class.o \ sega/dreamcast/holly/CoreBits.class.o \ sega/dreamcast/holly/Core.class.o \ diff --git a/example/DreamcastVideo2.java b/example/DreamcastVideo2.java index 3381c99..4d6334b 100644 --- a/example/DreamcastVideo2.java +++ b/example/DreamcastVideo2.java @@ -15,12 +15,18 @@ import sega.dreamcast.holly.TAGlobalParameter; import sega.dreamcast.systembus.Systembus; import sega.dreamcast.systembus.SystembusBits; import sega.dreamcast.MemoryMap; +import model.UntitledModel; +import model.Vec3; +import model.FacePNT; +import model.ModelObject; import java.misc.Memory; +/* class Vec2 { float x; float y; } +*/ class DreamcastVideo2 { static final int framebuffer_width = 640; @@ -30,10 +36,17 @@ class DreamcastVideo2 { static TAVertexParameter.polygon_type_0 vt0; static TAGlobalParameter.end_of_list eol; - static Vec2[] vtx; + //static Vec2[] vtx; static float theta; + static int colors[] = { + 5156825, 14787722, 9529551, + 4729017, 10213073, 15956866, + 5362273, 8377157, 9797796, + 11479204, 4042586, 16676239 + }; + static { int parameter_control_word = TAParameter.para_control__para_type__polygon_or_modifier_volume | TAParameter.para_control__list_type__opaque @@ -61,10 +74,11 @@ class DreamcastVideo2 { 0.0f, // x 0.0f, // y 0.1f, // z - 0xffff0000); // color (green) + 0); // color eol = new TAGlobalParameter.end_of_list(TAParameter.para_control__para_type__end_of_list); + /* vtx = new Vec2[3]; for (int i = 0; i < 3; i++) vtx[i] = new Vec2(); @@ -74,16 +88,55 @@ class DreamcastVideo2 { vtx[1].y = -0.5f; vtx[2].x = -0.866025f; vtx[2].y = -0.5f; + */ } - public static void transform_vertex(Vec2 v) { - float x0 = v.x * Math.cos(theta) - v.y * Math.sin(theta); - float y0 = v.x * Math.sin(theta) + v.y * Math.cos(theta); - float x = x0 * 240 + 320; - float y = -y0 * 240 + 240; + public static void transform_vertex(Vec3[] position, FacePNT pnt) { + float px = position[pnt.position].x; + float py = position[pnt.position].y; + float pz = position[pnt.position].z; + + //px *= 0.5; + //py *= 0.5; + //pz *= 0.5; + + float x0 = px * Math.cos(theta) - py * Math.sin(theta); + float y0 = px * Math.sin(theta) + py * Math.cos(theta); + float z0 = pz; + + float theta2 = theta / 2.0f; + + float x1 = x0 * Math.cos(theta2) - z0 * Math.sin(theta2); + float y1 = y0; + float z1 = x0 * Math.sin(theta2) + z0 * Math.cos(theta2); + + // camera transform + float z2 = 3f + z1; + + // perspective + float x2 = x1 / z2; + float y2 = y1 / z2; + + // screen space + float x = x2 * 240f + 320f; + float y = -y2 * 240f + 240f; + float z = 1.0f / z2; DreamcastVideo2.vt0.x = x; DreamcastVideo2.vt0.y = y; + DreamcastVideo2.vt0.z = z; + } + + public static void transform_triangle(int n, Vec3[] position, FacePNT[] face) { + for (int i = 0; i < 3; i++) { + DreamcastVideo2.vt0.parameter_control_word = TAParameter.para_control__para_type__vertex_parameter; + if (i == 2) + DreamcastVideo2.vt0.parameter_control_word |= TAParameter.para_control__end_of_strip; + transform_vertex(position, face[i]); + DreamcastVideo2.vt0.base_color = colors[n]; + + Memory.putSQ1(DreamcastVideo2.vt0, MemoryMap.ta_fifo_polygon_converter); + } } public static void transfer_scene() { @@ -91,12 +144,9 @@ class DreamcastVideo2 { Memory.putSQ1(DreamcastVideo2.gt0, MemoryMap.ta_fifo_polygon_converter); // vertex parameters - for (int i = 0; i < 3; i++) { - DreamcastVideo2.vt0.parameter_control_word = TAParameter.para_control__para_type__vertex_parameter; - if (i == 2) - DreamcastVideo2.vt0.parameter_control_word |= TAParameter.para_control__end_of_strip; - transform_vertex(vtx[i]); - Memory.putSQ1(DreamcastVideo2.vt0, MemoryMap.ta_fifo_polygon_converter); + ModelObject obj = UntitledModel.objects[0]; + for (int i = 0; i < obj.faces.length; i ++) { + transform_triangle(i, UntitledModel.position, obj.faces[i]); } // end of list @@ -140,7 +190,7 @@ class DreamcastVideo2 { TextureMemoryAllocation.region_array_start[1], TextureMemoryAllocation.object_list_start[1]); - int background_color = 0xff00ff00; + int background_color = 0xff443300; Background.background(TextureMemoryAllocation.background_start[0], background_color); Background.background(TextureMemoryAllocation.background_start[1], @@ -167,7 +217,11 @@ class DreamcastVideo2 { TextureMemoryAllocation.framebuffer_start[core], framebuffer_width); Core.wait_end_of_render_tsp(); + + while (!(CoreBits.spg_status__vsync(Memory.getU4(Holly.SPG_STATUS)) != 0)); Memory.putU4(Holly.FB_R_SOF1, TextureMemoryAllocation.framebuffer_start[core]); + while ((CoreBits.spg_status__vsync(Memory.getU4(Holly.SPG_STATUS)) != 0)); + core = (core + 1) % 1; theta += Math.DEGREES_TO_RADIANS; diff --git a/generate.sh b/generate.sh index 1262085..7352973 100644 --- a/generate.sh +++ b/generate.sh @@ -12,3 +12,5 @@ python regs/bits_gen.py ../dreamcast/regs/systembus_bits.csv systembus Systembus python regs/ta_parameters.py ../dreamcast/regs/vertex_parameter_format.csv holly TAVertexParameter > sega/dreamcast/holly/TAVertexParameter.java python regs/ta_parameters.py ../dreamcast/regs/global_parameter_format.csv holly TAGlobalParameter > sega/dreamcast/holly/TAGlobalParameter.java + +PYTHONPATH=./regs/ python ../model_generator/generate_java.py ../untitled.obj UntitledModel > ./models/UntitledModel.java diff --git a/generate_classpath.sh b/generate_classpath.sh index 66a41c4..69cf1bb 100644 --- a/generate_classpath.sh +++ b/generate_classpath.sh @@ -69,9 +69,9 @@ function classpath_h () { set -e find sega/ java/ -name '*.class' -exec rm -f {} \; -find example/ sega/ java/ -name '*.java' | sort | make_class -find example/ sega/ java/ -name '*.class' | sort | rename_class_files -find example/ sega/ java/ -name '*.class' | sort | classpath_mk -find example/ sega/ java/ -name '*.class' | sort | classpath_inc_c -find example/ sega/ java/ -name '*.class' | sort | make_header -find example/ sega/ java/ -name '*.class' | sort | classpath_h +find model/ example/ sega/ java/ -name '*.java' | sort | make_class +find model/ example/ sega/ java/ -name '*.class' | sort | rename_class_files +find model/ example/ sega/ java/ -name '*.class' | sort | classpath_mk +find model/ example/ sega/ java/ -name '*.class' | sort | classpath_inc_c +find model/ example/ sega/ java/ -name '*.class' | sort | make_header +find model/ example/ sega/ java/ -name '*.class' | sort | classpath_h diff --git a/model/FacePNT.class.h b/model/FacePNT.class.h new file mode 100644 index 0000000..957e231 --- /dev/null +++ b/model/FacePNT.class.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern C { +#endif + +extern uint32_t _binary_model_FacePNT_class_start __asm("_binary_model_FacePNT_class_start"); +extern uint32_t _binary_model_FacePNT_class_end __asm("_binary_model_FacePNT_class_end"); +extern uint32_t _binary_model_FacePNT_class_size __asm("_binary_model_FacePNT_class_size"); + +#ifdef __cplusplus +} +#endif diff --git a/model/FacePNT.java b/model/FacePNT.java new file mode 100644 index 0000000..6b8d666 --- /dev/null +++ b/model/FacePNT.java @@ -0,0 +1,13 @@ +package model; + +public class FacePNT { + public int position; + public int normal; + public int texture_coordinate; + + FacePNT(int position, int normal, int texture_coordinate) { + this.position = position; + this.normal = normal; + this.texture_coordinate = texture_coordinate; + } +} diff --git a/model/ModelObject.class.h b/model/ModelObject.class.h new file mode 100644 index 0000000..d1ac000 --- /dev/null +++ b/model/ModelObject.class.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern C { +#endif + +extern uint32_t _binary_model_ModelObject_class_start __asm("_binary_model_ModelObject_class_start"); +extern uint32_t _binary_model_ModelObject_class_end __asm("_binary_model_ModelObject_class_end"); +extern uint32_t _binary_model_ModelObject_class_size __asm("_binary_model_ModelObject_class_size"); + +#ifdef __cplusplus +} +#endif diff --git a/model/ModelObject.java b/model/ModelObject.java new file mode 100644 index 0000000..aee5001 --- /dev/null +++ b/model/ModelObject.java @@ -0,0 +1,5 @@ +package model; + +public class ModelObject { + public FacePNT[][] faces; +} diff --git a/model/UntitledModel.class.h b/model/UntitledModel.class.h new file mode 100644 index 0000000..16ee7a6 --- /dev/null +++ b/model/UntitledModel.class.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern C { +#endif + +extern uint32_t _binary_model_UntitledModel_class_start __asm("_binary_model_UntitledModel_class_start"); +extern uint32_t _binary_model_UntitledModel_class_end __asm("_binary_model_UntitledModel_class_end"); +extern uint32_t _binary_model_UntitledModel_class_size __asm("_binary_model_UntitledModel_class_size"); + +#ifdef __cplusplus +} +#endif diff --git a/model/UntitledModel.java b/model/UntitledModel.java new file mode 100644 index 0000000..5c6529c --- /dev/null +++ b/model/UntitledModel.java @@ -0,0 +1,72 @@ +package model; + +public class UntitledModel { + public static Vec3[] position; + public static Vec3[] normal; + public static Vec2[] texture; + public static ModelObject[] objects; + private UntitledModel() { + } + static { + position = new Vec3[8]; + normal = new Vec3[6]; + texture = new Vec2[4]; + objects = new ModelObject[1]; + position[0] = new Vec3(1.0f, 1.0f, -1.0f); + position[1] = new Vec3(1.0f, -1.0f, -1.0f); + position[2] = new Vec3(1.0f, 1.0f, 1.0f); + position[3] = new Vec3(1.0f, -1.0f, 1.0f); + position[4] = new Vec3(-1.0f, 1.0f, -1.0f); + position[5] = new Vec3(-1.0f, -1.0f, -1.0f); + position[6] = new Vec3(-1.0f, 1.0f, 1.0f); + position[7] = new Vec3(-1.0f, -1.0f, 1.0f); + normal[0] = new Vec3(0.0f, 1.0f, 0.0f); + normal[1] = new Vec3(0.0f, 0.0f, 1.0f); + normal[2] = new Vec3(-1.0f, 0.0f, 0.0f); + normal[3] = new Vec3(0.0f, -1.0f, 0.0f); + normal[4] = new Vec3(1.0f, 0.0f, 0.0f); + normal[5] = new Vec3(0.0f, 0.0f, -1.0f); + texture[0] = new Vec2(1.0f, 0.0f); + texture[1] = new Vec2(0.0f, 1.0f); + texture[2] = new Vec2(0.0f, 0.0f); + texture[3] = new Vec2(1.0f, 1.0f); + objects[0] = new ModelObject(); + objects[0].faces = new FacePNT[12][3]; + objects[0].faces[0][0] = new FacePNT(4, 0, 0); + objects[0].faces[0][1] = new FacePNT(2, 1, 0); + objects[0].faces[0][2] = new FacePNT(0, 2, 0); + objects[0].faces[1][0] = new FacePNT(2, 0, 1); + objects[0].faces[1][1] = new FacePNT(7, 1, 1); + objects[0].faces[1][2] = new FacePNT(3, 2, 1); + objects[0].faces[2][0] = new FacePNT(6, 0, 2); + objects[0].faces[2][1] = new FacePNT(5, 1, 2); + objects[0].faces[2][2] = new FacePNT(7, 2, 2); + objects[0].faces[3][0] = new FacePNT(1, 0, 3); + objects[0].faces[3][1] = new FacePNT(7, 1, 3); + objects[0].faces[3][2] = new FacePNT(5, 2, 3); + objects[0].faces[4][0] = new FacePNT(0, 0, 4); + objects[0].faces[4][1] = new FacePNT(3, 1, 4); + objects[0].faces[4][2] = new FacePNT(1, 2, 4); + objects[0].faces[5][0] = new FacePNT(4, 0, 5); + objects[0].faces[5][1] = new FacePNT(1, 1, 5); + objects[0].faces[5][2] = new FacePNT(5, 2, 5); + objects[0].faces[6][0] = new FacePNT(4, 0, 0); + objects[0].faces[6][1] = new FacePNT(6, 3, 0); + objects[0].faces[6][2] = new FacePNT(2, 1, 0); + objects[0].faces[7][0] = new FacePNT(2, 0, 1); + objects[0].faces[7][1] = new FacePNT(6, 3, 1); + objects[0].faces[7][2] = new FacePNT(7, 1, 1); + objects[0].faces[8][0] = new FacePNT(6, 0, 2); + objects[0].faces[8][1] = new FacePNT(4, 3, 2); + objects[0].faces[8][2] = new FacePNT(5, 1, 2); + objects[0].faces[9][0] = new FacePNT(1, 0, 3); + objects[0].faces[9][1] = new FacePNT(3, 3, 3); + objects[0].faces[9][2] = new FacePNT(7, 1, 3); + objects[0].faces[10][0] = new FacePNT(0, 0, 4); + objects[0].faces[10][1] = new FacePNT(2, 3, 4); + objects[0].faces[10][2] = new FacePNT(3, 1, 4); + objects[0].faces[11][0] = new FacePNT(4, 0, 5); + objects[0].faces[11][1] = new FacePNT(0, 3, 5); + objects[0].faces[11][2] = new FacePNT(1, 1, 5); + } +} diff --git a/model/Vec2.class.h b/model/Vec2.class.h new file mode 100644 index 0000000..a06a833 --- /dev/null +++ b/model/Vec2.class.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern C { +#endif + +extern uint32_t _binary_model_Vec2_class_start __asm("_binary_model_Vec2_class_start"); +extern uint32_t _binary_model_Vec2_class_end __asm("_binary_model_Vec2_class_end"); +extern uint32_t _binary_model_Vec2_class_size __asm("_binary_model_Vec2_class_size"); + +#ifdef __cplusplus +} +#endif diff --git a/model/Vec2.java b/model/Vec2.java new file mode 100644 index 0000000..73b0868 --- /dev/null +++ b/model/Vec2.java @@ -0,0 +1,11 @@ +package model; + +public class Vec2 { + public float x; + public float y; + + Vec2(float x, float y) { + this.x = x; + this.y = y; + } +} diff --git a/model/Vec3.class.h b/model/Vec3.class.h new file mode 100644 index 0000000..3ebef29 --- /dev/null +++ b/model/Vec3.class.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern C { +#endif + +extern uint32_t _binary_model_Vec3_class_start __asm("_binary_model_Vec3_class_start"); +extern uint32_t _binary_model_Vec3_class_end __asm("_binary_model_Vec3_class_end"); +extern uint32_t _binary_model_Vec3_class_size __asm("_binary_model_Vec3_class_size"); + +#ifdef __cplusplus +} +#endif diff --git a/model/Vec3.java b/model/Vec3.java new file mode 100644 index 0000000..e2cacb1 --- /dev/null +++ b/model/Vec3.java @@ -0,0 +1,13 @@ +package model; + +public class Vec3 { + public float x; + public float y; + public float z; + + Vec3(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } +}