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.
Page Index Toggle Pages: 1
texture mapping (Read 605 times)
texture mapping
Mar 4th, 2009, 4:07am
 
Hello,

Can anyone tell me what exactly am I doing wrong in my code?
1. the mapped image is not showing
2. the whole body of my object disappears when I add the texture() function to my code.

Code:

PImage itten;

void setup(){
size(1000, 1000, P3D);
itten = loadImage("B_shapes.gif");
}

void draw(){
background(255);
lights();
translate(width/2, height/2);
rotateY(map(mouseX, 0, width, 0, PI));
rotateZ(map(mouseY, 0, height, 0, -PI));
noStroke();
fill(#999999);//hex color
teapot(40, 70, 80, 18);
spout(6, 10, 40, 18);
handle(6, 6, 30, 4);
}

void teapot(float top, float bottom, float tHeight, int facets){
float angle = 0;
float angleInc = TWO_PI/facets;
beginShape(QUAD_STRIP);
for (int i = 0; i < facets+1; ++i){
tTop(top, angle);
tBottom(bottom, tHeight, angle);
angle += angleInc;
}
endShape();



if(bottom != 0){
angle = 0;
beginShape(TRIANGLE_FAN);
vertex(0, tHeight, 0);
for (int i = 0; i < facets + 1; i++){
vertex(bottom * cos(angle), tHeight, bottom * sin(angle));
angle += angleInc;
}
endShape();
}
}

void spout(float top, float bottom, float tHeight, int facets){
//same code as the teapot function
fill(#999999);
pushMatrix();
translate(72, 10, -40);
rotateZ(PI/2);
rotateY(PI/3);
rotateX(PI/4);
float angle = 0;
float angleInc = TWO_PI/facets;
beginShape(QUAD_STRIP);
for (int i = 0; i < facets+1; ++i){
vertex(top*cos(angle), 0, top*sin(angle));
vertex(bottom*cos(angle), tHeight, bottom*sin(angle));
angle += angleInc;
}
endShape();
popMatrix();
}


void handle(float top, float bottom, float tHeight, int facets){
fill(#333333);
rotateZ(PI/2);
rotateY(map(900, 0, width, 0, PI));
rotateX(map(160, 0, height, 0, -PI));
float angle = 0;
float angleInc = TWO_PI/facets;
pushMatrix();
translate(-40, 45, 25);
beginShape(QUAD_STRIP);
for (int i = 0; i < facets+1; ++i){
vertex(top*cos(angle), 0, top*sin(angle));
vertex(bottom*cos(angle), tHeight, bottom*sin(angle));
angle += angleInc;
}
endShape();

if(top != 0){
angle = 0;
beginShape(TRIANGLE_FAN);
//center point
vertex(0, 0, 0);
for (int i = 0; i < facets + 1; i++){
vertex(top * cos(angle), 0, top * sin(angle));
angle += angleInc;
}
endShape();
}


if(bottom != 0){
angle = 0;
beginShape(TRIANGLE_FAN);
//center point
vertex(0, tHeight, 0);
for (int i = 0; i < facets + 1; i++){
vertex(bottom * cos(angle), tHeight, bottom * sin(angle));
angle += angleInc;
}
endShape();
}
popMatrix();
}

void tTop(float top, float ang){
texture(itten);
vertex(top*cos(ang), 0, top*sin(ang));
}

void tBottom(float bottom, float tHeight, float ang){
vertex(bottom*cos(ang), tHeight, bottom*sin(ang));
}


Thank you for your help!!
Re: texture mapping
Reply #1 - Mar 4th, 2009, 11:49am
 
how big is your image? please post a link to it so we can try it.

if it's not a power of 2 (256, 512, 1024...) in each dimension then you might have trouble. this is an opengl thing.
Re: texture mapping
Reply #2 - Mar 4th, 2009, 8:59pm
 
two things...

-you only should call texture() once within a beginShape() and endShape(), and before all the calls to vertex(). So take it out of tTop() and put it in teapot() before the for loop.

-to use a texture, each call to vertex() needs to have 5 coordinates: x,y,z coordinates in the space, and u and v coordinates which are the x,y coordinate of the texture that should be represented at that point.
Re: texture mapping
Reply #3 - Mar 6th, 2009, 7:42pm
 
Thank you very much for answering so quickly.
I tried what you suggested. But I'm having a few problems again with my new code. In this link I show you what my problem is with its respective image.

http://4.bp.blogspot.com/_waAs1NiCgSQ/SbFsgouellI/AAAAAAAAAIQ/h_qJ4rgO0PY/s1600-...

The sources I have on Processing don't really reveal too much about 3D texture(), I guess that's why I'm having these problems.

Here's my new code:
Code:



PImage itten;

void setup(){
size(1000, 1000, P3D);
itten = loadImage("B_shapes.gif");
textureMode(NORMALIZED);
}

void draw(){
background(255);//25
lights();
translate(width/2, height/2);//center of the screen
rotateY(map(mouseX, 0, width, 0, PI));
rotateZ(map(mouseY, 0, height, 0, -PI));
noStroke();
fill(#999999);//hex color
teapot(40, 70, 80, 18);
spout(6, 10, 40, 18);
handle(6, 6, 30, 4);
}

void teapot(float top, float bottom, float tHeight, int facets){
float angle = 0;
float angleInc = TWO_PI/facets;
beginShape(QUAD_STRIP);
texture(itten);
for (int i = 0; i < facets+1; ++i){
vertex(top*cos(angle), 0, top*sin(angle), 0, 1);//x, y, z, u, v
vertex(bottom*cos(angle), tHeight, bottom*sin(angle), 1, 0);
angle += angleInc;
}
endShape();



if(bottom != 0){
angle = 0;
beginShape(TRIANGLE_FAN);
vertex(0, tHeight, 0);
for (int i = 0; i < facets + 1; i++){
vertex(bottom * cos(angle), tHeight, bottom * sin(angle));
angle += angleInc;
}
endShape();
}
}

void spout(float top, float bottom, float tHeight, int facets){
fill(#999999);
pushMatrix();
translate(72, 10, -40);
rotateZ(PI/2);
rotateY(PI/3);
rotateX(PI/4);
float angle = 0;
float angleInc = TWO_PI/facets;
beginShape(QUAD_STRIP);
for (int i = 0; i < facets+1; ++i){
vertex(top*cos(angle), 0, top*sin(angle));
vertex(bottom*cos(angle), tHeight, bottom*sin(angle));
angle += angleInc;
}
endShape();
popMatrix();
}


void handle(float top, float bottom, float tHeight, int facets){
fill(#333333);
rotateZ(PI/2);
rotateY(map(900, 0, width, 0, PI));
rotateX(map(160, 0, height, 0, -PI));
float angle = 0;
float angleInc = TWO_PI/facets;
pushMatrix();
translate(-40, 45, 25);
beginShape(QUAD_STRIP);
for (int i = 0; i < facets+1; ++i){
vertex(top*cos(angle), 0, top*sin(angle));
vertex(bottom*cos(angle), tHeight, bottom*sin(angle));
angle += angleInc;
}
endShape();

if(top != 0){
angle = 0;
beginShape(TRIANGLE_FAN);
//center point
vertex(0, 0, 0);
for (int i = 0; i < facets + 1; i++){
vertex(top * cos(angle), 0, top * sin(angle));
angle += angleInc;
}
endShape();
}


if(bottom != 0){
angle = 0;
beginShape(TRIANGLE_FAN);
vertex(0, tHeight, 0);
for (int i = 0; i < facets + 1; i++){
vertex(bottom * cos(angle), tHeight, bottom * sin(angle));
angle += angleInc;
}
endShape();
}
popMatrix();
}





Re: texture mapping
Reply #4 - Mar 12th, 2009, 5:15pm
 
I'm really stuck on this. Can anyone please help?
Thank you.
Re: texture mapping
Reply #5 - Mar 12th, 2009, 10:55pm
 
11Levels wrote on Mar 6th, 2009, 7:42pm:
Code:

 for (int i = 0; i < facets+1; ++i){
   vertex(top*cos(angle), 0, top*sin(angle), 0, 1);//x, y, z, u, v
   vertex(bottom*cos(angle), tHeight, bottom*sin(angle), 1, 0);
   angle += angleInc;
 }


this (the body) is the only place you're using the u and v parts of vertex which is why the body is the only part that has a texture.

what's more, you're using constant values for u and v so it's no wonder all the body panels look identical.

you need to increment the texture coords the same way you're incrementing the x and y:

Code:

...
float tinc = 1 / (float)facets;
for (int i = 0; i < facets + 1; ++i){
vertex(top*cos(angle), 0, top*sin(angle), 0, i * tinc);//x, y, z, u, v
vertex(bottom * cos(angle), tHeight, bottom * sin(angle), 1, (i + 1) * tinc);
angle += angleInc;
}


and similar for the other two bits.
Re: texture mapping
Reply #6 - Mar 13th, 2009, 1:51pm
 
that code assumes you want to map the texture all the way around the body. of you want to apply the texture to all 18 of your body panels in turn then you just need to use 0, 0 for the u, v in the first vertex and 1, 1 in the second one

(the way you had it, from (0, 1) to (1, 0), was just using the diagonal of the texture all the way around)
Page Index Toggle Pages: 1