OpenGL之旋转、平移、缩放

旋转:glRotatef(angle,x,y,z)
(x,y,z)为轴
平移:glTranslatef(x,y,z)
(x,y,z)为平移向量
在z轴上平移有缩放的味道
缩放:glScalef(x,y,z)
(x,y,z)为各方向上放大倍数

#include 
#include 
#include 
#include 
#define GLUT_DISABLE_ATEXIT_HACK

GLfloat AngleX;
GLfloat AngleY;
GLfloat sca[3] = {1,1,1};
GLfloat tra[3] = {0,0,0};

void display1(){                    //sphere,cone
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    {
        glRotatef(AngleX, 1.0f, 0.0f, 0.0f);
        glRotated(AngleY, 0.0f, 1.0f, 0.0f);
        glScalef(sca[0], sca[1], sca[2]);
        glTranslatef(tra[0], tra[1], tra[2]);
        glutWireSphere(36, 25, 25);
        glutWireCone(25, 20, 30, 30);
    }
    glPopMatrix();

    glutSwapBuffers();
}

void display2(){                //cube
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    {
        glRotatef(AngleX, 1.0f, 0.0f, 0.0f);
        glRotated(AngleY, 0.0f, 1.0f, 0.0f);
        glScalef(sca[0], sca[1], sca[2]);
        glTranslatef(tra[0], tra[1], tra[2]);

        glBegin(GL_POLYGON);//FRONT
        glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);//white
        glVertex3f(50.0f, 50.0f, 50.0f);

        glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);//yellow
        glVertex3f(50.0f, -50.0f, 50.0f);

        glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)0);//red
        glVertex3f(-50.0f, -50.0f, 50.0f);

        glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);//white
        glVertex3f(-50.0f, 50.0f, 50.0f);
        glEnd();



        glBegin(GL_POLYGON);//BACK
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(50.0f, 50.0f, -50.0f);//青色

        glColor3f(0.0f, 1.0f, 0.0f);//绿
        glVertex3f(50.0f, -50.0f, -50.0f);

        glColor3f(0.0f, 0.0f, 0.0f);//黑
        glVertex3f(-50.0f, -50.0f, -50.0f);

        glColor3f(0.0f, 0.0f, 1.0f);//蓝
        glVertex3f(-50.0f, 50.0f, -50.0f);
        glEnd();




        glBegin(GL_POLYGON);//UP
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(50.0f, 50.0f, -50.0f);

        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex3f(50.0f, 50.0f, 50.0f);

        glColor3f(1.0f, 0.0f, 1.0f);
        glVertex3f(-50.0f, 50.0f, 50.0f);

        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-50.0f, 50.0f, -50.0f);
        glEnd();





        glBegin(GL_POLYGON);//DOWN
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(50.0f, -50.0f, -50.0f);

        glColor3f(1.0f, 1.0f, 0.0f);
        glVertex3f(50.0f, -50.0f, 50.0f);

        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-50.0f, -50.0f, 50.0f);

        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-50.0f, -50.0f, -50.0f);
        glEnd();




        glBegin(GL_POLYGON);//LEFT
        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex3f(50.0f, 50.0f, 50.0f);

        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(50.0f, 50.0f, -50.0f);

        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(50.0f, -50.0f, -50.0f);

        glColor3f(1.0f, 1.0f, 0.0f);
        glVertex3f(50.0f, -50.0f, 50.0f);
        glEnd();




        glBegin(GL_POLYGON);//RIGHT
        glColor3f(1.0f, 0.0f, 1.0f);
        glVertex3f(-50.0f, 50.0f, 50.0f);

        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-50.0f, 50.0f, -50.0f);

        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-50.0f, -50.0f, -50.0f);

        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-50.0f, -50.0f, 50.0f);
        glEnd();



    }

    glPopMatrix();



    glutSwapBuffers();


}  //cube


void reshape(int w, int h){
    GLfloat aspect = (GLfloat)w / (GLfloat)h;
    GLfloat nRange = 100.0f;

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    if (w <= h){
        glOrtho(-nRange, nRange, -nRange*aspect, nRange*aspect, -nRange, nRange);

    }
    else
        glOrtho(-nRange, nRange, -nRange / aspect, nRange / aspect, -nRange, nRange);

}

void Skeyboard(GLint key, GLint x, GLint y){   //control Rotate
    if (key == GLUT_KEY_UP)
        AngleX -= 5.0f;
    if (key == GLUT_KEY_DOWN)
        AngleX += 5.0f;
    if (key == GLUT_KEY_LEFT)
        AngleY -= 5.0f;
    if (key == GLUT_KEY_RIGHT)
        AngleY += 5.0f;

    if (AngleX > 355.0f)
        AngleX = 0.0f;
    if (AngleX < 0.0f)
        AngleX = 355.0f;

    glutPostRedisplay();
}

void Nkeyboard(unsigned char key, int x, int y){   //control scale and translate
    if (key == 'i'){
        for (int i = 0; i < 3; i++)
            sca[i]++;
    }
    if (key == 'k'){
        for (int i = 0; i < 3; i++)
            sca[i]--;
    }
    if (key == 'w'){
        tra[1]++;
    }
    if (key == 's'){
        tra[1]--;
    }
    if (key == 'a'){
        tra[0]--;
    }
    if (key == 'd'){
        tra[0]++;
    }
    if (key = 'q'){
        tra[2]++;
    }
    if (key = 'e'){
        tra[2]--;
    }

    glutPostRedisplay();
}


void init(){
    AngleX = 45.0f;
    AngleY = 315.f;

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_DITHER);
    glShadeModel(GL_SMOOTH);

}

int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(800, 600);
    glutCreateWindow("3D");

    glutReshapeFunc(reshape);
    //glutDisplayFunc(display2);
    glutDisplayFunc(display1);
    glutSpecialFunc(Skeyboard);
    glutKeyboardFunc(Nkeyboard);

    init();
    glutMainLoop();
    return 0;
}

你可能感兴趣的:(OpenGL)