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

📄 projection.c

📁 计算机图形学~想必是很多人需要的~在此共享一下
💻 C
📖 第 1 页 / 共 2 页
字号:
        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, light_pos);        glPopMatrix();        drawmodel();        glDisable(GL_LIGHTING);    }        glPushMatrix();        glMultMatrixd(inverse);        glLightfv(GL_LIGHT0, GL_POSITION, light_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();    if (mode == PERSPECTIVE)        gluPerspective(perspective[0].value, perspective[1].value,         perspective[2].value, perspective[3].value);    else if (mode == ORTHO)        glOrtho(ortho[0].value, ortho[1].value, ortho[2].value,        ortho[3].value, ortho[4].value, ortho[5].value);    else if (mode == FRUSTUM)        glFrustum(frustum[0].value, frustum[1].value, frustum[2].value,        frustum[3].value, frustum[4].value, frustum[5].value);    glGetDoublev(GL_PROJECTION_MATRIX, projection);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    gluLookAt(lookat[0].value, lookat[1].value, lookat[2].value,        lookat[3].value, lookat[4].value, lookat[5].value,        lookat[6].value, lookat[7].value, lookat[8].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);        glColor3ub(255,255,255);        setfont("helvetica", 18);        if (mode == PERSPECTIVE) {        drawstr(180, perspective[0].y-40, "fovy");         drawstr(230, perspective[0].y-40, "aspect");         drawstr(300, perspective[0].y-40, "zNear");         drawstr(360, perspective[0].y-40, "zFar");    } else {        drawstr(120, perspective[0].y-40, "left");         drawstr(180, perspective[0].y-40, "right");         drawstr(230, perspective[0].y-40, "bottom");        drawstr(310, perspective[0].y-40, "top");         drawstr(360, perspective[0].y-40, "near");        drawstr(420, perspective[0].y-40, "far");    }        if (mode == PERSPECTIVE) {        drawstr(40, perspective[0].y, "gluPerspective(");        drawstr(230, perspective[0].y, ",");         drawstr(290, perspective[0].y, ",");        drawstr(350, perspective[0].y, ",");        drawstr(410, perspective[0].y, ");");    } else if (mode == FRUSTUM) {        drawstr(20, frustum[0].y, "glFrustum(");        drawstr(170, frustum[0].y, ",");        drawstr(230, frustum[0].y, ",");         drawstr(290, frustum[0].y, ",");        drawstr(350, frustum[0].y, ",");        drawstr(410, frustum[0].y, ",");         drawstr(470, frustum[0].y, ");");    } else {        drawstr(35, ortho[0].y, "glOrtho(");        drawstr(170, ortho[0].y, ",");        drawstr(230, ortho[0].y, ",");         drawstr(290, ortho[0].y, ",");        drawstr(350, ortho[0].y, ",");        drawstr(410, ortho[0].y, ",");         drawstr(470, ortho[0].y, ");");    }        drawstr(78, lookat[0].y, "gluLookAt(");    drawstr(230, lookat[0].y, ",");     drawstr(290, lookat[0].y, ",");    drawstr(350, lookat[0].y, ",");    drawstr(380, lookat[0].y, "<- eye");    drawstr(230, lookat[3].y, ",");     drawstr(290, lookat[3].y, ",");    drawstr(350, lookat[3].y, ",");    drawstr(380, lookat[3].y, "<- center");    drawstr(230, lookat[6].y, ",");     drawstr(290, lookat[6].y, ",");    drawstr(350, lookat[6].y, ");");    drawstr(380, lookat[6].y, "<- up");        if (mode == PERSPECTIVE) {        cell_draw(&perspective[0]);        cell_draw(&perspective[1]);        cell_draw(&perspective[2]);        cell_draw(&perspective[3]);     } else if (mode == FRUSTUM) {        cell_draw(&frustum[0]);        cell_draw(&frustum[1]);        cell_draw(&frustum[2]);        cell_draw(&frustum[3]);        cell_draw(&frustum[4]);        cell_draw(&frustum[5]);    } else if (mode == ORTHO) {        cell_draw(&ortho[0]);        cell_draw(&ortho[1]);        cell_draw(&ortho[2]);        cell_draw(&ortho[3]);        cell_draw(&ortho[4]);        cell_draw(&ortho[5]);    }           cell_draw(&lookat[0]);    cell_draw(&lookat[1]);    cell_draw(&lookat[2]);    cell_draw(&lookat[3]);    cell_draw(&lookat[4]);    cell_draw(&lookat[5]);    cell_draw(&lookat[6]);    cell_draw(&lookat[7]);    cell_draw(&lookat[8]);        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) {        if (mode == PERSPECTIVE) {        /* mouse should only hit _one_ of the cells, so adding up all            the hits just propagates a single hit. */            selection += cell_hit(&perspective[0], x, y);            selection += cell_hit(&perspective[1], x, y);            selection += cell_hit(&perspective[2], x, y);            selection += cell_hit(&perspective[3], x, y);        } else if (mode == FRUSTUM) {            selection += cell_hit(&frustum[0], x, y);            selection += cell_hit(&frustum[1], x, y);            selection += cell_hit(&frustum[2], x, y);            selection += cell_hit(&frustum[3], x, y);            selection += cell_hit(&frustum[4], x, y);            selection += cell_hit(&frustum[5], x, y);        } else if (mode == ORTHO) {            selection += cell_hit(&ortho[0], x, y);            selection += cell_hit(&ortho[1], x, y);            selection += cell_hit(&ortho[2], x, y);            selection += cell_hit(&ortho[3], x, y);            selection += cell_hit(&ortho[4], x, y);            selection += cell_hit(&ortho[5], x, y);        }        selection += cell_hit(&lookat[0], x, y);        selection += cell_hit(&lookat[1], x, y);        selection += cell_hit(&lookat[2], x, y);        selection += cell_hit(&lookat[3], x, y);        selection += cell_hit(&lookat[4], x, y);        selection += cell_hit(&lookat[5], x, y);        selection += cell_hit(&lookat[6], x, y);        selection += cell_hit(&lookat[7], x, y);        selection += cell_hit(&lookat[8], x, y);    }        old_y = y;        redisplay_all();}voidcommand_motion(int x, int y){    cell_update(&perspective[0], old_y-y);    cell_update(&perspective[1], old_y-y);    cell_update(&perspective[2], old_y-y);    cell_update(&perspective[3], old_y-y);    cell_update(&frustum[0], old_y-y);    cell_update(&frustum[1], old_y-y);    cell_update(&frustum[2], old_y-y);    cell_update(&frustum[3], old_y-y);    cell_update(&frustum[4], old_y-y);    cell_update(&frustum[5], old_y-y);    cell_update(&ortho[0], old_y-y);    cell_update(&ortho[1], old_y-y);    cell_update(&ortho[2], old_y-y);    cell_update(&ortho[3], old_y-y);    cell_update(&ortho[4], old_y-y);    cell_update(&ortho[5], old_y-y);    cell_update(&lookat[0], old_y-y);    cell_update(&lookat[1], old_y-y);    cell_update(&lookat[2], old_y-y);    cell_update(&lookat[3], old_y-y);    cell_update(&lookat[4], old_y-y);    cell_update(&lookat[5], old_y-y);    cell_update(&lookat[6], old_y-y);    cell_update(&lookat[7], old_y-y);    cell_update(&lookat[8], 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("Projection");    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("[o]  glOrtho", 'o');    glutAddMenuEntry("[f]  glFrustum", 'f');    glutAddMenuEntry("[p]  gluPerspective", 'p');    glutAddMenuEntry("", 0);    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 + -