Skip to content

Commit 6e72d01

Browse files
committed
replace crab-rave with lit-crab
1 parent 598a1a5 commit 6e72d01

File tree

6 files changed

+27792
-32547
lines changed

6 files changed

+27792
-32547
lines changed

examples/crab-rave/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ authors = ["Paul Sajna <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
psp = { path = "../../psp" }
9-
wavefront_obj = "8.0.0"
8+
psp = {path = "../../psp"}

examples/crab-rave/src/crab.rs

Lines changed: 27692 additions & 4626 deletions
Large diffs are not rendered by default.

examples/crab-rave/src/main.rs

Lines changed: 99 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,94 @@
1-
#![feature(restricted_std)]
1+
#![no_std]
22
#![no_main]
33

4-
use core::{ffi::c_void, f32::consts::PI};
4+
use core::f32::consts::PI;
55

6-
use psp::{sys, vram_alloc::get_vram_allocator, BUF_WIDTH, SCREEN_HEIGHT, SCREEN_WIDTH,
6+
use psp::{
7+
math::{cosf32, sinf32},
78
sys::{
8-
TexturePixelFormat, DisplayPixelFormat, GuPrimitive, VertexType, GuState,
9+
self, ClearBuffer, DepthFunc, DisplayPixelFormat, FrontFaceDirection, GuContextType,
10+
GuPrimitive, GuState, GuSyncBehavior, GuSyncMode, LightComponent, LightType::Pointlight,
11+
MatrixMode, ScePspFVector3, ShadingModel, TexturePixelFormat, VertexType,
12+
913
sceAudioChReserve, sceAudioOutput, sceAudioGetChannelRestLen,
10-
sceAudioSetChannelDataLen, ThreadAttributes, ScePspFVector3,
14+
sceAudioSetChannelDataLen,
1115
},
16+
vram_alloc::get_vram_allocator,
17+
Align16, BUF_WIDTH, SCREEN_HEIGHT, SCREEN_WIDTH,
1218
};
1319

1420
mod crab;
21+
use crab::CRAB_VERTICES;
1522

1623
psp::module!("crab-rave", 1, 1);
1724

18-
static mut LIST: psp::Align16<[u32; 0x40000]> = psp::Align16([0; 0x40000]);
25+
const LIGHT_DISTANCE: f32 = 5.0;
26+
27+
static mut LIST: Align16<[u32; 0x40000]> = Align16([0; 0x40000]);
1928

2029
static AUDIO_CLIP: [u8; 672320] = *include_bytes!("../assets/crabrave.pcm");
2130

2231
const MAX_VOL: i32 = 0x8000;
2332
const MAX_SAMPLES: usize = 65472;
2433
const CHANNEL: i32 = 0;
2534

26-
27-
#[no_mangle]
2835
fn psp_main() {
2936
psp::enable_home_button();
3037

31-
let mut allocator = get_vram_allocator().unwrap();
32-
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
33-
let fbp1 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
34-
let zbp = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444).as_mut_ptr_from_zero();
38+
let np_vertex_format: VertexType = VertexType::NORMAL_32BITF | VertexType::VERTEX_32BITF;
3539

3640
unsafe {
3741
sceAudioChReserve(CHANNEL, MAX_SAMPLES as i32, psp::sys::AudioFormat::Stereo)
3842
};
3943
let mut start_pos: usize = 0;
4044
let mut restlen = 0;
4145

42-
unsafe {sys::sceKernelChangeCurrentThreadAttr(0, ThreadAttributes::VFPU)};
43-
44-
4546
unsafe {
47+
sys::sceKernelChangeCurrentThreadAttr(0, sys::ThreadAttributes::VFPU);
48+
sys::sceKernelDcacheWritebackAll();
49+
50+
let mut allocator = get_vram_allocator().unwrap();
51+
let fbp0 = allocator
52+
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888)
53+
.as_mut_ptr_from_zero();
54+
let fbp1 = allocator
55+
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888)
56+
.as_mut_ptr_from_zero();
57+
let zbp = allocator
58+
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444)
59+
.as_mut_ptr_from_zero();
4660

4761
sys::sceGuInit();
48-
sys::sceGuStart(
49-
sys::GuContextType::Direct,
50-
&mut LIST as *mut _ as *mut c_void,
51-
);
62+
sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut _ as _);
5263
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0 as _, BUF_WIDTH as i32);
53-
sys::sceGuDispBuffer(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, fbp1 as _, BUF_WIDTH as i32);
64+
sys::sceGuDispBuffer(
65+
SCREEN_WIDTH as i32,
66+
SCREEN_HEIGHT as i32,
67+
fbp1 as _,
68+
BUF_WIDTH as i32,
69+
);
5470
sys::sceGuDepthBuffer(zbp as _, BUF_WIDTH as i32);
55-
sys::sceGuOffset(2048 - (SCREEN_WIDTH/2), 2048 - (SCREEN_HEIGHT/2));
71+
sys::sceGuOffset(2048 - (SCREEN_WIDTH / 2), 2048 - (SCREEN_HEIGHT / 2));
5672
sys::sceGuViewport(2048, 2048, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
57-
sys::sceGuDepthRange(65535, 0);
73+
sys::sceGuDepthRange(0xc350, 0x2710);
5874
sys::sceGuScissor(0, 0, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
5975
sys::sceGuEnable(GuState::ScissorTest);
76+
sys::sceGuDepthFunc(DepthFunc::GreaterOrEqual);
77+
sys::sceGuFrontFace(FrontFaceDirection::Clockwise);
78+
sys::sceGuShadeModel(ShadingModel::Smooth);
79+
sys::sceGuEnable(GuState::DepthTest);
80+
sys::sceGuEnable(GuState::CullFace);
81+
sys::sceGuEnable(GuState::ClipPlanes);
6082
sys::sceGuEnable(GuState::Lighting);
6183
sys::sceGuEnable(GuState::Light0);
62-
sys::sceGuFrontFace(sys::FrontFaceDirection::Clockwise);
84+
sys::sceGuEnable(GuState::Light1);
85+
sys::sceGuEnable(GuState::Light2);
86+
sys::sceGuEnable(GuState::Light3);
6387
sys::sceGuFinish();
64-
sys::sceGuSync(sys::GuSyncMode::Finish, sys::GuSyncBehavior::Wait);
88+
sys::sceGuSync(GuSyncMode::Finish, GuSyncBehavior::Wait);
6589
sys::sceDisplayWaitVblankStart();
6690
sys::sceGuDisplay(true);
91+
6792
let mut val = 0.0;
6893

6994
loop {
@@ -95,60 +120,78 @@ fn psp_main() {
95120

96121
restlen = sceAudioGetChannelRestLen(CHANNEL);
97122

98-
99-
sys::sceGuStart(
100-
sys::GuContextType::Direct,
101-
&mut LIST as *mut _ as *mut c_void
102-
);
103-
sys::sceGuClearColor(0xff554433);
123+
sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut _ as _);
124+
sys::sceGuClearColor(0x554433);
104125
sys::sceGuClearDepth(0);
105-
sys::sceGuClear(
106-
sys::ClearBuffer::COLOR_BUFFER_BIT |
107-
sys::ClearBuffer::DEPTH_BUFFER_BIT
108-
);
126+
sys::sceGuClear(ClearBuffer::COLOR_BUFFER_BIT | ClearBuffer::DEPTH_BUFFER_BIT);
127+
128+
for i in 0..4 {
129+
let pos = ScePspFVector3 {
130+
x: cosf32(i as f32 * (PI / 2.0) + val as f32 * (PI / 180.0)) * LIGHT_DISTANCE,
131+
y: 0.0,
132+
z: (sinf32(i as f32 * (PI / 2.0) + val as f32 * (PI / 180.0)) * LIGHT_DISTANCE),
133+
};
134+
sys::sceGuLight(
135+
i,
136+
Pointlight,
137+
LightComponent::DIFFUSE | LightComponent::SPECULAR,
138+
&pos,
139+
);
140+
sys::sceGuLightColor(i, LightComponent::DIFFUSE, 0xff00_ffff);
141+
sys::sceGuLightColor(i, LightComponent::SPECULAR, 0xffff_ffff);
142+
sys::sceGuLightAtt(i, 0.0, 1.0, 0.0);
143+
}
144+
145+
sys::sceGuSpecular(12.0);
146+
sys::sceGuAmbient(0x0022_2222);
109147

110-
sys::sceGumMatrixMode(sys::MatrixMode::Projection);
148+
sys::sceGumMatrixMode(MatrixMode::Projection);
111149
sys::sceGumLoadIdentity();
112-
sys::sceGumPerspective(75.0, 16.0 / 9.0, 0.5, 1000.0);
150+
sys::sceGumPerspective(75.0, 16.0 / 9.0, 1.0, 1000.0);
113151

114-
sys::sceGumMatrixMode(sys::MatrixMode::View);
152+
sys::sceGumMatrixMode(MatrixMode::View);
153+
let pos = ScePspFVector3 {
154+
x: 0.0,
155+
y: 0.0,
156+
z: -9.0,
157+
};
115158
sys::sceGumLoadIdentity();
159+
sys::sceGumTranslate(&pos);
116160

117-
sys::sceGumMatrixMode(sys::MatrixMode::Model);
161+
sys::sceGumMatrixMode(MatrixMode::Model);
162+
let pos = ScePspFVector3 {
163+
x: 0.0,
164+
y: -1.5,
165+
z: 0.0,
166+
};
118167
sys::sceGumLoadIdentity();
168+
sys::sceGumTranslate(&pos);
119169

170+
sys::sceGuColor(0xff7777);
120171

121-
let pos = sys::ScePspFVector3 { x: -1.0, y: 0.0, z: -8.0 };
172+
sys::sceGumMatrixMode(MatrixMode::Model);
122173
let rot = ScePspFVector3 {
123174
x: val * 0.79 * (PI / 180.0),
124175
y: val * 0.98 * (PI / 180.0),
125176
z: val * 1.32 * (PI / 180.0),
126177
};
127-
128-
sys::sceGumTranslate(&pos);
178+
sys::sceGumLoadIdentity();
129179
sys::sceGumRotateXYZ(&rot);
130180

131-
sys::sceGuLight(0, sys::LightType::Pointlight, sys::LightComponent::DIFFUSE,
132-
&ScePspFVector3 {
133-
x: -1.0, y: 0.0, z: -2.0
134-
}
135-
);
136-
137-
sys::sceGuLightColor(0, sys::LightComponent::DIFFUSE, 0xff00ffff);
138-
sys::sceGuMaterial(sys::LightComponent::DIFFUSE, 0xff0000ff);
139-
181+
sys::sceGuColor(0xff0000ff);
140182
sys::sceGumDrawArray(
141183
GuPrimitive::Triangles,
142-
VertexType::VERTEX_32BITF | VertexType::TRANSFORM_3D | VertexType::INDEX_16BIT,
143-
crab::INDICES.0.len() as i32,
144-
&crab::INDICES as *const _ as *const _,
145-
&crab::VERTICES as *const _ as *const _,
184+
np_vertex_format | VertexType::TRANSFORM_3D,
185+
CRAB_VERTICES.0.len() as i32,
186+
core::ptr::null(),
187+
&CRAB_VERTICES.0 as *const _ as _,
146188
);
147-
148189
sys::sceGuFinish();
149-
sys::sceGuSync(sys::GuSyncMode::Finish, sys::GuSyncBehavior::Wait);
190+
sys::sceGuSync(GuSyncMode::Finish, GuSyncBehavior::Wait);
191+
150192
sys::sceDisplayWaitVblankStart();
151193
sys::sceGuSwapBuffers();
194+
152195
val += 1.0;
153196
}
154197
}

examples/lit-crab/Cargo.toml

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)