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

📄 lightmaterial.c

📁 Example source codes in C++ using 3d studio max models
💻 C
📖 第 1 页 / 共 3 页
字号:
    drawstr(10, spot_cutoff.y+70,        "glLighti(GL_LIGHT0, GL_SPOT_CUTOFF, spot_cutoff);");        setfont("helvetica", 18);    drawstr(10, Kc.y,         "GLfloat Kc =          , Kl =          , Kq =          ;");        setfont("helvetica", 12);    drawstr(10, Kq.y+30, "glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, Kc);");    drawstr(10, Kq.y+50, "glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, Kl);");    drawstr(10, Kq.y+70, "glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, Kq);");        setfont("helvetica", 18);    cell_draw(&spot_direction[0]);    cell_draw(&spot_direction[1]);    cell_draw(&spot_direction[2]);        cell_draw(&spot_exponent);    cell_draw(&spot_cutoff);        cell_draw(&Kc);    cell_draw(&Kl);    cell_draw(&Kq);        if (!selection) {        glColor3ub(255, 255, 0);        drawstr(10, 525,            "Click on the arguments and move the mouse to modify values.");    }           glutSwapBuffers();}voidmaterial_display(void){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        glColor3ub(255, 255, 255);        lighting_display();        setfont("helvetica", 18);    drawstr(10, material_Ka[0].y, "GLfloat material_Ka[ ] = {");    drawstr(10, material_Kd[0].y, "GLfloat material_Kd[ ] = {");    drawstr(10, material_Ks[0].y, "GLfloat material_Ks[ ] = {");    drawstr(10, material_Ke[0].y, "GLfloat material_Ke[ ] = {");    drawstr(10, material_Se.y, "GLfloat material_Se =          ;");    drawstr(material_Ka[0].x+50, material_Ka[0].y, ",");    drawstr(material_Ka[1].x+50, material_Ka[1].y, ",");    drawstr(material_Ka[2].x+50, material_Ka[2].y, ",");    drawstr(material_Ka[3].x+50, material_Ka[3].y, "};");    drawstr(material_Kd[0].x+50, material_Kd[0].y, ",");    drawstr(material_Kd[1].x+50, material_Kd[1].y, ",");    drawstr(material_Kd[2].x+50, material_Kd[2].y, ",");    drawstr(material_Kd[3].x+50, material_Kd[3].y, "};");    drawstr(material_Ks[0].x+50, material_Ks[0].y, ",");    drawstr(material_Ks[1].x+50, material_Ks[1].y, ",");    drawstr(material_Ks[2].x+50, material_Ks[2].y, ",");    drawstr(material_Ks[3].x+50, material_Ks[3].y, "};");    drawstr(material_Ke[0].x+50, material_Ke[0].y, ",");    drawstr(material_Ke[1].x+50, material_Ke[1].y, ",");    drawstr(material_Ke[2].x+50, material_Ke[2].y, ",");    drawstr(material_Ke[3].x+50, material_Ke[3].y, "};");        setfont("helvetica", 12);    drawstr(10, material_Se.y+30,        "glMaterialfv(GL_FRONT, GL_AMBIENT, material_Ka);");    drawstr(10, material_Se.y+50,        "glMaterialfv(GL_FRONT, GL_DIFFUSE, material_Kd);");    drawstr(10, material_Se.y+70,        "glMaterialfv(GL_FRONT, GL_SPECULAR, material_Ks);");    drawstr(10, material_Se.y+90,        "glMaterialfv(GL_FRONT, GL_EMISSION, material_Ke);");    drawstr(10, material_Se.y+110,        "glMaterialfv(GL_FRONT, GL_SHININESS, material_Se);");        setfont("helvetica", 18);        cell_draw(&material_Ka[0]);    cell_draw(&material_Ka[1]);    cell_draw(&material_Ka[2]);    cell_draw(&material_Ka[3]);        cell_draw(&material_Kd[0]);    cell_draw(&material_Kd[1]);    cell_draw(&material_Kd[2]);    cell_draw(&material_Kd[3]);        cell_draw(&material_Ks[0]);    cell_draw(&material_Ks[1]);    cell_draw(&material_Ks[2]);    cell_draw(&material_Ks[3]);        cell_draw(&material_Ke[0]);    cell_draw(&material_Ke[1]);    cell_draw(&material_Ke[2]);    cell_draw(&material_Ke[3]);        cell_draw(&material_Se);        if (!selection) {        glColor3ub(255, 255, 0);        drawstr(10, 525,            "Click on the arguments and move the mouse to modify values.");    }           glutSwapBuffers();}voidlmodel_display(void){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        glColor3ub(255, 255, 255);        lighting_display();        setfont("helvetica", 18);    drawstr(10, lmodel_Ka[0].y, "GLfloat lmodel_Ka[ ] = {");    drawstr(lmodel_Ka[0].x+50, lmodel_Ka[0].y, ",");    drawstr(lmodel_Ka[1].x+50, lmodel_Ka[1].y, ",");    drawstr(lmodel_Ka[2].x+50, lmodel_Ka[2].y, ",");    drawstr(lmodel_Ka[3].x+50, lmodel_Ka[3].y, "};");        setfont("helvetica", 12);    drawstr(10, lmodel_Ka[3].y+30,        "glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_Ka);");        setfont("helvetica", 18);    drawstr(10, local_viewer.y, "glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, ");    drawstr(local_viewer.x+35, local_viewer.y, ");");        drawstr(10, two_side.y, "glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, ");    drawstr(two_side.x+35, two_side.y, ");");        cell_draw(&lmodel_Ka[0]);    cell_draw(&lmodel_Ka[1]);    cell_draw(&lmodel_Ka[2]);    cell_draw(&lmodel_Ka[3]);        cell_draw(&local_viewer);    cell_draw(&two_side);        if (!selection) {        glColor3ub(255, 255, 0);        drawstr(10, 525,            "Click on the arguments and move the mouse to modify values.");    }           glutSwapBuffers();}voidlighting_mouse(int x, int y){/* mouse should only hit _one_ of the cells, so adding up all    the hits just propagates a single hit. */    selection += cell_hit(&light_pos[0], x, y);    selection += cell_hit(&light_pos[1], x, y);    selection += cell_hit(&light_pos[2], x, y);    selection += cell_hit(&light_pos[3], x, y);    selection += cell_hit(&light_Ka[0], x, y);    selection += cell_hit(&light_Ka[1], x, y);    selection += cell_hit(&light_Ka[2], x, y);    selection += cell_hit(&light_Ka[3], x, y);    selection += cell_hit(&light_Kd[0], x, y);    selection += cell_hit(&light_Kd[1], x, y);    selection += cell_hit(&light_Kd[2], x, y);    selection += cell_hit(&light_Kd[3], x, y);    selection += cell_hit(&light_Ks[0], x, y);    selection += cell_hit(&light_Ks[1], x, y);    selection += cell_hit(&light_Ks[2], x, y);    selection += cell_hit(&light_Ks[3], x, y);}voidmaterial_mouse(int button, int state, int x, int y){    selection = 0;        if (state == GLUT_DOWN) {        lighting_mouse(x, y);        /* mouse should only hit _one_ of the cells, so adding up all        the hits just propagates a single hit. */        selection += cell_hit(&material_Ka[0], x, y);        selection += cell_hit(&material_Ka[1], x, y);        selection += cell_hit(&material_Ka[2], x, y);        selection += cell_hit(&material_Ka[3], x, y);        selection += cell_hit(&material_Kd[0], x, y);        selection += cell_hit(&material_Kd[1], x, y);        selection += cell_hit(&material_Kd[2], x, y);        selection += cell_hit(&material_Kd[3], x, y);        selection += cell_hit(&material_Ks[0], x, y);        selection += cell_hit(&material_Ks[1], x, y);        selection += cell_hit(&material_Ks[2], x, y);        selection += cell_hit(&material_Ks[3], x, y);        selection += cell_hit(&material_Ke[0], x, y);        selection += cell_hit(&material_Ke[1], x, y);        selection += cell_hit(&material_Ke[2], x, y);        selection += cell_hit(&material_Ke[3], x, y);        selection += cell_hit(&material_Se, x, y);    }        old_y = y;        redisplay_all();}voidspotlight_mouse(int button, int state, int x, int y){    selection = 0;        if (state == GLUT_DOWN) {        lighting_mouse(x, y);        /* mouse should only hit _one_ of the cells, so adding up all        the hits just propagates a single hit. */        selection += cell_hit(&spot_direction[0], x, y);        selection += cell_hit(&spot_direction[1], x, y);        selection += cell_hit(&spot_direction[2], x, y);        selection += cell_hit(&spot_exponent, x, y);        selection += cell_hit(&spot_cutoff, x, y);        selection += cell_hit(&Kc, x, y);        selection += cell_hit(&Kl, x, y);        selection += cell_hit(&Kq, x, y);    }        old_y = y;        redisplay_all();}voidlmodel_mouse(int button, int state, int x, int y){    selection = 0;        if (state == GLUT_DOWN) {        lighting_mouse(x, y);        /* mouse should only hit _one_ of the cells, so adding up all        the hits just propagates a single hit. */        selection += cell_hit(&lmodel_Ka[0], x, y);        selection += cell_hit(&lmodel_Ka[1], x, y);        selection += cell_hit(&lmodel_Ka[2], x, y);        selection += cell_hit(&lmodel_Ka[3], x, y);        selection += cell_hit(&local_viewer, x, y);        selection += cell_hit(&two_side, x, y);    }        old_y = y;        redisplay_all();}voidcommand_motion(int x, int y){    cell_update(&light_pos[0], old_y-y);    cell_update(&light_pos[1], old_y-y);    cell_update(&light_pos[2], old_y-y);    cell_update(&light_pos[3], old_y-y);    cell_update(&light_Ka[0], old_y-y);    cell_update(&light_Ka[1], old_y-y);    cell_update(&light_Ka[2], old_y-y);    cell_update(&light_Ka[3], old_y-y);    cell_update(&light_Kd[0], old_y-y);    cell_update(&light_Kd[1], old_y-y);    cell_update(&light_Kd[2], old_y-y);    cell_update(&light_Kd[3], old_y-y);    cell_update(&light_Ks[0], old_y-y);    cell_update(&light_Ks[1], old_y-y);    cell_update(&light_Ks[2], old_y-y);    cell_update(&light_Ks[3], old_y-y);    cell_update(&spot_direction[0], old_y-y);    cell_update(&spot_direction[1], old_y-y);    cell_update(&spot_direction[2], old_y-y);    cell_update(&spot_exponent, old_y-y);    cell_update(&spot_cutoff, old_y-y);    cell_update(&Kc, old_y-y);    cell_update(&Kl, old_y-y);    cell_update(&Kq, old_y-y);    cell_update(&material_Ka[0], old_y-y);    cell_update(&material_Ka[1], old_y-y);    cell_update(&material_Ka[2], old_y-y);    cell_update(&material_Ka[3], old_y-y);    cell_update(&material_Kd[0], old_y-y);    cell_update(&material_Kd[1], old_y-y);    cell_update(&material_Kd[2], old_y-y);    cell_update(&material_Kd[3], old_y-y);    cell_update(&material_Ks[0], old_y-y);    cell_update(&material_Ks[1], old_y-y);    cell_update(&material_Ks[2], old_y-y);    cell_update(&material_Ks[3], old_y-y);    cell_update(&material_Ke[0], old_y-y);    cell_update(&material_Ke[1], old_y-y);    cell_update(&material_Ke[2], old_y-y);    cell_update(&material_Ke[3], old_y-y);    cell_update(&material_Se, old_y-y);    cell_update(&lmodel_Ka[0], old_y-y);    cell_update(&lmodel_Ka[1], old_y-y);    cell_update(&lmodel_Ka[2], old_y-y);    cell_update(&lmodel_Ka[3], old_y-y);    cell_update(&local_viewer, old_y-y);    cell_update(&two_side, old_y-y);        old_y = y;        redisplay_all();}voidredisplay_all(void){    glutSetWindow(command);    glutPostRedisplay();    glutSetWindow(world);    world_reshape(sub_width, sub_height);    glutPostRedisplay();    glutSetWindow(screen);    screen_reshape(sub_width, sub_height);    glutPostRedisplay();}voidmain_keyboard(unsigned char key, int x, int y){    switch (key) {    case 'r':        light_pos[0].value = -2.0;        light_pos[1].value = 2.0;        light_pos[2].value = 2.0;        light_pos[3].value = 1.0;        light_Ka[0].value = 0;        light_Ka[1].value = 0;        light_Ka[2].value = 0;        light_Ka[3].value = 1;        light_Kd[0].value = 1;        light_Kd[1].value = 1;        light_Kd[2].value = 1;        light_Kd[3].value = 1;        light_Ks[0].value = 1;        light_Ks[1].value = 1;        light_Ks[2].value = 1;        light_Ks[3].value = 1;        spot_direction[0].value = 1.0;        spot_direction[1].value = -1.0;        spot_direction[2].value = -1.0;        spot_exponent.value = 30.0;        spot_cutoff.value = 91.0;        Kc.value = 1.0;        Kl.value = 0.0;        Kq.value = 0.0;        new_material(Pewter);        lmodel_Ka[0].value = 0.2;        lmodel_Ka[1].value = 0.2;        lmodel_Ka[2].value = 0.2;        lmodel_Ka[3].value = 1.0;        local_viewer.value = 0;        two_side.value = 0;        break;    case 'm':        glutSetWindow(command);        glutMouseFunc(material_mouse);        glutDisplayFunc(material_display);        break;    case 's':        glutSetWindow(command);        glutMouseFunc(spotlight_mouse);        glutDisplayFunc(spotlight_display);        break;    case 'l':        glutSetWindow(command);        glutMouseFunc(lmodel_mouse);        glutDisplayFunc(lmodel_display);        break;    case 27:        exit(0);    }        redisplay_all();}voidcommand_menu(int value){    main_keyboard((unsigned char)value, 0, 0);}intmain(int argc, char** argv){    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);    glutInitWindowSize((512+GAP*3)*3/2, 512+GAP*3);    glutInitWindowPosition(50, 50);    glutInit(&argc, argv);        window = glutCreateWindow("Light & Material");    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("Materials", 0);    glutAddMenuEntry("", 0);    glutAddMenuEntry("Brass", 1);    glutAddMenuEntry("Bronze", 2);    glutAddMenuEntry("Polished_Bronze", 3);    glutAddMenuEntry("Chrome", 4);    glutAddMenuEntry("Copper", 5);    glutAddMenuEntry("Polished_Copper", 6);    glutAddMenuEntry("Gold", 7);    glutAddMenuEntry("Polished_Gold", 8);    glutAddMenuEntry("Pewter", 9);    glutAddMenuEntry("Silver", 10);    glutAddMenuEntry("Polished_Silver", 11);    glutAddMenuEntry("Emerald", 12);    glutAddMenuEntry("Jade", 13);    glutAddMenuEntry("Obsidian", 14);    glutAddMenuEntry("Pearl", 15);    glutAddMenuEntry("Ruby", 16);    glutAddMenuEntry("Turquoise", 17);    glutAddMenuEntry("Black_Plastic", 18);    glutAddMenuEntry("Black_Rubber", 19);    glutAttachMenu(GLUT_RIGHT_BUTTON);        screen = glutCreateSubWindow(window, GAP+256+GAP, GAP, 256, 256);    glutReshapeFunc(screen_reshape);    glutDisplayFunc(screen_display);    glutKeyboardFunc(main_keyboard);    glutMotionFunc(screen_motion);    glutMouseFunc(screen_mouse);    glutCreateMenu(screen_menu);    glutAddMenuEntry("Models", 0);    glutAddMenuEntry("", 0);    glutAddMenuEntry("Torus", 'n');    glutAddMenuEntry("Flat plane", 'l');    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(material_display);    glutMotionFunc(command_motion);    glutMouseFunc(material_mouse);    glutKeyboardFunc(main_keyboard);    glutCreateMenu(command_menu);    glutAddMenuEntry("Light & Material", 0);    glutAddMenuEntry("", 0);    glutAddMenuEntry("Material parameters", 'm');    glutAddMenuEntry("Light model parameters", 'l');    glutAddMenuEntry("Spotlight & attenuation", 's');    glutAddMenuEntry("Reset parameters (r)", 'r');    glutAddMenuEntry("", 0);    glutAddMenuEntry("Quit", 27);    glutAttachMenu(GLUT_RIGHT_BUTTON);        new_material(Pewter);    redisplay_all();        glutMainLoop();    return 0;}

⌨️ 快捷键说明

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