添加 3D 模型时纹理映射消失

Texture Mapping disappeared when added 3D models

我正在尝试使用 SOIL_load_OGL_texture 制作背景,但是当我添加我的 3D 模型时,纹理消失了。 3D 模型的代码与纹理代码是分开的,但是当我将它组合起来时,背景变成黑色,我的 3D 模型面向背面。这里的问题是因为协调吗?


我希望输出是面向前方的 3D 模型,它在纹理的前面。

#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include "SOIL2.h"

GLuint myTexture;
const GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };

static int head = 0, rear = -90, lear = -90, body = 0, lleg = -90, rleg = -90, ball = 0, stage = 0;
GLfloat ballRadius = 0.1f;


void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3d(0.5, 0.85, 0.9);  // head
    glPushMatrix();
    glTranslated(0, 0.25, 0.15);
    glRotated((GLfloat)head, 0, 1, 0);
    glutSolidSphere(0.2, 40, 50);
    glPopMatrix();

    glPushMatrix();         //ear
    glTranslated(0.11, 0.3, 0.18);
    glRotated((GLfloat)rear, 1, 0, 0);
    glutSolidCone(0.08, 0.2, 16, 16);
    glPopMatrix();

    glPushMatrix();         //ear
    glTranslated(-0.11, 0.3, 0.18);
    glRotated((GLfloat)lear, 1, 0, 0);
    glutSolidCone(0.08, 0.2, 16, 16);
    glPopMatrix();

    glColor3d(0.0, 0.0, 0.0);  //eyes
    glPushMatrix();
    glTranslated(0.07, 0.3, 0.33);
    glRotated(-5, 0, 0, 1);
    glScalef(0.5, 0.1, 0.1);
    glutSolidCube(0.1);
    glPopMatrix();

    glPushMatrix();      //eyes
    glTranslated(-0.07, 0.3, 0.33);
    glRotated(-5, 0, 0, -1);
    glScalef(0.5, 0.1, 0.1);
    glutSolidCube(0.1);
    glPopMatrix();

    glPushMatrix();      //mouth
    glTranslated(0.0, 0.25, 0.35);
    glRotated(0, 0, 0, 0);
    glScalef(1, 0.1, 0.1);
    glutSolidCube(0.1);
    glPopMatrix();

    glColor3d(1.0, 1.0, 1.0);
    glPushMatrix();   // teeth
    glTranslated(-0.045, 0.255, 0.34);
    glRotated(-90, 1, 0, 0);
    glutSolidCone(0.01, 0.02, 16, 16);
    glPopMatrix();

    glPushMatrix();   // teeth
    glTranslated(0.045, 0.255, 0.34);
    glRotated(-90, 1, 0, 0);
    glutSolidCone(0.01, 0.02, 16, 16);
    glPopMatrix();

    glColor3d(0.5, 0.85, 0.88);  // body
    glTranslatef(0, 0, 0.1);
    glPushMatrix();
    glRotated((GLfloat)body, 1, 0, 0);
    glutSolidSphere(0.3, 40, 50);
    glPopMatrix();


    glPushMatrix();
    glTranslatef(0, 0.7, 0.0);
    glRotatef((GLfloat)stage, 0.0, 1.0, 0.0);
    glTranslatef(1.0, 0.0, 0.0);
    glPushMatrix();
    glScalef(2.0, 0.4, 1.0);
    glutWireCube(1.0);
    glPopMatrix();

    //glFlush();

    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 

    glBindTexture(GL_TEXTURE_2D, myTexture);

    glEnable(GL_TEXTURE_2D); 
    glBegin(GL_QUADS);

    /*glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); */

    glNormal3f(0.0, 1.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2.5f, -2.5f, 2.5f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(2.5f, -2.5f, 2.5f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(2.5f, -2.5f, -2.5f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-2.5f, -2.5f, -2.5f);

    glEnd();

    glDisable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, 0); 

    glutSwapBuffers();


}

void reshape(int width, int height)
{
    if (height == 0) height = 1;                // To prevent divide by 0
    GLfloat aspect = (GLfloat)width / (GLfloat)height;
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //glFrustum(-ar + 1, ar - 1, -1.0, 1.0, 2.0, 90.0);
    //gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutCreateWindow("SOIL test");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);   

    myTexture = SOIL_load_OGL_texture("background.png", 0, 1, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_INVERT_Y);
    if (!myTexture)
    {
        printf("soil failed to load texture\n");
    }
    glEnable(GL_TEXTURE_2D);

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

    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glutMainLoop();
    return EXIT_SUCCESS;
}

如果你想在后面绘制背景图,那么你必须先绘制背景,但禁用Depth Test(之前必须清除深度缓冲区)。
这会导致其他几何图形覆盖背景。

绘制背景时不设置投影矩阵,投影矩阵为单位矩阵。所以背景的z坐标必须在[-1, 1],否则会被近平面或远平面裁剪。

如果启用纹理,则默认情况下纹理元素的颜色乘以当前颜色,因为默认情况下纹理环境模式 (GL_TEXTURE_ENV_MODE) 为 GL_MODULATE。参见 glTexEnv

这会导致纹理的纹素颜色 "mixed" 是您通过 glColor3d 设置的最后一种颜色。

在渲染纹理之前设置 "white" 颜色:

glColor3d(1.0, 1.0, 1.0);

请注意,OpenGL 是一个状态引擎。如果设置了颜色,则该颜色会一直保留到再次更改为止,甚至超出帧范围。所以这在第一帧中可能不是问题,但在以后的帧中会是。

固定功能灯光模型也应用在背景上。这将导致固定功能的 gouraud 灯模型外观不佳。我建议在绘制背景之前禁用照明。

glDisable(GL_LIGHTING);

并在绘制几何之前启用光照

glEnable(GL_LIGHTING);

函数 display 可能如下所示:

void display(void)
{
    // clear color buffer and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // prepare for drawing the back ground
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glColor3d(1.0, 1.0, 1.0);

    // projection and model view for the background
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 

    // draw the background
    glBindTexture(GL_TEXTURE_2D, myTexture);

    glEnable(GL_TEXTURE_2D); 
    glBegin(GL_QUADS);

    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

    glEnd();

    // prepare for drawing the geometry
    glDisable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, 0); 

    glDisable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);

    // projection and model view for the geoemtry
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //glFrustum(-ar + 1, ar - 1, -1.0, 1.0, 2.0, 90.0);
    //gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw geometry
    glColor3d(0.5, 0.85, 0.9);  // head

    // ...

    glFlush();
    glutSwapBuffers();
    glutPostRedisplay();
}