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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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() {
  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() {
  36. glColorGLC(diffuse);
  37. }
  38. // Polygonpoint
  39. Polygonpoint::Polygonpoint() {
  40. point = 0;
  41. tex = 0;
  42. normal = 0;
  43. }
  44. void Polygonpoint::use() {
  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) {
  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. backupmat.diffuse.set(1.0f, 1.0f, 1.0f);
  90. }
  91. bool Model::isLoaded() {
  92. return loaded;
  93. }
  94. void Model::unload() {
  95. if(meshdata)
  96. delete[](meshdata);
  97. if(polydata)
  98. delete[](polydata);
  99. if(matdata)
  100. delete[](matdata);
  101. if(texdata)
  102. delete[](texdata);
  103. if(normdata)
  104. delete[](normdata);
  105. meshdataanz = polydataanz = matdataanz = texdataanz = normdataanz = 0;
  106. meshdata = 0;
  107. polydata = 0;
  108. matdata = 0;
  109. texdata = 0;
  110. normdata = 0;
  111. loaded = false;
  112. }
  113. void Model::render() {
  114. if(!loaded) {
  115. // std::cout << "Model not loaded" << std::endl;
  116. return;
  117. }
  118. glPushMatrix();
  119. // std::cout << " --- begin --- " << std::endl;
  120. for(unsigned int i=0; i<polydataanz; i++) {
  121. // glColor3f((float)i/polydataanz, 0.0f, 1.0f);
  122. polydata[i].render(GL_LINE_LOOP);
  123. }
  124. glPopMatrix();
  125. // std::cout << " --- end --- " << std::endl;
  126. }
  127. Model::~Model() {
  128. unload();
  129. }
  130. const Punkt3D* Model::getMeshData(unsigned int *meshanz) {
  131. *meshanz = meshdataanz;
  132. return (const Punkt3D*)meshdata;
  133. }
  134. } // namespace segl