MythTV  master
surf3d.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 
5 #include "surf3d.h"
6 
7 void grid3d_free(grid3d **grid)
8 {
9  free ((*grid)->surf.vertex);
10  free ((*grid)->surf.svertex);
11  free (*grid);
12  *grid = nullptr;
13 }
14 
15 grid3d *grid3d_new (int sizex, int defx, int sizez, int defz, v3d center) {
16  int x = defx;
17  int y = defz;
18  auto *g = (grid3d*)malloc (sizeof(grid3d));
19  surf3d *s = &(g->surf);
20  s->nbvertex = x*y;
21  s->vertex = (v3d*)malloc (x*y*sizeof(v3d));
22  s->svertex = (v3d*)malloc(x*y*sizeof(v3d));
23  s->center = center;
24 
25  g->defx=defx;
26  g->sizex=sizex;
27  g->defz=defz;
28  g->sizez=sizez;
29  g->mode=0;
30 
31  while (y) {
32  --y;
33  x = defx;
34  while (x) {
35  --x;
36  s->vertex[x+defx*y].x = (x-defx/2.0F)*sizex/defx;
37  s->vertex[x+defx*y].y = 0;
38  s->vertex[x+defx*y].z = (y-defz/2.0F)*sizez/defz;
39  }
40  }
41  return g;
42 }
43 
44 //#undef HAVE_MMX
45 #include "drawmethods.h"
46 
47 void surf3d_draw (surf3d *s, int color, int dist, int *buf, int *back, int W,int H) {
48  v2d v2 {};
49 
50  for (int i=0;i<s->nbvertex;i++) {
51  V3D_TO_V2D(s->svertex[i],v2,W,H,dist);
52  int *p1 = buf + v2.x + (v2.y*W);
53  int *p2 = back + v2.x + (v2.y*W);
54  if ((v2.x>=0) && (v2.y>=0) && (v2.x<W) && (v2.y<H)) {
55  *p1 = color;
56  }
57 
58  /* Squelch a gcc warning */
59  (void)p2;
60  }
61 }
62 
63 void grid3d_draw (grid3d *g, int color, int colorlow,
64  int dist, int *buf, int *back, int W,int H) {
65  v2d v2 {};
66  v2d v2x {};
67 
68  for (int x=0;x<g->defx;x++) {
69  V3D_TO_V2D(g->surf.svertex[x],v2x,W,H,dist);
70 
71  for (int z=1;z<g->defz;z++) {
72  V3D_TO_V2D(g->surf.svertex[z*g->defx + x],v2,W,H,dist);
73  if (((v2.x != -666) || (v2.y!=-666))
74  && ((v2x.x != -666) || (v2x.y!=-666))) {
75  draw_line(buf,v2x.x,v2x.y,v2.x,v2.y, colorlow, W, H);
76  draw_line(back,v2x.x,v2x.y,v2.x,v2.y, color, W, H);
78  }
79  v2x = v2;
80  }
81  }
82 }
83 
84 void surf3d_rotate (surf3d *s, float angle) {
85  float cosa = NAN;
86  float sina = NAN;
87  SINCOS(angle,sina,cosa);
88  for (int i=0;i<s->nbvertex;i++) {
89  Y_ROTATE_V3D(s->vertex[i],s->svertex[i],cosa,sina);
90  }
91 }
92 
94  for (int i=0;i<s->nbvertex;i++) {
95  TRANSLATE_V3D(s->center,s->svertex[i]);
96  }
97 }
98 
99 void grid3d_update (grid3d *g, float angle, const float *vals, float dist) {
100  float cosa = NAN;
101  float sina = NAN;
102  surf3d *s = &(g->surf);
103  v3d cam = s->center;
104  cam.z += dist;
105 
106  SINCOS((angle/4.3F),sina,cosa);
107  cam.y += sina*2.0F;
108  SINCOS(angle,sina,cosa);
109 
110  if (g->mode==0) {
111  if (vals)
112  for (int i=0;i<g->defx;i++)
113  s->vertex[i].y = s->vertex[i].y*0.2F + vals[i]*0.8F;
114 
115  for (int i=g->defx;i<s->nbvertex;i++) {
116  s->vertex[i].y *= 0.255F;
117  s->vertex[i].y += (s->vertex[i-g->defx].y * 0.777F);
118  }
119  }
120 
121  for (int i=0;i<s->nbvertex;i++) {
122  Y_ROTATE_V3D(s->vertex[i],s->svertex[i],cosa,sina);
123  TRANSLATE_V3D(cam,s->svertex[i]);
124  }
125 }
grid3d_free
void grid3d_free(grid3d **grid)
Definition: surf3d.cpp:7
v3d::z
float z
Definition: v3d.h:11
v3d
Definition: v3d.h:10
grid3d::mode
int mode
Definition: surf3d.h:21
draw_line
static void draw_line(int *data, int x1, int y1, int x2, int y2, int col, int screenx, int screeny)
Definition: drawmethods.h:46
back
static guint32 * back
Definition: goom_core.cpp:29
grid3d::defz
int defz
Definition: surf3d.h:19
v3d::x
float x
Definition: v3d.h:11
grid3d_new
grid3d * grid3d_new(int sizex, int defx, int sizez, int defz, v3d center)
Definition: surf3d.cpp:15
grid3d_update
void grid3d_update(grid3d *g, float angle, const float *vals, float dist)
Definition: surf3d.cpp:99
V3D_TO_V2D
#define V3D_TO_V2D(v3, v2, width, height, distance)
Definition: v3d.h:24
vals
static float * vals
Definition: tentacle3d.cpp:16
surf3d::center
v3d center
Definition: surf3d.h:11
DRAWMETHOD_DONE
#define DRAWMETHOD_DONE()
Definition: drawmethods.h:38
grid3d_draw
void grid3d_draw(grid3d *g, int color, int colorlow, int dist, int *buf, int *back, int W, int H)
Definition: surf3d.cpp:63
surf3d_translate
void surf3d_translate(surf3d *s)
Definition: surf3d.cpp:93
grid3d::defx
int defx
Definition: surf3d.h:17
p2
static guint32 * p2
Definition: goom_core.cpp:30
drawmethods.h
surf3d_draw
void surf3d_draw(surf3d *s, int color, int dist, int *buf, int *back, int W, int H)
Definition: surf3d.cpp:47
v2d
Definition: v3d.h:14
p1
static guint32 * p1
Definition: goom_core.cpp:30
TRANSLATE_V3D
#define TRANSLATE_V3D(vsrc, vdest)
Definition: v3d.h:51
surf3d.h
surf3d_rotate
void surf3d_rotate(surf3d *s, float angle)
Definition: surf3d.cpp:84
surf3d
Definition: surf3d.h:6
Y_ROTATE_V3D
#define Y_ROTATE_V3D(vi, vf, sina, cosa)
Definition: v3d.h:41
v3d::y
float y
Definition: v3d.h:11
grid3d::surf
surf3d surf
Definition: surf3d.h:15
surf3d::nbvertex
int nbvertex
Definition: surf3d.h:9
surf3d::svertex
v3d * svertex
Definition: surf3d.h:8
surf3d::vertex
v3d * vertex
Definition: surf3d.h:7
for
for(int x=0;x< dirList.count() - 1;x++)
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1172
SINCOS
#define SINCOS(f, s, c)
Definition: mathtools.h:28
grid3d
Definition: surf3d.h:14