-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimode.c
269 lines (237 loc) · 4.95 KB
/
imode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*****************************************************************************/
/*
* Early C implementation of an extra lite version of the PiSHi engine.
*
* by EMMIR 2018-2022
*
* YouTube: https://www.youtube.com/c/LMP88
*
* This software is released into the public domain.
*/
/*****************************************************************************/
#include "pl.h"
/* imode.c
*
* Simple immediate mode geometry interface.
*
*/
static struct PL_OBJ p; /* product */
static struct PL_OBJ wc;
static vtx[POMAXV]; /* temp storage for vertices */
static struct PL_POLY ply[POMAXV / 4]; /* temp storage for polygons */
static ptype PTRIS;
static nv 0; /* entered so far */
static np 0; /* entered so far */
static struct PL_TEX *curtex NULL;
static cverts[4];
static vnum 0;
static ctexc[2 * 4];
static tnum 0;
static cr 077;
static cg 077;
static cb 077;
static cu 0;
static cv 0;
static int
addvtx(x, y, z)
{
register i, v;
for (i = 0; i < nv; i++) {
v = i * PVLEN;
if ((vtx[v ] == x) &&
(vtx[v + 1] == y) &&
(vtx[v + 2] == z)) {
return(i);
}
}
v = nv * PVLEN;
vtx[v ] = x;
vtx[v + 1] = y;
vtx[v + 2] = z;
return(nv++);
}
static
addply()
{
extern bufset();
struct PL_POLY *t;
register i, b, edges;
t = &ply[np];
bufset(t, 0, sizeof(struct PL_POLY));
t->tex = curtex;
edges = 3;
if (ptype == PQUADS) {
edges = 4;
/* check for a quad with two identical vertices,
* if it has any, turn it into a triangle
*/
if (cverts[0] == cverts[1]) {
edges = 3;
cverts[1] = cverts[2];
ctexc[2 * 1 ] = ctexc[2 * 2];
ctexc[2 * 1 + 1] = ctexc[2 * 2 + 1];
cverts[2] = cverts[3];
ctexc[2 * 2 ] = ctexc[2 * 3];
ctexc[2 * 2 + 1] = ctexc[2 * 3 + 1];
}
if (cverts[2] == cverts[3]) {
edges = 3;
}
}
t->color = c18to24(cr << 12 | cg << 6 | cb << 0);
for (i = 0; i < edges; i++) {
b = i * PPOLY_VLEN;
t->v[b ] = cverts[i];
t->v[b + 1] = ctexc[2 * i];
t->v[b + 2] = ctexc[2 * i + 1];
}
b = i * PPOLY_VLEN;
t->v[b ] = cverts[0];
t->v[b + 1] = ctexc[0];
t->v[b + 2] = ctexc[1];
t->nv = edges;
np++;
}
imbeg()
{
extern umemfr();
register i;
if (p.c) {
umemfr(p.c);
}
p.c = NULL;
p.nc = 0;
if (p.p) {
for (i = 0; i < p.np; i++) {
p.p[i].color = 0;
p.p[i].nv = 0;
p.p[i].tex = NULL;
}
umemfr(p.p);
}
p.p = NULL;
p.np = 0;
nv = 0;
np = 0;
vnum = 0;
tnum = 0;
}
imtype(type)
{
/* reset when primitive type is changed */
if (type != ptype) {
vnum = 0;
tnum = 0;
}
ptype = type;
}
imtex(tex)
struct PL_TEX *tex;
{
curtex = tex;
}
imcolr(r, g, b)
{
cr = r;
cg = g;
cb = b;
}
imtexc(u, v)
{
cu = u;
cv = v;
}
imvtx(x, y, z)
{
cverts[vnum++] = addvtx(x, y, z);
ctexc[tnum++] = cu;
ctexc[tnum++] = cv;
switch (ptype) {
case PTRIS:
if (vnum == 3) {
addply();
vnum = 0;
tnum = 0;
}
break;
case PQUADS:
if (vnum == 4) {
addply();
vnum = 0;
tnum = 0;
}
break;
default:
vnum = 0;
tnum = 0;
break;
}
}
imend()
{
extern char *umemgt();
extern bufcpy(), uerror();
register i;
if ((nv == 0) || (np == 0)) {
return;
}
if (p.c) {
uerror(PERR_MISC, "imode", "end without beg v");
return;
}
p.c = umemgt(nv * PVLEN, sizeof(int));
if (p.c == NULL) { goto nomem; }
for (i = 0; i < (nv * PVLEN); i++) {
p.c[i] = vtx[i];
}
p.nc = nv;
if (p.p) {
uerror(PERR_MISC, "imode", "end without beg p");
return;
}
p.p = umemgt(np + 1, sizeof(struct PL_POLY));
if (p.p == NULL) { goto nomem; }
for (i = 0; i < np; i++) {
bufcpy(&p.p[i], &ply[i], sizeof(struct PL_POLY));
}
p.np = np;
return;
nomem:
uerror(PERR_NO_MEM, "imode", "no memory");
}
iminit()
{
extern ocpy();
if (nv && np) {
ocpy(&wc, &p);
}
}
imdraw()
{
extern odraw();
if (nv && np) {
odraw(&wc);
}
}
imexport(dst)
struct PL_OBJ *dst;
{
extern char *umemgt();
extern bufcpy(), uerror();
register i;
dst->c = umemgt(p.nc * PVLEN, sizeof(int));
if (dst->c == NULL) { goto nomem; }
for (i = 0; i < (p.nc * PVLEN); i++) {
dst->c[i] = p.c[i];
}
dst->nc = p.nc;
dst->p = umemgt(p.np, sizeof(struct PL_POLY));
if (dst->p == NULL) { goto nomem; }
for (i = 0; i < np; i++) {
bufcpy(&dst->p[i], &p.p[i], sizeof(struct PL_POLY));
}
dst->np = p.np;
return;
nomem:
uerror(PERR_NO_MEM, "imode", "no memory");
}