00001 #include "surf3d.h"
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005
00006 void grid3d_free(grid3d **grid)
00007 {
00008 free ((*grid)->surf.vertex);
00009 free ((*grid)->surf.svertex);
00010 free (*grid);
00011 *grid = NULL;
00012 }
00013
00014 grid3d *grid3d_new (int sizex, int defx, int sizez, int defz, v3d center) {
00015 int x = defx;
00016 int y = defz;
00017 grid3d *g = malloc (sizeof(grid3d));
00018 surf3d *s = &(g->surf);
00019 s->nbvertex = x*y;
00020 s->vertex = malloc (x*y*sizeof(v3d));
00021 s->svertex = malloc (x*y*sizeof(v3d));
00022 s->center = center;
00023
00024 g->defx=defx;
00025 g->sizex=sizex;
00026 g->defz=defz;
00027 g->sizez=sizez;
00028 g->mode=0;
00029
00030 while (y) {
00031 --y;
00032 x = defx;
00033 while (x) {
00034 --x;
00035 s->vertex[x+defx*y].x = (float)(x-defx/2)*sizex/defx;
00036 s->vertex[x+defx*y].y = 0;
00037 s->vertex[x+defx*y].z = (float)(y-defz/2)*sizez/defz;
00038 }
00039 }
00040 return g;
00041 }
00042
00043
00044 #include "drawmethods.h"
00045
00046 void surf3d_draw (surf3d *s, int color, int dist, int *buf, int *back, int W,int H) {
00047 int i;
00048 int *p1;
00049 int *p2;
00050 v2d v2;
00051
00052 for (i=0;i<s->nbvertex;i++) {
00053 V3D_TO_V2D(s->svertex[i],v2,W,H,dist);
00054 p1 = buf + v2.x + (v2.y*W);
00055 p2 = back + v2.x + (v2.y*W);
00056 if ((v2.x>=0) && (v2.y>=0) && (v2.x<W) && (v2.y<H)) {
00057 *p1 = color;
00058 }
00059 }
00060
00061
00062 (void)p2;
00063 }
00064
00065 void grid3d_draw (grid3d *g, int color, int colorlow,
00066 int dist, int *buf, int *back, int W,int H) {
00067 int x;
00068
00069
00070 v2d v2,v2x;
00071
00072 for (x=0;x<g->defx;x++) {
00073 int z;
00074 V3D_TO_V2D(g->surf.svertex[x],v2x,W,H,dist);
00075
00076 for (z=1;z<g->defz;z++) {
00077 V3D_TO_V2D(g->surf.svertex[z*g->defx + x],v2,W,H,dist);
00078 if (((v2.x != -666) || (v2.y!=-666))
00079 && ((v2x.x != -666) || (v2x.y!=-666))) {
00080 draw_line(buf,v2x.x,v2x.y,v2.x,v2.y, colorlow, W, H);
00081 draw_line(back,v2x.x,v2x.y,v2.x,v2.y, color, W, H);
00082 DRAWMETHOD_DONE();
00083 }
00084 v2x = v2;
00085 }
00086 }
00087 }
00088
00089 void surf3d_rotate (surf3d *s, float angle) {
00090 int i;
00091 float cosa;
00092 float sina;
00093 SINCOS(angle,sina,cosa);
00094 for (i=0;i<s->nbvertex;i++) {
00095 Y_ROTATE_V3D(s->vertex[i],s->svertex[i],cosa,sina);
00096 }
00097 }
00098
00099 void surf3d_translate (surf3d *s) {
00100 int i;
00101 for (i=0;i<s->nbvertex;i++) {
00102 TRANSLATE_V3D(s->center,s->svertex[i]);
00103 }
00104 }
00105
00106 void grid3d_update (grid3d *g, float angle, float *vals, float dist) {
00107 int i;
00108 float cosa;
00109 float sina;
00110 surf3d *s = &(g->surf);
00111 v3d cam = s->center;
00112 cam.z += dist;
00113
00114 SINCOS((angle/4.3f),sina,cosa);
00115 cam.y += sina*2.0f;
00116 SINCOS(angle,sina,cosa);
00117
00118 if (g->mode==0) {
00119 if (vals)
00120 for (i=0;i<g->defx;i++)
00121 s->vertex[i].y = s->vertex[i].y*0.2 + vals[i]*0.8;
00122
00123 for (i=g->defx;i<s->nbvertex;i++) {
00124 s->vertex[i].y *= 0.255f;
00125 s->vertex[i].y += (s->vertex[i-g->defx].y * 0.777f);
00126 }
00127 }
00128
00129 for (i=0;i<s->nbvertex;i++) {
00130 Y_ROTATE_V3D(s->vertex[i],s->svertex[i],cosa,sina);
00131 TRANSLATE_V3D(cam,s->svertex[i]);
00132 }
00133 }