draw_floor.cpp 3.9 KB

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