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