draw a rotating cube

This commit is contained in:
Zack Buhman 2024-12-28 11:07:26 -06:00
parent dc787f172b
commit a6830aea78
16 changed files with 280 additions and 20 deletions

View File

@ -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"

View File

@ -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,

View File

@ -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 \

View File

@ -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;

View File

@ -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

View File

@ -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

15
model/FacePNT.class.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#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

13
model/FacePNT.java Normal file
View File

@ -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;
}
}

15
model/ModelObject.class.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#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

5
model/ModelObject.java Normal file
View File

@ -0,0 +1,5 @@
package model;
public class ModelObject {
public FacePNT[][] faces;
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#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

72
model/UntitledModel.java Normal file
View File

@ -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);
}
}

15
model/Vec2.class.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#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

11
model/Vec2.java Normal file
View File

@ -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;
}
}

15
model/Vec3.class.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#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

13
model/Vec3.java Normal file
View File

@ -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;
}
}