We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Help with UV texture mapping
Page Index Toggle Pages: 1
Help with UV texture mapping (Read 2373 times)
Help with UV texture mapping
Oct 28th, 2009, 11:47am
 
Hi I am working on a bezierPoint shape, it is all working fine but I am having problems mapping a texture to the mesh

Problems:
- in U the texure is repeating 4 times
- in V texture is rendering upside down

The brown line shoud be in the top center, the black line should point  to the top center as well, the white line should be in the bottom

Here is the sketch
http://nardove.com/p5/umbrella_shape_12x/

Any help will be much appreciated
rS
Re: Help with UV texture mapping
Reply #1 - Oct 29th, 2009, 12:45am
 
Here is an image and a bit of explanation, in the hopes that you can understand better what is going on with my sketch.


So the black arrow points north, and it is position in the middle, in my sketch the arrow points south, and it is repeating 4 times along U

...

Cheers
rS
Re: Help with UV texture mapping
Reply #2 - Oct 29th, 2009, 2:20am
 
Well the reason the arrow poinst south is beacuse the values for V are from 1.0 to 0.0

And the reason it is repeating in U, is because I am getting values from 0.004234 to 0.83244 four times in the second (inner) for loop

When I shoud get U from 0.0 to 1.0 and the same for V, OK I understand what is going on but I am having a hard time to sort it out

Any ideas will be much appreciated

Cheers
rS
Re: Help with UV texture mapping
Reply #3 - Oct 29th, 2009, 10:51am
 
Ok here is another frustration...

I am draw the geometry using some formulas found in the opengl forum, but still the same result, so I think is not the way I draw the mesh, the problem is in the UV mapping (did I said that already?!?), anyway I cant work it out!

The UV mapping formulas I got them here:
http://en.wikipedia.org/wiki/UV_mapping

Here is a more clean code
Code:
import peasy.*;

PeasyCam pCamera;

float radius = 50.0;
float rho = radius;
float factor = TWO_PI / 20.0;
float x, y, z, u, v;

PVector[] sphereVertexPoints;

PImage skin;


void setup() {
 size(320, 240, P3D);
 
 pCamera = new PeasyCam(this, 150);
 
 skin = loadImage("uv_texture.jpg");
 textureMode(NORMALIZED);
 noStroke();
}


void draw() {
 background(50);
 //noFill(); stroke(255);
 fill(200, 130, 0);
 
 // stage lighting
 directionalLight(255, 255, 255, -100, 100, -100);
 ambientLight(120, 120, 120);
 
 
 for(float phi = 0.0; phi < HALF_PI; phi += factor) {
   beginShape(QUAD_STRIP);
   texture(skin);
   for(float theta = 0.0; theta < TWO_PI + factor; theta += factor) {
     x = rho * sin(phi) * cos(theta);
     z = rho * sin(phi) * sin(theta);
     y = -rho * cos(phi);
     
     u = abs(x / sqrt(x*x + y*y + z*z));
     v = abs(y / sqrt(x*x + y*y + z*z));
     
     normal(x, y, z);
     vertex(x, y, z, u, v);
     
     x = rho * sin(phi + factor) * cos(theta);
     z = rho * sin(phi + factor) * sin(theta);
     y = -rho * cos(phi + factor);
     
     u = abs(x / sqrt(x*x + y*y + z*z));
     v = abs(y / sqrt(x*x + y*y + z*z));
     
     normal(x, y, z);
     vertex(x, y, z, u, v);
   }
   endShape(CLOSE);
 }
}


Runing our of hair!!! and going crazy talking alone here, give me a hand... please
rS
Re: Help with UV texture mapping
Reply #4 - Oct 29th, 2009, 12:38pm
 
You are right in that it is the UV mapping. Since the origin of the umbrella is (0,0,0) then
Code:
radius = sqrt(x*x + y*y + z*z) 


and the value of x will go
0 -> 50  -> 0 -> -50 -> 0
since you take the absolute value then effectively the u coordinate will go
0 -> 1 -> 0 -> -1 -> 0
there is no simple way to change the mapping Angry

Using floats in for loops is very unusual and probably not a good idea so I hope you don't mind I have modified your code so as to use ints in the for loops

Code:

import peasy.*;

PeasyCam pCamera;

float radius = 50.0;
float rho = radius;
float x, y, z, u, v;

float phi;
int phiSteps = 20;
float phiFactor = HALF_PI / phiSteps;

float theta;
int thetaSteps = 20;
float thetaFactor = TWO_PI / thetaSteps;

PVector[] sphereVertexPoints;

PImage skin;


void setup() {
 size(320, 240, P3D);

 pCamera = new PeasyCam(this, 150);

 skin = loadImage("uv_texture.jpg");
 textureMode(NORMALIZED);
 noStroke();
}


void draw() {
 background(50);
 //noFill(); stroke(255);
 fill(200, 130, 0);

 // stage lighting
 directionalLight(255, 255, 255, -100, 100, -100);
 ambientLight(120, 120, 120);

 phi = 0.0;
 for(int p = 0; p < phiSteps; p++) {
   beginShape(QUAD_STRIP);
   texture(skin);
   theta = 0.0;
   for(int t = 0; t < thetaSteps + 1; t++) {
     x = rho * sin(phi) * cos(theta);
     z = rho * sin(phi) * sin(theta);
     y = -rho * cos(phi);

     u = (float)t / thetaSteps;
     v = (float)p / phiSteps;


     normal(x, y, z);
     vertex(x, y, z, u, v);

     x = rho * sin(phi + phiFactor) * cos(theta);
     z = rho * sin(phi + phiFactor) * sin(theta);
     y = -rho * cos(phi + phiFactor);

     u = (float)t / thetaSteps;
     v = (float)(p + 1) / phiSteps;

     normal(x, y, z);
     vertex(x, y, z, u, v);
   
     theta += thetaFactor;
   }
   phi += phiFactor;
   endShape(CLOSE);
 }
}


It should be fairly self explanatory but there are now two variables to define the number of horizontal (phiSteps) and vertical strips (thetaSteps) that make up the umbrella.
Cheesy

P.S.
Quite a bit more can be done to make the code more efficient such as inside the inner loop pre-calculating the sin and cos functions before calculating the xyz coordinates...
Re: Help with UV texture mapping
Reply #5 - Oct 29th, 2009, 1:10pm
 
Hi Quark, this is brilliant!!! thanks a lot

rS
Re: Help with UV texture mapping
Reply #6 - Jan 11th, 2010, 4:06am
 
is there anyway to draw the sphere with a gridview?
Re: Help with UV texture mapping
Reply #7 - Jan 12th, 2010, 3:49am
 
Quote:
is there anyway to draw the sphere with a gridview?

Yes this should work.

Code:

 stroke(255,255,0); // yellow lines
 strokeWeight(1); // Whatever thickness you want
 noFill(); // no filling of quads
 for(int p = 0; p < phiSteps; p++) {
   beginShape(QUAD_STRIP);
   theta = 0.0;
   for(int t = 0; t < thetaSteps + 1; t++) {
     x = rho * sin(phi) * cos(theta);
     z = rho * sin(phi) * sin(theta);
     y = -rho * cos(phi);

     normal(x, y, z);
     vertex(x, y, z);

     x = rho * sin(phi + phiFactor) * cos(theta);
     z = rho * sin(phi + phiFactor) * sin(theta);
     y = -rho * cos(phi + phiFactor);

     normal(x, y, z);
     vertex(x, y, z);
   
     theta += thetaFactor;
   }
   phi += phiFactor;
   endShape(CLOSE);
 }
Page Index Toggle Pages: 1