draw_floor.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include "../opengl/OpenGL_convenience.h"
  10. IGL_INLINE void igl::opengl2::draw_floor(const float * colorA, const float * colorB,
  11. const int GridSizeX,
  12. const int GridSizeY)
  13. {
  14. const float SizeX = 0.5f*100./(double)GridSizeX;
  15. const float SizeY = 0.5f*100./(double)GridSizeY;
  16. // old settings
  17. int old_lighting=0,old_color_material=0;
  18. glGetIntegerv(GL_LIGHTING,&old_lighting);
  19. glGetIntegerv(GL_COLOR_MATERIAL,&old_color_material);
  20. glDisable(GL_LIGHTING);
  21. glColorMaterial( GL_FRONT, GL_EMISSION);
  22. glEnable(GL_COLOR_MATERIAL);
  23. glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  24. // Set material
  25. const float black[] = {0.,0.,0.,1.};
  26. glMaterialfv(GL_FRONT, GL_AMBIENT, black);
  27. glMaterialfv(GL_FRONT, GL_DIFFUSE, black);
  28. glMaterialfv(GL_FRONT, GL_SPECULAR, black);
  29. glMaterialfv(GL_FRONT, GL_EMISSION, black);
  30. glMaterialf(GL_FRONT, GL_SHININESS,0);
  31. const bool use_lighting = false;
  32. if(use_lighting)
  33. {
  34. glEnable(GL_LIGHTING);
  35. }else
  36. {
  37. glDisable(GL_LIGHTING);
  38. }
  39. glBegin(GL_QUADS);
  40. glNormal3f(0,1,0);
  41. for (int x =-GridSizeX/2;x<GridSizeX/2;++x)
  42. {
  43. for (int y =-GridSizeY/2;y<GridSizeY/2;++y)
  44. {
  45. if ((x+y)&0x00000001) //modulo 2
  46. {
  47. glColor4fv(colorA);
  48. }else
  49. {
  50. glColor4fv(colorB);
  51. }
  52. glVertex3f( x*SizeX,0,(y+1)*SizeY);
  53. glVertex3f((x+1)*SizeX,0,(y+1)*SizeY);
  54. glVertex3f((x+1)*SizeX,0, y*SizeY);
  55. glVertex3f( x*SizeX,0, y*SizeY);
  56. }
  57. }
  58. glEnd();
  59. (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
  60. (old_color_material? glEnable(GL_COLOR_MATERIAL) : glDisable(GL_COLOR_MATERIAL));
  61. }
  62. IGL_INLINE void igl::opengl2::draw_floor()
  63. {
  64. const float grey[] = {0.80,0.80,0.80,1.};
  65. const float white[] = {0.95,0.95,0.95,1.};
  66. igl::opengl2::draw_floor(grey,white);
  67. }
  68. IGL_INLINE void igl::opengl2::draw_floor_outline(const float * colorA, const float * colorB,
  69. const int GridSizeX,
  70. const int GridSizeY)
  71. {
  72. const float SizeX = 0.5f*100./(double)GridSizeX;
  73. const float SizeY = 0.5f*100./(double)GridSizeY;
  74. float old_line_width =0;
  75. // old settings
  76. int old_lighting=0,old_color_material=0;
  77. glGetIntegerv(GL_LIGHTING,&old_lighting);
  78. glGetIntegerv(GL_COLOR_MATERIAL,&old_color_material);
  79. glDisable(GL_LIGHTING);
  80. // Set material
  81. const float black[] = {0.,0.,0.,1.};
  82. glMaterialfv(GL_FRONT, GL_AMBIENT, black);
  83. glMaterialfv(GL_FRONT, GL_DIFFUSE, black);
  84. glMaterialfv(GL_FRONT, GL_SPECULAR, black);
  85. glMaterialfv(GL_FRONT, GL_EMISSION, black);
  86. glMaterialf(GL_FRONT, GL_SHININESS,0);
  87. const bool use_lighting = false;
  88. if(use_lighting)
  89. {
  90. glEnable(GL_LIGHTING);
  91. }else
  92. {
  93. glDisable(GL_LIGHTING);
  94. }
  95. glLineWidth(2.0f);
  96. glBegin(GL_LINES);
  97. for (int x =-GridSizeX/2;x<=GridSizeX/2;++x)
  98. {
  99. if(x!=(GridSizeX/2))
  100. {
  101. for(int s = -1;s<2;s+=2)
  102. {
  103. int y = s*(GridSizeY/2);
  104. int cy = y==(GridSizeY/2) ? y-1 : y;
  105. if ((x+cy)&0x00000001) //modulo 2
  106. {
  107. glColor4fv(colorA);
  108. //glColor3f(1,0,0);
  109. }else
  110. {
  111. glColor4fv(colorB);
  112. //glColor3f(0,0,1);
  113. }
  114. glVertex3f((x+1)*SizeX,0,y*SizeY);
  115. glVertex3f( x*SizeX,0,y*SizeY);
  116. }
  117. }
  118. if(x==-(GridSizeX/2) || x==(GridSizeX/2))
  119. {
  120. int cx = x==(GridSizeX/2) ? x-1 : x;
  121. for (int y =-GridSizeY/2;y<GridSizeY/2;++y)
  122. {
  123. if ((cx+y)&0x00000001) //modulo 2
  124. {
  125. glColor4fv(colorA);
  126. //glColor3f(1,0,0);
  127. }else
  128. {
  129. glColor4fv(colorB);
  130. //glColor3f(0,0,1);
  131. }
  132. glVertex3f(x*SizeX,0,(y+1)*SizeY);
  133. glVertex3f(x*SizeX,0, y*SizeY);
  134. }
  135. }
  136. }
  137. glEnd();
  138. glGetFloatv(GL_LINE_WIDTH,&old_line_width);
  139. glLineWidth(old_line_width);
  140. (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
  141. (old_color_material? glEnable(GL_COLOR_MATERIAL) : glDisable(GL_COLOR_MATERIAL));
  142. }
  143. IGL_INLINE void igl::opengl2::draw_floor_outline()
  144. {
  145. const float grey[] = {0.80,0.80,0.80,1.};
  146. const float white[] = {0.95,0.95,0.95,1.};
  147. igl::opengl2::draw_floor_outline(grey,white);
  148. }
  149. #ifdef IGL_STATIC_LIBRARY
  150. // Explicit template specialization
  151. #endif