ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
teapot.c File Reference
#include <kernel.h>
#include <stdlib.h>
#include <tamtypes.h>
#include <math3d.h>
#include <packet.h>
#include <dma_tags.h>
#include <gif_tags.h>
#include <gs_psm.h>
#include <dma.h>
#include <graph.h>
#include <draw.h>
#include <draw3d.h>
#include "mesh_data.c"
+ Include dependency graph for teapot.c:

Go to the source code of this file.

Functions

void init_gs (framebuffer_t *frame, zbuffer_t *z)
 
void init_drawing_environment (framebuffer_t *frame, zbuffer_t *z)
 
void flip_buffers (packet_t *flip, framebuffer_t *frame)
 
qword_trender_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)
 
int render (framebuffer_t *frame, zbuffer_t *z)
 
int main (int argc, char **argv)
 

Variables

VECTOR camera_position = { 0.00f, 0.00f, 100.00f, 1.00f }
 
VECTOR camera_rotation = { 0.00f, 0.00f, 0.00f, 1.00f }
 
VECTORtemp_normals
 
VECTORtemp_lights
 
VECTORtemp_colours
 
VECTORtemp_vertices
 
xyz_txyz
 
color_trgbaq
 
int light_count = 4
 
VECTOR light_direction [4]
 
VECTOR light_colour [4]
 
int light_type [4]
 

Function Documentation

◆ flip_buffers()

void flip_buffers ( packet_t flip,
framebuffer_t frame 
)

Definition at line 125 of file teapot.c.

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 }
void dma_wait_fast(void)
Definition: dma.c:115
#define DMA_CHANNEL_GIF
Definition: dma.h:24
int dma_channel_send_normal_ucab(int channel, void *data, int qwc, int flags)
Definition: dma.c:267
void draw_wait_finish(void)
Definition: draw.c:200
qword_t * draw_finish(qword_t *q)
Definition: draw.c:188
qword_t * draw_framebuffer(qword_t *q, int context, framebuffer_t *frame)
qword_t * data
Definition: packet.h:27

References packet_t::data, DMA_CHANNEL_GIF, dma_channel_send_normal_ucab(), dma_wait_fast(), draw_finish(), draw_framebuffer(), and draw_wait_finish().

Referenced by render().

◆ init_drawing_environment()

void init_drawing_environment ( framebuffer_t frame,
zbuffer_t z 
)

Definition at line 100 of file teapot.c.

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 }
int dma_channel_send_normal(int channel, void *data, int qwc, int flags, int spr)
Definition: dma.c:238
qword_t * draw_setup_environment(qword_t *q, int context, framebuffer_t *frame, zbuffer_t *z)
Definition: draw.c:11
qword_t * draw_primitive_xyoffset(qword_t *q, int context, float x, float y)
packet_t packet
Definition: font.c:24
packet_t * packet_init(int qwords, int type)
Definition: packet.c:8
#define PACKET_NORMAL
Definition: packet.h:14

References packet_t::data, DMA_CHANNEL_GIF, dma_channel_send_normal(), dma_wait_fast(), draw_finish(), draw_primitive_xyoffset(), draw_setup_environment(), packet, packet_init(), and PACKET_NORMAL.

Referenced by main().

◆ init_gs()

void init_gs ( framebuffer_t frame,
zbuffer_t z 
)

Definition at line 66 of file teapot.c.

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 }
#define DRAW_ENABLE
Definition: draw.h:25
#define ZTEST_METHOD_GREATER_EQUAL
Definition: draw_tests.h:34
int graph_initialize(int fbp, int width, int height, int psm, int x, int y)
Definition: graph.c:6
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
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
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

References framebuffer_t::address, zbuffer_t::address, DRAW_ENABLE, zbuffer_t::enable, GRAPH_ALIGN_PAGE, graph_initialize(), graph_vram_allocate(), GS_PSM_32, GS_ZBUF_32, framebuffer_t::height, framebuffer_t::mask, zbuffer_t::mask, zbuffer_t::method, framebuffer_t::psm, framebuffer_t::width, zbuffer_t::zsm, and ZTEST_METHOD_GREATER_EQUAL.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 336 of file teapot.c.

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 dma_channel_initialize(int channel, void *handler, int flags)
Definition: dma.c:58
void dma_channel_fast_waits(int channel)
Definition: dma.c:108
s32 SleepThread(void)
#define NULL
Definition: tamtypes.h:91
int render(framebuffer_t *frame, zbuffer_t *z)
Definition: teapot.c:211
void init_drawing_environment(framebuffer_t *frame, zbuffer_t *z)
Definition: teapot.c:100
void init_gs(framebuffer_t *frame, zbuffer_t *z)
Definition: teapot.c:66

References dma_channel_fast_waits(), DMA_CHANNEL_GIF, dma_channel_initialize(), init_drawing_environment(), init_gs(), NULL, render(), and SleepThread().

◆ render()

int render ( framebuffer_t frame,
zbuffer_t z 
)

Definition at line 211 of file teapot.c.

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 }
int vertex_count
Definition: mesh_data.c:29
VECTOR object_position
Definition: cube.c:32
VECTOR object_rotation
Definition: cube.c:33
int dma_channel_send_chain(int channel, void *data, int qwc, int flags, int spr)
Definition: dma.c:184
#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
MATRIX view_screen
Definition: main.c:57
prim_t prim
Definition: main.c:74
u8 context
Definition: main.c:71
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
#define PRIM_SHADE_GOURAUD
#define PRIM_UNFIXED
#define PRIM_TRIANGLE
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
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
static GS_GIF_PACKET packets[2][GIF_PACKET_MAX]
Definition: main.c:29
float MATRIX[16]
Definition: math3d.h:23
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
#define PACKET_UCAB
Definition: packet.h:15
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 long u64
Definition: tamtypes.h:34
VECTOR * temp_normals
Definition: teapot.c:35
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
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
xyz_t * xyz
Definition: teapot.c:40
float q
Definition: draw_types.h:46

References color_t::a, prim_t::antialiasing, color_t::b, prim_t::blending, prim_t::colorfix, context, create_view_screen(), packet_t::data, DMA_CHANNEL_GIF, dma_channel_send_chain(), dma_wait_fast(), DMATAG_CNT, DMATAG_END, draw_clear(), DRAW_DISABLE, draw_disable_tests(), DRAW_ENABLE, draw_enable_tests(), draw_finish(), draw_wait_finish(), flip_buffers(), prim_t::fogging, color_t::g, graph_aspect_ratio(), graph_set_framebuffer_filtered(), graph_wait_vsync(), framebuffer_t::height, prim_t::mapping, prim_t::mapping_type, object_position, object_rotation, packet_init(), PACKET_NORMAL, PACKET_UCAB, packets, prim, PRIM_SHADE_GOURAUD, PRIM_TRIANGLE, PRIM_UNFIXED, color_t::q, color_t::r, render_teapot(), rgbaq, prim_t::shading, temp_colours, temp_lights, temp_normals, temp_vertices, prim_t::type, vertex_count, view_screen, framebuffer_t::width, and xyz.

Referenced by main().

◆ render_teapot()

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 at line 140 of file teapot.c.

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 }
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
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
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
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
void calculate_normals(VECTOR *output, int count, VECTOR *normals, MATRIX local_light)
Definition: math3d.c:446
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
VECTOR normals[1598]
Definition: mesh_data.c:4756
VECTOR camera_position
Definition: teapot.c:32
VECTOR light_direction[4]
Definition: teapot.c:45
int light_count
Definition: teapot.c:43
int light_type[4]
Definition: teapot.c:59
VECTOR light_colour[4]
Definition: teapot.c:52
VECTOR camera_rotation
Definition: teapot.c:33
u64 rgbaq
Definition: draw_types.h:40
u64 dw[2]
Definition: tamtypes.h:85
u64 xyz
Definition: draw_types.h:19

References color_t::a, calculate_colours(), calculate_lights(), calculate_normals(), calculate_vertices(), camera_position, camera_rotation, colours, create_local_light(), create_local_screen(), create_local_world(), create_world_view(), DMATAG_CNT, draw_convert_rgbq(), draw_convert_xyz(), draw_prim_end(), draw_prim_start(), DRAW_RGBAQ_REGLIST, qword_t::dw, light_colour, light_count, light_direction, light_type, local_screen, local_world, normals, object_position, object_rotation, points, points_count, prim, color_t::rgbaq, rgbaq, temp_colours, temp_lights, temp_normals, temp_vertices, vertex_count, vertices, view_screen, world_view, xyz_t::xyz, and xyz.

Referenced by render().

Variable Documentation

◆ camera_position

VECTOR camera_position = { 0.00f, 0.00f, 100.00f, 1.00f }

Definition at line 32 of file teapot.c.

Referenced by render_teapot().

◆ camera_rotation

VECTOR camera_rotation = { 0.00f, 0.00f, 0.00f, 1.00f }

Definition at line 33 of file teapot.c.

Referenced by render_teapot().

◆ light_colour

VECTOR light_colour[4]
Initial value:
= {
{ 0.00f, 0.00f, 0.00f, 1.00f },
{ 1.00f, 0.00f, 0.00f, 1.00f },
{ 0.30f, 0.30f, 0.30f, 1.00f },
{ 0.50f, 0.50f, 0.50f, 1.00f }
}

Definition at line 52 of file teapot.c.

Referenced by calculate_lights(), and render_teapot().

◆ light_count

int light_count = 4

Definition at line 43 of file teapot.c.

Referenced by calculate_lights(), and render_teapot().

◆ light_direction

VECTOR light_direction[4]
Initial value:
= {
{ 0.00f, 0.00f, 0.00f, 1.00f },
{ 1.00f, 0.00f, -1.00f, 1.00f },
{ 0.00f, 1.00f, -1.00f, 1.00f },
{ -1.00f, -1.00f, -1.00f, 1.00f }
}

Definition at line 45 of file teapot.c.

Referenced by calculate_lights(), and render_teapot().

◆ light_type

int light_type[4]
Initial value:
= {
}
#define LIGHT_DIRECTIONAL
Definition: math3d.h:108
#define LIGHT_AMBIENT
Definition: math3d.h:106

Definition at line 59 of file teapot.c.

Referenced by calculate_lights(), render_teapot(), and VuxLightNormal().

◆ rgbaq

color_t* rgbaq

Definition at line 41 of file teapot.c.

Referenced by render(), and render_teapot().

◆ temp_colours

VECTOR* temp_colours

Definition at line 37 of file teapot.c.

Referenced by render(), and render_teapot().

◆ temp_lights

VECTOR* temp_lights

Definition at line 36 of file teapot.c.

Referenced by render(), and render_teapot().

◆ temp_normals

VECTOR* temp_normals

Definition at line 35 of file teapot.c.

Referenced by render(), and render_teapot().

◆ temp_vertices

VECTOR* temp_vertices

Definition at line 38 of file teapot.c.

Referenced by render(), and render_teapot().

◆ xyz

xyz_t* xyz

Definition at line 40 of file teapot.c.

Referenced by render(), and render_teapot().