Can somebody help me with glTexCoord2d?

BlitzMax Forums/BlitzMax Programming/Can somebody help me with glTexCoord2d?

SofaKng(Posted 2007) [#1]
Let's say I want to create a huge polygon that is basically a 2D terrain. (That means that the points on the top of the object will vary in height, but on the bottom they will all be zero.)

Can I use this polygon as a cookie-cutter on a texture? (eg. my texture image will fill my polygon and will not repeat, etc)

I'm thinking I have to use a bunch of glTexCoord2d() calls in-between glVertex2f() calls but I'm not sure.

If this method won't work, will I have to create a TON of quads 1-pixel in width?

Thanks for your help!


ImaginaryHuman(Posted 2007) [#2]
Yes you can. That's what an `image` is when you draw it - it's a piece of a texture drawn to fill the pixels within a rectangle.

You need not worry about making individual 1-pixel quads.

Assuming you have uploaded your texture to video ram and used glBindTexture to select it and glEnable(GL_TEXTURE_2D) to switch texturing on, you'd begin a loop such as :

glBegin(GL_POLYGON)
...
glEnd()

And then inbetween you'd put data for each vertex, ie:

glBegin(GL_POLYGON)
glColor3ub(red,green,blue)
glTexCoord2f(tx,ty)
glVertex2f(vx,vy)
...
glColor3ub(red,green,blue)
glTexCoord2f(tx,ty)
glVertex2f(vx,vy)
glEnd()

etc... you define the color of the point on the polygon first, then the coordinate within the texture which will be `tied to` that point, and then the coordinates on-screen where the point should appear, and make sure you do it in that order. glVertex should be the last of the three. I advise also using an array to store all your polygon coords. You might also think about using GL_TRIANGLES instead of GL_POLYGON but polygon will automatically turn it into triangles for you.

So you just need to go through your array of vertex colors (unsigned bytes), texture coordinates (which are 0 to 1.0 remember not pixel coords), and your pixel coords (0..ScreenWidth-1 or ScreenHeight-1) etc. You can of course draw off-screen and it should clip it for you, if you just want to draw the entire polygon and let the system figure out what is visible.

You can entirely feasibly have one texture containing an image big enough for the whole landscape, and then each frame define a polygon including texture coords to draw whatever cookie-cutter part you want to be visible. If you work out your texture coords e.g. (1.0/TextureWidth)*X and (1.0/TextureHeight)*Y it should match up with the vertex coords and not distort things. You could also animate your polygon over the frames. If you associate given texture coords with vertices, and then you *move* the vertices without re-calculating the correct texture coords, the texture will stretch to fill the space. Image warping!


SofaKng(Posted 2007) [#3]
Thanks for the response...

What's the point of the glColor calls?

Also, it seems like OpenGL doesn't allow concave polygons so using one big polygon for my 2D terrain wouldn't work I don't think. =(


ImaginaryHuman(Posted 2007) [#4]
You don't have to do a glColor for every point in the polygon if you don't want to add tints to each vertex, but you have the option of making each vertex have a color (usually just white).

If it doesn't allow concave poly's you'll have to split it into multiple triangles. See the OpenGL redbook, it talks about that.