ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
cube.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 object_position = { 0.00f, 0.00f, 0.00f, 1.00f };
33 VECTOR object_rotation = { 0.00f, 0.00f, 0.00f, 1.00f };
34 
35 VECTOR camera_position = { 0.00f, 0.00f, 100.00f, 1.00f };
36 VECTOR camera_rotation = { 0.00f, 0.00f, 0.00f, 1.00f };
37 
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 }
59 
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 }
84 
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 }
221 
222 int main(int argc, char **argv)
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 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
int render(framebuffer_t *frame, zbuffer_t *z)
Definition: cube.c:85
VECTOR camera_position
Definition: cube.c:35
void init_drawing_environment(framebuffer_t *frame, zbuffer_t *z)
Definition: cube.c:60
int main(int argc, char **argv)
Definition: cube.c:222
VECTOR object_position
Definition: cube.c:32
void init_gs(framebuffer_t *frame, zbuffer_t *z)
Definition: cube.c:38
VECTOR object_rotation
Definition: cube.c:33
VECTOR camera_rotation
Definition: cube.c:36
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
#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
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
#define PRIM_SHADE_GOURAUD
#define PRIM_UNFIXED
#define PRIM_MAP_ST
#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
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
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
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
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
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