PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
draw3d.c
1#include <gif_tags.h>
2
3#include <gs_gp.h>
4#include <gs_privileged.h>
5
6#include <draw.h>
7
8// Starting position of primitive
9static qword_t *__prim_start = NULL;
10
11qword_t *draw_prim_start(qword_t *q, int context, prim_t *prim, color_t *color)
12{
13
14 // Set the primitive register in packed mode, but don't end the packet
15 PACK_GIFTAG(q,GIF_SET_TAG(2,0,0,0,GIF_FLG_PACKED,1),GIF_REG_AD);
16 q++;
17 PACK_GIFTAG(q,GS_SET_PRIM(prim->type,prim->shading,prim->mapping,prim->fogging,
18 prim->blending,prim->antialiasing,prim->mapping_type,
19 context,prim->colorfix),GS_REG_PRIM);
20 q++;
21 PACK_GIFTAG(q,color->rgbaq,GIF_REG_RGBAQ);
22 q++;
23
24 // Save the position for the reglist giftag that draw_primitive_end will add
25 __prim_start = q;
26 q++;
27
28 // Return the current qword pointer
29 return q;
30
31}
32
33qword_t *draw_prim_end(qword_t *q, int nreg, u64 reglist)
34{
35
36 u32 vertex_qwords;
37 u32 vertex_loops;
38
39 // Determine number qwords that were needed minus the reglist giftag
40 vertex_qwords = q - __prim_start - 1;
41
42 // Determine number of loops based on qwords used and number of regs per loop
43 vertex_loops = (vertex_qwords * 2) / nreg;
44
45 // Now set the giftag of the vertex reglist chain
46 __prim_start->dw[0] = GIF_SET_TAG(vertex_loops,1,0,0,GIF_FLG_REGLIST,nreg);
47
48 // Set the higher 64bits to the reglist
49 __prim_start->dw[1] = reglist;
50
51 // Return the current qword pointer
52 return q;
53
54}
55
56int draw_convert_rgbq(color_t *output, int count, vertex_f_t *vertices, color_f_t *colours, unsigned char alpha)
57{
58
59 int i;
60 float q = 1.00f;
61
62 // For each colour...
63 for (i=0;i<count;i++)
64 {
65
66 // Calculate the Q value.
67 if (vertices[i].w != 0)
68 {
69
70 q = 1 / vertices[i].w;
71
72 }
73
74 // Calculate the RGBA values.
75 output[i].r = (int)(colours[i].r * 128.0f);
76 output[i].g = (int)(colours[i].g * 128.0f);
77 output[i].b = (int)(colours[i].b * 128.0f);
78 output[i].a = alpha;
79 output[i].q = q;
80
81 }
82
83 // End function.
84 return 0;
85
86}
87
88int draw_convert_rgbaq(color_t *output, int count, vertex_f_t *vertices, color_f_t *colours)
89{
90
91 int i;
92 float q = 1.00f;
93
94 // For each colour...
95 for (i=0;i<count;i++)
96 {
97
98 // Calculate the Q value.
99 if (vertices[i].w != 0)
100 {
101
102 q = 1 / vertices[i].w;
103
104 }
105
106 // Calculate the RGBA values.
107 output[i].r = (int)(colours[i].r * 128.0f);
108 output[i].g = (int)(colours[i].g * 128.0f);
109 output[i].b = (int)(colours[i].b * 128.0f);
110 output[i].a = (int)(colours[i].a * 128.0f);
111 output[i].q = q;
112
113 }
114
115 // End function.
116 return 0;
117
118}
119
120int draw_convert_st(texel_t *output, int count, vertex_f_t *vertices, texel_f_t *coords)
121{
122
123 int i = 0;
124 float q = 1.00f;
125
126 // For each coordinate...
127 for (i=0;i<count;i++)
128 {
129
130 // Calculate the Q value.
131 if (vertices[i].w != 0)
132 {
133 q = 1 / vertices[i].w;
134 }
135
136 // Calculate the S and T values.
137 output[i].s = coords[i].s * q;
138 output[i].t = coords[i].t * q;
139
140 }
141
142 // End function.
143 return 0;
144
145}
146
147int draw_convert_xyz(xyz_t *output, float x, float y, int z, int count, vertex_f_t *vertices)
148{
149
150 int i;
151
152 int center_x;
153 int center_y;
154
155 unsigned int max_z;
156
157 center_x = ftoi4(x);
158 center_y = ftoi4(y);
159
160 max_z = 1 << (z - 1);
161
162 // For each colour...
163 for (i=0;i<count;i++)
164 {
165
166 // Calculate the XYZ values.
167 output[i].x = (short)((vertices[i].x + 1.0f) * center_x);
168 output[i].y = (short)((vertices[i].y + 1.0f) * -center_y);
169 output[i].z = (unsigned int)((vertices[i].z + 1.0f) * max_z);
170
171 }
172
173 // End function.
174 return 0;
175
176}
int draw_convert_st(texel_t *output, int count, vertex_f_t *vertices, texel_f_t *coords)
Definition draw3d.c:120
int draw_convert_rgbq(color_t *output, int count, vertex_f_t *vertices, color_f_t *colours, unsigned char alpha)
Definition draw3d.c:56
qword_t * draw_prim_end(qword_t *q, int nreg, u64 reglist)
Definition draw3d.c:33
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_start(qword_t *q, int context, prim_t *prim, color_t *color)
Definition draw3d.c:11
int draw_convert_rgbaq(color_t *output, int count, vertex_f_t *vertices, color_f_t *colours)
Definition draw3d.c:88
#define GIF_REG_RGBAQ
Definition gif_tags.h:44
#define GIF_REG_AD
Definition gif_tags.h:72
#define GIF_FLG_REGLIST
Definition gif_tags.h:37
#define GIF_FLG_PACKED
Definition gif_tags.h:35
#define GS_REG_PRIM
Definition gs_gp.h:13
u32 count
start sector of fragmented bd/file