draw_floor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. static const int GridSizeX = 100;
  12. static const int GridSizeY = 100;
  13. static const float SizeX = 0.5f;
  14. static const float SizeY = 0.5f;
  15. IGL_INLINE void igl::draw_floor(const float * colorA, const float * colorB)
  16. {
  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. {
  71. float old_line_width =0;
  72. // old settings
  73. int old_lighting=0,old_color_material=0;
  74. glGetIntegerv(GL_LIGHTING,&old_lighting);
  75. glGetIntegerv(GL_COLOR_MATERIAL,&old_color_material);
  76. glDisable(GL_LIGHTING);
  77. // Set material
  78. const float black[] = {0.,0.,0.,1.};
  79. glMaterialfv(GL_FRONT, GL_AMBIENT, black);
  80. glMaterialfv(GL_FRONT, GL_DIFFUSE, black);
  81. glMaterialfv(GL_FRONT, GL_SPECULAR, black);
  82. glMaterialfv(GL_FRONT, GL_EMISSION, black);
  83. glMaterialf(GL_FRONT, GL_SHININESS,0);
  84. const bool use_lighting = false;
  85. if(use_lighting)
  86. {
  87. glEnable(GL_LIGHTING);
  88. }else
  89. {
  90. glDisable(GL_LIGHTING);
  91. }
  92. glLineWidth(2.0f);
  93. glBegin(GL_LINES);
  94. for (int x =-GridSizeX/2;x<=GridSizeX/2;++x)
  95. {
  96. if(x!=(GridSizeX/2))
  97. {
  98. for(int s = -1;s<2;s+=2)
  99. {
  100. int y = s*(GridSizeY/2);
  101. int cy = y==(GridSizeY/2) ? y-1 : y;
  102. if ((x+cy)&0x00000001) //modulo 2
  103. {
  104. glColor4fv(colorA);
  105. //glColor3f(1,0,0);
  106. }else
  107. {
  108. glColor4fv(colorB);
  109. //glColor3f(0,0,1);
  110. }
  111. glVertex3f((x+1)*SizeX,0,y*SizeY);
  112. glVertex3f( x*SizeX,0,y*SizeY);
  113. }
  114. }
  115. if(x==-(GridSizeX/2) || x==(GridSizeX/2))
  116. {
  117. int cx = x==(GridSizeX/2) ? x-1 : x;
  118. for (int y =-GridSizeY/2;y<GridSizeY/2;++y)
  119. {
  120. if ((cx+y)&0x00000001) //modulo 2
  121. {
  122. glColor4fv(colorA);
  123. //glColor3f(1,0,0);
  124. }else
  125. {
  126. glColor4fv(colorB);
  127. //glColor3f(0,0,1);
  128. }
  129. glVertex3f(x*SizeX,0,(y+1)*SizeY);
  130. glVertex3f(x*SizeX,0, y*SizeY);
  131. }
  132. }
  133. }
  134. glEnd();
  135. glGetFloatv(GL_LINE_WIDTH,&old_line_width);
  136. glLineWidth(old_line_width);
  137. (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
  138. (old_color_material? glEnable(GL_COLOR_MATERIAL) : glDisable(GL_COLOR_MATERIAL));
  139. }
  140. IGL_INLINE void igl::draw_floor_outline()
  141. {
  142. const float grey[] = {0.80,0.80,0.80,1.};
  143. const float white[] = {0.95,0.95,0.95,1.};
  144. igl::draw_floor_outline(grey,white);
  145. }
  146. #endif