PDA

View Full Version : mi_eval_color in struct problem



haggi
December 30th, 2008, 13:50
I try to evaluate the color from a struct array and I'm failing.

My struct looks like this:


declare shader
color "megaTex" (
integer "randomSeed" default 1234, #:shortname randomseed
#array color "cols", #:shortname cols
array struct "Textures" {
integer "count" default 10, #:shortname count
color "color" default .0 .5 1.0 1.0, #:shortname color
scalar "minScale" default 0.5, #:shortname minscale
scalar "maxScale" default 2.0, #:shortname maxscale
scalar "maxRot" default 180.0 #:shortname maxrot
}
)
version 1
apply texture
end declare

Now I try to evaluate the color in the "Textures" struct as follows:


int i_obj = *mi_eval_integer(&paras->i_obj);
int n_obj = *mi_eval_integer(&paras->n_obj);
texStruct *ts = (texStruct *)mi_eval(state, &paras->texStructures) + i_obj;

for( int i = 0; i < n_obj; i++)
{
texStruct tStruct = ts[i];
mi_info("struct %d tcount %d scalemin %f scalemax %f", i, tStruct.count, tStruct.minScale, tStruct.maxScale ); //<- this is printed correct so I suppose I access the structure correctly
miColor micol = *mi_eval_color( &tStruct.color );
*result = micol;
mi_info("Eval col %f %f %f", micol.r, micol.g, micol.b);
}

return miTRUE;


This works if I use only a color. But as soon as I try to connect a texture to the "color" entry, I get a black color only.
If I use a simple color array instead of a structure array, the texture is evaluated without any problems.

I suppose there is something wrong in my code.

Would be great if anyone has an idea how I can solve the problem.

Update:

After a suggestion of bart, I tried the following:


for( int i = 0; i < n_obj; i++)
{
texStruct tStruct = paras->texStructures[ i + i_obj ];
miColor micol = *mi_eval_color( &tStruct.color );
}


This leads to an error:

DB 0.2 fatal 041052: accessing unknown tag 0x1

An access like this:


for( int i = 0; i < n_obj; i++)
{
texStruct tStruct = paras->texStructures[ i + i_obj ];
miColor micol = *mi_eval_color( &paras->texStructures[ i + i_obj ].color );
}


does not produce an error but the result is always black (0,0,0).

This only happens for the evaluation of the color. I can access the other values without problems, well... I dont evaluate them with a mi_eval.. command, this may be the reason why it works.

edit: I tried to evaluate the other struct values and they work without problems in any case.

bart
December 30th, 2008, 17:12
Can we see your two structs in the C code?
One for the struct in the array, and one for the struct passed as params containing it?

Can you print the pointers you get for tStruct and tStruct.color?

haggi
December 30th, 2008, 17:42
mi file:


declare shader
color "megaTex" (
integer "randomSeed" default 1234, #: shortname randomseed
color "baseColor", #: shortname basecolor
array color "colors", #: shortname colors
array struct "Textures" {
integer "count" default 10, #: shortname count
color "color" default .0 .5 1.0 1.0, #: shortname color
scalar "minScale" default 0.2, #: shortname minscale
scalar "maxScale" default 0.3, #: shortname maxscale
scalar "maxRot" default 120.0, #: shortname maxrot
integer "blendType" default 0, #: shortname blendType
scalar "blendMin" default .8, #: shortname blendMin
scalar "blendMax" default 1.0 #: shortname blendMax

} #: shortname textureList
)
version 1
apply texture
end declare



c structure:




struct texStruct{
int count;
miColor color;
miScalar minScale;
miScalar maxScale;
miScalar maxRot;
miInteger blendType;
miScalar blendMin;
miScalar blendMax;
};

struct megaTex_paras{
int randomSeed;
miColor baseColor;

int i_col;
int n_col;
miColor colors[1];

int i_obj;
int n_obj;
texStruct texStructures[1];
};



Addresses:
tStruct : 334473852
tStruct.color: 334473856

bart
December 30th, 2008, 17:50
For testing and debugging purposes, could you temporarily remove the color array?

Not sure this matters, but just to be certain in your texStruct try
miInteger count

Again, then take a look at what the pointers for the struct end the color end up being before the eval.

bart
December 30th, 2008, 17:55
oops, something just rang a bell in my recollection.

Currently, defaults are not supported for input parameters inside of structs.
What happens if you remove the defaults declared inside the struct?

haggi
December 30th, 2008, 18:09
No difference at all.
I still get a

fatal 041061: invalid tag value 0xad1f4b8 requested

with the access via


texStruct tStruct = paras->texStructures[ i + i_obj ];
miColor micol = *mi_eval_color( &tStruct.color );

bart
December 30th, 2008, 18:19
So what is the tag at tStruct.color? Can you print that before the mi_eval_color?
And then use the query codes to identify what it is.

Did you remove the color array, in case that was making a problem with the offset, or is it still ok if a static (non-shader) color is connected to the input?

haggi
December 30th, 2008, 22:59
Thanks to Halfdan Ingvarsson it works now.

Instead of trying to copy the struct with this:



texStruct tStruct = paras->texStructures[ i + i_obj ];
miColor micol = *mi_eval_color( &tStruct.color );


he suggested to use a pointer.



texStruct *tStruct = &paras->texStructures[ i + i_obj ];
miColor micol = *mi_eval_color( &tStruct->color );


What works because mr needs the complete structure instead of a copy to evaluate the correct offset data. Great, thanks to all!