draw_floor.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "draw_floor.h"
  9. #ifndef IGL_NO_OPENGL
  10. #include "OpenGL_convenience.h"
  11. IGL_INLINE void igl::draw_floor(const float * colorA, const float * colorB,
  12. const int GridSizeX,
  13. const int GridSizeY)
  14. {
  15. const float SizeX = 0.5f*100./(double)GridSizeX;
  16. const float SizeY = 0.5f*100./(double)GridSizeY;
  17. // old settings
  18. int old_lighting=0,old_color_material=0;
  19. glGetIntegerv(GL_LIGHTING,&old_lighting);
  20. glGetIntegerv(GL_COLOR_MATERIAL,&old_color_material);
  21. glDisable(GL_LIGHTING);
  22. glColorMaterial( GL_FRONT, GL_EMISSION);
  23. glEnable(GL_COLOR_MATERIAL);
  24. glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  25. // Set material
  26. const float black[] = {0.,0.,0.,1.};
  27. glMaterialfv(GL_FRONT, GL_AMBIENT, black);
  28. glMaterialfv(GL_FRONT, GL_DIFFUSE, black);
  29. glMaterialfv(GL_FRONT, GL_SPECULAR, black);
  30. glMaterialfv(GL_FRONT, GL_EMISSION, black);
  31. glMaterialf(GL_FRONT, GL_SHININESS,0);
  32. const bool use_lighting = false;
  33. if(use_lighting)
  34. {
  35. glEnable(GL_LIGHTING);
  36. }else
  37. {
  38. glDisable(GL_LIGHTING);
  39. }
  40. glBegin(GL_QUADS);
  41. glNormal3f(0,1,0);
  42. for (int x =-GridSizeX/2;x<GridSizeX/2;++x)
  43. {
  44. for (int y =-GridSizeY/2;y<GridSizeY/2;++y)
  45. {
  46. if ((x+y)&0x00000001) //modulo 2
  47. {
  48. glColor4fv(colorA);
  49. }else
  50. {
  51. glColor4fv(colorB);
  52. }
  53. glVertex3f( x*SizeX,0,(y+1)*SizeY);
  54. glVertex3f((x+1)*SizeX,0,(y+1)*SizeY);
  55. glVertex3f((x+1)*SizeX,0, y*SizeY);
  56. glVertex3f( x*SizeX,0, y*SizeY);
  57. }
  58. }
  59. glEnd();
  60. (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
  61. (old_color_material? glEnable(GL_COLOR_MATERIAL) : glDisable(GL_COLOR_MATERIAL));
  62. }
  63. IGL_INLINE void igl::draw_floor()
  64. {
  65. const float grey[] = {0.80,0.80,0.80,1.};
  66. const float white[] = {0.95,0.95,0.95,1.};
  67. igl::draw_floor(grey,white);
  68. }
  69. IGL_INLINE void igl::draw_floor_outline(const float * colorA, const float * colorB,
  70. const int GridSizeX,
  71. const int GridSizeY)
  72. {
  73. const float SizeX = 0.5f*100./(double)GridSizeX;
  74. const float SizeY = 0.5f*100./(double)GridSizeY;
  75. float old_line_width =0;
  76. // old settings
  77. int old_lighting=0,old_color_material=0;
  78. glGetIntegerv(GL_LIGHTING,&old_lighting);
  79. glGetIntegerv(GL_COLOR_MATERIAL,&old_color_material);
  80. glDisable(GL_LIGHTING);
  81. // Set material
  82. const float black[] = {0.,0.,0.,1.};
  83. glMaterialfv(GL_FRONT, GL_AMBIENT, black);
  84. glMaterialfv(GL_FRONT, GL_DIFFUSE, black);
  85. glMaterialfv(GL_FRONT, GL_SPECULAR, black);
  86. glMaterialfv(GL_FRONT, GL_EMISSION, black);
  87. glMaterialf(GL_FRONT, GL_SHININESS,0);
  88. const bool use_lighting = false;
  89. if(use_lighting)
  90. {
  91. glEnable(GL_LIGHTING);
  92. }else
  93. {
  94. glDisable(GL_LIGHTING);
  95. }
  96. glLineWidth(2.0f);
  97. glBegin(GL_LINES);
  98. for (int x =-GridSizeX/2;x<=GridSizeX/2;++x)
  99. {
  100. if(x!=(GridSizeX/2))
  101. {
  102. for(int s = -1;s<2;s+=2)
  103. {
  104. int y = s*(GridSizeY/2);
  105. int cy = y==(GridSizeY/2) ? y-1 : y;
  106. if ((x+cy)&0x00000001) //modulo 2
  107. {
  108. glColor4fv(colorA);
  109. //glColor3f(1,0,0);
  110. }else
  111. {
  112. glColor4fv(colorB);
  113. //glColor3f(0,0,1);
  114. }
  115. glVertex3f((x+1)*SizeX,0,y*SizeY);
  116. glVertex3f( x*SizeX,0,y*SizeY);
  117. }
  118. }
  119. if(x==-(GridSizeX/2) || x==(GridSizeX/2))
  120. {
  121. int cx = x==(GridSizeX/2) ? x-1 : x;
  122. for (int y =-GridSizeY/2;y<GridSizeY/2;++y)
  123. {
  124. if ((cx+y)&0x00000001) //modulo 2
  125. {
  126. glColor4fv(colorA);
  127. //glColor3f(1,0,0);
  128. }else
  129. {
  130. glColor4fv(colorB);
  131. //glColor3f(0,0,1);
  132. }
  133. glVertex3f(x*SizeX,0,(y+1)*SizeY);
  134. glVertex3f(x*SizeX,0, y*SizeY);
  135. }
  136. }
  137. }
  138. glEnd();
  139. glGetFloatv(GL_LINE_WIDTH,&old_line_width);
  140. glLineWidth(old_line_width);
  141. (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
  142. (old_color_material? glEnable(GL_COLOR_MATERIAL) : glDisable(GL_COLOR_MATERIAL));
  143. }
  144. IGL_INLINE void igl::draw_floor_outline()
  145. {
  146. const float grey[] = {0.80,0.80,0.80,1.};
  147. const float white[] = {0.95,0.95,0.95,1.};
  148. igl::draw_floor_outline(grey,white);
  149. }
  150. #endif