-
Notifications
You must be signed in to change notification settings - Fork 1
/
xclip.c
139 lines (130 loc) · 4.81 KB
/
xclip.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
/**
* This file belongs to the 'xlab' game engine.
* Copyright 2009 xfacter
* Copyright 2016 wickles
* This work is licensed under the LGPLv3
* subject to all terms as reproduced in the included LICENSE file.
*/
#include <string.h>
#include <pspgu.h>
#include <pspgum.h>
#include "xconfig.h"
#include "xmath.h"
#include "xclip.h"
static void normalize_plane(ScePspFVector4* plane)
{
float inv_mag = x_sqrtf(SQR(plane->x) + SQR(plane->y) + SQR(plane->z));
if (inv_mag == 0.0f)
{
plane->w = 0.0f;
return;
}
inv_mag = 1.0f/inv_mag;
plane->x *= inv_mag;
plane->y *= inv_mag;
plane->z *= inv_mag;
plane->w *= inv_mag;
}
void xClipGetFrustum(xFrustum* out)
{
ScePspFMatrix4 projection, view, model, clip;
sceGumMatrixMode(GU_PROJECTION);
sceGumStoreMatrix(&projection);
sceGumMatrixMode(GU_VIEW);
sceGumStoreMatrix(&view);
sceGumMatrixMode(GU_MODEL);
sceGumStoreMatrix(&model);
gumMultMatrix(&clip, &projection, &view);
gumMultMatrix(&clip, &clip, &model);
float* t = (float*)&clip;
int i;
for (i = 0; i < 3; i++)
{
out->planes[i*2+0].x = t[0+3] + t[0+i];
out->planes[i*2+0].y = t[4+3] + t[4+i];
out->planes[i*2+0].z = t[8+3] + t[8+i];
out->planes[i*2+0].w = t[12+3] + t[12+i];
normalize_plane(&out->planes[i*2+0]);
out->planes[i*2+1].x = t[0+3] - t[0+i];
out->planes[i*2+1].y = t[4+3] - t[4+i];
out->planes[i*2+1].z = t[8+3] - t[8+i];
out->planes[i*2+1].w = t[12+3] - t[12+i];
normalize_plane(&out->planes[i*2+1]);
}
}
int xClipPointInFrustum(xFrustum* f, ScePspFVector3* p)
{
int i;
for (i = 0; i < 6; i++)
{
if (p->x*f->planes[i].x + p->y*f->planes[i].y + p->z*f->planes[i].z + f->planes[i].w < 0.0f)
{
return 0;
}
}
return 1;
}
/*
int xClipDrawArrayFrustum(ScePspFVector4* planes, int prim, int vtype, int count, void* indices, void* vertices)
{
if ((texture_bits(vtype) && texture_bits(vtype) != GU_TEXTURE_32BITF) || (vertex_bits(vtype) && vertex_bits(vtype) != GU_VERTEX_32BITF) ||
(prim != GU_TRIANGLES && prim != GU_TRIANGLE_STRIP && prim != GU_TRIANGLE_FAN))
{
X_LOG("Could not clip vertices. Unsupported texture or vertex type.", 0);
sceGumDrawArray(prim, vtype, count, indices, vertices);
return 0;
}
int vert_size = total_size(vtype);
void* clipped_verts = sceGuGetMemory(count*vert_size);
int clipped = 0;
int prev;
float t0, t1, tt;
float* ptr_clip;
float* ptr_this;
float* ptr_prev;
int i, j;
for (j = 0; j < 6; j++)
{
for (i = 0; i < count; i++)
{
if (prim == GU_TRIANGLES)
{
if (i % 3 == 0) prev = i + 2;
else prev = i - 1;
}
else if (prim == GU_TRIANGLE_STRIP)
{
if (i == 0) prev = i + 1;
else prev = i - 1;
}
else if (prim == GU_TRIANGLE_FAN)
{
if (i == 0) prev = count - 1;
else prev = i - 1;
}
ptr_this = (float*)((u32)vertices + vertex_index_offset(vtype, i, indices) + vertex_offset(vtype));
ptr_prev = (float*)((u32)vertices + vertex_index_offset(vtype, prev, indices) + vertex_offset(vtype));
t0 = planes[j].x*ptr_prev[0] + planes[j].y*ptr_prev[1] + planes[j].z*ptr_prev[2] + planes[j].w;
t1 = planes[j].x*ptr_this[0] + planes[j].y*ptr_this[1] + planes[j].z*ptr_this[2] + planes[j].w;
if (((t0 >= 0) ^ (t1 >= 0)) && t0 != t1)
{
tt = t0/(t0-t1);
ptr_this = (float*)((u32)clipped_verts + i*vert_size + texture_offset(vtype));
ptr_this = (float*)((u32)vertices + vertex_index_offset(vtype, i, indices) + texture_offset(vtype));
ptr_prev = (float*)((u32)vertices + vertex_index_offset(vtype, prev, indices) + texture_offset(vtype));
ptr_clip[0] = ptr_prev[0] + tt*(ptr_this[0] - ptr_prev[0]);
ptr_clip[1] = ptr_prev[1] + tt*(ptr_this[1] - ptr_prev[1]);
ptr_this = (float*)((u32)clipped_verts + i*vert_size + vertex_offset(vtype));
ptr_this = (float*)((u32)vertices + vertex_index_offset(vtype, i, indices) + vertex_offset(vtype));
ptr_prev = (float*)((u32)vertices + vertex_index_offset(vtype, prev, indices) + vertex_offset(vtype));
ptr_clip[0] = ptr_prev[0] + tt*(ptr_this[0] - ptr_prev[0]);
ptr_clip[1] = ptr_prev[1] + tt*(ptr_this[1] - ptr_prev[1]);
ptr_clip[2] = ptr_prev[2] + tt*(ptr_this[2] - ptr_prev[2]);
clipped += 1;
}
}
}
sceGumDrawArray(prim, vtype & ~GU_INDEX_BITS, count, 0, clipped_verts);
return clipped;
}
*/