Forums

You are not logged in.

#1 2011-11-24 20:30:16

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Sx API to create a command line tool to give a value of a tdl at a UV

Hey Delightful People,

I am not the best c++ programmer in the world, im probably in the bottom 5 %.

But I have written some stuff that didnt segfault too often in the past.

What I was wondering about is using the Sx API to get the value of a texture file at a coordinate.

Read in the name of the texture file, u-coordinate, v-coordinate from std in.

Write the name of the color value to std out.


ie
>texLookup /tmp/my.tdl 0.5 0.2
0.1233 0.2134 0.2312

Looking at the documentation for the Sx API I havent got a clue where to start.

But I can write the surface shader:

Code:

surface texLookUp
(
 uniform string textureFile = "";
)
{
varying color textureValue = texture(textureFile,s,t);
Ci = textureValue;
Oi = 1;
Ci *= Oi;
}

But I have no clue where to start with the C++ stuff

Sam


-------------------------------
Sam Hodge

Offline

 

#2 2011-11-24 21:02:05

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

Oh yeah for bonus points write it as a python command rather than stdin and stdout

Sam


-------------------------------
Sam Hodge

Offline

 

#3 2011-11-25 01:24:21

ogre
Member
From: Poland
Registered: 2007-01-22
Posts: 573
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

Hello samhodge

TDL textures are TIFF files (mipmapped) so there's a chance that LibTIFF (www.LibTIFF.org) will read it.
Unless you really ned sxApi for some other reasons.

Last edited by ogre (2011-11-25 01:29:32)


- Michal

Offline

 

#4 2011-11-25 08:13:53

olivier
3Delight Developer
Registered: 2007-01-09
Posts: 1930

Re: Sx API to create a command line tool to give a value of a tdl at a UV

Actually, you should use the Rx API if all you want is a texture lookup. Look in rx.h for RxTexture, RxTexturePoints1 or RxTexturePoints4. You will need to call RiBegin() before you use this and RiEnd() after.

Offline

 

#5 2011-11-25 14:34:49

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

Cool, will the texture be filtered ?

It would be great to get some examples in the distribution to help out people who learn by example

I'll see if I can get something happening


-------------------------------
Sam Hodge

Offline

 

#6 2011-11-25 16:11:46

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

Nearly there

texLookUp.cpp

Code:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

#include <ri.h>
#include <rx.h>

using namespace std;

int main(int argc, char* argv[]){
   double u_coord = 0.0;
   double v_coord = 0.0;
   string texturePath = "missing.tdl";
      
   for(int i = 1; i < argc; i++)
   {
      if (i == 1)
      {
          //cout << "Texture: " << argv[i] << endl;
    texturePath = (string)argv[i];
      }
      if (i ==  2)
      {
          //cout << "U Coordinate: " << atof(argv[i]) << endl;
    u_coord = (double)atof(argv[i]);
      }
      if (i ==  3)
      {
          //cout << "V Coordinate: " << atof(argv[i]) << endl;
    v_coord = (double)atof(argv[i]);
      }
   }
   
   const char *c = texturePath.c_str();
   RtFloat ResultColorR=0.0;
   RtFloat ResultColorG=0.0;
   RtFloat ResultColorB=0.0;
       
   RiBegin(RI_NULL);
       RtInt isLookUp = RxTexture(
           c,
           0,
        3,
        u_coord,
        v_coord,
        u_coord,
        v_coord,
        u_coord,
        v_coord,
        u_coord,
        v_coord,
        &ResultColorR,
        &ResultColorG,
        &ResultColorB
       
       );
   RiEnd();
   cout << ResultColorR << "," << ResultColorG << "," << ResultColorB << endl;  
   return 0;     
}

Makefile

Code:

all:texLookUp

texLookUp: texLookUp.cpp
    g++ -o texLookUp -O3 -I${DELIGHT}/include/ -L${DELIGHT}/lib/ -l3delight texLookUp.cpp

What am I doing wrong?

Last edited by samhodge (2011-11-25 16:41:32)


-------------------------------
Sam Hodge

Offline

 

#7 2011-11-25 16:35:56

eddgarpv
Member
From: Mexico
Registered: 2007-02-20
Posts: 241
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

I'll just point the obvious typo for the 5% in here

Code:

        &ResultColorR,
        &ResultColorG,
        &ResultColorB  
       
       );
   RiEnd();

Offline

 

#8 2011-11-25 16:41:59

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

got that one

Sam


-------------------------------
Sam Hodge

Offline

 

#9 2011-11-25 17:30:18

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

So now it compiles but it segfaults when running itself with the three arguements

Any help would be apprieciated

Sam


-------------------------------
Sam Hodge

Offline

 

#10 2011-11-25 18:00:36

olivier
3Delight Developer
Registered: 2007-01-09
Posts: 1930

Re: Sx API to create a command line tool to give a value of a tdl at a UV

I believe you want:

Code:

RtFloat color[3]; // or RtColor color;
...,
v_coord,
&color[0],
RI_NULL);

Offline

 

#11 2011-11-25 19:15:36

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

Thanks I'll give that a try when I get home


-------------------------------
Sam Hodge

Offline

 

#12 2011-11-25 21:00:41

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

At the risk of embaressing myself it now runs without segfaulting but doesnt give the result I would expect from the texture lookup

texLookUp.cpp

Code:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

#include <rx.h>

using namespace std;

int main(int argc, char* argv[]){
   double u_coord = 0.0;
   double v_coord = 0.0;
   string texturePath = "missing.tdl";
      
   for(int i = 1; i < argc; i++)
   {
      if (i == 1)
      {
          //cout << "Texture: " << argv[i] << endl;
    texturePath = (string)argv[i];
      }
      if (i ==  2)
      {
          //cout << "U Coordinate: " << atof(argv[i]) << endl;
    u_coord = (double)atof(argv[i]);
      }
      if (i ==  3)
      {
          //cout << "V Coordinate: " << atof(argv[i]) << endl;
    v_coord = (double)atof(argv[i]);
      }
   }
   
   const char *c = texturePath.c_str();
   RtFloat ResultColor[3];
   RtInt texLookUp;       
   RiBegin(RI_NULL);
   texLookUp = RxTexture(
           c,
           0,
        3,
        (RtFloat)u_coord,
        (RtFloat)v_coord,
        (RtFloat)u_coord,
        (RtFloat)v_coord,
        (RtFloat)u_coord,
        (RtFloat)v_coord,
        (RtFloat)u_coord,
        (RtFloat)v_coord,
        &ResultColor[0],
        &ResultColor[1],
        &ResultColor[2],                
        RI_NULL
       );
   RiEnd();
   cout << (double)ResultColor[0] << "," << (double)ResultColor[1] << "," << (double)ResultColor[2] << endl;
   return texLookUp;
    
}

Makefile

Code:

all:texLookUp

texLookUp: texLookUp.cpp
    g++ -o texLookUp -O3 -I${DELIGHT}/include/ -L${DELIGHT}/lib/ -l3delight texLookUp.cpp

Results

Code:

samh@trevor:~/Development/texLookUp$ ./texLookUp /home/samh/Development/texLookUp/wang.tdl 0.1 .1
-1.20443,3.98914e-34,4.35757e-39

-------------------------------
Sam Hodge

Offline

 

#13 2011-11-25 21:37:47

aghiles
3Delight Developer
Registered: 2007-01-03
Posts: 1081
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

The end of the function call should be like this:

Code:

&ResultColor[0],
RI_NULL
);

Offline

 

#14 2011-11-25 22:49:13

samhodge
Member
From: Adelaide, South Australia
Registered: 2007-03-14
Posts: 100
Website

Re: Sx API to create a command line tool to give a value of a tdl at a UV

Thanks guys.

I never really got past chapter 1 in my C++ books

here is the finished product

texLookUp.cpp

Code:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

#include <rx.h>

using namespace std;

int main(int argc, char* argv[]){
   double u_coord = 0.0;
   double v_coord = 0.0;
   string texturePath = "missing.tdl";
      
   for(int i = 1; i < argc; i++)
   {
      if (i == 1)
      {
          //cout << "Texture: " << argv[i] << endl;
    texturePath = (string)argv[i];
      }
      if (i ==  2)
      {
          //cout << "U Coordinate: " << atof(argv[i]) << endl;
    u_coord = (double)atof(argv[i]);
      }
      if (i ==  3)
      {
          //cout << "V Coordinate: " << atof(argv[i]) << endl;
    v_coord = (double)atof(argv[i]);
      }
   }
   
   const char *c = texturePath.c_str();
   RtFloat ResultColor[3];
   RtInt texLookUp;       
   RiBegin(RI_NULL);
   texLookUp = RxTexture(
           c,
           0,
        3,
        (RtFloat)u_coord,
        (RtFloat)v_coord,
        (RtFloat)u_coord,
        (RtFloat)v_coord,
        (RtFloat)u_coord,
        (RtFloat)v_coord,
        (RtFloat)u_coord,
        (RtFloat)v_coord,
        &ResultColor[0],             
        RI_NULL
       );
   RiEnd();
   cout << (double)ResultColor[0] << "," << (double)ResultColor[1] << "," << (double)ResultColor[2] << endl;
   return texLookUp;
    
}

Makefile

Code:

all:texLookUp

texLookUp: texLookUp.cpp
    g++ -o texLookUp -O3 -I${DELIGHT}/include/ -L${DELIGHT}/lib/ -l3delight texLookUp.cpp

Testing

Code:

samh@trevor:~/Development/texLookUp$ ./texLookUp /home/samh/Development/texLookUp/wang.tdl .2 .99
0.776471,0.843137,0.870588

Job Done!

Thanks again, I think ill get better at coding with practise

Sam


-------------------------------
Sam Hodge

Offline

 

Board footer

PunBB is © Copyright 2002–2005 Rickard Andersson

You are here: Resources » Forums
Follow us on Twitter
© 2006-2011 dna research. All Rights Reserved.
Trademarks & Copyrights Notices