0

私は背景テクスチャに取り組んでいます。私がやりたいことは、背景画像を設定したいということです。私のコードでは、オルソに切り替え、ウィンドウサイズいっぱいの正方形を描画し、テクスチャを使用しました。その後、3D に切り替えて 3D 画像を描画します。背景テクスチャと雪だるまを描画しますが、それらはすべて 1 秒で消えます。エラーコードがどこにあるのかわかりません。私が思うに、マトリックスのプッシュとポップに何か関係がなければなりません。以下は、私の InitGl、draw() と drawsnowman()、main と reshape() のコードです。問題は、3D から 2D への切り替え中の draw() 関数にあると思います。アドバイス?

int main (int argc, char **argv) 
{
     glutInit (&argc, argv);
     glutInitDisplayMode (GLUT_DOUBLE);
     glutInitWindowSize (500, 500);
     glutInitWindowPosition (100, 100);
     glutCreateWindow ("A basic OpenGL Window");
     glutKeyboardFunc(key);

     if( !initGL() ) {                                                   // NEW (16)
        printf( "Unable to initialize graphics library!\n" );
        return 1;
    }

     glutDisplayFunc (display);
    // glutIdleFunc (display);
    glutIdleFunc(idle);
     glutReshapeFunc (reshape);

    //Load our texture
     texture = LoadTexture( "texture.bmp", 256, 256 );

    glutMainLoop ();

    //Free our texture
     FreeTexture( texture );

    return 0;
 }


void drawSnowMan(void)
{
    GLUquadricObj *pObj;
    //glDisable(GL_TEXTURE_2D);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

// save the world matrix
    glPushMatrix();

        glTranslatef(xpos, -0.5, -5.0);
        glRotated(rotX,1,0,0);                             // ******** NEW (11)
        glRotated(rotY,0,1,0);
        pObj = gluNewQuadric();
        gluQuadricNormals(pObj, GLU_SMOOTH);
    //glRotated(rotZ,0,0,1);

    glPushMatrix();
        //setting up light effect for base, mid and head spheres. all red!
        ambient[0] = 1.0; ambient[1] = 0.0; ambient[2] = 0.0;
        diffuse[0] = 1.0; diffuse[1] = 0.0; diffuse[2] = 0.0;
        specular[0] = 0.7; specular[1] = 0.6; specular[2] = 0.5;
        glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
        glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, shiness);

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        //glTranslatef(0.0 ,0.75f, 0.0f);
        //bottom sphere. dont need to gltranslate again because it will use the previous gltranslate
        //which is declared outside
        glutSolidSphere(0.70f,20,20);

        //mid sphere
        glTranslatef(0.0f, 1.0f, 0.0f);
        glutSolidSphere(0.45f,20,20);

        //top sphere
        glTranslatef(0.0f, 0.6f, 0.0f);
        glutSolidSphere(0.30f,20,20);

        //eyes

    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);
    glPopMatrix();

    //drawing hat
    glPushMatrix();
            //black color. move it 1.85 in y position because thats where the head is
            //rotate the cylinder and draw cylinder
            glColor3f(0.0f, 0.0f, 0.0f);
            glTranslatef(0.0f, 1.85f, 0.0f);
            glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
            gluCylinder(pObj, 0.17f, 0.17f, 0.4f, 26, 13);

            //drawing brim. disable cull_face. draw disk, so front part. disabl
             glDisable(GL_CULL_FACE);
            gluDisk(pObj, 0.17f, 0.28f, 26, 13);
            glEnable(GL_CULL_FACE);


            glTranslatef(0.0f, 0.0f, 0.40f);
             gluDisk(pObj, 0.17f, 0.28f, 26, 13);
    glPopMatrix();

    glPushMatrix();

        glPushMatrix();
        glColor3f(1.0,1.0,1.0);
        glTranslatef(2.0f, 1.0f, 0.0f);
        glRotatef(90.0,0.0, 0.0,-5.0);
        glScalef (0.01, 0.2, 0.06);      /* modeling transformation */
        //glutSolidCone(0.1, 0.1, 10.0, 14.0);
        glutSolidCube(1.5);

        glPopMatrix();

        glPushMatrix();

        glTranslatef(2.0f, 0.8f, 0.0f);
        glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
        gluCylinder(pObj, 0.04f, 0.04f, 0.2f, 26, 13);

        glPopMatrix();

        //blade
        glPushMatrix();
            ambient[0] = 0.0; ambient[1] = 1.0; ambient[2] = 0.0;
            diffuse[0] = 1.0; diffuse[1] = 0.0; diffuse[2] = 0.0;
            specular[0] = 0.7; specular[1] = 0.6; specular[2] = 0.5;
            glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
            glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
            glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
            glMaterialfv(GL_FRONT, GL_SHININESS, shiness);

            glEnable(GL_LIGHTING);
            glEnable(GL_LIGHT1);

            glTranslatef(2.0f, 1.0f, 0.0f);
            glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
            gluCylinder(pObj, 0.03f, 0.03f, 1.1f, 26, 13);

            glDisable(GL_LIGHTING);
            glDisable(GL_LIGHT0);
        glPopMatrix();

    glPopMatrix();

    //big push matrix for eyes, and nose
    glPushMatrix();
        //eyes color = black light. set defuse all 0, ambient = black. turns the eyes black
        /*ambient[0] = 0.0; ambient[1] = 0.0; ambient[2] = 0.0;
        diffuse[0] = 0.0; diffuse[1] = 0.0; diffuse[2] = 0.0;
        specular[0] = 0.7; specular[1] = 0.6; specular[2] = 0.5;
        glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
        glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, shiness);*/

        glColor3f(0.0, 0.0, 0.0);

        //left eye
        glPushMatrix();
            glTranslatef(-0.17, 1.7, 0.25 );
            glutSolidSphere(0.05, 10.0, 10.0);
        glPopMatrix();

        //right eye
        glPushMatrix();
            glTranslatef(0.17, 1.7, 0.25 );
            glutSolidSphere(0.05, 10.0, 10.0);
        glPopMatrix();

        //drawing nose
        glPushMatrix();
            glTranslatef(0.0, 1.6, 0.25 );
            glutSolidCone(0.08f,0.5f,10,2);
        glPopMatrix();

    glPopMatrix(); // end big push matrix for eyes and nose


glPopMatrix();

} // end of drawsnowman()


void display (void) {
     glClearColor(0.25f, 0.25f, 0.50f, 1.0f ); // blueish color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   // glLoadIdentity();

    // MODEL VIEW is set up in IniitGL
    //so save it
    glPushMatrix();
            //switch to projection matrix
            //swithc to ortho.
            //draw texture
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, -1.0);
         glDisable(GL_DEPTH_TEST);
         glDisable(GL_LIGHTING);
         glDepthMask(GL_FALSE);
         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();

         glEnable(GL_TEXTURE_2D);
         glBindTexture(GL_TEXTURE_2D, texture);


// Draw a textured quad
        glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
        glTexCoord2f(0, 1); glVertex3f(0, 1, 0);
        glTexCoord2f(1, 1); glVertex3f(1, 1, 0);
        glTexCoord2f(1, 0); glVertex3f(1, 0, 0);
        glEnd();


        glDepthMask(GL_TRUE);
        glEnable(GL_DEPTH_TEST);

    glPopMatrix();// pop the 3d model view
    glMatrixMode(GL_PROJECTION);
        glMatrixMode(GL_MODELVIEW);


// You can ignore this. just a different of way drawing texture and still does not work
   /* glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, -1.0);


    glDisable(GL_DEPTH_TEST);
 glDisable(GL_LIGHTING);
 glDepthMask(GL_FALSE);
    glMatrixMode(GL_MODELVIEW);


   glPushMatrix();

   glDepthMask( false );
    glLoadIdentity();


    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);


// Draw a textured quad
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3f(0, 1, 0);
    glTexCoord2f(1, 1); glVertex3f(1, 1, 0);
    glTexCoord2f(1, 0); glVertex3f(1, 0, 0);
    glEnd();


    glDepthMask(GL_TRUE);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    /*glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective (60, 800 / 600, 1.0, 100.0);;
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(  0.0, 0.0, 0.0,  // Where would the camera be?
                0.0, 0.0,-1.0,  // Where would it be looking?
                0.0, 1.0, 0.0); // What would be the "up" vector?fa*/


   /* glDisable(GL_TEXTURE_2D);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    //gluPerspective (45, SCREEN_WIDTH / SCREEN_HEIGHT, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();*/

      drawSnowMan(); // draw snow man


     glutSwapBuffers();
     angle ++;
 }

 void reshape (int w, int h) {
     glViewport (0, 0, (GLsizei)w, (GLsizei)h);
     glMatrixMode (GL_PROJECTION);
     glLoadIdentity ();
     gluPerspective (45, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
     glMatrixMode (GL_MODELVIEW);
 }

bool initGL (void) {

    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    // Switch to the "camera" mode
    glMatrixMode(GL_PROJECTION);  // Camera
    glLoadIdentity();

    // Change the camera to a 3D view
    glFrustum( -1 * (float) SCREEN_WIDTH / SCREEN_HEIGHT,
                    (float) SCREEN_WIDTH / SCREEN_HEIGHT,
                    -1.0,
                     1.0,
                     1.5,
                     1000.0);

   glClearColor(0.25f, 0.25f, 0.50f, 1.0f );

    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);                         // NEW (5)
    glEnable(GL_LINE_SMOOTH);                                       // NEW (6)

    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);                      // NEW (7)
    glEnable(GL_POLYGON_SMOOTH);                                    // NEW (8)

    // this is a specular light
    GLfloat mat_specular[] = { 1.0 , 1.0 , 1.0 , 1.0 };    // Color of a "shiny material
    GLfloat mat_shininess[] = { 50.0 };                 // How shiny is it?
   // GLfloat mat_ambient_and_diffuse[] = { 0.0, 1.0, 0.0, 1.0 };

    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };  // Infinitely far away. Direction light
    GLfloat light_position1[] = {2.0,1.5,0.0,1.0}; // saber light


    // How to calculate the surface normal for pixels
    glShadeModel(GL_SMOOTH); // Try this as well GL_FLAT

    // Setup up some material reflective properties
   // glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   // glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    //glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_and_diffuse);
    //glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient_and_diffuse);

    // Finally actually make the light
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT1, GL_POSITION, light_position1);

    //Check for error
    GLenum error = glGetError();                                    // NEW (9)
    if( error != GL_NO_ERROR ) {
        printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
        return false;
    }

    return true;
}

void idle(void) {
    glutPostRedisplay();
}
4

2 に答える 2

1
于 2013-03-29T13:09:51.557 に答える
0

背景テクスチャが消え続ける理由が少しわかったと思います。Cullface と depth の何かをオンにしました。最後のマトリックスをポップする前にオフにしましたが、機能します。

ただし、問題が 1 つあります。テクスチャ画像は背景が雪なのですが、真っ赤になってしまいます。だから最初はスノーガールの照明が反射したせいだと思った。私はすべてのライトを消しましたが、まだ赤です。理由がわかりません。雪だるま本来の色にテクスチャーの色が溶け込んでいるのではないでしょうか?

于 2013-03-29T16:33:54.347 に答える