|
| 1 | +package example; |
| 2 | + |
| 3 | +import sega.dreamcast.holly.Background; |
| 4 | +import sega.dreamcast.holly.Core; |
| 5 | +import sega.dreamcast.holly.CoreBits; |
| 6 | +import sega.dreamcast.holly.Holly; |
| 7 | +import sega.dreamcast.holly.RegionArray; |
| 8 | +import sega.dreamcast.holly.TABits; |
| 9 | +import sega.dreamcast.holly.TAFIFOPolygonConverter; |
| 10 | +import sega.dreamcast.holly.TAParameter; |
| 11 | +import sega.dreamcast.holly.TextureMemoryAllocation; |
| 12 | +import sega.dreamcast.holly.ISPTSP; |
| 13 | +import sega.dreamcast.holly.TAVertexParameter; |
| 14 | +import sega.dreamcast.holly.TAGlobalParameter; |
| 15 | +import sega.dreamcast.holly.VideoOutput; |
| 16 | +import sega.dreamcast.holly.VideoOutputMode; |
| 17 | +import sega.dreamcast.MemoryMap; |
| 18 | +import model.CubeModel; |
| 19 | +import model.Vec3; |
| 20 | +import model.Vec2; |
| 21 | +import model.FacePTN; |
| 22 | +import model.ModelObject; |
| 23 | +import jvm.internal.Memory; |
| 24 | + |
| 25 | +public class JavaCubeMemory { |
| 26 | + static final int framebuffer_width = 640; |
| 27 | + static final int framebuffer_height = 480; |
| 28 | + |
| 29 | + static Vec2[] quad; |
| 30 | + static Vec2[] quad_uv; |
| 31 | + |
| 32 | + static float theta; |
| 33 | + |
| 34 | + public static int[] texture_extents; |
| 35 | + |
| 36 | + static String[] texture_filenames = { |
| 37 | + "JAVA_CUP.DAT;1", |
| 38 | + "JAVA_TEX.DAT;1", |
| 39 | + }; |
| 40 | + |
| 41 | + static final int parameter_control_word = TAParameter.para_control__para_type__polygon_or_modifier_volume |
| 42 | + | TAParameter.para_control__list_type__translucent |
| 43 | + | TAParameter.obj_control__col_type__packed_color |
| 44 | + | TAParameter.obj_control__texture; |
| 45 | + |
| 46 | + static final int isp_tsp_instruction_word = ISPTSP.isp_tsp_instruction_word__depth_compare_mode__greater |
| 47 | + | ISPTSP.isp_tsp_instruction_word__culling_mode__no_culling; |
| 48 | + |
| 49 | + static final int tsp_instruction_word = ISPTSP.tsp_instruction_word__src_alpha_instr__src_alpha |
| 50 | + | ISPTSP.tsp_instruction_word__dst_alpha_instr__inverse_src_alpha |
| 51 | + | ISPTSP.tsp_instruction_word__fog_control__no_fog |
| 52 | + | ISPTSP.tsp_instruction_word__texture_u_size__512 |
| 53 | + | ISPTSP.tsp_instruction_word__texture_v_size__1024 |
| 54 | + | ISPTSP.tsp_instruction_word__use_alpha; |
| 55 | + |
| 56 | + static final int texture_address = TextureMemoryAllocation.texture_regions[1][0] + 512; |
| 57 | + static final int texture_control_word = ISPTSP.texture_control_word__pixel_format__4444 |
| 58 | + | ISPTSP.texture_control_word__scan_order__non_twiddled |
| 59 | + | ISPTSP.texture_control_word__texture_address(texture_address / 8); |
| 60 | + |
| 61 | + static { |
| 62 | + texture_extents = new int[texture_filenames.length]; |
| 63 | + for (int i = 0; i < texture_filenames.length; i++) { |
| 64 | + texture_extents[i] = -1; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static void transform_vertex(int parameter_control_word, |
| 69 | + Vec3[] position, |
| 70 | + Vec2[] texture, |
| 71 | + FacePTN ptn) { |
| 72 | + |
| 73 | + float px = position[ptn.position].x; |
| 74 | + float py = position[ptn.position].y; |
| 75 | + float pz = position[ptn.position].z; |
| 76 | + |
| 77 | + float theta2 = theta * 2.0f; |
| 78 | + float scale = (Math.sin(theta2) + 3.0f) * 0.3f; |
| 79 | + |
| 80 | + px *= scale; |
| 81 | + py *= scale; |
| 82 | + pz *= scale; |
| 83 | + |
| 84 | + float x0 = px * Math.cos(theta) - py * Math.sin(theta); |
| 85 | + float y0 = px * Math.sin(theta) + py * Math.cos(theta); |
| 86 | + float z0 = pz; |
| 87 | + |
| 88 | + float theta05 = theta * 0.5f; |
| 89 | + |
| 90 | + float x1 = x0 * Math.cos(theta05) - z0 * Math.sin(theta05); |
| 91 | + float y1 = y0; |
| 92 | + float z1 = x0 * Math.sin(theta05) + z0 * Math.cos(theta05); |
| 93 | + |
| 94 | + // camera transform |
| 95 | + float z2 = 3f + z1; |
| 96 | + |
| 97 | + // perspective |
| 98 | + float x2 = x1 / z2; |
| 99 | + float y2 = y1 / z2; |
| 100 | + |
| 101 | + // screen space |
| 102 | + float x = x2 * 240f + 320f; |
| 103 | + float y = -y2 * 240f + 240f; |
| 104 | + float z = 1.0f / z2; |
| 105 | + |
| 106 | + TAVertexParameter.polygon_type_3(parameter_control_word, |
| 107 | + x, |
| 108 | + y, |
| 109 | + z, |
| 110 | + texture[ptn.texture].x, // u |
| 111 | + texture[ptn.texture].y, // v |
| 112 | + 0xff000000, // base_color |
| 113 | + 0); // offset_color |
| 114 | + } |
| 115 | + |
| 116 | + public static void transform_triangle(int n, Vec3[] position, Vec2[] texture, FacePTN[] face) { |
| 117 | + for (int i = 0; i < 3; i++) { |
| 118 | + int parameter_control_word = TAParameter.para_control__para_type__vertex_parameter; |
| 119 | + if (i == 2) |
| 120 | + parameter_control_word |= TAParameter.para_control__end_of_strip; |
| 121 | + transform_vertex(parameter_control_word, position, texture, face[i]); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public static void transfer_cube_scene() { |
| 126 | + // global parameters |
| 127 | + TAGlobalParameter.polygon_type_0(parameter_control_word, |
| 128 | + isp_tsp_instruction_word, |
| 129 | + tsp_instruction_word, |
| 130 | + texture_control_word, |
| 131 | + 0, // data_size_for_sort_dma |
| 132 | + 0); // next_address_for_sort_dma |
| 133 | + |
| 134 | + // triangle parameters |
| 135 | + ModelObject obj = CubeModel.objects[0]; |
| 136 | + for (int i = 0; i < obj.faces.length; i ++) { |
| 137 | + transform_triangle(i, CubeModel.position, CubeModel.texture, obj.faces[i]); |
| 138 | + } |
| 139 | + |
| 140 | + // end of list |
| 141 | + TAGlobalParameter.end_of_list(TAParameter.para_control__para_type__end_of_list); |
| 142 | + } |
| 143 | + |
| 144 | + public static void transfer_textures() { |
| 145 | + int texture = MemoryMap.texture_memory64 + TextureMemoryAllocation.texture_regions[1][0] + 512; |
| 146 | + |
| 147 | + int length = 512 * 512 * 2 * 2 / 4; |
| 148 | + int address = 0xac400000; |
| 149 | + for (int i = 0; i < length; i++) { |
| 150 | + int value = Memory.getU4(address); |
| 151 | + Memory.putU4(texture, value); |
| 152 | + if ((i & 2047) == 0) { |
| 153 | + System.out.print(i); |
| 154 | + System.out.print(' '); |
| 155 | + System.out.print(address); |
| 156 | + System.out.print(' '); |
| 157 | + System.out.print(texture); |
| 158 | + } |
| 159 | + address += 4; |
| 160 | + texture += 4; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + public static void main() { |
| 165 | + System.out.println("main1"); |
| 166 | + int ta_alloc = |
| 167 | + TABits.ta_alloc_ctrl__opb_mode__increasing_addresses |
| 168 | + | TABits.ta_alloc_ctrl__pt_opb__no_list |
| 169 | + | TABits.ta_alloc_ctrl__tm_opb__no_list |
| 170 | + | TABits.ta_alloc_ctrl__t_opb__8x4byte |
| 171 | + | TABits.ta_alloc_ctrl__om_opb__no_list |
| 172 | + | TABits.ta_alloc_ctrl__o_opb__no_list |
| 173 | + ; |
| 174 | + |
| 175 | + RegionArray.OPBSize[] opb_size = { |
| 176 | + new RegionArray.OPBSize(0, // opaque |
| 177 | + 0, // opaque_modifier_volume |
| 178 | + 8 * 4, // translucent |
| 179 | + 0, // translucent_modifier_volume |
| 180 | + 0) // punch_through |
| 181 | + }; |
| 182 | + int opb_size_total = opb_size[0].total(); |
| 183 | + |
| 184 | + int background_color = 0xff100a00; |
| 185 | + Background.background(TextureMemoryAllocation.background_start[0], |
| 186 | + 0x00c0c0c0); // sega white |
| 187 | + Background.background(TextureMemoryAllocation.background_start[1], |
| 188 | + 0x00c0c0c0);//background_color); // dark black |
| 189 | + |
| 190 | + Memory.putU4(Holly.VO_BORDER_COL, background_color); |
| 191 | + |
| 192 | + int num_render_passes = opb_size.length; |
| 193 | + RegionArray.region_array(framebuffer_width / 32, |
| 194 | + framebuffer_height / 32, |
| 195 | + opb_size, |
| 196 | + num_render_passes, |
| 197 | + TextureMemoryAllocation.region_array_start[0], |
| 198 | + TextureMemoryAllocation.object_list_start[0]); |
| 199 | + RegionArray.region_array(framebuffer_width / 32, |
| 200 | + framebuffer_height / 32, |
| 201 | + opb_size, |
| 202 | + num_render_passes, |
| 203 | + TextureMemoryAllocation.region_array_start[1], |
| 204 | + TextureMemoryAllocation.object_list_start[1]); |
| 205 | + |
| 206 | + Core.init(); |
| 207 | + |
| 208 | + System.out.println("transfer_textures"); |
| 209 | + transfer_textures(); |
| 210 | + System.out.println("transfer_textures2"); |
| 211 | + |
| 212 | + VideoOutput.set_framebuffer_resolution(640, 480); |
| 213 | + VideoOutput.set_mode(VideoOutputMode.vga); |
| 214 | + |
| 215 | + int core = 0; |
| 216 | + int ta = 0; |
| 217 | + System.out.println("main"); |
| 218 | + while (true) { |
| 219 | + // unpipelined render loop |
| 220 | + TAFIFOPolygonConverter.init(TextureMemoryAllocation.isp_tsp_parameters_start[ta], |
| 221 | + TextureMemoryAllocation.isp_tsp_parameters_end[ta], |
| 222 | + TextureMemoryAllocation.object_list_start[ta], |
| 223 | + TextureMemoryAllocation.object_list_end[ta], |
| 224 | + opb_size_total, |
| 225 | + ta_alloc, |
| 226 | + framebuffer_width / 32, |
| 227 | + framebuffer_height / 32); |
| 228 | + transfer_cube_scene(); |
| 229 | + TAFIFOPolygonConverter.wait_translucent_list(); |
| 230 | + |
| 231 | + Core.start_render(TextureMemoryAllocation.region_array_start[ta], |
| 232 | + TextureMemoryAllocation.isp_tsp_parameters_start[ta], |
| 233 | + TextureMemoryAllocation.background_start[1], |
| 234 | + TextureMemoryAllocation.framebuffer_start[core], |
| 235 | + framebuffer_width); |
| 236 | + Core.wait_end_of_render_tsp(); |
| 237 | + |
| 238 | + while (!(CoreBits.spg_status__vsync(Memory.getU4(Holly.SPG_STATUS)) != 0)); |
| 239 | + Memory.putU4(Holly.FB_R_SOF1, TextureMemoryAllocation.framebuffer_start[core]); |
| 240 | + while ((CoreBits.spg_status__vsync(Memory.getU4(Holly.SPG_STATUS)) != 0)); |
| 241 | + |
| 242 | + core = (core + 1) % 1; |
| 243 | + |
| 244 | + theta += Math.DEGREES_TO_RADIANS; |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments