简体   繁体   English

OpenGL C ++不会显示纹理

[英]OpenGL C++ wont display texture

I just implemented a texture solution based on this solution which compiles correctly, however I don't see any changes to my scene. 我刚刚实现了基于此解决方案的纹理解决方案, 该解决方案正确编译,但我没有看到我的场景有任何变化。 在此输入图像描述 . The camera is currently inside a quad I drew if this matters. 如果这很重要的话,相机目前在我画的四边形内。

Below is my code for mapping the texture. 下面是我映射纹理的代码。

GLuint LoadTexture( const char * filename )
{

    GLuint texture;

    int width, height;

    unsigned char * data;

    FILE * file;

    file = fopen( filename, "rb" );

    if ( file == NULL ) return 0;
    width = 1024;
    height = 512;
    data = (unsigned char *)malloc( width * height * 3 );
    //int size = fseek(file,);
    fread( data, width * height * 3, 1, file );
    fclose( file );

    for(int i = 0; i < width * height ; ++i)
    {
        int index = i*3;
        unsigned char B,R;
        B = data[index];
        R = data[index+2];

        data[index] = R;
        data[index+2] = B;

    }


    glGenTextures( 1, &texture );
    glBindTexture( GL_TEXTURE_2D, texture );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );


    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
    free( data );

    return texture;
}

void quad(int a,int b,int c,int d,float scaleX,float scaleY,float scaleZ )
{
    glBegin(GL_QUADS);
        glVertex3f(ver[a][0]*scaleX,ver[a][1]*scaleY,ver[a][2]*scaleZ);
        glVertex3f(ver[b][0]*scaleX,ver[b][1]*scaleY,ver[b][2]*scaleZ);
        glVertex3f(ver[c][0]*scaleX,ver[c][1]*scaleY,ver[c][2]*scaleZ);
        glVertex3f(ver[d][0]*scaleX,ver[d][1]*scaleY,ver[d][2]*scaleZ);
    glEnd();
}

void room1()
{
    GLuint texture;
    texture = LoadTexture( "brick.bmp" );
    glBindTexture (GL_TEXTURE_2D, texture);
    glColor3fv(color[6]);
    quad(0,3,2,1,10,5,11);
    glColor3fv(color[1]);
    quad(2,3,7,6,10,5,11);
    glColor3fv(color[2]);
    quad(0,4,7,3,10,5,11);
    glColor3fv(color[3]);
    quad(1,2,6,5,10,5,11);
    glColor3fv(color[4]);
    quad(4,5,6,7,10,5,11);
    glColor3fv(color[5]);
    quad(0,1,5,4,10,5,11);
}

Try to specify texture coordinanes to vertices you draw like this 尝试将纹理coordinanes指定为您绘制的顶点,如下所示

void quad(int a,int b,int c,int d,float scaleX,float scaleY,float scaleZ )
{
    glBegin(GL_QUADS);

        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(ver[a][0]*scaleX,ver[a][1]*scaleY,ver[a][2]*scaleZ);

        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(ver[b][0]*scaleX,ver[b][1]*scaleY,ver[b][2]*scaleZ);

        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(ver[c][0]*scaleX,ver[c][1]*scaleY,ver[c][2]*scaleZ);

        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(ver[d][0]*scaleX,ver[d][1]*scaleY,ver[d][2]*scaleZ);
    glEnd();
}

You may modify texture coords values to achive expected texture orientation. 您可以修改纹理坐标值以达到预期的纹理方向。 And do not forget enable texturing by usage glEnable(GL_TEXTURE_2D); 并且不要忘记使用glEnable(GL_TEXTURE_2D);启用纹理glEnable(GL_TEXTURE_2D); call. 呼叫。

Plus your code will be extra slow because you use 此外,您的代码将因使用而变得非常慢

GLuint texture;
texture = LoadTexture( "brick.bmp" );
glBindTexture (GL_TEXTURE_2D, texture);

at each render call (texture loading is very slow operation), main recomendation is to load texture once at some initialization function. 在每次渲染调用时(纹理加载都是非常慢的操作),主要的推荐是在一些初始化函数中加载纹理一次。 And more you didnot release video memory wich you used for texture. 而且你没有释放出用于纹理的视频内存。 Тo deal with it call. Тo处理它的电话。

glDeleteTextures(1, &texture);

Texture memory releasing proces must be performed at application exit time. 纹理存储器释放过程必须在应用程序退出时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM