mercredi 22 juin 2016

OpenGL: Wrapping texture around cylinder

I am trying to add textures to a cylinder to draw a stone well. I'm starting with a cylinder and then mapping a stone texture I found here but am getting some weird results. Here is the function I am using:

void draw_well(double x, double y, double z,
    double dx, double dy, double dz,
    double th)
{
    //  Set specular color to white
    float white[] = {1,1,1,1};
    float black[] = {0,0,0,1};
    glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shinyvec);
    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
    glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);

    glPushMatrix();

    //  Offset
    glTranslated(x,y,z);
    glRotated(th,0,1,0);
    glScaled(dx,dy,dz);

    //  Enable textures
    glEnable(GL_TEXTURE_2D);
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

    glBindTexture(GL_TEXTURE_2D,texture[0]); // Stone texture

    glBegin(GL_QUAD_STRIP);
    for (int i = 0; i <= 359; i++)
    {
        glNormal3d(Cos(i), 1, Sin(i));

        glTexCoord2f(0,0); glVertex3f(Cos(i), -1, Sin(i));
        glTexCoord2f(0,1); glVertex3f(Cos(i), 1, Sin(i));
        glTexCoord2f(1,1); glVertex3f(Cos(i + 1), 1, Sin(i + 1));
        glTexCoord2f(1,0); glVertex3f(Cos(i + 1), -1, Sin(i + 1));
    }
    glEnd();

    glPopMatrix();
    glDisable(GL_TEXTURE_2D);
}


// Later down in the display function
draw_well(0, 0, 0, 1, 1, 1, 0);

and the output I receive is

enter image description here

I'm still pretty new to OpenGL and more specifically textures so my understanding is pretty limited. My thought process here is that I would map the texture to each QUAD used to make the cylinder, but clearly I am doing something wrong. Any explanation on what is causing this weird output and how to fix it would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire