0

我正在使用 opengl 渲染场景,并使用一些示例代码和 FreeImage API 加载纹理。

这是我所看到的链接

[图片已删除]

我可以确认所有纹理坐标都根据需要提供给glTexCoord2f 0.0f 和1.0f 以及顶点坐标。

每个渲染的三角形似乎都粘贴了(并重复)完整的纹理,而不是坐标指定的纹理区域。

纹理为 1024x1024。

这是加载纹理的函数

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;
}

这些是用于渲染的函数

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();
}
4

1 回答 1

0

我在一个更大的代码库中工作,并且在不同区域以不同的方式进行纹理处理意味着来自其他纹理对象的状态没有被禁用。

在完成每个顶点纹理之前,请确保关闭其他种类的纹理。

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
于 2012-07-31T18:17:36.080 回答