Pregunta

I wrote this code to apply texture to the bunny model provided in ply format. Bunny gets drawn with the texture, but instead of looking like fur, the texture looks like coarse points.

void GLRenderer::paintGL()
{
    // Set light
    GLfloat light_position[] = { lightPositionX, lightPositionY, lightPositionZ, 0.0 };
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Enabling texture
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    bindTexture(QImage("fur4.bmp"));

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glEnable(GL_NORMALIZE);
    glLoadIdentity();

    glBegin(GL_TRIANGLES);

        for( int i = 0 ; i < objPLYParser.g_vpX.size() ; i+=3 )
        {
                float vector1[3], vector2[3], vCross[3], normalizationValue;
                vector1[0] = objPLYParser.g_vpX[i] - objPLYParser.g_vpX[i+1];
                vector1[1] = objPLYParser.g_vpY[i] - objPLYParser.g_vpY[i+1];
                vector1[2] = objPLYParser.g_vpZ[i] - objPLYParser.g_vpZ[i+1];

                vector2[0] = objPLYParser.g_vpX[i] - objPLYParser.g_vpX[i+2];
                vector2[1] = objPLYParser.g_vpY[i] - objPLYParser.g_vpY[i+2];
                vector2[2] = objPLYParser.g_vpZ[i] - objPLYParser.g_vpZ[i+2];

                // Cross product
                vCross[0] = vector1[1] * vector2[2] - vector2[1] * vector1[2];
                vCross[1] = vector2[0] * vector1[2] - vector1[0] * vector2[2];
                vCross[2] = vector1[0] * vector2[1] - vector2[0] * vector1[1];

                // Value to do normalization with
                normalizationValue = sqrt( vCross[0]*vCross[0] + vCross[1]*vCross[1] + vCross[2]*vCross[2] );

                 float normal[3];
                 normal[0] = vCross[0]/normalizationValue;
                 normal[1] = vCross[1]/normalizationValue;
                 normal[2] = vCross[2]/normalizationValue;

                 glNormal3f(normal[0],normal[1],normal[2]);

            glTexCoord2f(0.0, 0.0);
            glVertex3f(objPLYParser.g_vpX[i],objPLYParser.g_vpY[i],objPLYParser.g_vpZ[i]);

            glTexCoord2f( 1.0,0.0);
            glVertex3f(objPLYParser.g_vpX[i+1],objPLYParser.g_vpY[i+1],objPLYParser.g_vpZ[i+1]);

            glTexCoord2f(1.0, 1.0);
            glVertex3f(objPLYParser.g_vpX[i+2],objPLYParser.g_vpY[i+2],objPLYParser.g_vpZ[i+2]);
        }


    glEnd();
    glDisable(GL_TEXTURE_2D);
}

Texture files i tried :

enter image description here

enter image description here

Output I got :

enter image description here

enter image description here

Any suggestions what I might be doing wrong here ?

¿Fue útil?

Solución

In these lines:

glTexCoord2f(0.0, 0.0);
...

glTexCoord2f( 1.0,0.0);
...

glTexCoord2f(1.0, 1.0);
...

You supply texture coordinates which map half the entire texture to every individual triangle. What you're seeing is the whole fur texture tiled densely over the mesh.

You need to supply better texture coordinates.

Otros consejos

For a start you can use a simple object space mapping, for example for a (x, y, z) vertex you could use (x*scale, (y+z)*scale) texture coordinates. You can then tweak scale.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top