libc3: remerged
[simavr] / examples / shared / libc3 / src / c3lines.c
1 /*
2         c3lines.c
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of libc3.
7
8         libc3 is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         libc3 is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with libc3.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "c3object.h"
24 #include "c3context.h"
25 #include "c3driver_geometry.h"
26 #include "c3lines.h"
27
28
29 void
30 c3lines_prepare(
31                 c3vertex_p  vertices,           // points A,B pairs
32                 size_t          count,
33                 c3vertex_array_p v,                     // triangles
34                 c3tex_array_p tex,
35                 c3f lineWidth,
36                 c3mat4p m)
37 {
38         c3tex_array_clear(tex);
39         c3vertex_array_clear(v);
40         for (int l = 0; l < count; l += 2) {
41                 c3vec3 a = c3mat4_mulv3(m, vertices[l]);
42                 c3vec3 b = c3mat4_mulv3(m, vertices[l+1]);
43
44                 c3vec3 e = c3vec3_mulf(c3vec3_normalize(c3vec3_sub(b, a)), lineWidth);
45
46                 c3vec3 N = c3vec3f(-e.y, e.x, 0);
47                 c3vec3 S = c3vec3_minus(N);
48                 c3vec3 NE = c3vec3_add(N, e);
49                 c3vec3 NW = c3vec3_sub(N, e);
50                 c3vec3 SW = c3vec3_minus(NE);
51                 c3vec3 SE = c3vec3_minus(NW);
52 #if 0
53                 c3vertex_array_add(v, c3vec3_add(a, SW));
54                 c3vertex_array_add(v, c3vec3_add(a, NW));
55                 c3vertex_array_add(v, c3vec3_add(a, S));
56                 c3vertex_array_add(v, c3vec3_add(a, N));
57                 c3vertex_array_add(v, c3vec3_add(b, S));
58                 c3vertex_array_add(v, c3vec3_add(b, N));
59                 c3vertex_array_add(v, c3vec3_add(b, SE));
60                 c3vertex_array_add(v, c3vec3_add(b, NE));
61 #endif
62
63                 const float ts = 1;
64
65                 c3vertex_array_add(v, c3vec3_add(a, SW));
66                 c3vertex_array_add(v, c3vec3_add(a, S));
67                 c3vertex_array_add(v, c3vec3_add(a, NW));
68                 c3tex_array_add(tex, c3vec2f(ts * 0  , ts * 0  ));
69                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0  ));
70                 c3tex_array_add(tex, c3vec2f(ts * 0  , ts * 1  ));
71
72                 c3vertex_array_add(v, c3vec3_add(a, S));
73                 c3vertex_array_add(v, c3vec3_add(a, N));
74                 c3vertex_array_add(v, c3vec3_add(a, NW));
75                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0  ));
76                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1  ));
77                 c3tex_array_add(tex, c3vec2f(ts * 0  , ts * 1  ));
78
79                 c3vertex_array_add(v, c3vec3_add(a, N));
80                 c3vertex_array_add(v, c3vec3_add(b, S));
81                 c3vertex_array_add(v, c3vec3_add(b, N));
82                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1  ));
83                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0  ));
84                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1  ));
85
86                 c3vertex_array_add(v, c3vec3_add(a, N));
87                 c3vertex_array_add(v, c3vec3_add(a, S));
88                 c3vertex_array_add(v, c3vec3_add(b, S));
89                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1  ));
90                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0  ));
91                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0  ));
92
93                 c3vertex_array_add(v, c3vec3_add(b, N));
94                 c3vertex_array_add(v, c3vec3_add(b, S));
95                 c3vertex_array_add(v, c3vec3_add(b, SE));
96                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1  ));
97                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 0  ));
98                 c3tex_array_add(tex, c3vec2f(ts * 1  , ts * 0  ));
99
100                 c3vertex_array_add(v, c3vec3_add(b, N));
101                 c3vertex_array_add(v, c3vec3_add(b, SE));
102                 c3vertex_array_add(v, c3vec3_add(b, NE));
103                 c3tex_array_add(tex, c3vec2f(ts * 0.5, ts * 1  ));
104                 c3tex_array_add(tex, c3vec2f(ts * 1  , ts * 0  ));
105                 c3tex_array_add(tex, c3vec2f(ts * 1  , ts * 1  ));
106
107         }
108 }
109
110 void
111 c3lines_init(
112                 c3geometry_p g,
113                 c3vertex_p  vertices,           // points A,B pairs
114                 size_t          count,
115                 c3f             lineWidth)
116 {
117         c3mat4 i = identity3D();
118         c3lines_prepare(vertices, count, &g->vertice, &g->textures, lineWidth, &i);
119         g->type.type = C3_LINES_TYPE;
120 }
121
122 #if 0
123 static void
124 _c3lines_project(
125                 c3geometry_p g,
126                 const struct c3driver_geometry_t *d,
127                 c3mat4p m)
128
129 const  c3driver_geometry_t c3lines_driver = {
130         .project = _c3lines_project,
131 };
132
133 const  c3driver_geometry_t c3geometry_driver;
134
135 c3geometry_set_lines(
136                 c3f lineWidth)
137 {
138         static const c3driver_geometry_t * list[] = {
139                         &c3lines_driver, &c3geometry_driver, NULL,
140         };
141 }
142 #endif