<p>I see you use glEnable(GL_TEXTURE_GEN_S); but also use glTexCoord(...) to load coordinates. These are mutually exclusive features IIRC. If you are supplying your own texture coordinates you should not need to use glEnable(GL_TEXTURE_GEN_n).</p>

CC BY-SA 3.0
2016-07-07T11:33:37.720Z
You're right! I just tried to delete those lines and the first example now behaves like the second one. At this point I think the second example has just a "rotated texture" issue, I'll try and let you know.
phagio
CC BY-SA 3.0
2016-07-07T12:20:48.917Z
Yep, I confirm, that did the trick. Thank you immensely!
phagio
CC BY-SA 3.0
2016-07-07T03:39:29.670Z
2016-07-07T03:39:29.670Z
PaulHK
<pre><code>for (i = 0, i &lt; 3, ++i)
    glTexCoord3d(texturePoint[i].x, texturePoint[i].y, 0);
    glVertex3d(v[i]-&gt;x(), v[i]-&gt;y(), v[i]-&gt;z());
</code></pre>

<p>This code calls <code>glTexCoord3d</code> three times and <code>glVertex3d</code> only once. You need more braces around your <code>for</code> loop.</p>

CC BY-SA 3.0
2016-06-17T13:29:43.373Z
Hi and thank you :) sorry, it is a typo: the cycle is correct (otherwise i would be out of scope). I'm adding the necessary braces.
phagio
CC BY-SA 3.0
2016-06-17T12:38:47.507Z
2016-06-17T12:38:47.507Z
Dan Hulme
<p>Your geometry may be prepared for Direct3D, which has texture space origin placed differently (top-left vs bottom-left corner). Try flipping either the texture's Y coordinates (i.e. load it from the bottom to the top), or the V texture coordinate (<code>glTexCoord3d(texturePoint[i].x, 1.0f - texturePoint[i].y, 0);</code>).</p>

CC BY-SA 3.0
2016-06-20T19:38:34.540Z
Tried that and didn't work. I think it's not the case of Direct3D geometry, because I'm on Ubuntu - isn't Direct3D a Microsoft exclusive?
phagio
CC BY-SA 3.0
2016-07-21T07:28:24.947Z
Update: it actually had something to do with that, although it was a minor fault.
phagio
CC BY-SA 3.0
2016-06-20T07:43:31.553Z
2016-06-20T07:43:31.553Z
IneQuation
<p>I have a mesh loaded by an obj file (wavefront), complete with normals and texture coordinates per face, and its relative texture available. In my C++ / OpenGL code (assignment for a computer graphics class) I bind correctly the texture, but fail at correctly mapping texture coordinates with face vertexes. Below I show the code for texture binding and the chain of calls to rasterize the object. I also show images for the expected result and the actual results (2 of them, one for each render setting). To be specific, it seems that the first render "tessellates" the plain image on the rasterized shape of the whole object, while the second maps the image once on the shape of the object.</p>

<p>What is going on here?</p>

<p><a href="https://i.sstatic.net/nQ2Ez.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nQ2Ez.png" alt="Expected result"></a>
<a href="https://i.sstatic.net/LEOSJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LEOSJ.png" alt="Result #1"></a>
<a href="https://i.sstatic.net/q7TXB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/q7TXB.png" alt="Result #2 (don&#39;t mind the background)"></a></p>

<p>Bind procedure:</p>

<pre><code>SDL_Surface *s = IMG_Load(filename.c_str());

glBindTexture(GL_TEXTURE_2D, textureIndex);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB,
        s-&gt;w, s-&gt;h, GL_RGB, GL_UNSIGNED_BYTE, s-&gt;pixels);
glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_MAG_FILTER,
        GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_MIN_FILTER,
        GL_LINEAR_MIPMAP_LINEAR );
</code></pre>

<p>Raster procedure (pseudo code intended):</p>

<pre><code>// gl pushMatrix, Traslation and rotations;
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureIndex);
glColor3f(1,1,1);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glDisable(GL_LIGHTING);
glBegin(GL_TRIANGLES);
for (face f in faces) {
  glNormal3d(faceNormal.x, faceNormal.y, faceNormal.z);
  for (i = 0, i &lt; 3, ++i) {
    glTexCoord3d(texturePoint[i].x, texturePoint[i].y, 0);
    glVertex3d(vertex[i].x, vertex[i].y, vertex[i].z);
  }
}
glEnd();
glPopMatrix();
// gl PopMatrix to exit from traslation / rotation context.
</code></pre>

CC BY-SA 3.0
2016-06-19T23:54:04.283Z
What's your `GL_TEXTURE_MATRIX` set to? Perhaps it has some scaling in it? (Also, what's the matrix mode for this code? You push the current matrix, but don't appear to be setting the mode before doing so.)
user1118321
CC BY-SA 3.0
2016-06-20T19:44:46.640Z
Before the bind code:
`glMatrixMode(GL_PROJECTION ); glLoadIdentity(); gluPerspective(70, 3.0/4, 0.2, 1000 ); glMatrixMode(GL_MODELVIEW); glLoadIdentity();`
No scaling of sort (actually there was, but when you mentioned it I removed it and it didn't change the result by much).
phagio
<opengl><texture><legacy-opengl>
OpenGL Vertex - Texture Mapping
phagio
# OpenGL Vertex - Texture Mapping

(Asked by: username_0 on 2016-06-17)

I have a mesh loaded by an obj file (wavefront), complete with normals and texture coordinates per face, and its relative texture available. In my C++ / OpenGL code (assignment for a computer graphics class) I bind correctly the texture, but fail at correctly mapping texture coordinates with face vertexes. Below I show the code for texture binding and the chain of calls to rasterize the object. I also show images for the expected result and the actual results (2 of them, one for each render setting). To be specific, it seems that the first render "tessellates" the plain image on the rasterized shape of the whole object, while the second maps the image once on the shape of the object.

What is going on here?

[![Expected result](https://i.sstatic.net/nQ2Ez.png)](https://i.sstatic.net/nQ2Ez.png)
[![Result #1](https://i.sstatic.net/LEOSJ.png)](https://i.sstatic.net/LEOSJ.png)
[![Result #2 (don't mind the background)](https://i.sstatic.net/q7TXB.png)](https://i.sstatic.net/q7TXB.png)

Bind procedure:

```
SDL_Surface *s = IMG_Load(filename.c_str());

glBindTexture(GL_TEXTURE_2D, textureIndex);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB,
        s->w, s->h, GL_RGB, GL_UNSIGNED_BYTE, s->pixels);
glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_MAG_FILTER,
        GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_MIN_FILTER,
        GL_LINEAR_MIPMAP_LINEAR );

```

Raster procedure (pseudo code intended):

```
// gl pushMatrix, Traslation and rotations;
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureIndex);
glColor3f(1,1,1);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glDisable(GL_LIGHTING);
glBegin(GL_TRIANGLES);
for (face f in faces) {
  glNormal3d(faceNormal.x, faceNormal.y, faceNormal.z);
  for (i = 0, i < 3, ++i) {
    glTexCoord3d(texturePoint[i].x, texturePoint[i].y, 0);
    glVertex3d(vertex[i].x, vertex[i].y, vertex[i].z);
  }
}
glEnd();
glPopMatrix();
// gl PopMatrix to exit from traslation / rotation context.

```

**Tags:** opengl, texture, legacy-opengl

> **Comments:**
> **username_1**: What's your `GL_TEXTURE_MATRIX` set to? Perhaps it has some scaling in it? (Also, what's the matrix mode for this code? You push the current matrix, but don't appear to be setting the mode before doing so.)  
> **username_0**: Before the bind code: `glMatrixMode(GL_PROJECTION ); glLoadIdentity(); gluPerspective(70, 3.0/4, 0.2, 1000 ); glMatrixMode(GL_MODELVIEW); glLoadIdentity();` No scaling of sort (actually there was, but when you mentioned it I removed it and it didn't change the result by much).  

---

## 3 answers

### Answer by username_2 on 2016-07-07. Score: 2 | Accepted answer

I see you use glEnable(GL\_TEXTURE\_GEN\_S); but also use glTexCoord(...) to load coordinates. These are mutually exclusive features IIRC. If you are supplying your own texture coordinates you should not need to use glEnable(GL\_TEXTURE\_GEN\_n).

> **Comments:**
> **username_0**: You're right! I just tried to delete those lines and the first example now behaves like the second one. At this point I think the second example has just a "rotated texture" issue, I'll try and let you know.  
> **username_0**: Yep, I confirm, that did the trick. Thank you immensely!  

---

### Answer by username_3 on 2016-06-17. Score: 2

```
for (i = 0, i < 3, ++i)
    glTexCoord3d(texturePoint[i].x, texturePoint[i].y, 0);
    glVertex3d(v[i]->x(), v[i]->y(), v[i]->z());

```

This code calls `glTexCoord3d` three times and `glVertex3d` only once. You need more braces around your `for` loop.

> **Comments:**
> **username_0**: Hi and thank you :) sorry, it is a typo: the cycle is correct (otherwise i would be out of scope). I'm adding the necessary braces.  

---

### Answer by username_4 on 2016-06-20. Score: 1

Your geometry may be prepared for Direct3D, which has texture space origin placed differently (top-left vs bottom-left corner). Try flipping either the texture's Y coordinates (i.e. load it from the bottom to the top), or the V texture coordinate (`glTexCoord3d(texturePoint[i].x, 1.0f - texturePoint[i].y, 0);`).

> **Comments:**
> **username_0**: Tried that and didn't work. I think it's not the case of Direct3D geometry, because I'm on Ubuntu - isn't Direct3D a Microsoft exclusive?  
> **username_0**: Update: it actually had something to do with that, although it was a minor fault.  

---

