Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

model.cpp 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "model.h"
  2. ModelObject::ModelObject() {
  3. }
  4. void ModelObject::clear() {
  5. normal.clear();
  6. vertex.clear();
  7. mapcoord.clear();
  8. polygon.clear();
  9. }
  10. void ModelObject::calcNormales() {
  11. normal.clear();
  12. Punkt3D a, b, c;
  13. for(unsigned int i=0; i<polygon.size(); i++) {
  14. a = vertex.at((unsigned short)polygon[i].x) - vertex.at((unsigned short)polygon[i].z);
  15. b = vertex.at((unsigned short)polygon[i].y) - vertex.at((unsigned short)polygon[i].z);
  16. c = a.kreuzprodukt(b);
  17. c.normalize();
  18. normal.push_back(c);
  19. // c.print("Normale");
  20. }
  21. }
  22. void ModelObject::render() {
  23. // std::cout << "render " << name << " mit " << polygon.size()<< std::endl;
  24. // std::cout << "Vertex: " << vertex.size() << std::endl;
  25. // if(mat.name[0]=='g') {
  26. // std::cout << "Render NOT: " << name << std::endl;
  27. // return;
  28. // }
  29. // mat.diffuse.print(mat.name);
  30. glBegin(GL_TRIANGLES);
  31. for(unsigned int i=0; i<polygon.size(); i++) {
  32. glColorGLC(mat.diffuse);
  33. glNormal3f(normal.at(i));
  34. glTexCoord2f(mapcoord.at((unsigned short)polygon[i].x));
  35. glVertex3f(vertex.at( (unsigned short)polygon[i].x));
  36. glTexCoord2f(mapcoord.at((unsigned short)polygon[i].y));
  37. glVertex3f(vertex.at( (unsigned short)polygon[i].y));
  38. glTexCoord2f(mapcoord.at((unsigned short)polygon[i].z));
  39. glVertex3f(vertex.at( (unsigned short)polygon[i].z));
  40. // vertex.at( (unsigned short)polygon[i].x).print();
  41. }
  42. glEnd();
  43. }
  44. std::string ModelObject::getName() {
  45. return name;
  46. }
  47. Material::Material() {
  48. clear();
  49. }
  50. void Material::clear() {
  51. name = "";
  52. ambient.set(0.0f, 0.0f, 0.0f);
  53. diffuse.set(0.0f, 0.0f, 0.0f);
  54. specular.set(0.0f, 0.0f, 0.0f);
  55. percent = 0.0f;
  56. }
  57. std::string Material::getName() {
  58. return name;
  59. }
  60. Model::Model() {
  61. }
  62. void Model::clear() {
  63. objects.clear();
  64. }
  65. void Model::addObject(ModelObject c) {
  66. objects.push_back(c);
  67. }
  68. void Model::render() {
  69. // std::cout << "Render Model " << objects.size() << std::endl;
  70. for(unsigned int i=0; i<objects.size(); i++)
  71. objects[i].render();
  72. }
  73. void Model::addMaterial(Material m) {
  74. materials.push_back(m);
  75. }
  76. Material Model::findMaterial(std::string name) {
  77. for(unsigned int i=0; i<materials.size(); i++) {
  78. if(name==materials[i].getName()) {
  79. std::cout<<"found!"<<std::endl;
  80. return materials[i];
  81. }
  82. }
  83. return Material();
  84. }