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 grid3d_draw (grid3d *g, int color, int colorlow,
00063 int dist, int *buf, int *back, int W,int H) {
00064 int x;
00065
00066
00067 v2d v2,v2x;
00068
00069 for (x=0;x<g->defx;x++) {
00070 int z;
00071 V3D_TO_V2D(g->surf.svertex[x],v2x,W,H,dist);
00072
00073 for (z=1;z<g->defz;z++) {
00074 V3D_TO_V2D(g->surf.svertex[z*g->defx + x],v2,W,H,dist);
00075 if (((v2.x != -666) || (v2.y!=-666))
00076 && ((v2x.x != -666) || (v2x.y!=-666))) {
00077 draw_line(buf,v2x.x,v2x.y,v2.x,v2.y, colorlow, W, H);
00078 draw_line(back,v2x.x,v2x.y,v2.x,v2.y, color, W, H);
00079 DRAWMETHOD_DONE();
00080 }
00081 v2x = v2;
00082 }
00083 }
00084 }
00085
00086 void surf3d_rotate (surf3d *s, float angle) {
00087 int i;
00088 float cosa;
00089 float sina;
00090 SINCOS(angle,sina,cosa);
00091 for (i=0;i<s->nbvertex;i++) {
00092 Y_ROTATE_V3D(s->vertex[i],s->svertex[i],cosa,sina);
00093 }
00094 }
00095
00096 void surf3d_translate (surf3d *s) {
00097 int i;
00098 for (i=0;i<s->nbvertex;i++) {
00099 TRANSLATE_V3D(s->center,s->svertex[i]);
00100 }
00101 }
00102
00103 void grid3d_update (grid3d *g, float angle, float *vals, float dist) {
00104 int i;
00105 float cosa;
00106 float sina;
00107 surf3d *s = &(g->surf);
00108 v3d cam = s->center;
00109 cam.z += dist;
00110
00111 SINCOS((angle/4.3f),sina,cosa);
00112 cam.y += sina*2.0f;
00113 SINCOS(angle,sina,cosa);
00114
00115 if (g->mode==0) {
00116 if (vals)
00117 for (i=0;i<g->defx;i++)
00118 s->vertex[i].y = s->vertex[i].y*0.2 + vals[i]*0.8;
00119
00120 for (i=g->defx;i<s->nbvertex;i++) {
00121 s->vertex[i].y *= 0.255f;
00122 s->vertex[i].y += (s->vertex[i-g->defx].y * 0.777f);
00123 }
00124 }
00125
00126 for (i=0;i<s->nbvertex;i++) {
00127 Y_ROTATE_V3D(s->vertex[i],s->svertex[i],cosa,sina);
00128 TRANSLATE_V3D(cam,s->svertex[i]);
00129 }
00130 }