Discussion:
Texturing a triangle mesh
(too old to reply)
Johannes Bauer
2015-01-26 00:04:31 UTC
Permalink
Raw Message
Hi there,

I'm playing around with OpenGL and have run into a problem. Here's the
setup: I am rendering a cuboid that has a texture on the top. First I
used for all six sides of the the cuboid GL_QUADS, bound the texture
with the appropriate coordinates to it, perfect.

Now I'm not satisfied with the QUADS anymore and want instead a quad
with rounded corners. So I have a function setup that gives out the
coordinates of the corner points and I'm drawing a GL_TRIANGLE_FAN. At
certain points in the corners, I bind my texture coordinates.
Unfortunately, this doesn't work (looks very distorted, image only halt
there, hard to explain), I'm guessing that something is way wrong about
my approach.

I will try to post some code (Python, but easy enough to understand for
anyone I think, the relevant parts are very short):

Rendering the top side:

glBindTexture(GL_TEXTURE_2D, self._textures[piece.face])
glBegin(GL_TRIANGLE_FAN)
glNormal3f(0, 1, 0)

for (index, (x, y)) in enumerate(RoundedQuad(piece.width, piece.length,
rounding)):
if index == 0:
glTexCoord2f(0, 0)
self._cube(x, piece.height, y)
elif index == 1:
glTexCoord2f(0, 1)
self._cube(x, piece.height, y)
elif index == 24:
glTexCoord2f(1, 1)
self._cube(x, piece.height, y)
elif index == 25:
glTexCoord2f(1, 0)
self._cube(x, piece.height, y)
glVertex3f(x, piece.height, y)
glEnd()

How I load the textures:

texid = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0,
GL_RGB, GL_UNSIGNED_BYTE, img_data)

That's it already.

Can someone help me out and tell me where I'm going wrong?

Thank you very much,
Cheers,
Johannes
--
Wo hattest Du das Beben nochmal GENAU vorhergesagt?
Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$***@speranza.aioe.org>
Johannes Bauer
2015-01-26 00:06:38 UTC
Permalink
Raw Message
Post by Johannes Bauer
self._cube(x, piece.height, y)
My apologies. These were debugging statements that I left in (draw small
cubes to verify I hit the correct vertices -- which I do). Here again
with those lines:

glBindTexture(GL_TEXTURE_2D, self._textures[piece.face])
glBegin(GL_TRIANGLE_FAN)
glNormal3f(0, 1, 0)

for (index, (x, y)) in enumerate(RoundedQuad(piece.width, piece.length,
rounding)):
if index == 0:
glTexCoord2f(0, 0)
elif index == 1:
glTexCoord2f(0, 1)
elif index == 24:
glTexCoord2f(1, 1)
elif index == 25:
glTexCoord2f(1, 0)
glVertex3f(x, piece.height, y)
glEnd()

Cheers,
Johannes
--
Post by Johannes Bauer
Wo hattest Du das Beben nochmal GENAU vorhergesagt?
Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$***@speranza.aioe.org>
Nobody
2015-01-31 12:32:10 UTC
Permalink
Raw Message
Post by Johannes Bauer
for (index, (x, y)) in enumerate(RoundedQuad(piece.width, piece.length,
glTexCoord2f(0, 0)
glTexCoord2f(0, 1)
glTexCoord2f(1, 1)
glTexCoord2f(1, 0)
glVertex3f(x, piece.height, y)
glEnd()
You're only providing texture coordinates for 4 of the vertices. Vertices
1 through 23 will all have (0,1) as their texture coordinates (i.e. the
texture coordinate set for index==1 will persist until you reach index==24).

It should probably look something like:

for (x, y) in RoundedQuad(piece.width, piece.length, rounding):
glTexCoord2f(float(x-x0)/xsize, float(y-y0)/ysize)
glVertex3f(x, piece.height, y)
Cathy L.
2015-01-31 13:11:08 UTC
Permalink
Raw Message
Post by Nobody
You're only providing texture coordinates for 4 of the vertices. Vertices
1 through 23 will all have (0,1) as their texture coordinates (i.e. the
texture coordinate set for index==1 will persist until you reach index==24).
glTexCoord2f(float(x-x0)/xsize, float(y-y0)/ysize)
glVertex3f(x, piece.height, y)
So Nobody is still wandering around, I thought this NG was dead!

Loading...