comparison dmmodel.cpp @ 32:1215fdd0a8ab

Add support for specifying specular and shininess values per model.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Nov 2019 22:57:14 +0200
parents 2403030a0352
children 2e85c180afdf
comparison
equal deleted inserted replaced
31:6847715b46cd 32:1215fdd0a8ab
616 616
617 return true; 617 return true;
618 } 618 }
619 619
620 620
621 bool dmParseColor(DMTextFileInfo &info, const std::vector<std::string> tokens, const size_t offs, DMColor &color)
622 {
623 color.alpha = 0xff;
624
625 if (tokens.size() == offs + 1)
626 {
627 color.r = color.g = color.b = std::stoi(tokens[offs], 0, 0);
628 }
629 else
630 if (tokens.size() == offs + 3 || tokens.size() == offs + 4)
631 {
632 color.r = std::stoi(tokens[offs], 0, 0);
633 color.g = std::stoi(tokens[offs + 1], 0, 0);
634 color.b = std::stoi(tokens[offs + 2], 0, 0);
635
636 if (tokens.size() == offs + 4)
637 color.alpha = std::stoi(tokens[offs + 3], 0, 0);
638 }
639 else
640 {
641 return dmSyntaxError(info,
642 "Expected color values <value> or <red> <green> <blue> [<alpha>] for '"+ *info.key +"'");
643 }
644
645 return true;
646 }
647
648
649 bool DMSimpleScene::loadInfo(const std::string &filename) 621 bool DMSimpleScene::loadInfo(const std::string &filename)
650 { 622 {
651 DMTextFileInfo info; 623 DMTextFileInfo info;
652 DMModel *model = 0; 624 DMModel *model = 0;
653 DMLight *light = 0; 625 DMLight *light = 0;
698 670
699 models.push_back(newmodel); 671 models.push_back(newmodel);
700 model = &models.back(); 672 model = &models.back();
701 model->modelFile = tokens[1]; 673 model->modelFile = tokens[1];
702 info.state = 1; 674 info.state = 1;
703 }
704 else
705 if (info.state == 1 && key == "color")
706 {
707 if (!dmParseColor(info, tokens, 1, model->color))
708 return false;
709 } 675 }
710 else 676 else
711 if (info.state == 1 && (key == "translate" || key == "rotate" || key == "scale")) 677 if (info.state == 1 && (key == "translate" || key == "rotate" || key == "scale"))
712 { 678 {
713 DMVector3 vec; 679 DMVector3 vec;
726 else 692 else
727 if (key == "scale") 693 if (key == "scale")
728 model->scale = vec; 694 model->scale = vec;
729 } 695 }
730 else 696 else
697 if (info.state == 1 && key == "specular")
698 {
699 DMVector4 val;
700 val.p.w = 1.0f;
701
702 if (!dmParseVector(info, tokens, 1, val))
703 return false;
704
705 model->specular = val;
706 }
707 else
708 if (info.state == 1 && key == "shininess")
709 {
710 if (tokens.size() != 2)
711 {
712 return dmError(info,
713 "Expected argument for shininess");
714 }
715
716 model->shininess = std::stoi(tokens[1], 0, 0);
717 }
718 else
731 if (key == "light") 719 if (key == "light")
732 { 720 {
733 DMLight newlight; 721 DMLight newlight;
734 722
735 if (lights.size() >= 4) 723 if (lights.size() >= 4)
736 { 724 {
737 printf("ERROR: Too many lights defined.\n"); 725 return dmError(info,
738 return false; 726 "Too many lights defined (max 4)");
739 } 727 }
728
740 lights.push_back(newlight); 729 lights.push_back(newlight);
741 light = &lights.back(); 730 light = &lights.back();
742 ppos = &light->position; 731 ppos = &light->position;
743 ppointAt = &light->pointAt; 732 ppointAt = &light->pointAt;
744 info.state = 2; 733 info.state = 2;