<I&O.> | // jit.gl.terrain.c// draw 3d terrains. // from the gl.gridshape in the jitter sdk// author: jkc// © 2002 cycling '74// 20030920 - vers 0x01// 20031007 - vers 0x03// this is simply a hack to build proper terrains under jitter// not quite so simple. a very hard port! i'm still using limited 64 max settings.//later, get dynamical spawning of the global vector posit, vert, vertnorms, vertnormsegs and texts// author: andré sier// © André Sier 2003 sier@alunos.fcsh.unl.pt /*from jit common, the input float array methods long jit_attr_getfloat_array(void *x, t_symbol *s, long max, float *vals);t_jit_err jit_attr_setfloat_array(void *x, t_symbol *s, long count, float *vals); */ #include "jit.common.h"#include "jit.gl.h"#include "ext_strings.h"#include #ifndef M_PI#define M_PI 3.14159265358979323846#endif#define MAXGRID 64 //lets be safetypedef struct _jit_gl_terrain { t_object ob; void *ob3d; t_jit_glchunk *chunk; long recalc; t_symbol *shape; long dim[2]; float rad_minor; // for torus char gridmode; char displaylist; GLuint dlref; float pos[MAXGRID]; // a max of 64 args for the list char drawFaceNorms;} t_jit_gl_terrain;void *_jit_gl_terrain_class;t_jit_err jit_gl_terrain_init(void) ;t_jit_gl_terrain *jit_gl_terrain_new(t_symbol * dest_name);void jit_gl_terrain_free(t_jit_gl_terrain *x);t_jit_err jit_gl_terrain_shape(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv);t_jit_err jit_gl_terrain_dim(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv);t_jit_err jit_gl_terrain_rad_minor(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv);t_jit_err jit_gl_terrain_displaylist(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv);t_jit_err jit_gl_terrain_draw(t_jit_gl_terrain *x);t_jit_err jit_gl_terrain_dest_changed(t_jit_gl_terrain *x);void jit_gl_terrain_get_color(t_jit_gl_terrain *x, float *red, float *green, float *blue, float *alpha);t_jit_err jit_gl_terrain_recalc(t_jit_gl_terrain *x);t_jit_err jit_gl_terrain_dest_closing(t_jit_gl_terrain *x);//func to acess float listt_jit_err jit_gl_terrain_pos(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv);void calc_sphere(t_jit_gl_terrain *x);void calc_cylinder(t_jit_gl_terrain *x);void calc_torus(t_jit_gl_terrain *x);void calc_opencylinder(t_jit_gl_terrain *x);void calc_circle(t_jit_gl_terrain *x);void calc_cube(t_jit_gl_terrain *x);void calc_opencube(t_jit_gl_terrain *x);void calc_plane(t_jit_gl_terrain *x);// the calc terrain funcs void calc_terrain(t_jit_gl_terrain *x);void calc_terrain2(t_jit_gl_terrain *x);// the terrain funcs void calc_getFaceNorms(t_jit_gl_terrain *x);void calc_getVertNorms(t_jit_gl_terrain *x);void calc_getFaceNormSegs(t_jit_gl_terrain *x);void calc_getTexCoords(t_jit_gl_terrain *x);void calc_drawFaceNorms(t_jit_gl_terrain *x);void calc_terrain_ops(t_jit_gl_terrain *x);//global carve stufffloat posit[MAXGRID][MAXGRID]; //positionfloat vertNorms[MAXGRID][MAXGRID][3]; //normalsfloat faceNorms[2][MAXGRID][MAXGRID][3]; //facenormalsfloat faceNormSegs[2][2][MAXGRID][MAXGRID][3]; //facenormal segmentsfloat texCoords[MAXGRID][MAXGRID][2]; //texture coordinatesvoid color_surface(t_jit_gl_terrain *x);void draw_grid(t_jit_object *matrix, GLenum mode);t_symbol *ps_circle,*ps_sphere,*ps_torus,*ps_cylinder,*ps_opencylinder,*ps_cube,*ps_opencube,*ps_plane, *ps_terrain, *ps_terrain2;//vector void copy(float vec0[3], float vec1[3]);void sub(float vec0[3], float vec1[3], float vec2[3]);void add(float vec0[3], float vec1[3], float vec2[3]);void scalDiv(float vec[3], float c);void cross(float vec0[3], float vec1[3], float vec2[3]);void norm(float vec[3]);void set(float vec[3], float x, float y, float z);// --------------------------------------------------------------------------------//t_jit_err jit_gl_terrain_init(void) { long attrflags=0; long ob3d_flags=0; t_jit_object *attr; void * ob3d; _jit_gl_terrain_class = jit_class_new("jit_gl_terrain", (method)jit_gl_terrain_new, (method)jit_gl_terrain_free, sizeof(t_jit_gl_terrain),A_CANT,0L); //A_CANT = untyped // set up object extension for 3d object, customized with flags ob3d = jit_ob3d_setup(_jit_gl_terrain_class, calcoffset(t_jit_gl_terrain, ob3d), ob3d_flags); // add attributes attrflags = JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_USURP_LOW; // shape attr = jit_object_new(_jit_sym_jit_attr_offset,"shape",_jit_sym_symbol,attrflags, (method)0L,(method)jit_gl_terrain_shape,calcoffset(t_jit_gl_terrain, shape)); jit_class_addattr(_jit_gl_terrain_class,attr); attr = jit_object_new(_jit_sym_jit_attr_offset_array,"dim",_jit_sym_long,2,attrflags, (method)0L,(method)jit_gl_terrain_dim,0/*fix*/,calcoffset(t_jit_gl_terrain,dim)); jit_attr_addfilterset_clip(attr,2,0,TRUE,FALSE); jit_class_addattr(_jit_gl_terrain_class,attr); attr = jit_object_new(_jit_sym_jit_attr_offset,"rad_minor",_jit_sym_float32,attrflags, (method)0L,(method)jit_gl_terrain_rad_minor,calcoffset(t_jit_gl_terrain, rad_minor)); jit_class_addattr(_jit_gl_terrain_class,attr); attr = jit_object_new(_jit_sym_jit_attr_offset,"displaylist",_jit_sym_char,attrflags, (method)0L,(method)jit_gl_terrain_displaylist,calcoffset(t_jit_gl_terrain, displaylist)); jit_class_addattr(_jit_gl_terrain_class,attr); attr = jit_object_new(_jit_sym_jit_attr_offset,"gridmode",_jit_sym_char,attrflags, (method)0L,(method)0L,calcoffset(t_jit_gl_terrain, gridmode)); jit_class_addattr(_jit_gl_terrain_class,attr); post("before attrib_jit_sym_float32\, MAXGRID\, attrflags\,"); attr = jit_object_new(_jit_sym_jit_attr_offset_array,"pos",_jit_sym_float32, MAXGRID, attrflags, //add 64floats to pos (method)0L,(method)jit_gl_terrain_pos,0/*fix*/,calcoffset(t_jit_gl_terrain, pos)); jit_attr_addfilterset_clip(attr,MAXGRID,0,TRUE,FALSE); jit_class_addattr(_jit_gl_terrain_class,attr); post("after attrib_jit_sym_float32\, MAXGRID\, attrflags\,"); // handle draw method. called in automatic mode by jit.gl.render, or otherwise through ob3d when banged. // this is A_CANT because draw setup needs to happen in the ob3d beforehand. jit_class_addmethod(_jit_gl_terrain_class, (method)jit_gl_terrain_draw, "ob3d_draw", A_CANT, 0L); // handle dest_changed method. // this method is called by jit.render when the destination context changes: for example, // when the user moves the window from one monitor to another. Anything your object keeps // in the OpenGL machine -- textures, display lists, vertex shaders, etc. -- will need to // be rebuilt here. jit_class_addmethod(_jit_gl_terrain_class, (method)jit_gl_terrain_dest_changed, "dest_changed", A_CANT, 0L); // must register for ob3d jit_class_addmethod(_jit_gl_terrain_class, (method)jit_object_register, "register", A_CANT, 0L); jit_class_addmethod(_jit_gl_terrain_class, (method)jit_gl_terrain_dest_closing, "dest_closing", A_CANT, 0L); jit_class_register(_jit_gl_terrain_class); ps_circle = gensym("circle"); ps_sphere = gensym("sphere"); ps_torus = gensym("torus"); ps_cylinder = gensym("cylinder"); ps_opencylinder = gensym("opencylinder"); ps_cube = gensym("cube"); ps_opencube = gensym("opencube"); ps_plane = gensym("plane"); ps_terrain = gensym("terrain"); ps_terrain2 = gensym("terrain2"); return JIT_ERR_NONE;}t_jit_gl_terrain *jit_gl_terrain_new(t_symbol * dest_name){ t_jit_gl_terrain *x; // make jit object if (x = (t_jit_gl_terrain *)jit_object_alloc(_jit_gl_terrain_class)) { // create and attach ob3d jit_ob3d_new(x, dest_name); // set instance variable defaults x->shape = ps_sphere; x->dim[0] = 20; x->dim[1] = 20; x->chunk = jit_glchunk_grid_new(_jit_sym_gl_quad_grid, 12, x->dim[0], x->dim[1]); x->rad_minor = 0.5; x->recalc = 1; x->displaylist = 0; x->dlref = 0; x->gridmode = 0; } else { x = NULL; } return x;}t_jit_err jit_gl_terrain_shape(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv){ if (argc&&argv) { x->shape = jit_atom_getsym(argv); x->recalc = 1; } return JIT_ERR_NONE;}t_jit_err jit_gl_terrain_dim(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv){ if (argc&&argv&&x->chunk&&x->chunk->m_vertex) { t_jit_matrix_info info; jit_object_method(x->chunk->m_vertex,_jit_sym_dim, argc, argv); jit_object_method(x->chunk->m_vertex,_jit_sym_getinfo,&info); x->dim[0] = info.dim[0]; x->dim[1] = info.dim[1]; x->recalc = 1; } return JIT_ERR_NONE;}t_jit_err jit_gl_terrain_rad_minor(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv){ if (argc&&argv) { x->rad_minor = jit_atom_getfloat(argv); if (x->shape==ps_torus) x->recalc = 1; } return JIT_ERR_NONE;}//my_pos funct_jit_err jit_gl_terrain_pos(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv){ long i = 0; float val = 0.0f; if (argc&&argv) { for (i=0; i < argc; i++) { jit_atom_arg_getfloat (&val, i, argc, argv); x->pos[i] = val; } x->recalc = 1; } calc_terrain_ops(x); return JIT_ERR_NONE;}t_jit_err jit_gl_terrain_displaylist(t_jit_gl_terrain *x, void *attr, long argc, t_atom *argv){ if (argc&&argv) { x->displaylist = jit_atom_getlong(argv); x->recalc = 1; } return JIT_ERR_NONE;}t_jit_err jit_gl_terrain_recalc(t_jit_gl_terrain *x){ if (x->chunk&&x->chunk->m_vertex) { if (x->shape==ps_plane) { calc_plane(x); } else if (x->shape==ps_cube) { calc_cube(x); } else if (x->shape==ps_opencube) { calc_opencube(x); } else if (x->shape==ps_circle) { calc_circle(x); } else if (x->shape==ps_opencylinder) { calc_opencylinder(x); } else if (x->shape==ps_torus) { calc_torus(x); } else if (x->shape==ps_cylinder) { calc_cylinder(x); } else if (x->shape==ps_sphere) { calc_sphere(x); } else if (x->shape==ps_terrain) { calc_terrain(x); } else /*if (x->shape==ps_terrain2) */{ //whatever symbol, if unmatched before, do terrain2 calc_terrain2(x); } } return JIT_ERR_NONE;}void jit_gl_terrain_free(t_jit_gl_terrain *x){ if (x->chunk) jit_glchunk_delete(x->chunk); if (jit_ob3d_set_context(x)==JIT_ERR_NONE) { if (x->dlref) glDeleteLists(x->dlref,1); } jit_ob3d_free(x);}t_jit_err jit_gl_terrain_dest_closing(t_jit_gl_terrain *x){ if (x->dlref) { glDeleteLists(x->dlref,1); x->dlref=NULL; x->recalc=1; } return JIT_ERR_NONE;}t_jit_err jit_gl_terrain_draw(t_jit_gl_terrain *x){ t_jit_err result = JIT_ERR_NONE; GLenum prim; prim = (x->gridmode) ? GL_TRIANGLE_STRIP : GL_QUAD_STRIP; if (x->recalc) { jit_gl_terrain_recalc(x); if (x->displaylist) { if (x->dlref) { glDeleteLists(x->dlref,1); x->dlref = 0; } if (x->dlref=glGenLists(1)) { glNewList(x->displaylist, GL_COMPILE); if (x->chunk&&x->chunk->m_vertex) draw_grid(x->chunk->m_vertex, prim); glEndList(); } } x->recalc = 0; } // draw our chunk of OpenGL geometry. if (x->chunk&&x->chunk->m_vertex) { if (!jit_attr_getlong(x,gensym("matrixoutput"))) { if (x->displaylist&&x->dlref) glCallList(x->dlref); else draw_grid(x->chunk->m_vertex, prim); } else{ color_surface(x); result = jit_ob3d_draw_chunk(x->ob3d, x->chunk); //output matrix } } return result;}t_jit_err jit_gl_terrain_dest_changed(t_jit_gl_terrain *x){ if (x->dlref) x->recalc=1; return JIT_ERR_NONE;}void jit_gl_terrain_get_color(t_jit_gl_terrain *x, float *red, float *green, float *blue, float *alpha){ long ac=0; t_atom *av=NULL; jit_object_method(x,gensym("getcolor"),&ac,&av); if ((ac==4)&&av) { *red = jit_atom_getfloat(av); *green = jit_atom_getfloat(av+1); *blue = jit_atom_getfloat(av+2); *alpha = jit_atom_getfloat(av+3); jit_freebytes(av,ac*sizeof(t_atom)); }}void calc_sphere(t_jit_gl_terrain *x){ int nummajor = x->dim[1]-1; int numminor = x->dim[0]-1; double majorstep = (M_PI/(double)nummajor); double minorstep = (2.0*M_PI/(double)numminor); double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)numminor; float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); for (i=0;i<=nummajor;i++) { double a = i*majorstep; double r = jit_math_sin(a); double z = jit_math_cos(a); double c,x,y; p = (float *)(bp + i*rowstride); for (j=0;j<=numminor;j++) { c = j*minorstep; x = r*jit_math_cos(c); y = r*jit_math_sin(c); //vertex *p++ = x; *p++ = y; *p++ = z; //texture *p++ = j*numminor_inv; *p++ = i*nummajor_inv; //normals *p++ = x; *p++ = y; *p++ = z; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void calc_opencylinder(t_jit_gl_terrain *x){ int nummajor = x->dim[1]-1; int numminor = x->dim[0]-1; double majorstep = (2./(double)nummajor); double minorstep = (2.0*M_PI/(double)numminor); double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)numminor; float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); for (i=0;i<=nummajor;i++) { double z = 1. - i*majorstep; double c,x,y; p = (float *)(bp + i*rowstride); for (j=0;j<=numminor;j++) { c = j*minorstep; x = jit_math_cos(c); y = jit_math_sin(c); //vertex *p++ = x; *p++ = y; *p++ = z; //texture *p++ = j*numminor_inv; *p++ = i*nummajor_inv; //normals *p++ = x; *p++ = y; *p++ = 0.; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void calc_cylinder(t_jit_gl_terrain *x){ int nummajor = x->dim[1]-3; int numminor = x->dim[0]-1; double majorstep = (2./(double)nummajor); double minorstep = (2.0*M_PI/(double)numminor); double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)numminor; float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; if (x->dim[1]<3) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); p = (float *)bp; //top for (j=0;j<=numminor;j++) { //vertex *p++ = 0; *p++ = 0; *p++ = 1; //texture *p++ = j*numminor_inv; *p++ = 0; //normals *p++ = 0; *p++ = 0; *p++ = 1; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } //body for (i=0;i<=nummajor;i++) { double z = 1. - i*majorstep; double c,x,y; p = (float *)(bp + (i+1)*rowstride); for (j=0;j<=numminor;j++) { c = j*minorstep; x = jit_math_cos(c); y = jit_math_sin(c); //vertex *p++ = x; *p++ = y; *p++ = z; //texture *p++ = j*numminor_inv; *p++ = i*nummajor_inv; //normals *p++ = x; *p++ = y; *p++ = 0.; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } } //bottom p = (float *)(bp + (nummajor+2)*rowstride); for (j=0;j<=numminor;j++) { //vertex *p++ = 0; *p++ = 0; *p++ = -1; //texture *p++ = j*numminor_inv; *p++ = 1; //normals *p++ = 0; *p++ = 0; *p++ = -1; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; }}void calc_cube(t_jit_gl_terrain *x){ int nummajor = x->dim[1]-5; int numminor = x->dim[0]; double majorstep = (2./(double)nummajor); double minorstep; double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)(numminor-1); float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; double tx,ty,vx,vy,nx,ny; long numface0,numface1,numface2,numface3; long tface0,tface1,tface2; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; if (x->dim[1]<3) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); numface0 = numminor/4; tface0 = numface0; numface1 = numminor/2 - tface0; tface1 = tface0+numface1; numface2 = (3*numminor)/4 - tface1; tface2 = tface1+numface2; numface3 = numminor - tface2; p = (float *)bp; //top for (j=0;jnumminor;j++) { if (jtface1) { tx=(float)(j-tface0)/(float)(numface1-1); vx=1.-(2.*tx); vy=1; tx=1-tx; ty=0; } else if (j=nummajor;i++) { double vz = 1. - i*majorstep; p = (float *)(bp + (i+2)*rowstride); for (j=0;jtface0) { tx=(float)j/(float)(numface0-1); vx=1; vy=(2.*tx)-1.; nx=1; ny=0; } else if (jtface2) { tx=(float)(j-tface1)/(float)(numface2-1); vx=-1; vy=1.-(2.*tx); nx=-1; ny=0; } else { tx=(float)(j-tface2)/(float)(numface3-1); vx=(2.*tx)-1.; vy=-1; nx=0; ny=-1; } //vertex *p++ = vx; *p++ = vy; *p++ = vz; //texture //*p++ = j*numminor_inv; *p++ = tx; *p++ = i*nummajor_inv; //normals *p++ = nx; *p++ = ny; *p++ = 0; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } } //bottom p = (float *)(bp + (nummajor+3)*rowstride); for (j=0;jtface0) { tx=(float)j/(float)(numface0-1); vx=1; vy=(2.*tx)-1.; ty=1-tx; //swap tx=1; } else if (jtface2) { tx=(float)(j-tface1)/(float)(numface2-1); vx=-1; vy=1.-(2.*tx); ty=tx; //swap tx=0; } else { tx=(float)(j-tface2)/(float)(numface3-1); vx=(2.*tx)-1.; vy=-1; ty=1; } //vertex *p++ = vx; *p++ = vy; *p++ = -1; //texture *p++ = tx; *p++ = ty; //normals *p++ = 0; *p++ = 0; *p++ = -1; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } p = (float *)(bp + (nummajor+4)*rowstride); for (j=0;jdim[1]-1; int numminor = x->dim[0]; double majorstep = (2./(double)nummajor); double minorstep; double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)(numminor-1); float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; double tx,ty,vx,vy,nx,ny; long numface0,numface1,numface2,numface3; long tface0,tface1,tface2; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; if (x->dim[1]<3) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); numface0 = numminor/4; tface0 = numface0; numface1 = numminor/2 - tface0; tface1 = tface0+numface1; numface2 = (3*numminor)/4 - tface1; tface2 = tface1+numface2; numface3 = numminor - tface2; p = (float *)bp; //body for (i=0;i<=nummajor;i++) { double vz = 1. - i*majorstep; p = (float *)(bp + i*rowstride); for (j=0;jtface0) { tx=(float)j/(float)(numface0-1); vx=1; vy=(2.*tx)-1.; nx=1; ny=0; } else if (jtface2) { tx=(float)(j-tface1)/(float)(numface2-1); vx=-1; vy=1.-(2.*tx); nx=-1; ny=0; } else { tx=(float)(j-tface2)/(float)(numface3-1); vx=(2.*tx)-1.; vy=-1; nx=0; ny=-1; } //vertex *p++ = vx; *p++ = vy; *p++ = vz; //texture *p++ = tx; *p++ = i*nummajor_inv; //normals *p++ = nx; *p++ = ny; *p++ = 0; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void calc_circle(t_jit_gl_terrain *x){ int nummajor = x->dim[1]-1; int numminor = x->dim[0]-1; double majorstep = (1./(double)nummajor); double minorstep = (2.0*M_PI/(double)numminor); double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)numminor; float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); for (i=0;i<=nummajor;i++) { double r = 1. - i*majorstep; double c,x,y; p = (float *)(bp + i*rowstride); for (j=0;j<=numminor;j++) { c = 2.0*M_PI - j*minorstep; x = r*jit_math_cos(c); y = r*jit_math_sin(c); //vertex *p++ = x; *p++ = y; *p++ = 0; //texture *p++ = j*numminor_inv; *p++ = i*nummajor_inv; //normals *p++ = 0; *p++ = 0; *p++ = 1; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void calc_plane(t_jit_gl_terrain *x){ int nummajor = x->dim[1]-1; int numminor = x->dim[0]-1; double majorstep = (2.0/(double)nummajor); double minorstep = (2.0/(double)numminor); double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)numminor; float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); for (i=0;i<=nummajor;i++) { double x,y; y = i*majorstep-1.; p = (float *)(bp + i*rowstride); for (j=0;j<=numminor;j++) { x = 1.-j*minorstep; //vertex *p++ = x; *p++ = y; *p++ = 0; //texture *p++ = j*numminor_inv; *p++ = i*nummajor_inv; //normals *p++ = 0; *p++ = 0; *p++ = 1; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void calc_torus(t_jit_gl_terrain *x){ int nummajor = x->dim[1]-1; int numminor = x->dim[0]-1; double majorstep = (2.0*M_PI/(double)nummajor); double minorstep = (2.0*M_PI/(double)numminor); double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)numminor; float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; double radminor=x->rad_minor; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); for (i=0;i<=nummajor;i++) { double a = i*majorstep; double x = jit_math_cos(a); double y = jit_math_sin(a); double c,b,r,z; p = (float *)(bp + i*rowstride); for (j=0;j<=numminor;j++) { b = j*minorstep; c = jit_math_cos(b); r = radminor*c + 1.; z = jit_math_sin(b); //vertex *p++ = x*r; *p++ = y*r; *p++ = z*radminor; //texture *p++ = j*numminor_inv; *p++ = i*nummajor_inv; //normals *p++ = x*c; *p++ = y*c; *p++ = z; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void color_surface(t_jit_gl_terrain *x){ float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); p = (float *)bp; if ((p[8]==red)&&(p[9]==green)&&(p[10]==blue)&&(p[11]==alpha)) return; for (i=0;iinfo.dim[0];j++) { p += 8; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void calc_terrain(t_jit_gl_terrain *x){ // this is the plane calc method. fix it to call the terrain calcs // and put all the info into the float pointer *p int nummajor = x->dim[1]-1; int numminor = x->dim[0]-1; double majorstep = (2.0/(double)nummajor); double minorstep = (2.0/(double)numminor); double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)numminor; float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); for (i=0;i<=nummajor;i++) { double x,y; y = i*majorstep-1.; p = (float *)(bp + i*rowstride); for (j=0;j<=numminor;j++) { x = 1.-j*minorstep; //vertex *p++ = x; *p++ = y; *p++ = posit[i][j]; // z == posit[i][j] //texture *p++ = j*numminor_inv; *p++ = i*nummajor_inv; //normals *p++ = vertNorms[i][j][0]; *p++ = vertNorms[i][j][1]; *p++ = vertNorms[i][j][2]; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void calc_terrain2(t_jit_gl_terrain *x){ int nummajor = x->dim[1]-1; int numminor = x->dim[0]-1; double majorstep = (2.0/(double)nummajor); double minorstep = (2.0/(double)numminor); double nummajor_inv = 1./(double)nummajor; double numminor_inv = 1./(double)numminor; float *p; char *bp=NULL; void *m; int i,j,rowstride; t_jit_matrix_info info; float red,green,blue,alpha; if (!x->chunk) return; if (!(m=x->chunk->m_vertex)) return; jit_object_method(m,_jit_sym_getinfo,&info); jit_object_method(m,_jit_sym_getdata,&bp); if (!bp) return; rowstride = info.dimstride[1]; jit_gl_terrain_get_color(x,&red,&green,&blue,&alpha); for (i=0;i<=nummajor;i++) { double x,y; y = i*majorstep-1.; p = (float *)(bp + i*rowstride); for (j=0;j<=numminor;j++) { x = 1.-j*minorstep; //vertex *p++ = x; *p++ = y; *p++ = 0; //texture *p++ = j*numminor_inv; *p++ = i*nummajor_inv; //normals *p++ = 0; *p++ = 0; *p++ = 1; //color rgba *p++ = red; *p++ = green; *p++ = blue; *p++ = alpha; } }}void calc_terrain_ops(t_jit_gl_terrain *x){ post("calc_terrain_ops called"); //calc_interpol_pos2posit[0][y](x); //calc_fifo_the_posit_struct(x); //calc_vert_norms(x); //calc_textures(x); //calc_...(x);}/* face normals - for flat shading */void calc_getFaceNorms(t_jit_gl_terrain *x){ int i,j; float vec0[3], vec1[3], vec2[3], norm0[3], norm1[3]; float geom0[3], geom1[3], geom2[3], geom3[3]; for (i = 0; i < (x->dim[0]-2); ++i) { for (j = 0; j < (x->dim[1]-2); ++j) { /* get vectors from geometry points */ geom0[0] = i; geom0[1] = j; geom0[2] = posit[i][j]; geom1[0] = i; geom1[1] = j+1; geom1[2] = posit[i][j+1]; geom2[0] = i+1; geom2[1] = j; geom2[2] = posit[i+1][j]; geom3[0] = i+1; geom3[1] = j+1; geom3[2] = posit[i+1][j+1]; sub( vec0, geom1, geom0 ); sub( vec1, geom1, geom2 ); sub( vec2, geom1, geom3 ); /* get triangle face normals from vectors & normalize them */ cross( norm0, vec0, vec1 ); norm( norm0 ); cross( norm1, vec1, vec2 ); norm( norm1 ); copy( faceNorms[0][i][j], norm0 ); copy( faceNorms[1][i][j], norm1 ); } }}void calc_getVertNorms(t_jit_gl_terrain *x){ int i,j; float avg[3]; // (x->dim[0]-1) = max x // (x->dim[1]-1) = max y for (i = 0; i < (x->dim[0]-1); ++i) { for (j = 0; j < (x->dim[1]-1); ++j) { /* For each vertex, average normals from all faces sharing */ /* vertex. Check each quadrant in turn */ set(avg, 0.0, 0.0, 0.0); /* Right & above */ if (j < (x->dim[1]-2) && i < (x->dim[0]-2)) { add( avg, avg, faceNorms[0][i][j] ); } /* Right & below */ if (j < (x->dim[1]-2) && i > 0) { add( avg, avg, faceNorms[0][i-1][j] ); add( avg, avg, faceNorms[1][i-1][j] ); } /* Left & above */ if (j > 0 && i < (x->dim[0]-2)) { add( avg, avg, faceNorms[0][i][j-1] ); add( avg, avg, faceNorms[1][i][j-1] ); } /* Left & below */ if (j > 0 && i > 0) { add( avg, avg, faceNorms[1][i-1][j-1] ); } /* Normalize */ norm( avg ); copy( vertNorms[i][j], avg ); } }}void calc_getFaceNormSegs(t_jit_gl_terrain *x){ int i,j; float center0[3], center1[3], normSeg0[3], normSeg1[3]; float geom0[3], geom1[3], geom2[3], geom3[3]; for (i = 0; i < (x->dim[0]-2); ++i) { for (j = 0; j < (x->dim[1]-2); ++j) { geom0[0] = i; geom0[1] = j; geom0[2] = posit[i][j]; geom1[0] = i; geom1[1] = j+1; geom1[2] = posit[i][j+1]; geom2[0] = i+1; geom2[1] = j; geom2[2] = posit[i+1][j]; geom3[0] = i+1; geom3[1] = j+1; geom3[2] = posit[i+1][j+1]; /* find center of triangle face by averaging three vertices */ add( center0, geom2, geom0 ); add( center0, center0, geom1 ); scalDiv( center0, 3.0 ); add( center1, geom2, geom1 ); add( center1, center1, geom3 ); scalDiv( center1, 3.0 ); /* translate normal to center of triangle face to get normal segment */ add( normSeg0, center0, faceNorms[0][i][j] ); add( normSeg1, center1, faceNorms[1][i][j] ); copy( faceNormSegs[0][0][i][j], center0 ); copy( faceNormSegs[1][0][i][j], center1 ); copy( faceNormSegs[0][1][i][j], normSeg0 ); copy( faceNormSegs[1][1][i][j], normSeg1 ); } }}void calc_getTexCoords(t_jit_gl_terrain *x){ int i,j; for (i = 0; i < (x->dim[0]-1); ++i) { for (j = 0; j < (x->dim[1]-1); ++j) { texCoords[i][j][0] = (float)j/(float)(x->dim[0]-2); texCoords[i][j][1] = (float)i/(float)(x->dim[1]-2); } }}void calc_drawFaceNorms(t_jit_gl_terrain *x){ int i,j; glColor3f(1.0,1.0,1.0); for (i = 0; i < (x->dim[0]-1); ++i) { for (j = 0; j < (x->dim[1]-1); ++j) { glBegin(GL_LINES); glVertex3fv(faceNormSegs[0][0][i][j]); glVertex3fv(faceNormSegs[0][1][i][j]); glEnd(); glBegin(GL_LINES); glVertex3fv(faceNormSegs[1][0][i][j]); glVertex3fv(faceNormSegs[1][1][i][j]); glEnd(); } }}//_____________________________________________________// util#define _I_X 0#define _I_Y 1#define _I_Z 2#define _I_S 3#define _I_T 4#define _I_NX 5#define _I_NY 6#define _I_NZ 7#define _I_R 8#define _I_G 9#define _I_B 10#define _I_A 11//transform a vector into music//not drawing per vertex color in this objectvoid draw_grid(t_jit_object *matrix, GLenum mode){ float *p,*p2; char *bp=NULL; int i,j,rowstride,width,height,planecount,up=0; t_jit_matrix_info info; if (!matrix) return; jit_object_method(matrix,_jit_sym_getinfo,&info); jit_object_method(matrix,_jit_sym_getdata,&bp); if (!bp) return; planecount = info.planecount; rowstride = info.dimstride[1]; height = info.dim[1]-1; width = info.dim[0]; for (i=0;iwidth;j++) { glNormal3f(p[_I_NX],p[_I_NY],p[_I_NZ]);// glColor4f(p[_I_R],p[_I_G],p[_I_B],p[_I_A]); glTexCoord2f(p[_I_S],p[_I_T]); glVertex3f(p[_I_X],p[_I_Y],p[_I_Z]); glNormal3f(p2[_I_NX],p2[_I_NY],p2[_I_NZ]);// glColor4f(p2[_I_R],p2[_I_G],p2[_I_B],p2[_I_A]); glTexCoord2f(p2[_I_S],p2[_I_T]); glVertex3f(p2[_I_X],p2[_I_Y],p2[_I_Z]); p+=planecount; p2+=planecount; } break; case 11: case 10: case 9: case 8: for (j=0;jwidth;j++) { glTexCoord2f(p[_I_S],p[_I_T]); glVertex3f(p[_I_X],p[_I_Y],p[_I_Z]); glTexCoord2f(p2[_I_S],p2[_I_T]); glVertex3f(p2[_I_X],p2[_I_Y],p2[_I_Z]); p+=planecount; p2+=planecount; } break; case 4: case 3: for (j=0;jdrawFaceNorms) // { glDisable(GL_TEXTURE_2D); calc_getFaceNormSegs(x); calc_drawFaceNorms(x); //}*/}// math -- VECTOR3 ops LIBvoid copy(float vec0[3], float vec1[3]){ vec0[0] = vec1[0]; vec0[1] = vec1[1]; vec0[2] = vec1[2];}void sub(float vec0[3], float vec1[3], float vec2[3]){ vec0[0] = vec1[0] - vec2[0]; vec0[1] = vec1[1] - vec2[1]; vec0[2] = vec1[2] - vec2[2];}void add(float vec0[3], float vec1[3], float vec2[3]){ vec0[0] = vec1[0] + vec2[0]; vec0[1] = vec1[1] + vec2[1]; vec0[2] = vec1[2] + vec2[2];}void scalDiv(float vec[3], float c){ vec[0] /= c; vec[1] /= c; vec[2] /= c;}void cross(float vec0[3], float vec1[3], float vec2[3]){ vec0[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1]; vec0[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2]; vec0[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];}void norm(float vec[3]){ float c = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); scalDiv(vec, c); }void set(float vec[3], float x, float y, float z){ vec[0] = x; vec[1] = y; vec[2] = z;}/*---------------------------------------------------------- EOF----------------------------------------------------------*/ |