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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "model.h"
  2. namespace segl {
  3. // Class Meshpoint
  4. Meshpoint::Meshpoint() {
  5. }
  6. Meshpoint::Meshpoint(Punkt3D v, Punkt2D vt) {
  7. set(v, vt);
  8. }
  9. void Meshpoint::set(Punkt3D v, Punkt2D vt) {
  10. vertex = v;
  11. texcoord = vt;
  12. // normal = n;
  13. }
  14. void Meshpoint::use() const {
  15. glTexCoordP2D(texcoord);
  16. // glNormalP3D(normal);
  17. glVertexP3D(vertex);
  18. }
  19. // Class Material
  20. Material::Material() {
  21. name = "none";
  22. ambient.set(1.0f, 1.0f, 1.0f);
  23. diffuse.set(1.0f, 1.0f, 1.0f);
  24. specular.set(1.0f, 1.0f, 1.0f);
  25. }
  26. Material::Material(std::string _name, Color _a, Color _d, Color _s) {
  27. set(_name, _a, _d, _s);
  28. }
  29. void Material::set(std::string _name, Color _a, Color _d, Color _s) {
  30. name = _name;
  31. ambient = _a;
  32. diffuse = _d;
  33. specular = _s;
  34. }
  35. void Material::use() const {
  36. glColorGLC(diffuse);
  37. }
  38. // Polygonpoint
  39. Polygonpoint::Polygonpoint() {
  40. point = 0;
  41. tex = 0;
  42. normal = 0;
  43. }
  44. void Polygonpoint::use() const {
  45. if(normal)
  46. glNormalP3D(*normal);
  47. if(tex)
  48. glTexCoordP2D(*tex);
  49. if(point)
  50. glVertexP3D(*point);
  51. }
  52. // Class Meshpolygon
  53. Meshpolygon::Meshpolygon() {
  54. a = b = c = 0;
  55. }
  56. Meshpolygon::Meshpolygon(Meshpoint *_a, Meshpoint *_b, Meshpoint *_c) {
  57. set(_a, _b, _c);
  58. }
  59. void Meshpolygon::set(Meshpoint *_a, Meshpoint *_b, Meshpoint *_c) {
  60. a = _a;
  61. b = _b;
  62. c = _c;
  63. }
  64. void Meshpolygon::render(GLenum mode) const {
  65. // if(!a || !b || !c)
  66. // return;
  67. glBegin(mode);
  68. m1.use();
  69. m2.use();
  70. m3.use();
  71. glEnd();
  72. // std::cout << "Pos 1 " << *m1.point << std::endl;
  73. // std::cout << "Pos 2 " << *m2.point << std::endl;
  74. // std::cout << "Pos 3 " << *m3.point << std::endl;
  75. }
  76. // Class Model
  77. Model::Model() {
  78. meshdata = 0;
  79. meshdataanz = 0;
  80. matdata = 0;
  81. matdataanz = 0;
  82. polydata = 0;
  83. polydataanz = 0;
  84. texdata = 0;
  85. texdataanz = 0;
  86. normdata = 0;
  87. normdataanz = 0;
  88. loaded = false;
  89. boundingrad = 0.0f;
  90. backupmat.diffuse.set(1.0f, 1.0f, 1.0f);
  91. }
  92. bool Model::isLoaded() const {
  93. return loaded;
  94. }
  95. void Model::unload() {
  96. if(meshdata)
  97. delete[](meshdata);
  98. if(polydata)
  99. delete[](polydata);
  100. if(matdata)
  101. delete[](matdata);
  102. if(texdata)
  103. delete[](texdata);
  104. if(normdata)
  105. delete[](normdata);
  106. meshdataanz = polydataanz = matdataanz = texdataanz = normdataanz = 0;
  107. meshdata = 0;
  108. polydata = 0;
  109. matdata = 0;
  110. texdata = 0;
  111. normdata = 0;
  112. loaded = false;
  113. }
  114. void Model::render() const {
  115. if(!loaded) {
  116. // std::cout << "Model not loaded" << std::endl;
  117. return;
  118. }
  119. glPushMatrix();
  120. // std::cout << " --- begin --- " << std::endl;
  121. for(unsigned int i=0; i<polydataanz; i++) {
  122. // glColor3f((float)i/polydataanz, 0.0f, 1.0f);
  123. polydata[i].render(GL_LINE_LOOP);
  124. }
  125. glPopMatrix();
  126. // std::cout << " --- end --- " << std::endl;
  127. }
  128. Model::~Model() {
  129. unload();
  130. }
  131. const Punkt3D* Model::getMeshData(unsigned int *meshanz) const {
  132. if(meshanz != 0)
  133. *meshanz = meshdataanz;
  134. return (const Punkt3D*)meshdata;
  135. }
  136. const Meshpolygon* Model::getPolygonData(unsigned int *polyanz) const {
  137. if(polyanz != 0)
  138. *polyanz = polydataanz;
  139. return (const Meshpolygon*)polydata;
  140. }
  141. } // namespace segl