Mapping problem

Hi!
See this images:


The model is a cube. The first image is taken with the camera in front of the object, the second is taken by rotating the camera of 45° around the object.
Besides I’ve a text file, in this file, there is for each vertex of the cube, her projected coordinate on image plane (in pixel). This only for visible triangles.
Now see this code, the goal is map each (visible) triangle to a right triangle.


for(int i = 0; i < 2; i++)
{
    source = new wxImage(wxT(filenameComposer("photo", i, ".bmp")), wxBITMAP_TYPE_BMP);
        
    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, source->GetWidth(), source->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, source->GetData());

    for(int j = 0; j < model->numtriangles; j++)
    {
        fscanf(projectdata, "%f %f %f %f
", &v1[0], &v1[1], &v1[2], &v1[3]);
        fscanf(projectdata, "%f %f %f %f
", &v2[0], &v2[1], &v2[2], &v2[3]);
        fscanf(projectdata, "%f %f %f %f
", &v3[0], &v3[1], &v3[2], &v3[3]);

        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture[0]);
        if(fmod(j, 2.0) == 0.0)
        {
            glBegin(GL_TRIANGLES);
                glTexCoord4fv(v1); glVertex2f(-1.0f + xT, 0.5f + yT);
                glTexCoord4fv(v2); glVertex2f(-0.5f + xT, 0.5f + yT);
                glTexCoord4fv(v3); glVertex2f(-1.0f + xT, 1.0f + yT);
            glEnd();
        }
        else
        {
            glBegin(GL_TRIANGLES);
                glTexCoord4fv(v3); glVertex2f(-1.0f + xT, 1.0f + yT);
                glTexCoord4fv(v1); glVertex2f(-0.5f + xT, 0.5f + yT);
                glTexCoord4fv(v2); glVertex2f(-0.5f + xT, 1.0f + yT);
            glEnd();
            xT += 0.5;
        }

        if(xT == 2.0)
        {
            xT = 0.0;
            yT -= 0.5;
        }
        glDisable(GL_TEXTURE_2D);
    }
    xT = 0.0;
    yT = 0.0;
}

Some explanation:
The first cicle is for the two image that I’ve presented. From this two images I would obtain a “right triangle representation” of this, one for each image.
So, the image is loaded and then the texture is generated.
The second cicle is for each triangle in the model, I read, form a file, her projected coordinate. If the triangle is visible (from the camera), this coordinate exist, otherwise are 0.
Then I create the first right triangle and I map them the first triangle, second, third…
This is the result:

It’s correct, the triangles are represented in the fifth and sixth position becaus this is the original order. In the first image that I’ve posted, the two triangles visible are the fifth and the sixth.
The problem comes when I reads the next image (four triangle visible). I obtain the same image presented above, but I’m sure that the second image is loaded and the texture is generated.
So, where is the problem?
Thanks!

The problem maybe is that you reuse the same texture id. Try this:


GLuint texture[2];
glGenTextures(2, &texture[0]);

//Texture init
for(int i = 0; i < 2; i++)
{
 ...
glBindTexture(GL_TEXTURE_2D, texture[i]);
....
}

...
//Draw call
for(int i = 0; i < 2; i++)
{
  for(int j = 0; j < model->numtriangles; j++)
  {
   glBindTexture(GL_TEXTURE_2D, texture[i]);
   glBegin(...)
   ...
   glEnd(...)
  }
}


I see you are making your drawing call inside the main for loop. Don’t do that because each time you redraw you reload the texture image each time and you lose a lot of performance.

I’ve tried this, but the result is the same.
If I comment this code (at the end):


//xT = 0.0;
//yT = 0.0;

the first image generated is correct, but the second is this:

So the texture is loaded. But I don’t understand the results.
The positions of the triangles are “correct”, but there are again the triangles of first image.
Where is the problem?

It’s true, but I use drawing function one time for each image.

I’m solved, it was a stupid problem.
I call glClear() and I clear frame buffer.