opengl纹理glTexCoord2f无法正常工作

时间:2016-04-06 15:45:35

标签: opengl glut

我是opengl的新手。我对glTexCoord2f很困惑。我不知道如何提供坐标。我只知道坐标应该是(0,0)(0,1)(1,0)和(1,1)。当我在平面上提供纹理(这里的平面是用于门)时,它不会显示bmp图像正确..我已经尝试过随机改变坐标,但图像的形状发生了变化,但它并没有直接显示图像。显示图像的坐标是什么(直的垂直门)。

  glEnable(GL_TEXTURE_2D);{
    glBindTexture(GL_TEXTURE_2D, main_door);
    glBegin(GL_QUADS);{
        glTexCoord2f(0,0);
        glVertex3f(-400,0,0);
        glTexCoord2f(1,0);
        glVertex3f(400,0,0);
        glTexCoord2f(1,1);
        glVertex3f(400,0,1000);
        glTexCoord2f(1,0);
        glVertex3f(-400,0,1000);
    }glEnd();
}
glDisable(GL_TEXTURE_2D);

我的完整代码

     #include<stdio.h>
     #include<stdlib.h>
    #include<math.h>
      #include<windows.h> 
     #include<GL/glut.h>

     double cameraAngle;
     double move_X, move_Y, move_Z;
    int canDrawGrid, canDrawAxis,canDrawBase;

     double cameraRadius, cameraHeight, cameraAngleDelta;
     int num_texture  = -1;
     GLuint grassimg,md1img,brick_d123,dome_top,dome_t_d,dome_t_d_2,floorimg,sideimg,main_door;


     int LoadBitmapImage(char *filename)
     {
    int i, j=0;
    FILE *l_file;
    unsigned char *l_texture;

    BITMAPFILEHEADER fileheader;
    BITMAPINFOHEADER infoheader;
    RGBTRIPLE rgb;

    num_texture++;

    if( (l_file = fopen(filename, "rb"))==NULL) return (-1);

    fread(&fileheader, sizeof(fileheader), 1, l_file);

    fseek(l_file, sizeof(fileheader), SEEK_SET);
    fread(&infoheader, sizeof(infoheader), 1, l_file);

    l_texture = (byte *) malloc(infoheader.biWidth * infoheader.biHeight * 4);
    memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);
    for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
        {
                fread(&rgb, sizeof(rgb), 1, l_file);

                l_texture[j+0] = rgb.rgbtRed;
                l_texture[j+1] = rgb.rgbtGreen;
                l_texture[j+2] = rgb.rgbtBlue;
                l_texture[j+3] = 255;
                j += 4;
        }
    fclose(l_file);

    glBindTexture(GL_TEXTURE_2D, num_texture);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

    free(l_texture);

    return (num_texture);

}

void loadImage()
{
    grassimg = LoadBitmapImage("image/brick.bmp");
    brick_d123 = LoadBitmapImage("image/brick_d123.bmp");
    md1img = LoadBitmapImage("image/m_d_1.bmp");
        dome_top = LoadBitmapImage("image/dome_top.bmp");
        dome_t_d = LoadBitmapImage("image/dome_t_d.bmp");
        dome_t_d_2 = LoadBitmapImage("image/dome_t_d_2.bmp");
        floorimg = LoadBitmapImage("image/floor.bmp");
        sideimg = LoadBitmapImage("image/side_1.bmp");
        main_door = LoadBitmapImage("image/main_door.bmp");
    printf("Load successful");
}


void choto_gombuj(double tx,double ty, double tz,double deg,double axisX,double axisY,double axisZ,double sx,double sy, double sz){
    glPushMatrix();{
        glTranslatef(tx,ty,tz);
        glRotatef(deg,axisX,axisY,axisZ);
        glScalef(sx,sx,sz);
        //CG_base_cyclindeer
        GLUquadricObj *obj = gluNewQuadric();
        gluQuadricTexture(obj,GL_TRUE);

        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, brick_d123);
        gluCylinder(obj,190,190,60,20,20);
        }
        glDisable(GL_TEXTURE_2D);


        //CG_base_base_dakna
        glColor3f(0,0,0);

        glPushMatrix();{
                glTranslatef(0,0,59);
                glutSolidCone(189,1,20,20);
        }glPopMatrix();
        //CG_Cylinder_pillar_8
        glPushMatrix();{
        glTranslatef(120,120,59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, grassimg);
            gluCylinder(obj,12,12,220,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();


        glPushMatrix();{
        glTranslatef(170,0,59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, grassimg);
            gluCylinder(obj,12,12,220,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();



            glPushMatrix();{
        glTranslatef(120,-120,59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, grassimg);
            gluCylinder(obj,12,12,220,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();

        glPushMatrix();{
            glTranslatef(0,-170,59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, grassimg);
            gluCylinder(obj,12,12,220,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();

        glPushMatrix();{
            glTranslatef(-120,-120,59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, grassimg);
            gluCylinder(obj,12,12,220,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();

        glPushMatrix();{
            glTranslatef(-170,0,59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, grassimg);
            gluCylinder(obj,12,12,220,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();

        glPushMatrix();{
            glTranslatef(-120,120,59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, grassimg);
            gluCylinder(obj,12,12,220,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();

        glPushMatrix();{
            glTranslatef(0,170,59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, grassimg);
            gluCylinder(obj,12,12,220,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();



        glPushMatrix();{
            glTranslatef(0,0,220+59);
        glColor3f(1,1,1);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, md1img);
            gluCylinder(obj,220,0,60,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();




        // main_dome
        glPushMatrix();{
        glColor3f(1,1,1);
        glTranslatef(0,0,440);

        glPushMatrix();{
            glTranslatef(0,0,150);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, dome_t_d);
            GLUquadricObj *obj = gluNewQuadric();
            gluQuadricTexture(obj,GL_TRUE);
                gluCylinder(obj,160,0,130,20,20);
        }
        glDisable(GL_TEXTURE_2D);

        }glPopMatrix();

        double equ[4];
        equ[0]=0;
        equ[1]=0;
        equ[2]=1;
        equ[3]=150;

        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, dome_top);
            GLUquadricObj *obj = gluNewQuadric();
            gluQuadricTexture(obj,GL_TRUE);
            glClipPlane(GL_CLIP_PLANE0,equ);
            glEnable(GL_CLIP_PLANE0);{
                gluSphere(obj,220,20,20);
            }glDisable(GL_CLIP_PLANE0);
        }
        glDisable(GL_TEXTURE_2D);



    }glPopMatrix();


        //dome_top_balls
        glPushMatrix();{
                glTranslatef(0,0,740);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, dome_t_d_2);
            GLUquadricObj *obj = gluNewQuadric();
            gluQuadricTexture(obj,GL_TRUE);
                gluSphere(obj,40,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();


        glPushMatrix();{
                glTranslatef(0,0,810);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, dome_t_d_2);
            GLUquadricObj *obj = gluNewQuadric();
            gluQuadricTexture(obj,GL_TRUE);
                gluSphere(obj,35,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();



        glPushMatrix();{
                glTranslatef(0,0,865);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, dome_t_d_2);
            GLUquadricObj *obj = gluNewQuadric();
            gluQuadricTexture(obj,GL_TRUE);
                gluSphere(obj,30,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();

        glPushMatrix();{
                glTranslatef(0,0,910);
        glEnable(GL_TEXTURE_2D);{
            glBindTexture(GL_TEXTURE_2D, dome_t_d_2);
            GLUquadricObj *obj = gluNewQuadric();
            gluQuadricTexture(obj,GL_TRUE);
                gluSphere(obj,25,20,20);
        }
        glDisable(GL_TEXTURE_2D);
        }glPopMatrix();




}glPopMatrix();
}

void minar(double tx,double ty, double tz,double deg,double axisX,double axisY,double axisZ){
    glPushMatrix();{
        glTranslatef(tx,ty,tz);
        glRotatef(deg,axisX,axisY,axisZ);
    //Minar_base
    GLUquadricObj *obj = gluNewQuadric();
        glColor3f(0,0,0);
        glPushMatrix();{
            gluCylinder(obj,150,140,240,20,20);
        }glPopMatrix();
    //Minar_base_base_dakna
    glColor3f(1,0,0);
    glPushMatrix();{
            glTranslatef(0,0,240);
            glutSolidCone(142,1,20,20);
    }glPopMatrix();
    //Minar_main_pillar

    glColor3f(1,1,1);
    glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, grassimg);
        GLUquadricObj *obj = gluNewQuadric();
        gluQuadricTexture(obj,GL_TRUE);
        gluCylinder(obj,105,70,1500,20,20);
    }
    glDisable(GL_TEXTURE_2D);

    //Minar_design_3_plate_1
    glPushMatrix();{
        glTranslatef(0,0,600);
        glRotatef(180,0,1,0);
        glColor3f(1,1,1);

        glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, md1img);
        GLUquadricObj *obj = gluNewQuadric();
        gluQuadricTexture(obj,GL_TRUE);
        gluCylinder(obj,135,0,100,20,20);
        }
        glDisable(GL_TEXTURE_2D);

        glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, brick_d123);
        GLUquadricObj *obj = gluNewQuadric();
        gluQuadricTexture(obj,GL_TRUE);
        gluCylinder(obj,135,0,1,20,20);
        }
        glDisable(GL_TEXTURE_2D);


    }glPopMatrix();
    ////Minar_design_3_plate_2
    glPushMatrix();{
        glTranslatef(0,0,1050);
        glColor3f(1,1,1);
        glRotatef(180,0,1,0);
        glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, md1img);
        GLUquadricObj *obj = gluNewQuadric();
        gluQuadricTexture(obj,GL_TRUE);
        gluCylinder(obj,120,0,100,20,20);
        }
        glDisable(GL_TEXTURE_2D);

        glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, brick_d123);
        GLUquadricObj *obj = gluNewQuadric();
        gluQuadricTexture(obj,GL_TRUE);
        gluCylinder(obj,120,0,1,20,20);
        }
        glDisable(GL_TEXTURE_2D);

    }glPopMatrix();
    ////Minar__design_3_plate_3
    glPushMatrix();{
        glTranslatef(0,0,1500);
        glColor3f(1,1,1);
        glRotatef(180,0,1,0);
        glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, md1img);
        GLUquadricObj *obj = gluNewQuadric();
        gluQuadricTexture(obj,GL_TRUE);
        gluCylinder(obj,105,0,100,20,20);
        }
        glDisable(GL_TEXTURE_2D);

        glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, brick_d123);
        GLUquadricObj *obj = gluNewQuadric();
        gluQuadricTexture(obj,GL_TRUE);
        gluCylinder(obj,105,0,1,20,20);
        }
        glDisable(GL_TEXTURE_2D);


    }glPopMatrix();
    choto_gombuj(0,0,1500,0,0,0,0,0.4,0.4,0.4);
}glPopMatrix();
}


void display(){
    //codes for Models, Camera

    //clear the display
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0,0,0,0);  //color black
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     //clear buffers to preset values

    /***************************
    / set-up camera (view) here
    ****************************/ 
    //load the correct matrix -- MODEL-VIEW matrix
    glMatrixMode(GL_MODELVIEW);     //specify which matrix is the current matrix

    //initialize the matrix
    glLoadIdentity();               //replace the current matrix with the identity matrix [Diagonals have 1, others have 0]

    //now give three info
    //1. where is the camera (viewer)?
    //2. where is the camera looking?
    //3. Which direction is the camera's UP direction?

    //gluLookAt(0,-150,20,  0,0,0,  0,0,1);
    gluLookAt(cameraRadius*sin(cameraAngle), -cameraRadius*cos(cameraAngle), cameraHeight,      0,0,0,      0,0,1);

    //again select MODEL-VIEW
    glMatrixMode(GL_MODELVIEW);


    /**************************************************
    / Grid and axes Lines(You can remove them if u want)
    ***************************************************/
    // draw the three major AXES

    if(canDrawAxis==1){
        glBegin(GL_LINES);
            //X axis
            glColor3f(0, 1, 0); //100% Green
            glVertex3f(-4000, 0, 0);
            glVertex3f( 4000, 0, 0);

            //Y axis
            glColor3f(0, 0, 1); //100% Blue
            glVertex3f(0, -4000, 0);    // intentionally extended to -150 to 150, no big deal
            glVertex3f(0,  4000, 0);

            //Z axis
            glColor3f(1, 1, 1); //100% White
            glVertex3f( 0, 0, -4000);
            glVertex3f(0, 0, 4000);
        glEnd();
    }

    if(canDrawGrid == 1){
    //some gridlines along the field
        int i;

        glColor3f(0.5, 0.5, 0.5);   //grey
        glBegin(GL_LINES);
            for(i=-160;i<=160;i++){

                if(i==0)
                    continue;   //SKIP the MAIN axes

                //lines parallel to Y-axis
                glVertex3f(i*20, -3600, 0);
                glVertex3f(i*20,  3600, 0);

                //lines parallel to X-axis
                glVertex3f(-3600, i*20, 0);
                glVertex3f( 3600, i*20, 0);
            }
        glEnd();

    }



    /****************************
    / Add your objects from here
    ****************************/

    if(canDrawBase == 1){

        //base_floor
        glColor3f(0,1,0);  
        glBegin(GL_QUADS);{
            glVertex3f(-3600,3600,0);
            glVertex3f(3600,3600,0);
            glVertex3f(3600,-3600,0);
            glVertex3f(-3600,-3600,0);
        }glEnd();
    }
    //taj_base
    glColor3f(1,1,1);
    //right_base_wall
    glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, sideimg);
        glBegin(GL_QUADS);{
            glColor3f(1,1,1);
            glTexCoord2f(0,1);
            glVertex3f(2000,2000,0);
            glTexCoord2f(0,0);
            glVertex3f(2000,-2000,0);
            glTexCoord2f(1,0);
            glVertex3f(2000,-2000,240);
            glTexCoord2f(1,1);
            glVertex3f(2000,2000,240);
        }glEnd();
    }
    glDisable(GL_TEXTURE_2D);

    glColor3f(1,1,1);
    //left_base_wall
    glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, sideimg);
        glBegin(GL_QUADS);{
            glColor3f(1,1,1);
            glTexCoord2f(0,1);
            glVertex3f(-2000,2000,0);
            glTexCoord2f(0,0);
            glVertex3f(-2000,-2000,0);
            glTexCoord2f(1,0);
            glVertex3f(-2000,-2000,240);
            glTexCoord2f(1,1);
            glVertex3f(-2000,2000,240);
        }glEnd();
    }
    glDisable(GL_TEXTURE_2D);


    glColor3f(1,1,1);
    //back_base_wall
    glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, sideimg);
        glBegin(GL_QUADS);{
            glColor3f(1,1,1);
            glTexCoord2f(0,1);
            glVertex3f(-2000,2000,0);
            glTexCoord2f(0,0);
            glVertex3f(2000,2000,0);
            glTexCoord2f(1,0);
            glVertex3f(2000,2000,240);
            glTexCoord2f(1,1);
            glVertex3f(-2000,2000,240);
        }glEnd();
    }
    glDisable(GL_TEXTURE_2D);


    glColor3f(1,1,1);
        //font__base_wall
    glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, sideimg);
        glBegin(GL_QUADS);{
            glColor3f(1,1,1);
            glTexCoord2f(0,1);
            glVertex3f(-2000,-2000,0);
            glTexCoord2f(0,0);
            glVertex3f(2000,-2000,0);
            glTexCoord2f(1,0);
            glVertex3f(2000,-2000,240);
            glTexCoord2f(1,1);
            glVertex3f(-2000,-2000,240);
        }glEnd();
    }
    glDisable(GL_TEXTURE_2D);



    glColor3f(1,1,1);
        //font_top_wall
    glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, floorimg);
        glBegin(GL_QUADS);{
            glColor3f(1,1,1);
            glTexCoord2f(0,1);
            glVertex3f(-2000,2000,240);
            glTexCoord2f(0,0);
            glVertex3f(2000,2000,240);
            glTexCoord2f(1,0);
            glVertex3f(2000,-2000,240);
            glTexCoord2f(1,1);
            glVertex3f(-2000,-2000,240);
        }glEnd();
    }
    glDisable(GL_TEXTURE_2D);










    /*//taj_base_shiri_cube
    glColor3f(1,0,0);
    glPushMatrix();{
            glTranslatef(0,-2050,120);
            //glRotatef(-16,1,0,0);
            glScalef(500,100,240);
            glutSolidCube(1);
        }glPopMatrix();

    */


    minar(-1950,-1950,0,0,0,0,0);
    minar(1950,-1950,0,0,0,0,0);
    minar(1950,1950,0,0,0,0,0);
    minar(-1950,1950,0,0,0,0,0);


    //taj_main_building
        glColor3f(1,1,1);
        glPushMatrix();{
            glTranslatef(0,-1200,240);
    glEnable(GL_TEXTURE_2D);{
        glBindTexture(GL_TEXTURE_2D, main_door);
        glBegin(GL_QUADS);{
            glTexCoord2f(0,0);
            glVertex3f(-400,0,0);
            glTexCoord2f(1,0);
            glVertex3f(400,0,0);
            glTexCoord2f(1,1);
            glVertex3f(400,0,1000);
            glTexCoord2f(1,1);
            glVertex3f(-400,0,1000);
        }glEnd();
    }
    glDisable(GL_TEXTURE_2D);
        }glPopMatrix();



    //ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
    glutSwapBuffers();
}

void animate(){
    //codes for any changes in Models, Camera

    //cameraAngle += cameraAngleDelta;  // camera will rotate at 0.002 radians per frame.

    //codes for any changes in Models

    //MISSING SOMETHING? -- YES: add the following
    glutPostRedisplay();    //this will call the display AGAIN

}

void init(){
    //codes for initialization
    loadImage();
    cameraAngle = 0;    //angle in radian
    move_X = 0;
    move_Y = 0;
    move_Z = 0;
    canDrawGrid = 1;
    canDrawAxis = 1;
    canDrawBase = 1;
    cameraAngleDelta = .001;

    cameraRadius = 4000;
    cameraHeight = 500;

    //clear the screen
    glClearColor(0,0,0, 0);

    /************************
    / set-up projection here
    ************************/
    //load the PROJECTION matrix
    glMatrixMode(GL_PROJECTION);

    //initialize the matrix
    glLoadIdentity();

    /*
        gluPerspective() — set up a perspective projection matrix

        fovy -         Specifies the field of view angle, in degrees, in the y direction.
        aspect ratio - Specifies the aspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).
        zNear -        Specifies the distance from the viewer to the near clipping plane (always positive).
        zFar  -        Specifies the distance from the viewer to the far clipping plane (always positive).
    */

    gluPerspective(70,  1,  0.1,    10000.0);

}

void keyboardListener(unsigned char key, int x,int y){
    switch(key){

        case '1':
            move_X += 1;
            break;

        case '2':   
            move_X -= 1;
            break;

        case '3':   
            move_Y += 1;
            break;
        case '4':   
            move_Y -= 1;
            break;

        case '5':   
            move_Z += 1;
            break;
        case '6':   
            move_Z -= 1;
            break;
        case '8':   

            break;

        case 'g':
            canDrawGrid ^= 1;
            break;

        case 'h':
            canDrawAxis ^= 1;
            break;

        case 'p':
            break;

        case 'z':
            canDrawBase ^= 1;
            break;

        default:
            break;
    }
}

void specialKeyListener(int key, int x,int y){
    switch(key){
        case GLUT_KEY_DOWN:     //down arrow key
            cameraRadius += 40;
            break;
        case GLUT_KEY_UP:       // up arrow key
            if(cameraRadius > 40)
                cameraRadius -= 40;
            break;

        case GLUT_KEY_RIGHT:
            cameraAngle += 0.05;
            break;
        case GLUT_KEY_LEFT:
            cameraAngle -= 0.05;
            break;

        case GLUT_KEY_PAGE_UP:
            cameraHeight += 40;
            break;
        case GLUT_KEY_PAGE_DOWN:
            cameraHeight -= 40;
            break;

        case GLUT_KEY_INSERT:
            break;

        case GLUT_KEY_HOME:
        //  cameraAngleDelta = 0.001; 
            break;
        case GLUT_KEY_END:
        //  cameraAngleDelta = 0;
            break;

        default:
            break;
    }
}

void mouseListener(int button, int state, int x, int y){    //x, y is the x-y of the screen (2D)
    switch(button){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){     // 2 times?? in ONE click? -- solution is checking DOWN or UP
                cameraAngleDelta = -cameraAngleDelta;   
            }
            break;

        case GLUT_RIGHT_BUTTON:
            if(cameraRadius > 40)
                cameraRadius -= 40;
            break;

        case GLUT_MIDDLE_BUTTON:
            //........
            break;

        default:
            break;
    }
}

int main(int argc, char **argv){

    glutInit(&argc,argv);                           //initialize the GLUT library

    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);

    /*
        glutInitDisplayMode - inits display mode
        GLUT_DOUBLE - allows for display on the double buffer window
        GLUT_RGBA - shows color (Red, green, blue) and an alpha
        GLUT_DEPTH - allows for depth buffer
    */
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);   

    glutCreateWindow("Some Title");

    init();                     //codes for initialization

    glEnable(GL_DEPTH_TEST);    //enable Depth Testing

    glutDisplayFunc(display);   //display callback function
    glutIdleFunc(animate);      //what you want to do in the idle time (when no drawing is occuring)

    glutKeyboardFunc(keyboardListener);
    glutSpecialFunc(specialKeyListener);

    glutMouseFunc(mouseListener);

    glutMainLoop();     //The main loop of OpenGL

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的位图有哪些尺寸?

在使用RGB图像格式时,有很多人会措手不及,即在GL中默认情况下,像素行预计会以4字节边界开始。

根据你的描述,你的纹理坐标根本就不应该受到责备。

GL首先读取的实际数据被误解,它从错误的数据位置拉出纹理的每一行。如果您的位图宽度不是4的倍数,那么在解包传输给它的像素数据时,您需要告诉GL使用不同的规则。

通常,不兼容的纹理宽度可以按如下方式进行修正:

glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

非常重要的是要注意DIB(.bmp)图像已满足OpenGL期望的4字节行对齐规则。如果您正确阅读了.bmp图片,则首先不会出现此问题。

你还没有补偿每行末尾的填充,这个填充已经存在于宽度不是4的倍数的.bmp文件中。这需要先完成并生成GL命令以上不必要的。