纹理不起作用-OpenGL

时间:2018-07-20 12:06:09

标签: opengl

我不知道为什么,但是我的代码没有运行任何纹理。有人可以告诉我如何解决吗?

我正在使用一个函数来加载纹理并将GLuint返回到变量,然后在对象中设置此变量。

#include <windows.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <stdio.h>

float _angle = 0.0;
int zoomX = 0, zoomY=0;

GLuint texturaGrama;
GLfloat angle, fAspect, rotX, rotY, obsZ;

GLuint LoadTexture( const char * filename, int width, int height ){

    GLuint texture;

    unsigned char * data;

    FILE * file;

    file = fopen( filename, "rb" );

    if ( file == NULL ) return 0;
    data = (unsigned char *)malloc( width * height * 3 );
    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 Desenha(void)
    {
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
         glEnable(GL_COLOR_MATERIAL);
         glEnable(GL_TEXTURE_2D);

        // Grama
        glPushMatrix();
            glColor3f(0.0, 1.0, 0.0);

            glTranslatef(0,0,-6);
            glRotatef(_angle, 0.0, 1.0, 0.0);
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, texturaGrama);
            glBegin(GL_QUADS);
                glTexCoord2f(0.0f,0.0f);  glVertex3f(-50,-1.5,50);
                glTexCoord2f(1.0f,0.0f);  glVertex3f(-50,-1.5,-50);
                glTexCoord2f(1.0f,1.0f);  glVertex3f(50,-1.5,-50);
                glTexCoord2f(0.0f,0.0f);  glVertex3f(50,-1.5,50);
            glEnd();
            glDisable(GL_TEXTURE_2D);
    }

    void PosicionaObservador(void)
{
    // Especifica sistema de coordenadas do modelo
    glMatrixMode(GL_MODELVIEW);
    // Inicializa sistema de coordenadas do modelo
    glLoadIdentity();
    DefineIluminacao();
    // Especifica posição do observador e do alvo
    glTranslatef(zoomX,zoomY,-obsZ);
    glRotatef(rotX,1,0,0);
    glRotatef(rotY,0,1,0);
}

void Inicializa (void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

    texturaGrama = LoadTexture("grass.bmp", 256, 256);

    angle=50;
    rotX = 30;
    rotY = 0;
    obsZ = 10;
}
static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PosicionaObservador();
}


int main(void)
{
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
     glutInitWindowSize(800,600);
     glutInitWindowPosition(50,50);
     glutCreateWindow("Casa Sinistra");
     glutDisplayFunc(Desenha);
     glEnable(GL_DEPTH_TEST);
     glutSpecialFunc(MovimentoTela);
     glutReshapeFunc(resize);
     Inicializa();
     glutMainLoop();
}

我尝试了许多其他的库,但是它们都不起作用。我正在Windows 10的CodeBlocks中运行此代码。代码可以运行,但纹理不会出现。因为Windows发生了吗? 我需要添加任何库吗?

1 个答案:

答案 0 :(得分:2)

GLUT library必须由glutInit初始化。

在使用程序库的任何指令之前,在程序开始时初始化glut:

int main(int argc, char** argv)
{
     glutInit(&argc, argv); // <------------

     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
     glutInitWindowSize(800,600);
     glutInitWindowPosition(50,50);
     glutCreateWindow("Casa Sinistra");

     .....

由于您使用的是双缓冲窗口

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

您必须在渲染循环的末尾(循环中的最后一条语句)调用glutSwapBuffers,以对当前窗口执行缓冲区交换:

void Desenha(void)
{
    ......

    glutSwapBuffers();
}

顺便说一句,您的纹理坐标有误,因为uv坐标(0,0)是两倍(glTexCoord2f(0.0f,0.0f))。