1. Passing floats/doubles to C

To Robert Craig or anyone that can help me, please...
Mark Brown take note, this effects morfit functions.

I'm been coding Direct3D into Exotica lately and been working
with Mark Brown with morfit that needs floats and doubles as
variables inside of a C structure.
With my Direct3D/Exotica C code, I noticed I couldn't print out
the float/double values given within Euphoria's poked "C-Like"
structure. I found in the euphoria mailing archives the
post 'Passing Floats by Value' by David Guy which who basically
had the same problem as I. Robert Craig replied to that post to do this:

>- convert each argument, a, as:
>         a = bytes_to_int(atom_to_float32(a))

because he said "Euphoria only supports passing floating-point atoms
to C routines in the form of C doubles (8 bytes)."
Okay, that kludged method worked for a parameter.

My current problem is that I need to pass floats and/or doubles to a C
structure. When I poke in bytes_to_int(atom_to_float32(a)) for a float
or poke in atom_to_float64(a)), where a is a float or double, I still
only
get a 0.0 value.

In my C code, I have this as a structure:

typedef struct tagMATERIAL
{
        double diffuse_r;
        double diffuse_g;
        double diffuse_b;
        double diffuse_a;
        double ambient_r;
        double ambient_g;
        double ambient_b;
        double ambient_a;
        double specular_r;
        double specular_g;
        double specular_b;
        double specular_a;
        double emissive_r;
        double emissive_g;
        double emissive_b;
        double emissive_a;
        double power;
} MATERIAL;

and this c code to print the values within the C struct after
Euphoria updates it through the dll function:

DllExport int SET_MATERIALS(MATERIAL material)
{
    char txt[255];

        sprintf(txt,"%f",material.diffuse_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.diffuse_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.diffuse_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.diffuse_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.ambient_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.ambient_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.ambient_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.ambient_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.specular_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.specular_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.specular_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.specular_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.emissive_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.emissive_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.emissive_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.emissive_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.power);
        FILE_REPORT(txt);

        return 0;
}

The above C code all displays 0.0000 for me, not correct.
This is my Euhporia code that updates the C structure:

atom material
material=allocate(146)
.
.
.
poke4(material,    atom_to_float64(1.0))
poke4(material+8,  atom_to_float64(1.0))
poke4(material+16, atom_to_float64(1.0))
poke4(material+24, atom_to_float64(1.0))
poke4(material+32, atom_to_float64(1.0))
poke4(material+40, atom_to_float64(1.0))
poke4(material+48, atom_to_float64(1.0))
poke4(material+56, atom_to_float64(1.0))
poke4(material+64, atom_to_float64(1.0))
poke4(material+72, atom_to_float64(1.0))
poke4(material+80, atom_to_float64(1.0))
poke4(material+88, atom_to_float64(1.0))
poke4(material+96, atom_to_float64(0.0))
poke4(material+114, atom_to_float64(0.0))
poke4(material+122, atom_to_float64(0.0))
poke4(material+130, atom_to_float64(0.0))
poke4(material+138, atom_to_float64(0.0))

funcval=c_func(SET_MATERIALS,{material})
--------

Am I doing something wrong here???
I can take out the atom_to_float64() routine, but still
get 0.0 for each member value.

I had my C MATERIAL structure with all floats. They need to be
floats. But I can leave them doubles and let C do a type
conversion on them. I have to pass these values on to
the Direct3D's structure which each of it's structure members
are floats. Floats are important! and need to be able to pass
floats, not doubles.

My only alternative is to make my C function with 17 C_DOUBLE
parameters and all my other functions too that needs FLOATS inside
a structure. OR mabie passing them as an parameter array[17]. Arg!

Many, many, many thanks in advance if someone can give me a
workable solution here....

-- Todd Riggins

new topic     » topic index » view message » categorize

2. Re: Passing floats/doubles to C

Todd:

   Take a look at my MIXEDLIB.E in the recent contributors
   It might make your job easier for handling structures
   and converting things from "C".
   Below are examples:
   This code will work without worrying about pokes and offsets.

Bernie



include mixedlib.e

constant -- structure is created and initialized as all zeros
MATERIAL = struc(" diffuse_r  : double : 1 "&
   " diffuse_g  : double : 1 "&
   " diffuse_b  : double : 1 "&
   " diffuse_a  : double : 1 "&
   " ambient_r  : double : 1 "&
   " ambient_g  : double : 1 "&
   " ambient_b  : double : 1 "&
   " ambient_a  : double : 1 "&
   " specular_r : double : 1 "&
   " specular_g : double : 1 "&
   " specular_b : double : 1 "&
   " specular_a : double : 1 "&
   " emissive_r : double : 1 "&
   " emissive_g : double : 1 "&
   " emissive_b : double : 1 "&
   " emissive_a : double : 1 "&
   " power      : double : 1 ",HIGH)

-- declare a "C" type variable
double adubval

-- To set a structure value
Sets(MATERIAL, "ambient_r", 1.783)

-- To read a structure value
adubval = Gets(MATERIAL,  "ambient_r")

-- print it
printf(1,"%f", adubval)

-- or print like this
printf(1,"%f", Gets(MATERIAL,  "ambient_r"))

new topic     » goto parent     » topic index » view message » categorize

3. Re: Passing floats/doubles to C

Hi Bernie,

Bernie Ryan wrote:
>
> Todd:
>
>    Take a look at my MIXEDLIB.E in the recent contributors
>    It might make your job easier for handling structures
>    and converting things from "C".
>    Below are examples:
>    This code will work without worrying about pokes and offsets.
>
> Bernie

That looks interesting. But I need to pass the Euphoria made structure
to a "C"-coded structure in my dll. I need those values to be put into
a DIRECT3D structure that contains floats. So basically, I need
to convert( pass ) things to "C", not from.
Thanks for trying to help though...

-- Todd Riggins

new topic     » goto parent     » topic index » view message » categorize

4. Re: Passing floats/doubles to C

Arg. Now I have the C function to accept the data as a
double array of 17, like this:

 DllExport int SET_MATERIALS(double material[17])
 {
     char txt[255];

         sprintf(txt,"%f",material[0]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[1]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[2]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[3]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[4]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[5]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[6]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[7]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[8]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[9]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[10]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[11]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[12]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[13]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[14]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[15]);
         FILE_REPORT(txt);
         sprintf(txt,"%f",material[16]);
         FILE_REPORT(txt);

         return 0;
 }

Now it prints out:
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
54270830327981012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
0.000000
0.000000
0.000000

Go figure. This is really frustrating. I give up.
-- Todd Riggins

new topic     » goto parent     » topic index » view message » categorize

5. Re: Passing floats/doubles to C

On Sun, 13 Feb 2000 11:07:16 -0600, Todd Riggins <triggins at AIRMAIL.NET>
wrote:
>That looks interesting. But I need to pass the Euphoria made structure
>to a "C"-coded structure in my dll. I need those values to be put into
>a DIRECT3D structure that contains floats. So basically, I need
>to convert( pass ) things to "C", not from.
>Thanks for trying to help though...
>
>-- Todd Riggins

Todd: All you have to do is declare the DIRECT3D structure

-- a 3d structure  with 3 parameter or how many you need the HIGH
-- means to allocate it in high memory
 constant DIRECT3D  = struct(" 3ddata_1 : doulble : 1 "&
                             " 3ddata_2 : doulble : 1 "&
                             " 3ddata_3 : doulble : 1 ",HIGH)
-- put values in structure
  Sets(DIRECT3D, "3ddata_1", 2375.888)
  Sets(DIRECT3D, "3ddata_2", 2285.8)
  Sets(DIRECT3D, "3ddata_3", 2.888)

-- Then call your "C" routine and pass DIRECT3D constant which is
-- the pointer to the structure. If you need to know the size of
-- the declared structure just do use sizeof(DIRECT3D) function
-- If "C" returns a pointer to some other structure then declare that
-- structure giving it's exact discription, take the returned pointer
-- and do this
 mem_copy(declared_structure, returned_pointer, sizeof(declared_structure))
-- then you can use the names in the declared_structure to access the
-- structure

Bernie

new topic     » goto parent     » topic index » view message » categorize

6. Re: Passing floats/doubles to C

Todd Riggins writes:
> poke4(material,      atom_to_float64(1.0))
> poke4(material+8,  atom_to_float64(1.0))
> ...

atom_to_float64() returns a sequence of 8 bytes - a C double.
Use poke(material, atom_to_float64(1.0))
to store the 8 bytes in memory.
poke4 will actually store 4*8 = 32 bytes - wrong.

If you decide to use atom_to_float32() to store C *floats*,
you can also use poke() to store the 4 bytes.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view message » categorize

7. Re: Passing floats/doubles to C

Robert Craig wrote:
>
> Todd Riggins writes:
> > poke4(material,      atom_to_float64(1.0))
> > poke4(material+8,  atom_to_float64(1.0))
> > ...
>
> atom_to_float64() returns a sequence of 8 bytes - a C double.
> Use poke(material, atom_to_float64(1.0))
> to store the 8 bytes in memory.
> poke4 will actually store 4*8 = 32 bytes - wrong.
>
> If you decide to use atom_to_float32() to store C *floats*,
> you can also use poke() to store the 4 bytes.

Yes, I finally noticed that I had poke4(material, atom_to_float64(1.0))
and then changed it to poke(material, atom_to_float64(1.0)). It didn't
matter, it still gives me incorrect values, well, after the 12th
member now.
 Even though now, since I use the poke(material, atom_to_float64(1.0))
instead of the poke(material, atom_to_float32(1.0)) like what I need,
I get these values set in the C structure members in the DLL:
[ in my c function: DllExport int SET_MATERIALS(LPMATERIAL material)]
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
1.000000
54270830327981012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
0.000000
0.000000
0.000000
Just like if I use:DllExport int SET_MATERIALS(double material[17]), I
get those values.
 I set all the members to value of 1.0 in Euphoria and then after
it prints out the 12th member, it craps out on me. Why?

new topic     » goto parent     » topic index » view message » categorize

8. Re: Passing floats/doubles to C

On Sun, 13 Feb 2000 11:39:19 -0600, Todd Riggins <triggins at AIRMAIL.NET>
wrote:

>Arg. Now I have the C function to accept the data as a
>double array of 17, like this:

 Todd:
   All you have do is describe the structure with the editor,
   then cut the discription and paste it 16 more times, change
   each entry name so it's unique. Like this

  constant MATERIAL = struc(
                             <--- copy from here
   " 1_entrya : double : 1 "&
   " 1_entryb : double : 1 "&
   " 1_entryc : double : 1 "&
                             <--- copy to here
   ..... paste 15 times up to here
                             <--- pasted and make name unique
   " 17_entrya : double : 1 "&
   " 17_entryb : double : 1 "&
   " 17_entryc : double : 1 ",HIGH)

  Then you have a array of 17 structures that you can address any
  location in it by name

Bernie

new topic     » goto parent     » topic index » view message » categorize

9. Re: Passing floats/doubles to C

Bernie:
 okay, I'll give your lib a try. But I don't see how it's going to
help passing the structure your lib makes with double values
to my dll. I guess I will to see. Hopefully I'm doing something wrong.
I'll let ya know if it works...
Thanks...

-- Todd Riggins

new topic     » goto parent     » topic index » view message » categorize

10. Re: Passing floats/doubles to C

In various posts Todd Riggins writes:

>  DllExport int SET_MATERIALS(MATERIAL material)

>  DllExport int SET_MATERIALS(LPMATERIAL material)

>  DllExport int SET_MATERIALS(double material[17])

Be careful how you declare the C function.
C does not support passing arrays by value, and Euphoria
does not support passing C structures by value, only by
pointer. That makes the 1st declaration above
unworkable, and I think the 3rd one should be:
     double *material
if you want to pass a pointer to an array of doubles.
The middle one is ok too if the parameter coming from
Euphoria should be treated as a pointer to a C structure.

If you declare the C parameter incorrectly it could possibly
cause the strange values that are printed by the C program,
since the data on the stack will not be what C is expecting.

Also, I recommend that you pass a variety of interesting
numbers, not just 1.0.  It will make it easier to see
what has gone wrong.

I don't see any fundamental reason why your code can't
be made to work, but I know that it gets very tricky at this level.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view message » categorize

11. Re: Passing floats/doubles to C

Bernie:
Okay, I did it this way:
constant material=struc(" duffuse_r : float : 1 "&
                        " duffuse_g : float : 1 "&
                        " duffuse_b : float : 1 "&
                        " duffuse_a : float : 1 "&
                        " ambient_r : float : 1 "&
                        " ambient_g : float : 1 "&
                        " ambient_b : float : 1 "&
                        " ambient_a : float : 1 "&
                        " specular_r : float : 1 "&
                        " specular_g : float : 1 "&
                        " specular_b : float : 1 "&
                        " specular_a : float : 1 "&
                        " emissive_r : float : 1 "&
                        " emissive_g : float : 1 "&
                        " emissive_b : float : 1 "&
                        " emissive_a : float : 1 "&
                        " power : float : 1 ",HIGH)

Fill in the values:
  Sets(material, "duffuse_r",  1.0)
  Sets(material, "duffuse_r",  1.1)
  Sets(material, "duffuse_r",  1.2)
  Sets(material, "duffuse_r",  1.3)
  Sets(material, "ambient_r",  1.4)
  Sets(material, "ambient_r",  1.5)
  Sets(material, "ambient_r",  1.6)
  Sets(material, "ambient_r",  1.7)
  Sets(material, "specular_r", 1.8)
  Sets(material, "specular_r", 1.9)
  Sets(material, "specular_r", 1.11)
  Sets(material, "specular_r", 1.12)
  Sets(material, "emissive_r", 1.13)
  Sets(material, "emissive_r", 1.14)
  Sets(material, "emissive_r", 1.15)
  Sets(material, "emissive_r", 1.16)
  Sets(material, "power",      1.17)

Now I update my C structure through my dll function.
this is my C structure:

typedef struct tagMATERIAL
{
        float diffuse_r;
        float diffuse_g;
        float diffuse_b;
        float diffuse_a;
        float ambient_r;
        float ambient_g;
        float ambient_b;
        float ambient_a;
        float specular_r;
        float specular_g;
        float specular_b;
        float specular_a;
        float emissive_r;
        float emissive_g;
        float emissive_b;
        float emissive_a;
        float power;
} MATERIAL;

Now I get these values when I print out each member of my structure
in the same order as your structure:
1.300000
0.000000
0.000000
0.000000
1.700000
0.000000
0.000000
0.000000
1.120000
0.000000
0.000000
0.000000
1.160000
0.000000
0.000000
0.000000
1.170000

I get the same values if I use double for eveything...
At least I get different values...

-- Todd Riggins

new topic     » goto parent     » topic index » view message » categorize

12. Re: Passing floats/doubles to C

Robert:
In my exotica_api.ew file that defines the dll functions,
I have this:
SET_MATERIALS=link_c_func2(edx, "SET_MATERIALS",{C_POINTER},C_INT)

In my Exotica C code I have this for SET_MATERIALS:

DllExport int SET_MATERIALS(LPMATERIAL material)
{
    //D3DMATERIAL7 mtrl;
        HRESULT hr=0;
    char txt[255];
        FILE_REPORT("LPD3DMATERIAL7 material:");
        sprintf(txt,"%f",material->diffuse_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->diffuse_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->diffuse_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->diffuse_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->ambient_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->ambient_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->ambient_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->ambient_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->specular_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->specular_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->specular_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->specular_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->emissive_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->emissive_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->emissive_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->emissive_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material->power);
        FILE_REPORT(txt);
/*
        ZeroMemory( &mtrl, sizeof(mtrl) );
        mtrl.diffuse.r  = material.diffuse.r;
        mtrl.diffuse.g  = material.diffuse.g;
        mtrl.diffuse.b  = material.diffuse.b;
        mtrl.diffuse.a  = material.diffuse.a;
        mtrl.ambient.r  = material.ambient.r;
        mtrl.ambient.g  = material.ambient.g;
        mtrl.ambient.b  = material.ambient.b;
        mtrl.ambient.a  = material.ambient.a;
        mtrl.specular.r = material.specular.r;
        mtrl.specular.g = material.specular.g;
        mtrl.specular.b = material.specular.b;
        mtrl.specular.a = material.specular.a;
        mtrl.emissive.r = material.emissive.r;
        mtrl.emissive.g = material.emissive.g;
        mtrl.emissive.b = material.emissive.b;
        mtrl.emissive.a = material.emissive.a;
        mtrl.power      = material.power;

        g_pd3dDevice->lpVtbl->SetMaterial(g_pd3dDevice, &mtrl );
        if(hr!=D3D_OK)
        {
                FILE_REPORT(" !!! SET_MATERIALS FAILED !!!");
                return 1;
        }
*/
        return 0;
}

This for my MATERIAL C coded structure:

typedef struct tagMATERIAL
{
        double diffuse_r;
        double diffuse_g;
        double diffuse_b;
        double diffuse_a;
        double ambient_r;
        double ambient_g;
        double ambient_b;
        double ambient_a;
        double specular_r;
        double specular_g;
        double specular_b;
        double specular_a;
        double emissive_r;
        double emissive_g;
        double emissive_b;
        double emissive_a;
        double power;
} MATERIAL;

typedef struct tagMATERIAL __RPC_FAR *LPMATERIAL;

Now, in Euphoria code, I have this for my material structure:

atom material
material=allocate(146)
.
.
.
poke(material,    atom_to_float64(1.0))
poke(material+8,  atom_to_float64(1.1))
poke(material+16, atom_to_float64(1.2))
poke(material+24, atom_to_float64(1.3))
poke(material+32, atom_to_float64(1.4))
poke(material+40, atom_to_float64(1.5))
poke(material+48, atom_to_float64(1.6))
poke(material+56, atom_to_float64(1.7))
poke(material+64, atom_to_float64(1.8))
poke(material+72, atom_to_float64(1.9))
poke(material+80, atom_to_float64(1.11))
poke(material+88, atom_to_float64(1.12))
poke(material+96, atom_to_float64(1.13))
poke(material+114, atom_to_float64(1.14))
poke(material+122, atom_to_float64(1.15))
poke(material+130, atom_to_float64(1.16))
poke(material+138, atom_to_float64(1.17))

and then update my c coded structure with my SET_MATERIAL function:

funcval=c_func(SET_MATERIALS,{material})

Now, this is what is printed:
1.000000
1.100000
1.200000
1.300000
1.400000
1.500000
1.600000
1.700000
1.800000
1.900000
1.110000
1.120000
1.130000
0.000000
0.000000
-0.000000

when I change the C coded function SET_MATERIAL to this:
DllExport int SET_MATERIALS(double *material[17])
{
    //D3DMATERIAL7 mtrl;
        HRESULT hr=0;
    char txt[255];
        FILE_REPORT("LPD3DMATERIAL7 material:");
        sprintf(txt,"%f",material[0]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[1]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[2]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[3]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[4]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[5]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[6]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[7]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[8]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[9]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[10]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[11]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[12]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[13]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[14]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[15]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[16]);
        FILE_REPORT(txt);
/*
        ZeroMemory( &mtrl, sizeof(mtrl) );
        mtrl.diffuse.r  = material.diffuse.r;
        mtrl.diffuse.g  = material.diffuse.g;
        mtrl.diffuse.b  = material.diffuse.b;
        mtrl.diffuse.a  = material.diffuse.a;
        mtrl.ambient.r  = material.ambient.r;
        mtrl.ambient.g  = material.ambient.g;
        mtrl.ambient.b  = material.ambient.b;
        mtrl.ambient.a  = material.ambient.a;
        mtrl.specular.r = material.specular.r;
        mtrl.specular.g = material.specular.g;
        mtrl.specular.b = material.specular.b;
        mtrl.specular.a = material.specular.a;
        mtrl.emissive.r = material.emissive.r;
        mtrl.emissive.g = material.emissive.g;
        mtrl.emissive.b = material.emissive.b;
        mtrl.emissive.a = material.emissive.a;
        mtrl.power      = material.power;
    g_pd3dDevice->lpVtbl->SetMaterial(g_pd3dDevice, &mtrl );
        if(hr!=D3D_OK)
        {
                FILE_REPORT(" !!! SET_MATERIALS FAILED !!!");
                return 1;
        }
*/
        return 0;
}

I get these values printed:
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

and if I change it to:
DllExport int SET_MATERIALS(double *material)
{
    //D3DMATERIAL7 mtrl;
        HRESULT hr=0;
    char txt[255];
        FILE_REPORT("LPD3DMATERIAL7 material:");
        sprintf(txt,"%f",material[0]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[1]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[2]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[3]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[4]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[5]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[6]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[7]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[8]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[9]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[10]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[11]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[12]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[13]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[14]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[15]);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material[16]);
        FILE_REPORT(txt);
/*
        ZeroMemory( &mtrl, sizeof(mtrl) );
        mtrl.diffuse.r  = material.diffuse.r;
        mtrl.diffuse.g  = material.diffuse.g;
        mtrl.diffuse.b  = material.diffuse.b;
        mtrl.diffuse.a  = material.diffuse.a;
        mtrl.ambient.r  = material.ambient.r;
        mtrl.ambient.g  = material.ambient.g;
        mtrl.ambient.b  = material.ambient.b;
        mtrl.ambient.a  = material.ambient.a;
        mtrl.specular.r = material.specular.r;
        mtrl.specular.g = material.specular.g;
        mtrl.specular.b = material.specular.b;
        mtrl.specular.a = material.specular.a;
        mtrl.emissive.r = material.emissive.r;
        mtrl.emissive.g = material.emissive.g;
        mtrl.emissive.b = material.emissive.b;
        mtrl.emissive.a = material.emissive.a;
        mtrl.power      = material.power;
    g_pd3dDevice->lpVtbl->SetMaterial(g_pd3dDevice, &mtrl );
        if(hr!=D3D_OK)
        {
                FILE_REPORT(" !!! SET_MATERIALS FAILED !!!");
                return 1;
        }
*/
        return 0;
}

I get the same values as if I used the
DllExport int SET_MATERIALS(LPMATERIAL material)
way of doing it like the one way above:

1.000000
1.100000
1.200000
1.300000
1.400000
1.500000
1.600000
1.700000
1.800000
1.900000
1.110000
1.120000
1.130000
0.000000
0.000000
-0.000000

I understand the need for using pointers for passing the pointer to
the structure for it's data. I use MS Visual Studio 6.0, if that
helps anything.

-- Todd Riggins

new topic     » goto parent     » topic index » view message » categorize

13. Re: Passing floats/doubles to C

On Sun, 13 Feb 2000 13:45:22 -0500, Robert Craig <rds at ATTCANADA.NET> wrote:

>Todd Riggins writes:
>> poke4(material,      atom_to_float64(1.0))
>> poke4(material+8,  atom_to_float64(1.0))
>> ...
>
>atom_to_float64() returns a sequence of 8 bytes - a C double.
>Use poke(material, atom_to_float64(1.0))
>to store the 8 bytes in memory.
>poke4 will actually store 4*8 = 32 bytes - wrong.
>
>If you decide to use atom_to_float32() to store C *floats*,
>you can also use poke() to store the 4 bytes.
>
>Regards,
>   Rob Craig
>   Rapid Deployment Software
>   http://www.RapidEuphoria.com

A minor help in this situation would be some better documentation in the
manual. An even more minor glitch is this error in the manual:

poke Syntax:
poke(a, x)
Description:
If x is an atom, write a single byte value to memory address a.
If x is a sequence, write a sequence of byte values to consecutive memory
locations starting at location a.
Comments:
The lower 8-bits of each BYTE value, i.e. remainder(x, 256), is actually stored
in memory.

I believe that the word that I have put in all caps should eliminated or that
the words "sequence element" should be put in place of "byte value".

Of course, providing real structures that actually map the C structures
would get rid of the need for peeking and poking these values entirely.

Everett L.(Rett) Williams
rett at gvtc.com

Everett L.(Rett) Williams
rett at gvtc.com

new topic     » goto parent     » topic index » view message » categorize

14. Re: Passing floats/doubles to C

On Sun, 13 Feb 2000 16:27:26 -0600, Todd Riggins <triggins at AIRMAIL.NET>
wrote:

>Now I update my C structure through my dll function.
>this is my C structure:
>
>typedef struct tagMATERIAL
>{
>        float diffuse_r;
>        float diffuse_g;
>        float diffuse_b;
>        float diffuse_a;
>        float ambient_r;
>        float ambient_g;
>        float ambient_b;
>        float ambient_a;
>        float specular_r;
>        float specular_g;
>        float specular_b;
>        float specular_a;
>        float emissive_r;
>        float emissive_g;
>        float emissive_b;
>        float emissive_a;
>        float power;
>} MATERIAL;
>

Todd:
     I am confused you said that you were using doubles and
     in your last post you are using float ??

     double and float are both floating point numbers but
     they are different size numbers.

     Also in your Sets function you are setting the same names
     more than once.

>Fill in the values:
>  Sets(material, "duffuse_r",  1.0) <-------- same name
>  Sets(material, "duffuse_r",  1.1) <-------- same name
>  Sets(material, "duffuse_r",  1.2) <-------- same name
>  Sets(material, "duffuse_r",  1.3) <-------- last value you set
>  Sets(material, "ambient_r",  1.4) ETC....
>  Sets(material, "ambient_r",  1.5)
>  Sets(material, "ambient_r",  1.6)
>  Sets(material, "ambient_r",  1.7)
>  Sets(material, "specular_r", 1.8)
>  Sets(material, "specular_r", 1.9)
>  Sets(material, "specular_r", 1.11)
>  Sets(material, "specular_r", 1.12)
>  Sets(material, "emissive_r", 1.13)
>  Sets(material, "emissive_r", 1.14)
>  Sets(material, "emissive_r", 1.15)
>  Sets(material, "emissive_r", 1.16)
>  Sets(material, "power",      1.17)

>Now I get these values when I print out each member of my structure
>in the same order as your structure:
>1.300000    <-- the last thing you set this to was 1.3
>0.000000
>0.000000
>0.000000
>1.700000


 In other words It is working if you correct the Sets function names.

Bernie

new topic     » goto parent     » topic index » view message » categorize

15. Re: Passing floats/doubles to C

Bernie Ryan wrote:
> Todd:
>      I am confused you said that you were using doubles and
>      in your last post you are using float ??

I used both to test each type. I said something in that email
that I did use both. Mabie I just didn't word it right.

>      Also in your Sets function you are setting the same names
>      more than once.
>
> >Fill in the values:
> >  Sets(material, "duffuse_r",  1.0) <-------- same name
> >  Sets(material, "duffuse_r",  1.1) <-------- same name
> >  Sets(material, "duffuse_r",  1.2) <-------- same name
> >  Sets(material, "duffuse_r",  1.3) <-------- last value you set
> >  Sets(material, "ambient_r",  1.4) ETC....
> >  Sets(material, "ambient_r",  1.5)
> >  Sets(material, "ambient_r",  1.6)
> >  Sets(material, "ambient_r",  1.7)
> >  Sets(material, "specular_r", 1.8)
> >  Sets(material, "specular_r", 1.9)
> >  Sets(material, "specular_r", 1.11)
> >  Sets(material, "specular_r", 1.12)
> >  Sets(material, "emissive_r", 1.13)
> >  Sets(material, "emissive_r", 1.14)
> >  Sets(material, "emissive_r", 1.15)
> >  Sets(material, "emissive_r", 1.16)
> >  Sets(material, "power",      1.17)

How embarrassing. I fixed the values and now it works! Finally!
I like your library now. :)

But... I still don't know why I can't use just the
regular poke way to make it work. I still need to figure out
what I'm doing wrong, so I can at least tell Robert what I did wrong.

Thanks again...
- Todd Riggins

new topic     » goto parent     » topic index » view message » categorize

16. Re: Passing floats/doubles to C

Todd Riggins writes:
> ...
> poke(material+88, atom_to_float64(1.12))
> poke(material+96, atom_to_float64(1.13))
> poke(material+114, atom_to_float64(1.14))
> poke(material+122, atom_to_float64(1.15))
> poke(material+130, atom_to_float64(1.16))
> poke(material+138, atom_to_float64(1.17))

You jumped from 96 to 114.
You meant 104.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view message » categorize

17. Re: Passing floats/doubles to C

Now that I've settled down, thankyou very very very much.
It works now. Sorry about my clumsyness.
Thanks again...

-- Todd

Robert Craig wrote:
>
> Todd Riggins writes:
> > ...
> > poke(material+88, atom_to_float64(1.12))
> > poke(material+96, atom_to_float64(1.13))
> > poke(material+114, atom_to_float64(1.14))
> > poke(material+122, atom_to_float64(1.15))
> > poke(material+130, atom_to_float64(1.16))
> > poke(material+138, atom_to_float64(1.17))
>
> You jumped from 96 to 114.
> You meant 104.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu