Hi, an update and some questions.
I have at least seen the ripples forming in the texture-preview! This was very cool, given my experience with C.
I have a problem when I render, however.
When I ran some tests, based on the texture-preview, I found that the texvec[0] ranged from -0.978723 to 0.978723.
When I look at those values during a rendering, they do not seem to line-up so nicely. They range all over the place and jump around.
I am (with my terrible maths) trying to stretch the (texvec[0],texvec[1]) values to a number between 1 and 200 (the size of my heightfield array - 200 by 200) so that I get an ix,iy value that can index the array.
So, using texvec[0]:
texvec[0] —> ix —> index —> result[0] = source[index]/255.0
- is the basic plot, where “—>” means I do some calculation.
The problem is that the numbers for texvec[0] and [1] under a rendering are screwing me around. They seem to be different from the ones given under the texture-preview.
Where am I going wrong?
Can one of you brainiacs give me a low-down on texture co-ords and how they relate to a plugin?
I have included my code so far - be kind
Thanks.
/* Texture plugin : Waves and ripples etc.
Donn Ingle
Ref: http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
Ref: http://www.libsdl.org/projects/water/
Please forgive my coding and algorithms - I am not a programmer!
*/
#include "math.h"
#include "plugin.h"
/* ******************** GLOBAL VARIABLES ***************** */
char name[]= "Waves"; /*Didn't see this in Blender */
/* Subtype names must be less than 15 characters */
/*These seem to create 'group' buttons named "Bricks","Someshit" etc */
#define NR_TYPES 1
char stnames[NR_TYPES][16]= {"Waves"};
VarStruct varstr[]= {
{ NUM|INT, "Age", 10, 0, 200, "Run the sim to this age."}
};
/* The cast struct is for input in the main doit function
Varstr and Cast must have the same variables in the same order */
typedef struct Cast
{
int Age;
} Cast;
float result[8];
/* cfra: the current frame */
float cfra;
/* My vars and stuff */
int firstRun=1; /* To allow pre-run stuff to happen */
/*How wide and large my array is */
#define WIDTH 200
#define MAPSIZE WIDTH*WIDTH
/*The arrays and pointers to them */
int array1[MAPSIZE];
int array2[MAPSIZE];
int *swapper, *dest, *source;
void doWave(); /*The func that draws the waves */
void zero(); /*zero the arrays */
int plugin_tex_doit(int, Cast *, float *, float *, float *);
/* ******************** Fixed functions ***************** */
int plugin_tex_getversion(void)
{
return B_PLUGIN_VERSION;
}
void plugin_but_changed(int but)
{
printf("change
");
firstRun = 1; /* allow the pre-run stuff to happen again. */
zero(); /* Zero the arrays to clear them of old waves. */
}
void plugin_init(void)
{
/*plugin_init called only once on very first run */
//printf("plugin_init
");
/* printf("cfra = %f
",cfra); cfra is not available here */
//zero();
//printf("plugin_init ends
");
}
/* Zero the arrays to clear them of old waves. */
void zero(){
int i;
for (i=0;i<MAPSIZE;i++) {
array1[i] = 0;
array2[i] = 0;
}
}
/* this function should not be changed: */
void plugin_getinfo(PluginInfo *info)
{
info->name= name;
info->stypes= NR_TYPES;
info->nvars= sizeof(varstr)/sizeof(VarStruct);
info->snames= stnames[0];
info->result= result;
info->cfra= &cfra;
info->varstr= varstr;
info->init= plugin_init;
info->tex_doit= (TexDoit) plugin_tex_doit;
info->callback= plugin_but_changed;
}
/* Stolen code - total mystery, will apply mind later.... */
void doWave() {
int i;
for (i=WIDTH; i<MAPSIZE-WIDTH; i++)
{
dest[i] = (
((source[i-1]+
source[i+1]+
source[i-WIDTH]+
source[i+WIDTH]) >>1) ) -dest[i];
dest[i] -= (dest[i] >> 5);
}
/*Swap pointers to buffers! Thanks Mike! */
swapper = dest;
dest = source;
source = swapper;
}
int plugin_tex_doit(int stype, Cast *cast, float *texvec, float *dxt, float *dyt)
{
float u,v; /* To hold the tex coords */
int ix,iy,index; /* Various indexes into arrays */
int r; /* An for loop var */
/* Get the (u,v) coords */
u = texvec[0];
v = texvec[1];
/* Check for pre-run flag - do stuff just once */
if (firstRun==1) {
printf("The waves plugin is preparing the height-map.
");
/*Set the pointers to point to the arrays*/
source=array1;
dest=array2;
/* Drop a drop of water into the heightmap *?
/* (c*w)+r = i */
for (r=0;r<10;r++){
//index=((rand()%WIDTH+1)*WIDTH)+(rand()%WIDTH+1);
index = 1000 + (r * 10);
source[index] = 255; //Set a value
}
//printf("got here ok
");
/* run the simulation X number of times to get a visible effect */
zero();
printf("Running sim %d times
",cast->Age);
for (r=0; r < cast->Age; r++) doWave();
firstRun=0; /*No more runs */
printf("The waves plugin is finished preparing the height-map.
");
}
//printf("u is : %f
",u);
/* The basic plot is to take the values in the source[] array
* and to use the texvec[0] and [1] to access them
* and then convert the given value to between 0 and 1
* I sense a lot of maths coming up!
*/
/* My half-ass attempt at converting the u into an ix */
ix = (int) 100 * (1+u);
iy = (int) 100 * (1+v);
index=(ix*WIDTH)+iy; /* index into a 1D array */
//printf("source[index]=%d",source[index]);
//printf("index=%d
",index);
/* This shows wild results on render F12 */
/* But is fine under texture preview */
printf("(ix,iy):%d,%d
",ix,iy);
//if (source[index]>0) {
//printf("At %d,%d source[%d]=%d
",ix,iy,index,source[index]);
//}
/* Get the data and then try to convert it into range 0 to 1 */
/* My maths is crap. I struggle to ratio numbers together */
result[0]=(float)source[index]/255.0;
if (result[0]>0) printf("result[0]:%f
",result[0]);
if (result[0]>1) result[0]=1;
//result[0]=1;
/* RGB */
result[1]=0;
result[2]=0;
result[3]=1;
/* Alpha */
result[4]=1;
/* Some total mystery */
result[5]=0;
result[6]=0;
result[7]=0;
return 0;
}