Link to home
Start Free TrialLog in
Avatar of mad666
mad666

asked on

OpenGL texture mapping

How can I map a texture automatically in OpenGl ???
I tried to use OpenGl's automatic texture coord generation, but it don't seems to work.
mad666@infovia.com.ar
Avatar of Lischke
Lischke

Hi mad666,

what object do you have on which you want to apply a texture? Is your question about loading a texture into OpenGL or is it about generating texture coordinates?

Ciao, Mike
ASKER CERTIFIED SOLUTION
Avatar of TheNeil
TheNeil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mad666

ASKER

Send me code please...
mad666, you should accept an answer after you have seen the code not before. It could  well be that I had given you better code (although TheNeil's is surely not bad either).
Thanks Lischke (I think).

As for some code then this is what I use:

  PROCEDURE CalculateMapping(ax, ay, az, bx, by, bz, cx, cy, cz : REAL;
                                  rMaxX, rMinX, rMaxY, rMinY, rMaxZ, rMinZ : REAL;
                                  VAR p1x, p1y, p2x, p2y, p3x, p3y : REAL;
                                  iMapDir : LONGINT);
  VAR
    rSx : REAL;
    rSy : REAL;
    rSz : REAL;
  BEGIN
    rSx := 1 / ABS(rMaxX - rMinX);
    rSy := 1 / ABS(rMaxY - rMinY);
    rSz := 1 / ABS(rMaxZ - rMinZ);

    CASE iMapDir OF
      0 : BEGIN     //Side
            p1x := (ax - rMinX) * rSx;
            p2x := (bx - rMinX) * rSx;
            p3x := (cx - rMinX) * rSx;
            p1y := (ay - rMinY) * rSy;
            p2y := (by - rMinY) * rSy;
            p3y := (cy - rMinY) * rSy;
          END;
      1 : BEGIN     //Top
            p1x := (ax - rMinX) * rSx;
            p2x := (bx - rMinX) * rSx;
            p3x := (cx - rMinX) * rSx;
            p1y := (az - rMinZ) * rSz;
            p2y := (bz - rMinZ) * rSz;
            p3y := (cz - rMinZ) * rSz;
          END;
      2 : BEGIN     //Front
            p1x := (az - rMinZ) * rSz;
            p2x := (bz - rMinZ) * rSz;
            p3x := (cz - rMinZ) * rSz;
            p1y := (ay - rMinY) * rSy;
            p2y := (by - rMinY) * rSy;
            p3y := (cy - rMinY) * rSy;
          END;
    END;
  END;

It gets called like:

      GetXRange(rMinX, rMaxX, iObjectMatch);
      GetYRange(rMinY, rMaxY, iObjectMatch);
      GetZRange(rMinZ, rMaxZ, iObjectMatch);

            CalculateAlphaMapping(<Point 1.x>, <Point 1.y>, <Point 1.z>,
                             <Point 2.x>, <Point 2.y>, <Point 2.z>,
                             <Point 3.x>, <Point 3.y>, <Point 3.z>,
                             rMaxX, rMinX, rMaxY, rMinY, rMaxZ, rMinZ,
                             px[1], py[1], px[2], py[2], px[3], py[3],
                             <Mapping Direction>);

The results get placed into the three arrays px, py, and pz (all of which are defined as ARRAY [1..3] OF REAL). These then contain the mapping values for each of the three points. You will need to write the three routines to get your point ranges (in the above code mine are called GetXRange, GetYRange, and GetZRange). These though are a total doddle as they just iterate through your co-ordinate data and find the maximum and minimum values

Hope this is what you're after (and hope it justifies the points)

The Neil =:)