PDA

View Full Version : another texture question



jankri
07-27-2001, 06:39 AM
Sorry... I know there has been a billion questions on here about textures, but the searches I've done on the forum haven't really answered my question--

Basically I am texturing triangles. I create a texture, with its size based on the length of the triangle's longest size (ie the power of 2 larger than that). My program uses a constant alpha that is based on the resolution we want for the object, the average length of the sides of triangles etc, to map vertex coordinates to texture coordinates. My problem is that since texture coordinates have to be between 0 and 1 my alpha doesn't work for calculating texture coordinates (though it works quite well when deciding the size of my texture). Is there an option not to texture coordinates in the range of 0,1 (without causing the texture to repeat -- repeating texture is a terrible thing for what I am doing)? Or how can I scale the coordinates that I find to be between 0,1 if I don't know the values a head of time?

Thanks!
(if you need more information or I am just confusing let me know and I will try again)

[This message has been edited by jankri (edited 07-27-2001).]

Leyder Dylan
07-27-2001, 08:19 AM
Hi,

Put the code on the forum.

jankri
07-27-2001, 09:27 AM
I am not sure if I can post all the code I need to without making this ugly to read...

Must texture coordinates be between 0 and 1? is there no other option?

The texture coordinates I use must be exact, not just look good. That is b/c the textures I create are based on mathematical properties (first principle direction using LIC) of the surface so the texture has to be mapped accurately to accurately portray the vector field of LIC. (LIC is Line Integral Convolution) So I need a way to keep the mapping between vertex coordinates and texture coordinates accurate when creating the texture as well as mapping it.

(should I have posted this topic in the advanced forum?)

[This message has been edited by jankri (edited 07-27-2001).]

jankri
07-27-2001, 12:56 PM
I don't know if this helps at all but here is most of my code dealing with textures....
---------------------------------
/* here are some snipits from several functions: */

//initialization of important constants
ave_length = tot_len/num_e;
//average resolution desired - //maximum length -- from "opposite corners" of surface
res_ave_des =(res_total_des*ave_length)/max_length;
alpha = res_ave_des/ave_length;

/******************************/

//decide size of texture to create
for(j=1;j<=8;j++)
{
if ( pow(2, j) > tLength*alpha )
{
sizeX = sizeY = pow(2, j);
break;
}
}

//then I create the texture...
// and write the ppm file

/*************************************/
//later....
// when drawing triangles

sprintf(texfile, "textures/%i.ppm", i);

read_texture(texfile);

/* define texture map */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texname);
glBindTexture(GL_TEXTURE_2D, texname);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_ FILTER,GL_LINEAR_MIPMAP_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

/*set up mipmaps */
mip_return = gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, img_width,img_height, GL_RGBA, GL_UNSIGNED_BYTE, texImage);

if (mip_return !=0)
printf("ERROR: %s\n", gluErrorString(mip_return));
glBindTexture(GL_TEXTURE_2D, texname);

glEnable(GL_TEXTURE_2D);

/* calculation of texture coordinates */
calc_tcoord(triangle[i], texcoord);

glBegin(GL_TRIANGLES);
glTexCoord2d(texcoord[0], texcoord[1]);
glVertex3d(points[a].x, points[a].y, points[a].z);
glTexCoord2d(texcoord[2], texcoord[3]);
glVertex3d(points[B].x, points[B].y, points[B].z);
glTexCoord2d(texcoord[4], texcoord[5]);
glVertex3d(points[c].x, points[c].y, points[c].z);
glEnd();
glDisable(GL_TEXTURE_2D);

/*******************************************/
//in calc_tcoord function
//basic texture coords that are NOT between 0 and 1
// note: depends on what side is longest to where in //array what coordinate placed
// theta is the angle btwn the longest side and l2 side

tex[0] = 0;
tex[1] = 0;
tex[2]=alpha*(edges[longE].length);
tex[3]=0;
tex[4]=alpha*(edges[l2].length)*cos(theta);
tex[5]=alpha*(edges[l2].length)*sin(theta);

ffish
07-27-2001, 09:07 PM
I didn't look through your code but in answer to your question, you can have any size texture limits you want using the GL_TEXTURE matrix (like the GL_MODELVIEW, GL_PROJECTION AND GL_COLOR matrices). Here's what you do:


// ...
glMatrixMode(GL_TEXTURE);
glLoadIdentity(); // If you need to.
// Desired texture dimension, e.g. t_w = 256.
GLfloat size = 1.0f/(GLfloat)texture_width;
glScalef(size, size, size);
glMatrixMode(GL_MODELVIEW);
// ...
glEnable(GL_TEXTURE_2D);
glBindTexture(...);
glTexCoord2i(0, 0); // v1 ...
glTexCoord2i(texture_width-1, 0); // v2 ...
glTexCoord2i(texture_width-1, texture_height-1); // v3 ...
glTexCoord2i(0, texture_height-1); // v4 ...
// ...

Hope that helps.


[This message has been edited by ffish (edited 07-27-2001).]

jankri
07-30-2001, 11:01 AM
Thanks!
That worked great and was exactly what I wanted to know.

jankri
08-02-2001, 06:02 AM
ffish --

I tried what you sugested and I thought it was working, but when I test it with a more specific texture... it is not quite right.

why in your example did you use texture_width-1 ?

Thanks.

jankri
08-03-2001, 04:41 AM
Its all working now... the error was someplace else
Have a wonderful day everyone.

ffish
08-03-2001, 05:48 AM
In case you didn't work it out, it's because if you have for example a 256x256 texture, the boundaries of the texture are 0->255 = 256 units wide in the same way that an array with 10 elements goes from array[0]->array[9] (i.e. 0 is counted as the first element).

Hope that helps.

jankri
08-03-2001, 06:00 AM
Thanks for all your help...
yeah I figured out the size-1 thing.. but since I am doing triangles and not squares, I don't use the whole height or width..

my problem was the way I was reading and writing textures...
When you give openGL a pointer to a texture image that you've read in, does it always assume that you start with 0,0 ie the lower left hand corner? That was my problem, I was doing all my texture reading and writing top to bottom. When I reversed the way I was writing files all worked much better.