1 /* Goom Project
2 * Copyright (C) <2003> iOS-Software
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 #include "surf3d.h"
20 #include "goom_plugin_info.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 grid3d *
grid3d_new(int sizex,int defx,int sizez,int defz,v3d center)26 grid3d_new (int sizex, int defx, int sizez, int defz, v3d center)
27 {
28 int x = defx;
29 int y = defz;
30 grid3d *g = malloc (sizeof (grid3d));
31 surf3d *s = &(g->surf);
32
33 s->nbvertex = x * y;
34 s->vertex = malloc (x * y * sizeof (v3d));
35 s->svertex = malloc (x * y * sizeof (v3d));
36 s->center = center;
37
38 g->defx = defx;
39 g->sizex = sizex;
40 g->defz = defz;
41 g->sizez = sizez;
42 g->mode = 0;
43
44 while (y) {
45 --y;
46 x = defx;
47 while (x) {
48 --x;
49 s->vertex[x + defx * y].x = (float) (x - defx / 2) * sizex / defx;
50 s->vertex[x + defx * y].y = 0;
51 s->vertex[x + defx * y].z = (float) (y - defz / 2) * sizez / defz;
52 }
53 }
54 return g;
55 }
56
57 void
grid3d_free(grid3d * g)58 grid3d_free (grid3d * g)
59 {
60 surf3d *s = &(g->surf);
61
62 free (s->vertex);
63 free (s->svertex);
64
65 free (g);
66 }
67
68 void
grid3d_draw(PluginInfo * plug,grid3d * g,int color,int colorlow,int dist,Pixel * buf,Pixel * back,int W,int H)69 grid3d_draw (PluginInfo * plug, grid3d * g, int color, int colorlow,
70 int dist, Pixel * buf, Pixel * back, int W, int H)
71 {
72
73 int x;
74 v2d v2, v2x;
75
76 v2d *v2_array = malloc (g->surf.nbvertex * sizeof (v2d));
77
78 v3d_to_v2d (g->surf.svertex, g->surf.nbvertex, W, H, dist, v2_array);
79
80 for (x = 0; x < g->defx; x++) {
81 int z;
82
83 v2x = v2_array[x];
84
85 for (z = 1; z < g->defz; z++) {
86 v2 = v2_array[z * g->defx + x];
87 if (((v2.x != -666) || (v2.y != -666))
88 && ((v2x.x != -666) || (v2x.y != -666))) {
89 plug->methods.draw_line (buf, v2x.x, v2x.y, v2.x, v2.y, colorlow, W, H);
90 plug->methods.draw_line (back, v2x.x, v2x.y, v2.x, v2.y, color, W, H);
91 }
92 v2x = v2;
93 }
94 }
95
96 free (v2_array);
97 }
98
99 void
surf3d_rotate(surf3d * s,float angle)100 surf3d_rotate (surf3d * s, float angle)
101 {
102 int i;
103 float cosa;
104 float sina;
105
106 SINCOS (angle, sina, cosa);
107 for (i = 0; i < s->nbvertex; i++) {
108 Y_ROTATE_V3D (s->vertex[i], s->svertex[i], cosa, sina);
109 }
110 }
111
112 void
surf3d_translate(surf3d * s)113 surf3d_translate (surf3d * s)
114 {
115 int i;
116
117 for (i = 0; i < s->nbvertex; i++) {
118 TRANSLATE_V3D (s->center, s->svertex[i]);
119 }
120 }
121
122 void
grid3d_update(grid3d * g,float angle,float * vals,float dist)123 grid3d_update (grid3d * g, float angle, float *vals, float dist)
124 {
125 int i;
126 float cosa;
127 float sina;
128 surf3d *s = &(g->surf);
129 v3d cam = s->center;
130
131 cam.z += dist;
132
133 SINCOS ((angle / 4.3f), sina, cosa);
134 cam.y += sina * 2.0f;
135 SINCOS (angle, sina, cosa);
136
137 if (g->mode == 0) {
138 if (vals)
139 for (i = 0; i < g->defx; i++)
140 s->vertex[i].y = s->vertex[i].y * 0.2 + vals[i] * 0.8;
141
142 for (i = g->defx; i < s->nbvertex; i++) {
143 s->vertex[i].y *= 0.255f;
144 s->vertex[i].y += (s->vertex[i - g->defx].y * 0.777f);
145 }
146 }
147
148 for (i = 0; i < s->nbvertex; i++) {
149 Y_ROTATE_V3D (s->vertex[i], s->svertex[i], cosa, sina);
150 TRANSLATE_V3D (cam, s->svertex[i]);
151 }
152 }
153