0
votes

I'm rendering a scene using opengl with textures loaded using some sample code and the FreeImage API.

Here is a link to what i'm seeing

[Image Removed]

I can confirm that all texture coordinates are provided to glTexCoord2f between 0.0f and 1.0f as required along with the vertex coordinates.

Each of the rendered triangles appears to have the full texture pasted across it (and repeated) not the area of the texture specified by the coordinates.

The texture is 1024x1024.

This is the function for loading the texture

bool loadImageToTexture(const char* image_path, unsigned int &handle)
{
    if(!image_path)
        return false;

    FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
    FIBITMAP* dib(0);
    BYTE* bits(0);
    unsigned int width(0), height(0);

    //open the file
    fif = FreeImage_GetFileType(image_path, 0);
    if(fif == FIF_UNKNOWN)
        fif = FreeImage_GetFIFFromFilename(image_path);
    if(fif == FIF_UNKNOWN)
        return false;
    if(FreeImage_FIFSupportsReading(fif))
        dib = FreeImage_Load(fif, image_path);
    if(!dib)
        return false;

    //is the file of the correct type
    FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
    if(FIC_RGBALPHA != type)
    {
        //convert to type
        FIBITMAP* ndib = FreeImage_ConvertTo32Bits(dib);
        dib = ndib;
    }

    //get data for glTexImage2D
    bits = FreeImage_GetBits(dib);
    width = FreeImage_GetWidth(dib);
    height = FreeImage_GetHeight(dib);
    if((bits == 0) || (width == 0) || (height == 0))
        return false;

    //create the texture in open gl
    glGenTextures(1, &handle);
    glBindTexture(GL_TEXTURE_2D, handle);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glPixelStorei(GL_TEXTURE_2D, 4);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
                 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);

    //unload the image now loaded
    FreeImage_Unload(dib);

    return true;
}

These are the functions for rendering

inline void glTexture(float textureSize, float t, float u)
{
    glTexCoord2f(u/textureSize, (textureSize - t)/textureSize);
}
void processTriangle(const btVector3* triangle, const textureCoord* tc, const btVector3& normal,
                     const int partId, const int triangleIndex, const bool wireframe)
{
    if(wireframe)
    {
        glBegin(GL_LINES);
        glColor3f(1, 0, 0);
        glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
        glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
        glColor3f(0, 1, 0);
        glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
        glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
        glColor3f(0, 0, 1);
        glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
        glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
        glEnd();
    }
    else
    {
        glBegin(GL_TRIANGLES);

        glColor3f(1, 1, 1);
        //Normals are per triangle
        glNormal3f(normal.getX(), normal.getY(), normal.getZ());
        glTexture(1024.0f, tc[0].t, tc[0].u);
        glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
        glTexture(1024.0f, tc[1].t, tc[1].u);
        glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
        glTexture(1024.0f, tc[2].t, tc[2].u);
        glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());

        glEnd();
    }
}
void processAllTriangles(const btVector3& worldBoundsMin, const btVector3& worldBoundsMax)
{
    btVector3 triangle[3];
    textureCoord tc[3];

    //go through the index list build triangles and draw them
    unsigned int k = 0;
    for(unsigned int i = 0; i<dVertices.size(); i+=3, k++)
    {
        //first vertex
        triangle[0] = dVertices[i];
        tc[0] = dTexCoords[i];

        //second vertex
        triangle[1] = dVertices[i+1];
        tc[1] = dTexCoords[i+1];

        //third vertex
        triangle[2] = dVertices[i+2];
        tc[2] = dTexCoords[i+2];

        processTriangle(triangle, tc, dNormals[k], 0, 0, false);
    }
}
//draw the world given the players position
void render(btScalar* m, const btCollisionShape* shape, const btVector3& color, int debugMode,
            const btVector3& worldBoundsMin, const btVector3& worldBoundsMax)
{
    //render the world using the generated OpenGL lists
    //enable and specify pointers to vertex arrays
    glPushMatrix();
    glMultMatrixf(m);

    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);

    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glBindTexture(GL_TEXTURE_2D, m_texturehandle);

    processAllTriangles(worldBoundsMin, worldBoundsMax);

    glPopMatrix();
}
1

1 Answers

0
votes

I'm working in a larger code base and texturing is being done differently in different areas mean state from those other textured objects was not being disabled.

Before per vertex texturing is done make sure to turn off other varieties of texturing.

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);