Skip to main content
gamedev.net
🔒 Locked

Texture mapping question

Started by calvinhobbs 37 replies 4k views
Original Post
calvinhobbs
calvinhobbs
i'm a newbie .. so this may be a silly question .. i have a bitmap (256 x 256) which needs to be mapped onto a 3D surface made up of triangles .. but the triangles (or the points) are not in an orderly fashion i.e; Point-1 is not left-top most point, Point-2 is not beside it and so on .. Point-1 may be in the middle of the surface .. so, when i mapped the image onto the surface it does not look the same .. my question is: in order to have the texture mapped properly do i have to order the points on the surface first, say arranging it to start from the top row and then going down till the bottom ?? ... or is there any other way ? ... or am i getting the concept wrong ? ..plz can any1 explain ? ..tx a lot
-calvin
Instruo
Instruo
Exactly how are you getting your vertices? Are you manually describing them, or are you getting them from a file? Generally speaking, you should try to load complex objects from a file created by a 3d modelling program, as this will handle all of your texture coordinates for you. Only manually assign it if its a simple object (i.e. a plane or individual triangles), or if you're generating the object (i.e. a sphere)
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
calvinhobbs
calvinhobbs

o .. ok .. gotcha .. right now i assign it manually, i loop throught the triangles and assign the texel info onto each of the vertices .. thats right the 3d surface is from a modelling program itself .. coordinates are stored in a txt format as of now .. so do u think i need to load from the txt file itself? .. can u just tell me what OpenGL function which does that or some kind of example .. thanks a lot Instruo ...
-calvin
vincoof
vincoof
The vertex order doesn't matter.
The only thing that matters is that texture coordinate must be specified before the vertex position.
For instance the code below is good
glBegin(GL_QUADS);glTexCoord2i(0,0);glVertex2i(-1,-1);glTexCoord2i(1,0);glVertex2i(+1,-1);glTexCoord2i(1,1);glVertex2i(+1,+1);glTexCoord2i(0,1);glVertex2i(-1,+1);glEnd();

whereas the one below is B A D
glBegin(GL_QUADS);glVertex2i(-1,-1);glTexCoord2i(0,0);glVertex2i(+1,-1);glTexCoord2i(1,0);glVertex2i(+1,+1);glTexCoord2i(1,1);glVertex2i(-1,+1);glTexCoord2i(0,1);glEnd();
calvinhobbs
calvinhobbs
tx a lot "anonymous poster",vincoof .. i've written some code .. the bitmap is of size 256 x 256 ..

'triangles' array stores index of the triangle and vertices
'points' array stores x,y,z co-ordinate

glBegin(GL_TRIANGLES);
{
for (i = 0;i < trianglesTotal;++i)
{
glNormal3dv(normals);
glTexCoord2f(0.0,0.0); //*** see below
glVertex3d(points[triangles[0]][0],
points[triangles[0]][1], points[triangles[0]][2]);
glTexCoord2f(1.0,0.0);
glVertex3d(points[triangles[1]][0],
points[triangles[1]][1], points[triangles[1]][2]);
glTexCoord2f(0.0,1.0);
glVertex3d(points[triangles[2]][0],
points[trianglesi][2]][1], points[triangles[2]][2]);
}
}
*** - i've tried replacing with glTexCoord2f(points[triangles[0]][0]/MAX_X,
points[triangles[0]][1]/MAX_Y);
same with the other 2 vertices as well ...

no change in the texture at all .. am i going wrong somewhere? ..any help pleeeasee??

-calvin
vincoof
vincoof
Maybe you have enabled texture generation ?
Wheck if you have enabled GL_TEXTURE_GEN_S or GL_TEXTURE_GEN_T in your code. Because if you've enabled them, then calling glTexCoord doesn't matter because it is overriden.
ff8
ff8
i understood something from your code the texture it will be a triangle shape its points (0,0)(0,1)(1,0)
||0|00|000|0000|00000-------


and it will be a lost part from your texture is (0,0)(1,0)(1,1)
its shape like that :
 ------ \0000|  \000|   \00|    \0|     \| 


i hope you understand me
(note:i'm sorry for my bad english :( )
sorry for the mistake
vincoof
vincoof
------\    | \   |  \  |   \ |    \|

(Use tags to present things like that)
ff8
ff8
worst post replay i have
ok i'll explane it again
as i think you have texture its shape square (256x256)
(0,1) ----------(1,1)
###|********|
###|********|
###|********|
(0,0) ----------(1,0)
when you use glTexCoord you selected just a half the texture
(0,1)|\
###|*\
###|**\
###|***\
(0,0)-----\(1,0)
and when you run your project you just see half your texture
i hope my explane is correct :)
bye!

[Edited by - ff8 on June 26, 2004 11:20:22 AM]
vincoof
vincoof
Yes it's correct, and it's also what I've understood from his code.
(PS: use the <pre> and </pre> tags to write characters in fixed-width font)
calvinhobbs
calvinhobbs

tx ff8 .. got the picture .. i appreciate ur nice effort to explain the mistake ..

tx a lot vincoof .. yes i actually had enabled GL_TEXTURE_GEN_S ..
i disabled it and placed glTexCoord2f(points[triangles[0]][0]/MAX_X,
points[triangles[0]][1]/MAX_Y); to map each point on bitmap to the 3d surface within the range of 0.0 and 1.0 as "anonymous poster " had suggested ..

.. the texture has been mapped .. looks a lot more better but with only 1 problem .. a portion of the texture of the bitmap has been taken and placed all over the surface in a tile format .. do i need to increase the size of the bitmap or is there any other paramater missing? .. any clues? .. tx a lot ..
-calvin
ff8
ff8
please put the code after the correction mayby i can help you :)
HeavyStorm
HeavyStorm
Meaning that there was a part that shouldn't have texture but has got it?

Just make a call to glBindTexture, and pass the texture index as NULL.
vincoof
vincoof
It seems like your texture is clamped.
When you load your texture to the GL, call :
glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


What were you using for texture generation ? Can you post glTexGen calls ? In the long run it's much easier to use texgen since it will save the cost of calling glTexCoord.
calvinhobbs
calvinhobbs

ff8, vincoof i have pasted the code below:
heavystorm - no, all of the 3d surface which needs to be filled is completely filled but with tiles of a portion of the bitmap and not the entire bitmap .. just like cutting a portion of the bitmap say 50 x 50 square and pasting it all over the surface ...

The code:

glGenTextures(1,&(pDoc->texture));
glBindTexture(GL_TEXTURE_2D, pDoc->texture);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glTexImage2D(GL_TEXTURE_2D, 0, 3, nTextureWidth, nTextureHeight, 0,GL_BGR_EXT, GL_UNSIGNED_BYTE, pBitmapData);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glPushAttrib(GL_LIGHTING_BIT);

glBegin(GL_TRIANGLES);
{
for (i = 0;i < trianglesTotal;++i)
{
glNormal3dv(someNorms); glTexCoord2f(points[triangles[0]][0]/MAX_X,points[triangles[0]][1]/MAX_Y);
glVertex3d(points[triangles[0]][0],points[triangles[0]][1],
points[triangles[0]][2]);
glTexCoord2f(points[triangles[1]][0]/MAX_X,points[triangles[1]][1]/MAX_Y);
glVertex3d(points[triangles[1]][0],points[triangles[1]][1],
points[triangles[1]][2]);
glTexCoord2f(points[triangles[2]][0]/MAX_X,points[triangles[2]][1]/MAX_Y);
glVertex3d(points[triangles[2]][0],points[triangles[2]][1],
points[triangles[2]][2]);
}
}
glEnd();
glPopAttrib();
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D);
}
glPopMatrix();

any clues ... tx
-calvin
vincoof
vincoof
Tx for the code sample.
Actually I still think you have to re-enable texture coordinate generation, but with object-space planes. There you have enabled sphere mapping which is not the thing you need at all.
Replace your texgen calls with :
GLfloat repeatFactor = 1.f;GLfloat equationS = { repeatFactor, 0, 0, 0 };GLfloat equationT = { 0, repeatFactor, 0, 0 };glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);glTexGenfv(GL_S, GL_OBJECT_PLANE, equationS);glTexGenfv(GL_T, GL_OBJECT_PLANE, equationT);glEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);


then remove (or at least, comment for now) the glTexCoord calls.
and if you want to tile the texture more or less, you "just" have to play with the repeatFactor parameter.

gl
calvinhobbs
calvinhobbs

tx for the quick reply vincoof .. still no luck though! .. i've plugged in ur code in place of the existing one and commented out gltextCoord .. this time the bitmap does not seemed to have mapped at all .. there are very small 20 x 20 squares all over the 3d surface (these squares i think are from the bitmap, when zoomed in).. any ideas?? .. tx once again ..
-calvin
vincoof
vincoof
Quote:
Original post by calvinhobbs
there are very small 20 x 20 squares all over the 3d surface (these squares i think are from the bitmap, when zoomed in).. any ideas?? .. tx once again ..

This is due to the repeatFactor.
Try with other constants, such as 0.01f or 100.f, and see what happens.
calvinhobbs
calvinhobbs

tx a lot vincoof .. u r right .. i tried plugging different values for the repeat factor at one particular value (0.01f) the bitmap texture is exactly the mapped onto the surface but the good ol' problem of tiles remains .. at other values the tiles vanish as u had suggested but the texture does not seem to have applied to the surface .. at these values, the surface is smooth with one color from the bitmap being applied throughout the surface ... clues? ..
-calvin
vincoof
vincoof
Well, you could still try to setup another value between, like 0.1f ?
Feel free to play with many values you want. It won't crash the system or whatever :)

If you could post screenshots that'd be great because it would help alot understand exactly what happens and what you want. Actually I can only 'imagine' what you are talking about, but with screenshots everything would be much more clear for everyone I think.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.