ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
cube.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 cube.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)
 
int render (framebuffer_t *frame, zbuffer_t *z)
 
int main (int argc, char **argv)
 

Variables

VECTOR object_position = { 0.00f, 0.00f, 0.00f, 1.00f }
 
VECTOR object_rotation = { 0.00f, 0.00f, 0.00f, 1.00f }
 
VECTOR camera_position = { 0.00f, 0.00f, 100.00f, 1.00f }
 
VECTOR camera_rotation = { 0.00f, 0.00f, 0.00f, 1.00f }
 

Function Documentation

◆ init_drawing_environment()

void init_drawing_environment ( framebuffer_t frame,
zbuffer_t z 
)

Definition at line 60 of file cube.c.

61 {
62 
64 
65  // This is our generic qword pointer.
66  qword_t *q = packet->data;
67 
68  // This will setup a default drawing environment.
69  q = draw_setup_environment(q,0,frame,z);
70 
71  // Now reset the primitive origin to 2048-width/2,2048-height/2.
72  q = draw_primitive_xyoffset(q,0,(2048-320),(2048-256));
73 
74  // Finish setting up the environment.
75  q = draw_finish(q);
76 
77  // Now send the packet, no need to wait since it's the first.
79  dma_wait_fast();
80 
82 
83 }
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
#define DMA_CHANNEL_GIF
Definition: dma.h:24
qword_t * draw_setup_environment(qword_t *q, int context, framebuffer_t *frame, zbuffer_t *z)
Definition: draw.c:11
qword_t * draw_finish(qword_t *q)
Definition: draw.c:188
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
void packet_free(packet_t *packet)
Definition: packet.c:63
qword_t * data
Definition: packet.h:27

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

Referenced by main().

◆ init_gs()

void init_gs ( framebuffer_t frame,
zbuffer_t z 
)

Definition at line 38 of file cube.c.

39 {
40 
41  // Define a 32-bit 640x512 framebuffer.
42  frame->width = 640;
43  frame->height = 512;
44  frame->mask = 0;
45  frame->psm = GS_PSM_32;
46  frame->address = graph_vram_allocate(frame->width,frame->height, frame->psm, GRAPH_ALIGN_PAGE);
47 
48  // Enable the zbuffer.
49  z->enable = DRAW_ENABLE;
50  z->mask = 0;
52  z->zsm = GS_ZBUF_32;
54 
55  // Initialize the screen and tie the first framebuffer to the read circuits.
56  graph_initialize(frame->address,frame->width,frame->height,frame->psm,0,0);
57 
58 }
#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 222 of file cube.c.

223 {
224 
225  // The buffers to be used.
226  framebuffer_t frame;
227  zbuffer_t z;
228 
229  // Init GIF dma channel.
232 
233  // Init the GS, framebuffer, and zbuffer.
234  init_gs(&frame, &z);
235 
236  // Init the drawing environment and framebuffer.
237  init_drawing_environment(&frame,&z);
238 
239  // Render the cube
240  render(&frame,&z);
241 
242  // Sleep
243  SleepThread();
244 
245  // End program.
246  return 0;
247 
248 }
int render(framebuffer_t *frame, zbuffer_t *z)
Definition: cube.c:85
void init_drawing_environment(framebuffer_t *frame, zbuffer_t *z)
Definition: cube.c:60
void init_gs(framebuffer_t *frame, zbuffer_t *z)
Definition: cube.c:38
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

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 85 of file cube.c.

86 {
87 
88  int i;
89  int context = 0;
90 
91  // Matrices to setup the 3D environment and camera
96 
98 
99  prim_t prim;
100  color_t color;
101 
102  xyz_t *verts;
103  color_t *colors;
104 
105  // The data packets for double buffering dma sends.
106  packet_t *packets[2];
107  packet_t *current;
108  qword_t *q;
109  qword_t *dmatag;
110 
113 
114  // Allocate calculation space.
115  temp_vertices = memalign(128, sizeof(VECTOR) * vertex_count);
116 
117  // Allocate register space.
118  verts = memalign(128, sizeof(vertex_t) * vertex_count);
119  colors = memalign(128, sizeof(color_t) * vertex_count);
120 
121  // Define the triangle primitive we want to use.
130 
131  color.r = 0x80;
132  color.g = 0x80;
133  color.b = 0x80;
134  color.a = 0x80;
135  color.q = 1.0f;
136 
137  // Create the view_screen matrix.
138  create_view_screen(view_screen, graph_aspect_ratio(), -3.00f, 3.00f, -3.00f, 3.00f, 1.00f, 2000.00f);
139 
140  // Wait for any previous dma transfers to finish before starting.
141  dma_wait_fast();
142 
143  // The main loop...
144  for (;;)
145  {
146 
147  current = packets[context];
148 
149  // Spin the cube a bit.
150  object_rotation[0] += 0.008f; //while (object_rotation[0] > 3.14f) { object_rotation[0] -= 6.28f; }
151  object_rotation[1] += 0.012f; //while (object_rotation[1] > 3.14f) { object_rotation[1] -= 6.28f; }
152 
153  // Create the local_world matrix.
155 
156  // Create the world_view matrix.
158 
159  // Create the local_screen matrix.
161 
162  // Calculate the vertex values.
164 
165  // Convert floating point vertices to fixed point and translate to center of screen.
166  draw_convert_xyz(verts, 2048, 2048, 32, vertex_count, (vertex_f_t*)temp_vertices);
167 
168  // Convert floating point colours to fixed point.
170 
171  // Grab our dmatag pointer for the dma chain.
172  dmatag = current->data;
173 
174  // Now grab our qword pointer and increment past the dmatag.
175  q = dmatag;
176  q++;
177 
178  // Clear framebuffer but don't update zbuffer.
179  q = draw_disable_tests(q,0,z);
180  q = draw_clear(q,0,2048.0f-320.0f,2048.0f-256.0f,frame->width,frame->height,0x00,0x00,0x00);
181  q = draw_enable_tests(q,0,z);
182 
183  // Draw the triangles using triangle primitive type.
184  q = draw_prim_start(q,0,&prim, &color);
185 
186  for(i = 0; i < points_count; i++)
187  {
188  q->dw[0] = colors[points[i]].rgbaq;
189  q->dw[1] = verts[points[i]].xyz;
190  q++;
191  }
192 
194 
195  // Setup a finish event.
196  q = draw_finish(q);
197 
198  // Define our dmatag for the dma chain.
199  DMATAG_END(dmatag,(q-current->data)-1,0,0,0);
200 
201  // Now send our current dma chain.
202  dma_wait_fast();
203  dma_channel_send_chain(DMA_CHANNEL_GIF,current->data, q - current->data, 0, 0);
204 
205  // Now switch our packets so we can process data while the DMAC is working.
206  context ^= 1;
207 
208  // Wait for scene to finish drawing
210 
212 
213  }
214 
215  packet_free(packets[0]);
216  packet_free(packets[1]);
217 
218  return 0;
219 
220 }
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 camera_position
Definition: cube.c:35
VECTOR object_position
Definition: cube.c:32
VECTOR object_rotation
Definition: cube.c:33
VECTOR camera_rotation
Definition: cube.c:36
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
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
void draw_wait_finish(void)
Definition: draw.c:200
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_MAP_ST
#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
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
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
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
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
VECTOR * temp_vertices
Definition: teapot.c:38
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

References color_t::a, prim_t::antialiasing, color_t::b, prim_t::blending, calculate_vertices(), camera_position, camera_rotation, prim_t::colorfix, colours, context, create_local_screen(), create_local_world(), create_view_screen(), create_world_view(), packet_t::data, DMA_CHANNEL_GIF, dma_channel_send_chain(), dma_wait_fast(), DMATAG_END, draw_clear(), draw_convert_rgbq(), draw_convert_xyz(), DRAW_DISABLE, draw_disable_tests(), DRAW_ENABLE, draw_enable_tests(), draw_finish(), draw_prim_end(), draw_prim_start(), DRAW_RGBAQ_REGLIST, draw_wait_finish(), qword_t::dw, prim_t::fogging, color_t::g, graph_aspect_ratio(), graph_wait_vsync(), framebuffer_t::height, local_screen, local_world, prim_t::mapping, prim_t::mapping_type, object_position, object_rotation, packet_free(), packet_init(), PACKET_NORMAL, packets, points, points_count, prim, PRIM_MAP_ST, PRIM_SHADE_GOURAUD, PRIM_TRIANGLE, PRIM_UNFIXED, color_t::q, color_t::r, color_t::rgbaq, prim_t::shading, temp_vertices, prim_t::type, vertex_count, vertices, view_screen, framebuffer_t::width, world_view, and xyz_t::xyz.

Referenced by main().

Variable Documentation

◆ camera_position

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

Definition at line 35 of file cube.c.

Referenced by render().

◆ camera_rotation

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

Definition at line 36 of file cube.c.

Referenced by render().

◆ object_position

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

Definition at line 32 of file cube.c.

Referenced by render(), and render_teapot().

◆ object_rotation

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

Definition at line 33 of file cube.c.

Referenced by render(), and render_teapot().