ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
teapot.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # (c) 2005 Naomi Peori <naomi@peori.ca>
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 #
10 */
11 
12 #include <kernel.h>
13 #include <stdlib.h>
14 #include <tamtypes.h>
15 #include <math3d.h>
16 
17 #include <packet.h>
18 
19 #include <dma_tags.h>
20 #include <gif_tags.h>
21 #include <gs_psm.h>
22 
23 #include <dma.h>
24 
25 #include <graph.h>
26 
27 #include <draw.h>
28 #include <draw3d.h>
29 
30 #include "mesh_data.c"
31 
32 VECTOR camera_position = { 0.00f, 0.00f, 100.00f, 1.00f };
33 VECTOR camera_rotation = { 0.00f, 0.00f, 0.00f, 1.00f };
34 
39 
42 
43 int light_count = 4;
44 
46  { 0.00f, 0.00f, 0.00f, 1.00f },
47  { 1.00f, 0.00f, -1.00f, 1.00f },
48  { 0.00f, 1.00f, -1.00f, 1.00f },
49  { -1.00f, -1.00f, -1.00f, 1.00f }
50 };
51 
53  { 0.00f, 0.00f, 0.00f, 1.00f },
54  { 1.00f, 0.00f, 0.00f, 1.00f },
55  { 0.30f, 0.30f, 0.30f, 1.00f },
56  { 0.50f, 0.50f, 0.50f, 1.00f }
57 };
58 
59 int light_type[4] = {
64 };
65 
67 {
68 
69  // Define a 32-bit 640x512 framebuffer.
70  frame->width = 640;
71  frame->height = 512;
72  frame->mask = 0;
73  frame->psm = GS_PSM_32;
74 
75  // Allocate some vram for our framebuffer.
76  frame->address = graph_vram_allocate(frame->width,frame->height, frame->psm, GRAPH_ALIGN_PAGE);
77 
78  frame++;
79 
80  frame->width = 640;
81  frame->height = 512;
82  frame->mask = 0;
83  frame->psm = GS_PSM_32;
84 
85  // Allocate some vram for our framebuffer.
86  frame->address = graph_vram_allocate(frame->width,frame->height, frame->psm, GRAPH_ALIGN_PAGE);
87 
88  // Enable the zbuffer.
89  z->enable = DRAW_ENABLE;
90  z->mask = 0;
92  z->zsm = GS_ZBUF_32;
94 
95  // Initialize the screen and tie the first framebuffer to the read circuits.
96  graph_initialize(frame->address,frame->width,frame->height,frame->psm,0,0);
97 
98 }
99 
101 {
102 
104 
105  // This is our generic qword pointer.
106  qword_t *q = packet->data;
107 
108  // This will setup a default drawing environment.
109  q = draw_setup_environment(q,0,frame,z);
110 
111  // Now reset the primitive origin to 2048-width/2,2048-height/2.
112  q = draw_primitive_xyoffset(q,0,(2048-320),(2048-256));
113 
114  // Finish setting up the environment.
115  q = draw_finish(q);
116 
117  // Now send the packet, no need to wait since it's the first.
119  dma_wait_fast();
120 
121  free(packet);
122 
123 }
124 
126 {
127 
128  qword_t *q = flip->data;
129 
130  q = draw_framebuffer(q,0,frame);
131  q = draw_finish(q);
132 
133  dma_wait_fast();
135 
137 
138 }
139 
141 {
142 
143  int i;
144 
145  qword_t *dmatag;
146 
148  MATRIX local_light;
150 
152 
153  // Now grab our qword pointer and increment past the dmatag.
154  dmatag = q;
155  q++;
156 
157  // Spin the teapot a bit.
158  object_rotation[0] += 0.008f; while (object_rotation[0] > 3.14f) { object_rotation[0] -= 6.28f; }
159  object_rotation[1] += 0.112f; while (object_rotation[1] > 3.14f) { object_rotation[1] -= 6.28f; }
160 
161  // Create the local_world matrix.
163 
164  // Create the local_light matrix.
165  create_local_light(local_light, object_rotation);
166 
167  // Create the world_view matrix.
169 
170  // Create the local_screen matrix.
172 
173  // Calculate the normal values.
175 
176  // Calculate the lighting values.
178 
179  // Calculate the colour values after lighting.
181 
182  // Calculate the vertex values.
184 
185  // Convert floating point vertices to fixed point and translate to center of screen.
187 
188  // Convert floating point colours to fixed point.
190 
191  // Draw the triangles using triangle primitive type.
192  q = draw_prim_start(q,0,prim,color);
193 
194  for(i = 0; i < points_count; i++)
195  {
196  q->dw[0] = rgbaq[points[i]].rgbaq;
197  q->dw[1] = xyz[points[i]].xyz;
198  q++;
199  }
200 
202 
203  // Define our dmatag for the dma chain.
204  DMATAG_CNT(dmatag,q-dmatag-1,0,0,0);
205 
206 
207  return q;
208 
209 }
210 
212 {
213 
214  int context = 0;
215 
216  // Packets for doublebuffering dma sends
217  packet_t *packets[2];
218  packet_t *current;
219 
220  // This packet is special for framebuffer switching
221  packet_t *flip_pkt;
222 
223  qword_t *q;
224  qword_t *dmatag;
225 
226  prim_t prim;
227  color_t color;
228 
230 
231  packets[0] = packet_init(40000,PACKET_NORMAL);
232  packets[1] = packet_init(40000,PACKET_NORMAL);
233 
234  // Uncached accelerated
235  flip_pkt = packet_init(3,PACKET_UCAB);
236 
237  VECTOR object_position = { 0.00f, 0.00f, 0.00f, 1.00f };
238  VECTOR object_rotation = { 0.00f, 0.00f, 0.00f, 1.00f };
239 
240  // Define the triangle primitive we want to use.
249 
250  color.r = 0x80;
251  color.g = 0x80;
252  color.b = 0x80;
253  color.a = 0x80;
254  color.q = 1.0f;
255 
256  // Allocate calculation space.
257  temp_normals = memalign(128, sizeof(VECTOR) * vertex_count);
258  temp_lights = memalign(128, sizeof(VECTOR) * vertex_count);
259  temp_colours = memalign(128, sizeof(VECTOR) * vertex_count);
260  temp_vertices = memalign(128, sizeof(VECTOR) * vertex_count);
261 
262  // Allocate register space.
263  xyz = memalign(128, sizeof(u64) * vertex_count);
264  rgbaq = memalign(128, sizeof(u64) * vertex_count);
265 
266  // Create the view_screen matrix.
267  create_view_screen(view_screen, graph_aspect_ratio(), -3.00f, 3.00f, -3.00f, 3.00f, 1.00f, 2000.00f);
268 
269  for (;;)
270  {
271 
272  current = packets[context];
273 
274  q = current->data;
275 
276  dmatag = q;
277  q++;
278 
279  // Clear framebuffer without any pixel testing.
280  q = draw_disable_tests(q,0,z);
281  q = draw_clear(q,0,2048.0f-320.0f,2048.0f-256.0f,frame->width,frame->height,0x00,0x00,0x00);
282  q = draw_enable_tests(q,0,z);
283 
284  DMATAG_CNT(dmatag,q-dmatag - 1,0,0,0);
285 
286  //render teapots
287  color.a = 0x40;
288  object_position[0] = 30.0f;
289  q = render_teapot(q, view_screen, object_position, object_rotation, &prim, &color, frame, z);
290 
291  object_position[0] = -30.0f;
292  q = render_teapot(q, view_screen, object_position, object_rotation, &prim, &color, frame, z);
293 
294  color.a = 0x80;
295  object_position[0] = 0.0f;
296  object_position[1] = -20.0f;
297  q = render_teapot(q, view_screen, object_position, object_rotation, &prim, &color, frame, z);
298 
299  object_position[1] = 20.0f;
300  q = render_teapot(q, view_screen, object_position, object_rotation, &prim, &color, frame, z);
301 
302  object_position[0] = 0.0f;
303  object_position[1] = 0.0f;
304 
305  dmatag = q;
306  q++;
307 
308  q = draw_finish(q);
309 
310  DMATAG_END(dmatag,q-dmatag-1,0,0,0);
311 
312  // Now send our current dma chain.
313  dma_wait_fast();
314  dma_channel_send_chain(DMA_CHANNEL_GIF,current->data, q - current->data, 0, 0);
315 
316  // Either block until a vsync, or keep rendering until there's one available.
318 
320  graph_set_framebuffer_filtered(frame[context].address,frame[context].width,frame[context].psm,0,0);
321 
322  // Switch context.
323  context ^= 1;
324 
325  // We need to flip buffers outside of the chain, for some reason,
326  // so we use a separate small packet.
327  flip_buffers(flip_pkt,&frame[context]);
328 
329  }
330 
331  free(packets[0]);
332  free(packets[1]);
333 
334 }
335 
336 int main(int argc, char **argv)
337 {
338 
339  // The buffers to be used.
340  framebuffer_t frame[2];
341  zbuffer_t z;
342 
343  // Init GIF dma channel.
346 
347  // Init the GS, framebuffer, and zbuffer.
348  init_gs(frame, &z);
349 
350  // Init the drawing environment and framebuffer.
351  init_drawing_environment(frame,&z);
352 
353  // Render teapots
354  render(frame,&z);
355 
356  // Sleep
357  SleepThread();
358 
359  // End program.
360  return 0;
361 
362 }
int vertex_count
Definition: mesh_data.c:29
VECTOR vertices[24]
Definition: mesh_data.c:31
int points[36]
Definition: mesh_data.c:14
VECTOR colours[24]
Definition: mesh_data.c:58
int points_count
Definition: mesh_data.c:12
VECTOR object_position
Definition: cube.c:32
VECTOR object_rotation
Definition: cube.c:33
void dma_wait_fast(void)
Definition: dma.c:115
int dma_channel_send_normal(int channel, void *data, int qwc, int flags, int spr)
Definition: dma.c:238
int dma_channel_initialize(int channel, void *handler, int flags)
Definition: dma.c:58
#define DMA_CHANNEL_GIF
Definition: dma.h:24
void dma_channel_fast_waits(int channel)
Definition: dma.c:108
int dma_channel_send_chain(int channel, void *data, int qwc, int flags, int spr)
Definition: dma.c:184
int dma_channel_send_normal_ucab(int channel, void *data, int qwc, int flags)
Definition: dma.c:267
#define DMATAG_END(Q, QWC, SPR, W2, W3)
Definition: dma_tags.h:75
#define DMATAG_CNT(Q, QWC, SPR, W2, W3)
Definition: dma_tags.h:57
qword_t * draw_prim_start(qword_t *q, int context, prim_t *prim, color_t *color)
Definition: draw3d.c:11
int draw_convert_rgbq(color_t *output, int count, vertex_f_t *vertices, color_f_t *colours, unsigned char alpha)
Definition: draw3d.c:56
int draw_convert_xyz(xyz_t *output, float x, float y, int z, int count, vertex_f_t *vertices)
Definition: draw3d.c:147
qword_t * draw_prim_end(qword_t *q, int nreg, u64 reglist)
Definition: draw3d.c:33
#define DRAW_RGBAQ_REGLIST
Definition: draw3d.h:20
MATRIX world_view
Definition: main.c:57
MATRIX local_screen
Definition: main.c:57
MATRIX local_world
Definition: main.c:57
MATRIX view_screen
Definition: main.c:57
prim_t prim
Definition: main.c:74
u8 context
Definition: main.c:71
qword_t * draw_setup_environment(qword_t *q, int context, framebuffer_t *frame, zbuffer_t *z)
Definition: draw.c:11
void draw_wait_finish(void)
Definition: draw.c:200
#define DRAW_ENABLE
Definition: draw.h:25
qword_t * draw_finish(qword_t *q)
Definition: draw.c:188
qword_t * draw_clear(qword_t *q, int context, float x, float y, float width, float height, int r, int g, int b)
Definition: draw.c:148
#define DRAW_DISABLE
Definition: draw.h:24
qword_t * draw_framebuffer(qword_t *q, int context, framebuffer_t *frame)
#define PRIM_SHADE_GOURAUD
#define PRIM_UNFIXED
#define PRIM_TRIANGLE
qword_t * draw_primitive_xyoffset(qword_t *q, int context, float x, float y)
qword_t * draw_disable_tests(qword_t *q, int context, zbuffer_t *z)
Definition: draw.c:120
qword_t * draw_enable_tests(qword_t *q, int context, zbuffer_t *z)
Definition: draw.c:134
#define ZTEST_METHOD_GREATER_EQUAL
Definition: draw_tests.h:34
packet_t packet
Definition: font.c:24
int graph_initialize(int fbp, int width, int height, int psm, int x, int y)
Definition: graph.c:6
void graph_wait_vsync(void)
Definition: graph.c:99
void graph_set_framebuffer_filtered(int fbp, int width, int psm, int x, int y)
Definition: graph_mode.c:265
float graph_aspect_ratio(void)
Definition: graph_mode.c:397
int graph_vram_allocate(int width, int height, int psm, int alignment)
Definition: graph_vram.c:7
#define GRAPH_ALIGN_PAGE
Definition: graph_vram.h:13
#define GS_ZBUF_32
Definition: gs_psm.h:42
#define GS_PSM_32
Definition: gs_psm.h:11
s32 SleepThread(void)
static GS_GIF_PACKET packets[2][GIF_PACKET_MAX]
Definition: main.c:29
float MATRIX[16]
Definition: math3d.h:23
void create_local_screen(MATRIX local_screen, MATRIX local_world, MATRIX world_view, MATRIX view_screen)
Definition: math3d.c:434
void create_local_world(MATRIX local_world, VECTOR translation, VECTOR rotation)
Definition: math3d.c:377
void calculate_vertices(VECTOR *output, int count, VECTOR *vertices, MATRIX local_screen)
Definition: math3d.c:557
#define LIGHT_DIRECTIONAL
Definition: math3d.h:108
void create_world_view(MATRIX world_view, VECTOR translation, VECTOR rotation)
Definition: math3d.c:394
void calculate_lights(VECTOR *output, int count, VECTOR *normals, VECTOR *light_directions, VECTOR *light_colours, int *light_types, int light_count)
Definition: math3d.c:492
float VECTOR[4]
Definition: math3d.h:21
void create_view_screen(MATRIX view_screen, float aspect, float left, float right, float bottom, float top, float near, float far)
Definition: math3d.c:416
void calculate_normals(VECTOR *output, int count, VECTOR *normals, MATRIX local_light)
Definition: math3d.c:446
#define LIGHT_AMBIENT
Definition: math3d.h:106
void calculate_colours(VECTOR *output, int count, VECTOR *colours, VECTOR *lights)
Definition: math3d.c:539
void create_local_light(MATRIX local_light, VECTOR rotation)
Definition: math3d.c:386
packet_t * packet_init(int qwords, int type)
Definition: packet.c:8
#define PACKET_NORMAL
Definition: packet.h:14
#define PACKET_UCAB
Definition: packet.h:15
unsigned int address
Definition: draw_buffers.h:41
unsigned int height
Definition: draw_buffers.h:43
unsigned int psm
Definition: draw_buffers.h:44
unsigned int mask
Definition: draw_buffers.h:45
unsigned int width
Definition: draw_buffers.h:42
qword_t * data
Definition: packet.h:27
unsigned char type
unsigned char blending
unsigned char antialiasing
unsigned char colorfix
unsigned char shading
unsigned char fogging
unsigned char mapping_type
unsigned char mapping
unsigned int mask
Definition: draw_buffers.h:53
unsigned int method
Definition: draw_buffers.h:50
unsigned int address
Definition: draw_buffers.h:51
unsigned int enable
Definition: draw_buffers.h:49
unsigned int zsm
Definition: draw_buffers.h:52
#define NULL
Definition: tamtypes.h:91
unsigned long u64
Definition: tamtypes.h:34
VECTOR normals[1598]
Definition: mesh_data.c:4756
VECTOR * temp_normals
Definition: teapot.c:35
int render(framebuffer_t *frame, zbuffer_t *z)
Definition: teapot.c:211
VECTOR camera_position
Definition: teapot.c:32
VECTOR light_direction[4]
Definition: teapot.c:45
void init_drawing_environment(framebuffer_t *frame, zbuffer_t *z)
Definition: teapot.c:100
int main(int argc, char **argv)
Definition: teapot.c:336
int light_count
Definition: teapot.c:43
void init_gs(framebuffer_t *frame, zbuffer_t *z)
Definition: teapot.c:66
VECTOR * temp_lights
Definition: teapot.c:36
void flip_buffers(packet_t *flip, framebuffer_t *frame)
Definition: teapot.c:125
color_t * rgbaq
Definition: teapot.c:41
VECTOR * temp_colours
Definition: teapot.c:37
int light_type[4]
Definition: teapot.c:59
qword_t * render_teapot(qword_t *q, MATRIX view_screen, VECTOR object_position, VECTOR object_rotation, prim_t *prim, color_t *color, framebuffer_t *frame, zbuffer_t *z)
Definition: teapot.c:140
VECTOR * temp_vertices
Definition: teapot.c:38
VECTOR light_colour[4]
Definition: teapot.c:52
xyz_t * xyz
Definition: teapot.c:40
VECTOR camera_rotation
Definition: teapot.c:33
float q
Definition: draw_types.h:46
u64 rgbaq
Definition: draw_types.h:40
u64 dw[2]
Definition: tamtypes.h:85
u64 xyz
Definition: draw_types.h:19