GL_TEXTURE_2D出现照明问题?

时间:2015-12-09 09:15:24

标签: opengl 3d light 3ds

我想问你纹理和灯光。

我在OpenGL中使用3ds对象,以及BITMAP纹理。它工作得很好。当我使用灯光时,它没有反射光线。在我搜索的时候,我只是评论了这一行:glEnable(GL_TEXTURE_2D);并且纹理已经消失,但照明工作正常!

我有没有机会离开TEXTURE并添加灯光?为什么会这样?任何人都有任何想法?

EDITED

这是INIT()函数

void initialize(){
glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)

glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping

     glEnable(GL_LIGHTING);
     glEnable(GL_LIGHT0);
     glEnable(GL_NORMALIZE);

     // Light model parameters:
     // -------------------------------------------

     GLfloat lmKa[] = {0.0, 0.0, 0.0, 0.0 };
     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmKa);

     glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0);
     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 0.0);

     // -------------------------------------------
     // Spotlight Attenuation

     GLfloat spot_direction[] = {1.0, -1.0, -1.0 };
     GLint spot_exponent = 30;
     GLint spot_cutoff = 180;

     glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
     glLighti(GL_LIGHT0, GL_SPOT_EXPONENT, spot_exponent);
     glLighti(GL_LIGHT0, GL_SPOT_CUTOFF, spot_cutoff);

     GLfloat Kc = 1.0;
     GLfloat Kl = 0.0;
     GLfloat Kq = 0.0;

     glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION,Kc);
     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, Kl);
     glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, Kq);


     // -------------------------------------------
     // Lighting parameters:

     GLfloat light_pos[] = {0.0f, 5.0f, 5.0f, 1.0f};
     GLfloat light_Ka[]  = {1.0f, 0.5f, 0.5f, 1.0f};
     GLfloat light_Kd[]  = {1.0f, 0.1f, 0.1f, 1.0f};
     GLfloat light_Ks[]  = {1.0f, 1.0f, 1.0f, 1.0f};

     glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
     glLightfv(GL_LIGHT0, GL_AMBIENT, light_Ka);
     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_Kd);
     glLightfv(GL_LIGHT0, GL_SPECULAR, light_Ks);

     // -------------------------------------------
     // Material parameters:

     GLfloat material_Ka[] = {0.5f, 0.0f, 0.0f, 1.0f};
     GLfloat material_Kd[] = {0.4f, 0.4f, 0.5f, 1.0f};
     GLfloat material_Ks[] = {0.8f, 0.8f, 0.0f, 1.0f};
     GLfloat material_Ke[] = {0.1f, 0.0f, 0.0f, 0.0f};
     GLfloat material_Se = 20.0f;

     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_Ka);
     glMaterialfv(GL_FRONT, GL_DIFFUSE, material_Kd);
     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_Ks);
     glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_Ke);
     glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material_Se);
}

这是DISPLAY()函数(我正在使用QT)

void pointGL(){
    if(change)
     {
      ThreeDModels objectClass;

      objectClass.SpaceShip();
      change = false;
     }
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glPushMatrix();
    glScalef(0.05, 0.05, 0.05);

    ThreeDModels objectiModel;
    objectiModel.objectCreation(l_index);

    glPopMatrix();

}

加载3ds对象并添加纹理的其他方法的方法

 void ThreeDModels::SpaceShip()
 {
    Load3DS(&objecti, "C:/Users/Documents/3DModelRendering/3DModels/Spaceship/spaceship.3ds");
    int a =     LoadBitmap2("C:/Users/Documents/3DModelRendering/3DModels/Spaceship/spaceshiptexture.bmp");
 }


void ThreeDModels::objectCreation(int l_index)
{
glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)

for (l_index = 0; l_index<objecti.polygons_qty; l_index++)
{
    //----------------- FIRST VERTEX -----------------
    // Texture coordinates of the first vertex
    glTexCoord2f(objecti.mapcoord[objecti.polygon[l_index].a].u,
        objecti.mapcoord[objecti.polygon[l_index].a].v);
    // Coordinates of the first vertex
    glVertex3f(objecti.vertex[objecti.polygon[l_index].a].x,
        objecti.vertex[objecti.polygon[l_index].a].y,
        objecti.vertex[objecti.polygon[l_index].a].z); //Vertex definition

    //----------------- SECOND VERTEX -----------------
    // Texture coordinates of the second vertex
    glTexCoord2f(objecti.mapcoord[objecti.polygon[l_index].b].u,
        objecti.mapcoord[objecti.polygon[l_index].b].v);
    // Coordinates of the second vertex
    glVertex3f(objecti.vertex[objecti.polygon[l_index].b].x,
        objecti.vertex[objecti.polygon[l_index].b].y,
        objecti.vertex[objecti.polygon[l_index].b].z);

    //----------------- THIRD VERTEX -----------------
    // Texture coordinates of the third vertex
    glTexCoord2f(objecti.mapcoord[objecti.polygon[l_index].c].u,
        objecti.mapcoord[objecti.polygon[l_index].c].v);
    // Coordinates of the Third vertex
    glVertex3f(objecti.vertex[objecti.polygon[l_index].c].x,
        objecti.vertex[objecti.polygon[l_index].c].y,
        objecti.vertex[objecti.polygon[l_index].c].z);
    }
    glEnd();
}

1 个答案:

答案 0 :(得分:-1)

我找到了答案人:

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);LoadBitmap()方法中,只删除了这个,并且有效; D