ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
texture.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 "flower.c"
31 #include "mesh_data.c"
32 
33 extern unsigned char flower[];
34 
35 VECTOR object_position = { 0.00f, 0.00f, 0.00f, 1.00f };
36 VECTOR object_rotation = { 0.00f, 0.00f, 0.00f, 1.00f };
37 
38 VECTOR camera_position = { 0.00f, 0.00f, 100.00f, 1.00f };
39 VECTOR camera_rotation = { 0.00f, 0.00f, 0.00f, 1.00f };
40 
41 void init_gs(framebuffer_t *frame, zbuffer_t *z, texbuffer_t *texbuf)
42 {
43 
44  // Define a 32-bit 640x512 framebuffer.
45  frame->width = 640;
46  frame->height = 512;
47  frame->mask = 0;
48  frame->psm = GS_PSM_32;
49  frame->address = graph_vram_allocate(frame->width,frame->height, frame->psm, GRAPH_ALIGN_PAGE);
50 
51  // Enable the zbuffer.
52  z->enable = DRAW_ENABLE;
53  z->mask = 0;
55  z->zsm = GS_ZBUF_32;
57 
58  // Allocate some vram for the texture buffer
59  texbuf->width = 256;
60  texbuf->psm = GS_PSM_24;
62 
63  // Initialize the screen and tie the first framebuffer to the read circuits.
64  graph_initialize(frame->address,frame->width,frame->height,frame->psm,0,0);
65 
66 }
67 
69 {
70 
72 
73  // This is our generic qword pointer.
74  qword_t *q = packet->data;
75 
76  // This will setup a default drawing environment.
77  q = draw_setup_environment(q,0,frame,z);
78 
79  // Now reset the primitive origin to 2048-width/2,2048-height/2.
80  q = draw_primitive_xyoffset(q,0,(2048-320),(2048-256));
81 
82  // Finish setting up the environment.
83  q = draw_finish(q);
84 
85  // Now send the packet, no need to wait since it's the first.
87  dma_wait_fast();
88 
90 
91 }
92 
94 {
95 
97 
98  qword_t *q = packet->data;
99 
100  q = packet->data;
101 
102  q = draw_texture_transfer(q,flower,256,256,GS_PSM_24,texbuf->address,texbuf->width);
103  q = draw_texture_flush(q);
104 
106  dma_wait_fast();
107 
109 
110 }
111 
113 {
114 
116 
117  qword_t *q = packet->data;
118 
119  // Using a texture involves setting up a lot of information.
121 
122  lod_t lod;
123 
125  lod.max_level = 0;
128  lod.l = 0;
129  lod.k = 0;
130 
131  texbuf->info.width = draw_log2(256);
132  texbuf->info.height = draw_log2(256);
135 
137  clut.start = 0;
138  clut.psm = 0;
140  clut.address = 0;
141 
142  q = draw_texture_sampling(q,0,&lod);
143  q = draw_texturebuffer(q,0,texbuf,&clut);
144 
145  // Now send the packet, no need to wait since it's the first.
147  dma_wait_fast();
148 
150 
151 }
152 
154 {
155 
156  int i;
157  int context = 0;
158 
159  packet_t *packets[2];
160  packet_t *current;
161 
162  qword_t *q;
163  u64 *dw;
164 
169 
170  prim_t prim;
171  color_t color;
172 
174 
175  xyz_t *xyz;
176  color_t *rgbaq;
177  texel_t *st;
178 
181 
182  // Define the triangle primitive we want to use.
191 
192  color.r = 0x80;
193  color.g = 0x80;
194  color.b = 0x80;
195  color.a = 0x40;
196  color.q = 1.0f;
197 
198  // Allocate calculation space.
199  temp_vertices = memalign(128, sizeof(VECTOR) * vertex_count);
200 
201  // Allocate register space.
202  xyz = memalign(128, sizeof(u64) * vertex_count);
203  rgbaq = memalign(128, sizeof(u64) * vertex_count);
204  st = memalign(128, sizeof(u64) * vertex_count);
205 
206  // Create the view_screen matrix.
207  create_view_screen(view_screen, graph_aspect_ratio(), -3.00f, 3.00f, -3.00f, 3.00f, 1.00f, 2000.00f);
208 
209  // The main loop...
210  for (;;)
211  {
212 
213  current = packets[context];
214 
215  // Spin the cube a bit.
216  object_rotation[0] += 0.008f; while (object_rotation[0] > 3.14f) { object_rotation[0] -= 6.28f; }
217  object_rotation[1] += 0.012f; while (object_rotation[1] > 3.14f) { object_rotation[1] -= 6.28f; }
218 
219  // Create the local_world matrix.
221 
222  // Create the world_view matrix.
224 
225  // Create the local_screen matrix.
227 
228  // Calculate the vertex values.
230 
231  // Generate the XYZ register values.
233 
234  // Convert floating point colours to fixed point.
236 
237  // Generate the ST register values.
239 
240  q = current->data;
241 
242  // Clear framebuffer but don't update zbuffer.
243  q = draw_disable_tests(q,0,z);
244  q = draw_clear(q,0,2048.0f-320.0f,2048.0f-256.0f,frame->width,frame->height,0x40,0x40,0x40);
245  q = draw_enable_tests(q,0,z);
246 
247  // Draw the triangles using triangle primitive type.
248  // Use a 64-bit pointer to simplify adding data to the packet.
249  dw = (u64*)draw_prim_start(q,0,&prim, &color);
250 
251  for(i = 0; i < points_count; i++)
252  {
253  *dw++ = rgbaq[points[i]].rgbaq;
254  *dw++ = st[points[i]].uv;
255  *dw++ = xyz[points[i]].xyz;
256  }
257 
258  // Check if we're in middle of a qword or not.
259  if ((u32)dw % 16)
260  {
261 
262  *dw++ = 0;
263 
264  }
265 
266  // Only 3 registers rgbaq/st/xyz were used (standard STQ reglist)
268 
269  // Setup a finish event.
270  q = draw_finish(q);
271 
272  // Now send our current dma chain.
273  dma_wait_fast();
274  dma_channel_send_normal(DMA_CHANNEL_GIF,current->data, q - current->data, 0, 0);
275 
276  // Now switch our packets so we can process data while the DMAC is working.
277  context ^= 1;
278 
279  // Wait for scene to finish drawing
281 
283 
284  }
285 
286  free(packets[0]);
287  free(packets[1]);
288 
289  // End program.
290  return 0;
291 
292 }
293 
294 int main(int argc, char **argv)
295 {
296 
297  // The buffers to be used.
298  framebuffer_t frame;
299  zbuffer_t z;
300  texbuffer_t texbuf;
301 
302  // Init GIF dma channel.
305 
306  // Init the GS, framebuffer, zbuffer, and texture buffer.
307  init_gs(&frame, &z, &texbuf);
308 
309  // Init the drawing environment and framebuffer.
310  init_drawing_environment(&frame,&z);
311 
312  // Load the texture into vram.
313  load_texture(&texbuf);
314 
315  // Setup texture buffer
316  setup_texture(&texbuf);
317 
318  // Render textured cube
319  render(&frame,&z);
320 
321  // Sleep
322  SleepThread();
323 
324  // End program.
325  return 0;
326 
327 }
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
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 draw_convert_st(texel_t *output, int count, vertex_f_t *vertices, texel_f_t *coords)
Definition: draw3d.c:120
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
#define DRAW_STQ_REGLIST
Definition: draw3d.h:33
qword_t * draw_prim_end(qword_t *q, int nreg, u64 reglist)
Definition: draw3d.c:33
int render(framebuffer_t *frame, zbuffer_t *z)
Definition: texture.c:153
void load_texture(texbuffer_t *texbuf)
Definition: texture.c:93
VECTOR camera_position
Definition: texture.c:38
void init_drawing_environment(framebuffer_t *frame, zbuffer_t *z)
Definition: texture.c:68
int main(int argc, char **argv)
Definition: texture.c:294
unsigned char flower[]
VECTOR object_position
Definition: texture.c:35
VECTOR object_rotation
Definition: texture.c:36
void init_gs(framebuffer_t *frame, zbuffer_t *z, texbuffer_t *texbuf)
Definition: texture.c:41
void setup_texture(texbuffer_t *texbuf)
Definition: texture.c:112
VECTOR camera_rotation
Definition: texture.c:39
MATRIX world_view
Definition: main.c:57
MATRIX local_screen
Definition: main.c:57
MATRIX local_world
Definition: main.c:57
lod_t lod
Definition: main.c:86
MATRIX view_screen
Definition: main.c:57
prim_t prim
Definition: main.c:74
u8 context
Definition: main.c:71
clutbuffer_t clut
Definition: main.c:80
qword_t * draw_setup_environment(qword_t *q, int context, framebuffer_t *frame, zbuffer_t *z)
Definition: draw.c:11
qword_t * draw_texture_transfer(qword_t *q, void *src, int width, int height, int psm, int dest, int dest_width)
Definition: draw.c:223
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
qword_t * draw_texture_flush(qword_t *q)
Definition: draw.c:208
#define DRAW_DISABLE
Definition: draw.h:24
unsigned char draw_log2(unsigned int x)
Definition: draw.c:348
qword_t * draw_texturebuffer(qword_t *q, int context, texbuffer_t *texbuffer, clutbuffer_t *clut)
#define CLUT_NO_LOAD
Definition: draw_buffers.h:26
#define TEXTURE_FUNCTION_DECAL
Definition: draw_buffers.h:17
#define TEXTURE_COMPONENTS_RGB
Definition: draw_buffers.h:12
#define CLUT_STORAGE_MODE1
Definition: draw_buffers.h:22
#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)
#define LOD_MIN_NEAREST
Definition: draw_sampling.h:18
#define LOD_USE_K
Definition: draw_sampling.h:13
qword_t * draw_texture_sampling(qword_t *q, int context, lod_t *lod)
#define LOD_MAG_NEAREST
Definition: draw_sampling.h:16
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
#define GRAPH_ALIGN_BLOCK
Definition: graph_vram.h:16
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
#define GS_PSM_24
Definition: gs_psm.h:13
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 load_method
Definition: draw_buffers.h:68
unsigned int start
Definition: draw_buffers.h:67
unsigned int storage_mode
Definition: draw_buffers.h:66
unsigned int psm
Definition: draw_buffers.h:65
unsigned int address
Definition: draw_buffers.h:64
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 char mag_filter
Definition: draw_sampling.h:42
float k
Definition: draw_sampling.h:46
unsigned char l
Definition: draw_sampling.h:45
unsigned char calculation
Definition: draw_sampling.h:40
unsigned char min_filter
Definition: draw_sampling.h:43
unsigned char max_level
Definition: draw_sampling.h:41
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 address
Definition: draw_buffers.h:57
unsigned int width
Definition: draw_buffers.h:58
unsigned int psm
Definition: draw_buffers.h:59
texinfo_t info
Definition: draw_buffers.h:60
unsigned char height
Definition: draw_buffers.h:35
unsigned char function
Definition: draw_buffers.h:37
unsigned char components
Definition: draw_buffers.h:36
unsigned char width
Definition: draw_buffers.h:34
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 int u32
Definition: tamtypes.h:30
unsigned long u64
Definition: tamtypes.h:34
color_t * rgbaq
Definition: teapot.c:41
VECTOR * temp_vertices
Definition: teapot.c:38
xyz_t * xyz
Definition: teapot.c:40
VECTOR coordinates[24]
Definition: mesh_data.c:85
float q
Definition: draw_types.h:46
u64 rgbaq
Definition: draw_types.h:40
u64 uv
Definition: draw_types.h:28
u64 xyz
Definition: draw_types.h:19