PDA

View Full Version : Mapping problem

enigmagame
10-30-2008, 05:02 AM
Hi!
See this images:
http://img338.imageshack.us/img338/2811/photo1qn9.th.jpg (http://img338.imageshack.us/my.php?image=photo1qn9.jpg)http://img338.imageshack.us/images/thpix.gif (http://g.imageshack.us/thpix.php)
http://img338.imageshack.us/img338/1697/photo2by0.th.jpg (http://img338.imageshack.us/my.php?image=photo2by0.jpg)http://img338.imageshack.us/images/thpix.gif (http://g.imageshack.us/thpix.php)
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, &amp;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\n", &amp;v1[0], &amp;v1[1], &amp;v1[2], &amp;v1[3]);
fscanf(projectdata, "%f %f %f %f\n", &amp;v2[0], &amp;v2[1], &amp;v2[2], &amp;v2[3]);
fscanf(projectdata, "%f %f %f %f\n", &amp;v3[0], &amp;v3[1], &amp;v3[2], &amp;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:
http://img530.imageshack.us/img530/8016/texture1px7.th.jpg (http://img530.imageshack.us/my.php?image=texture1px7.jpg)http://img530.imageshack.us/images/thpix.gif (http://g.imageshack.us/thpix.php)
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!

trinitrotoluene
10-30-2008, 08:37 AM
The problem maybe is that you reuse the same texture id. Try this:

GLuint texture[2];
glGenTextures(2, &amp;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.

enigmagame
10-30-2008, 09:07 AM
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:
http://img510.imageshack.us/img510/3716/texture2nb0.th.jpg (http://img510.imageshack.us/my.php?image=texture2nb0.jpg)http://img510.imageshack.us/images/thpix.gif (http://g.imageshack.us/thpix.php)
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?

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.

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

enigmagame
10-30-2008, 09:59 AM
I'm solved, it was a stupid problem.
I call glClear() and I clear frame buffer.