⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transformation.c

📁 OpenGL的大海量资料收集
💻 C
📖 第 1 页 / 共 2 页
字号:
world_display(void){    GLfloat pos[] = { 0.0, 0.0, 1.0, 0.0 };    double length;    float l[3];        l[0] = at[0] - eye[0];     l[1] = at[0] - eye[1];     l[2] = at[0] - eye[2];    length = normalize(l);        invert(modelview, inverse);        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        if (world_draw) {        glEnable(GL_LIGHTING);        glPushMatrix();        glMultMatrixd(inverse);        glLightfv(GL_LIGHT0, GL_POSITION, pos);        glPopMatrix();        drawmodel();        glDisable(GL_LIGHTING);    }        glPushMatrix();        glMultMatrixd(inverse);        glLightfv(GL_LIGHT0, GL_POSITION, pos);        /* draw the axis and eye vector */    glPushMatrix();    glColor3ub(0, 0, 255);    glBegin(GL_LINE_STRIP);    glVertex3f(0.0, 0.0, 0.0);    glVertex3f(0.0, 0.0, -1.0*length);    glVertex3f(0.1, 0.0, -0.9*length);    glVertex3f(-0.1, 0.0, -0.9*length);    glVertex3f(0.0, 0.0, -1.0*length);    glVertex3f(0.0, 0.1, -0.9*length);    glVertex3f(0.0, -0.1, -0.9*length);    glVertex3f(0.0, 0.0, -1.0*length);    glEnd();    glColor3ub(255, 255, 0);    glRasterPos3f(0.0, 0.0, -1.1*length);    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'e');    glColor3ub(255, 0, 0);    glScalef(0.4, 0.4, 0.4);    drawaxes();    glPopMatrix();        invert(projection, inverse);    glMultMatrixd(inverse);        /* draw the viewing frustum */    glColor3f(0.2, 0.2, 0.2);    glBegin(GL_QUADS);    glVertex3i(1, 1, 1);    glVertex3i(-1, 1, 1);    glVertex3i(-1, -1, 1);    glVertex3i(1, -1, 1);    glEnd();        glColor3ub(128, 196, 128);    glBegin(GL_LINES);    glVertex3i(1, 1, -1);    glVertex3i(1, 1, 1);    glVertex3i(-1, 1, -1);    glVertex3i(-1, 1, 1);    glVertex3i(-1, -1, -1);    glVertex3i(-1, -1, 1);    glVertex3i(1, -1, -1);    glVertex3i(1, -1, 1);    glEnd();        glEnable(GL_BLEND);    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);    glColor4f(0.2, 0.2, 0.4, 0.5);    glBegin(GL_QUADS);    glVertex3i(1, 1, -1);    glVertex3i(-1, 1, -1);    glVertex3i(-1, -1, -1);    glVertex3i(1, -1, -1);    glEnd();    glDisable(GL_BLEND);        glPopMatrix();    glutSwapBuffers();}voidworld_menu(int value){    switch (value) {    case 'm':        world_draw = !world_draw;        break;    }    redisplay_all();}voidscreen_reshape(int width, int height){    glViewport(0, 0, width, height);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluPerspective(60.0, (float)width/height, 0.5, 8.0);    glGetDoublev(GL_PROJECTION_MATRIX, projection);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    gluLookAt(eye[0], eye[1], eye[2], at[0], at[1], at[2], up[0], up[1],up[2]);    if (swapped) {        glRotatef(rotation[0].value, rotation[1].value,             rotation[2].value, rotation[3].value);        glTranslatef(translation[0].value, translation[1].value,            translation[2].value);    } else {        glTranslatef(translation[0].value, translation[1].value,            translation[2].value);        glRotatef(rotation[0].value, rotation[1].value,             rotation[2].value, rotation[3].value);    }    glScalef(scale[0].value, scale[1].value, scale[2].value);    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);    glClearColor(0.2, 0.2, 0.2, 0.0);    glEnable(GL_DEPTH_TEST);    glEnable(GL_LIGHTING);    glEnable(GL_LIGHT0);}voidscreen_display(void){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    drawmodel();    glutSwapBuffers();}voidscreen_menu(int value){    char* name = 0;        switch (value) {    case 'a':        name = "data/al.obj";        break;    case 's':        name = "data/soccerball.obj";        break;    case 'd':        name = "data/dolphins.obj";        break;    case 'f':        name = "data/flowers.obj";        break;    case 'j':        name = "data/f-16.obj";        break;    case 'p':        name = "data/porsche.obj";        break;    case 'r':        name = "data/rose+vase.obj";        break;    }        if (name) {        pmodel = glmReadOBJ(name);        if (!pmodel) exit(0);        glmUnitize(pmodel);        glmFacetNormals(pmodel);        glmVertexNormals(pmodel, 90.0);    }        redisplay_all();}voidcommand_reshape(int width, int height){    glViewport(0, 0, width, height);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluOrtho2D(0, width, height, 0);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    glClearColor(0.0, 0.0, 0.0, 0.0);}voidcommand_display(void){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        if (swapped) {        translation[0].y = 80;        translation[1].y = 80;        translation[2].y = 80;        rotation[0].y = 40;        rotation[1].y = 40;        rotation[2].y = 40;        rotation[3].y = 40;    } else {        translation[0].y = 40;        translation[1].y = 40;        translation[2].y = 40;        rotation[0].y = 80;        rotation[1].y = 80;        rotation[2].y = 80;        rotation[3].y = 80;    }        glColor3ub(255, 255, 255);    setfont("helvetica", 18);    drawstr(30, rotation[0].y, "glRotatef(");    drawstr(170, rotation[0].y, ",");    drawstr(230, rotation[0].y, ",");     drawstr(290, rotation[0].y, ",");    drawstr(350, rotation[0].y, ");");    drawstr(10, translation[0].y, "glTranslatef(");    drawstr(170, translation[0].y, ",");    drawstr(230, translation[0].y, ",");     drawstr(290, translation[0].y, ");");    drawstr(38, 120, "glScalef(");    drawstr(170, 120, ",");    drawstr(230, 120, ",");     drawstr(290, 120, ");");    drawstr(41, 160, "glBegin(  . . .  );");    drawstr(41, 200, " . . .");        cell_draw(&translation[0]);    cell_draw(&translation[1]);    cell_draw(&translation[2]);        cell_draw(&rotation[0]);    cell_draw(&rotation[1]);    cell_draw(&rotation[2]);    cell_draw(&rotation[3]);        cell_draw(&scale[0]);    cell_draw(&scale[1]);    cell_draw(&scale[2]);        if (!selection) {        glColor3ub(255, 255, 0);        drawstr(10, 240,            "Click on the arguments and move the mouse to modify values.");    }           glutSwapBuffers();}int old_y;voidcommand_mouse(int button, int state, int x, int y){    selection = 0;        if (state == GLUT_DOWN) {    /* mouse should only hit _one_ of the cells, so adding up all        the hits just propagates a single hit. */        selection += cell_hit(&translation[0], x, y);        selection += cell_hit(&translation[1], x, y);        selection += cell_hit(&translation[2], x, y);        selection += cell_hit(&rotation[0], x, y);        selection += cell_hit(&rotation[1], x, y);        selection += cell_hit(&rotation[2], x, y);        selection += cell_hit(&rotation[3], x, y);        selection += cell_hit(&scale[0], x, y);        selection += cell_hit(&scale[1], x, y);        selection += cell_hit(&scale[2], x, y);    }        old_y = y;        redisplay_all();}voidcommand_motion(int x, int y){    cell_update(&translation[0], old_y-y);    cell_update(&translation[1], old_y-y);    cell_update(&translation[2], old_y-y);    cell_update(&rotation[0], old_y-y);    cell_update(&rotation[1], old_y-y);    cell_update(&rotation[2], old_y-y);    cell_update(&rotation[3], old_y-y);    cell_update(&scale[0], old_y-y);    cell_update(&scale[1], old_y-y);    cell_update(&scale[2], old_y-y);        old_y = y;        redisplay_all();}voidcommand_menu(int value){    main_keyboard((unsigned char)value, 0, 0);}voidredisplay_all(void){    glutSetWindow(command);    glutPostRedisplay();    glutSetWindow(world);    world_reshape(sub_width, sub_height);    glutPostRedisplay();    glutSetWindow(screen);    screen_reshape(sub_width, sub_height);    glutPostRedisplay();}intmain(int argc, char** argv){    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);    glutInitWindowSize(512+GAP*3, 512+GAP*3);    glutInitWindowPosition(50, 50);    glutInit(&argc, argv);        window = glutCreateWindow("Transformation");    glutReshapeFunc(main_reshape);    glutDisplayFunc(main_display);    glutKeyboardFunc(main_keyboard);        world = glutCreateSubWindow(window, GAP, GAP, 256, 256);    glutReshapeFunc(world_reshape);    glutDisplayFunc(world_display);    glutKeyboardFunc(main_keyboard);    glutCreateMenu(world_menu);    glutAddMenuEntry("Toggle model", 'm');    glutAttachMenu(GLUT_RIGHT_BUTTON);        screen = glutCreateSubWindow(window, GAP+256+GAP, GAP, 256, 256);    glutReshapeFunc(screen_reshape);    glutDisplayFunc(screen_display);    glutKeyboardFunc(main_keyboard);    glutCreateMenu(screen_menu);    glutAddMenuEntry("Models", 0);    glutAddMenuEntry("", 0);    glutAddMenuEntry("Soccerball", 's');    glutAddMenuEntry("Al Capone", 'a');    glutAddMenuEntry("F-16 Jet", 'j');    glutAddMenuEntry("Dolphins", 'd');    glutAddMenuEntry("Flowers", 'f');    glutAddMenuEntry("Porsche", 'p');    glutAddMenuEntry("Rose", 'r');    glutAttachMenu(GLUT_RIGHT_BUTTON);        command = glutCreateSubWindow(window, GAP+256+GAP, GAP+256+GAP, 256, 256);    glutReshapeFunc(command_reshape);    glutDisplayFunc(command_display);    glutMotionFunc(command_motion);    glutMouseFunc(command_mouse);    glutKeyboardFunc(main_keyboard);    glutCreateMenu(command_menu);    glutAddMenuEntry("Projection", 0);    glutAddMenuEntry("", 0);    glutAddMenuEntry("[s]  Swap translate/rotate", 's');    glutAddMenuEntry("[r]  Reset parameters", 'r');    glutAddMenuEntry("", 0);    glutAddMenuEntry("Quit", 27);    glutAttachMenu(GLUT_RIGHT_BUTTON);        redisplay_all();        glutMainLoop();        return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -