862
|
1 #include "CreatePolygon.h"
|
|
2
|
|
3 SchedDefineTask(CreatePolygon);
|
|
4
|
|
5 static void
|
|
6 ApplyMatrix(float *v, float *m)
|
|
7 {
|
|
8 float t[4];
|
|
9
|
|
10 t[0] = v[0];
|
|
11 t[1] = v[1];
|
|
12 t[2] = v[2];
|
|
13 t[3] = v[3];
|
|
14
|
|
15 for (int i = 0; i < 4; i++) {
|
|
16 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12];
|
|
17 }
|
|
18 }
|
|
19
|
|
20 static void
|
|
21 ApplyNormalMatrix(float *v, float *m)
|
|
22 {
|
|
23 float t[4];
|
|
24
|
|
25 t[0] = v[0];
|
|
26 t[1] = v[1];
|
|
27 t[2] = v[2];
|
|
28
|
|
29 for (int i = 0; i < 3; i++) {
|
|
30 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8];
|
|
31 }
|
|
32 }
|
|
33
|
|
34
|
|
35 static int
|
|
36 run(SchedTask *smanager, void *rbuf, void *wbuf)
|
|
37 {
|
|
38
|
|
39 TrianglePack *sg_tri = (TrianglePack*)smanager->get_input(0);
|
|
40 texture_list *sg_texture_info = (texture_list*)smanager->get_input(1);
|
|
41 float *sg_matrix = (float*)smanager->get_input(2);
|
|
42 TrianglePack *pp_tri = (TrianglePack*)smanager->get_output(0);
|
|
43 int *tri_num = (int*)smanager->get_param(0);
|
|
44
|
|
45 float *matrix = sg_matrix;
|
|
46 float *real_matrix = sg_matrix + 16;
|
|
47
|
|
48 float xyz1[4], xyz2[4], xyz3[4];
|
|
49 float normal1[4],normal2[4],normal3[4];
|
|
50
|
|
51 for (int i = 0; i < *tri_num; i++) {
|
|
52
|
|
53 TrianglePack *pp_cur_tri = &pp_tri[i];
|
|
54 TrianglePack *sg_cur_tri = &sg_tri[i];
|
|
55
|
|
56 xyz1[0] = sg_cur_tri->ver1.x;
|
|
57 xyz1[1] = sg_cur_tri->ver1.y;
|
|
58 xyz1[2] = sg_cur_tri->ver1.z*-1.0f;
|
|
59 xyz1[3] = 1.0f;
|
|
60
|
|
61 xyz2[0] = sg_cur_tri->ver2.x;
|
|
62 xyz2[1] = sg_cur_tri->ver2.y;
|
|
63 xyz2[2] = sg_cur_tri->ver2.z*-1.0f;
|
|
64 xyz2[3] = 1.0f;
|
|
65
|
|
66 xyz3[0] = sg_cur_tri->ver3.x;
|
|
67 xyz3[1] = sg_cur_tri->ver3.y;
|
|
68 xyz3[2] = sg_cur_tri->ver3.z*-1.0f;
|
|
69 xyz3[3] = 1.0f;
|
|
70
|
|
71 // matrix = 回転行列*透視変換行列
|
|
72 ApplyMatrix(xyz1, matrix);
|
|
73 ApplyMatrix(xyz2, matrix);
|
|
74 ApplyMatrix(xyz3, matrix);
|
|
75
|
|
76 xyz1[0] /= xyz1[2];
|
|
77 xyz1[1] /= xyz1[2];
|
|
78 xyz2[0] /= xyz2[2];
|
|
79 xyz2[1] /= xyz2[2];
|
|
80 xyz3[0] /= xyz3[2];
|
|
81 xyz3[1] /= xyz3[2];
|
|
82
|
|
83 pp_cur_tri->ver1.x = xyz1[0];
|
|
84 pp_cur_tri->ver1.y = xyz1[1];
|
|
85 pp_cur_tri->ver1.z = xyz1[2];
|
|
86 pp_cur_tri->ver1.tex_x = sg_cur_tri->ver1.tex_x;
|
|
87 pp_cur_tri->ver1.tex_y = sg_cur_tri->ver1.tex_y;
|
|
88
|
|
89 pp_cur_tri->ver2.x = xyz2[0];
|
|
90 pp_cur_tri->ver2.y = xyz2[1];
|
|
91 pp_cur_tri->ver2.z = xyz2[2];
|
|
92 pp_cur_tri->ver2.tex_x = sg_cur_tri->ver2.tex_x;
|
|
93 pp_cur_tri->ver2.tex_y = sg_cur_tri->ver2.tex_y;
|
|
94
|
|
95 pp_cur_tri->ver3.x = xyz3[0];
|
|
96 pp_cur_tri->ver3.y = xyz3[1];
|
|
97 pp_cur_tri->ver3.z = xyz3[2];
|
|
98 pp_cur_tri->ver3.tex_x = sg_cur_tri->ver3.tex_x;
|
|
99 pp_cur_tri->ver3.tex_y = sg_cur_tri->ver3.tex_y;
|
|
100
|
|
101 normal1[0] = sg_cur_tri->normal1.x;
|
|
102 normal1[1] = sg_cur_tri->normal1.y;
|
|
103 normal1[2] = sg_cur_tri->normal1.z*-1.0f;
|
|
104 normal1[3] = 0.0f;
|
|
105
|
|
106 normal1[0] = sg_cur_tri->normal2.x;
|
|
107 normal1[1] = sg_cur_tri->normal2.y;
|
|
108 normal1[2] = sg_cur_tri->normal2.z*-1.0f;
|
|
109 normal1[3] = 0.0f;
|
|
110
|
|
111 normal1[0] = sg_cur_tri->normal3.x;
|
|
112 normal1[1] = sg_cur_tri->normal3.y;
|
|
113 normal1[2] = sg_cur_tri->normal3.z*-1.0f;
|
|
114 normal1[3] = 0.0f;
|
|
115
|
|
116 ApplyNormalMatrix(normal1,real_matrix);
|
|
117 ApplyNormalMatrix(normal2,real_matrix);
|
|
118 ApplyNormalMatrix(normal3,real_matrix);
|
|
119
|
|
120 normal1[0] /= normal1[2];
|
|
121 normal1[1] /= normal1[2];
|
|
122
|
|
123 normal2[0] /= normal2[2];
|
|
124 normal2[1] /= normal2[2];
|
|
125
|
|
126 normal3[0] /= normal3[2];
|
|
127 normal3[1] /= normal3[2];
|
|
128
|
|
129 pp_cur_tri->normal1.x = normal1[0];
|
|
130 pp_cur_tri->normal1.y = normal1[1];
|
|
131 pp_cur_tri->normal1.z = normal1[2];
|
|
132
|
|
133 pp_cur_tri->normal2.x = normal2[0];
|
|
134 pp_cur_tri->normal2.y = normal2[1];
|
|
135 pp_cur_tri->normal2.z = normal2[2];
|
|
136
|
|
137 pp_cur_tri->normal3.x = normal3[0];
|
|
138 pp_cur_tri->normal3.y = normal3[1];
|
|
139 pp_cur_tri->normal3.z = normal3[2];
|
|
140
|
|
141 pp_cur_tri->tex_info.addr = sg_texture_info->pixels;
|
|
142 pp_cur_tri->tex_info.width = sg_texture_info->t_w;
|
|
143 pp_cur_tri->tex_info.height = sg_texture_info->t_h;
|
|
144 pp_cur_tri->tex_info.scale_max = sg_texture_info->scale_max;
|
|
145
|
|
146 }
|
|
147
|
|
148 return 0;
|
|
149
|
|
150 }
|