PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/2/blockworld_using_builtin_transformation_functions/blockworld_complex_figures.c

https://github.com/rajatkhanduja/Dabblings-in-OpenGL
C | 257 lines | 213 code | 32 blank | 12 comment | 9 complexity | 03e23b881d6d5b2961c53cb5ab712de5 MD5 | raw file
  1. #include <blockworld.h>
  2. #include <GL/glut.h>
  3. #include <GL/gl.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #define DIFF 0.05
  7. #define SHELTER_DIFF 0.05
  8. #define WINDOW_BORDER 0.1
  9. #define ROOF_EXT 0.08
  10. #define B_FACTOR 1.4
  11. GLdouble view_angle = 0;
  12. static void bwRoof (GLdouble base_l, GLdouble base_b,
  13. GLdouble height, GLdouble cuboid_height)
  14. {
  15. GLdouble cur_height = 0;
  16. GLint i = 0;
  17. while (height - cur_height > 0)
  18. {
  19. glPushMatrix ();
  20. glTranslated ( -ROOF_EXT + (i * cuboid_height * B_FACTOR) / 2 , cur_height, 0);
  21. /* Using bwCuboid gives different behavior */
  22. if (height - cur_height > cuboid_height)
  23. bwCuboid( base_l + ROOF_EXT * 2, base_b, cuboid_height);
  24. else
  25. bwCuboid (base_l + ROOF_EXT * 2, base_b, (height - cur_height));
  26. glPopMatrix ();
  27. cur_height += cuboid_height;
  28. base_l -= cuboid_height * B_FACTOR;
  29. base_b -= cuboid_height;
  30. i++;
  31. }
  32. }
  33. #define COLRANDRGB ((rand() % 501) / 1000.0f + 0.5) // [0.5, 1.0]
  34. #define SIZESMOKEPARTICLE 0.01d
  35. static void bwSmokeParticle()
  36. {
  37. GLfloat col = COLRANDRGB, texturesmoke[] = {col, col, col, 1.0f};
  38. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, texturesmoke);
  39. bwCube(SIZESMOKEPARTICLE);
  40. }
  41. #define SIZESMOKECLOUD 0.4d
  42. void bwSmoke()
  43. {
  44. GLdouble i, j, k;
  45. for(i = 0.0d; i < SIZESMOKECLOUD; i += SIZESMOKEPARTICLE)
  46. for(j = 0.0d; j < SIZESMOKECLOUD; j += SIZESMOKEPARTICLE)
  47. for(k = 0.0d; k < SIZESMOKECLOUD; k += SIZESMOKEPARTICLE)
  48. {
  49. if((rand() % 1000) > 10) // 1% probabilistic density
  50. continue;
  51. glPushMatrix();
  52. glTranslated(i, j, k);
  53. bwSmokeParticle();
  54. glPopMatrix();
  55. }
  56. }
  57. void bwHouse (void)
  58. {
  59. GLdouble base1, base2_l, base2_b;
  60. GLdouble height_cuboid1, height_cuboid2;
  61. GLdouble height_pyramid;
  62. GLdouble door_width, door_height;
  63. GLdouble chimney_base, chimney_height;
  64. GLdouble door_knob_size;
  65. GLdouble window_edge, window_height;
  66. GLdouble glass_edge;
  67. static GLfloat door_texture[] = { 0.33, 0.17, 0.03, 1};
  68. static GLfloat pyramid_texture[] = { 0.7, 0.3, 0.3, 1};
  69. static GLfloat cuboid1_texture[] = { 0.9, 0.6, 0.4, 1};
  70. static GLfloat cuboid2_texture[] = { 0.7, 0.7, 0.5, 1};
  71. static GLfloat chimney_texture[] = { 0.3, 0.3, 0.3, 1};
  72. static GLfloat knob_texture[] = { 1.0, 1.0, 0.0, 1};
  73. static GLfloat window_texture[] = { 0.0, 0.0, 0.0, 1};
  74. static GLfloat glass_texture[] = { 1.0, 1.0, 1.0, 1};
  75. base1 = 4;
  76. base2_l = 5;
  77. base2_b = 3;
  78. height_cuboid1 = 6;
  79. height_cuboid2 = 6;
  80. height_pyramid = 2 ;
  81. door_width = 1.5;
  82. door_height = height_cuboid1 / 2;
  83. chimney_base = 0.5;
  84. chimney_height = 1.9;
  85. door_knob_size = 0.1;
  86. window_edge = 1.8;
  87. window_height = 2.5;
  88. glass_edge = (window_edge - 3 * WINDOW_BORDER) / 2;
  89. // Create one cuboid and place roof on top
  90. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, cuboid1_texture);
  91. //bwRectangle (base1, base1, height_cuboid1);
  92. bwCuboid(base1, base1, height_cuboid1);
  93. glPushMatrix ();
  94. glTranslated (0, height_cuboid1, 0);
  95. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pyramid_texture);
  96. bwRoof (base1 + base2_l, base1 + 3 * SHELTER_DIFF, height_pyramid, 0.05);
  97. glPopMatrix ();
  98. // Create the second cuboid
  99. glPushMatrix ();
  100. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, cuboid2_texture);
  101. glTranslated (base1, 0, 0);
  102. //bwRectangle (base2_l, base1, height_cuboid2);
  103. bwCuboid(base2_l, base1, height_cuboid2);
  104. glPopMatrix ();
  105. // Create a rectangle (as door)
  106. GLdouble z_deviation = base1 + DIFF;
  107. glBegin (GL_POLYGON);
  108. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, door_texture);
  109. glVertex3f (door_width/2 + base1/2, 0 + DIFF,z_deviation);
  110. glVertex3f (door_width/2 + base1/2, door_height, z_deviation);
  111. glVertex3f (-door_width/2 + base1/2, door_height, z_deviation);
  112. glVertex3f (-door_width/2 + base1/2, 0 + DIFF, z_deviation);
  113. glEnd ();
  114. // Door knob
  115. glPushMatrix ();
  116. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, knob_texture);
  117. glTranslated ( base1 / 2 - door_width /3, door_height / 2, z_deviation);
  118. bwCube (door_knob_size);
  119. glPopMatrix ();
  120. // Create a square as a Window
  121. glBegin (GL_POLYGON);
  122. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, window_texture);
  123. GLdouble x_deviation = base1 + base2_l / 2;
  124. GLdouble y_deviation;
  125. glVertex3f (x_deviation - window_edge / 2, window_height, z_deviation);
  126. glVertex3f (x_deviation - window_edge / 2, window_height + window_edge, z_deviation);
  127. glVertex3f (x_deviation + window_edge / 2, window_height + window_edge, z_deviation);
  128. glVertex3f (x_deviation + window_edge / 2, window_height , z_deviation);
  129. glEnd();
  130. glBegin (GL_POLYGON);
  131. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glass_texture);
  132. x_deviation += (-window_edge / 2) + WINDOW_BORDER;
  133. z_deviation += DIFF/10;
  134. y_deviation = window_height + WINDOW_BORDER;
  135. glVertex3f (x_deviation, y_deviation, z_deviation);
  136. glVertex3f (x_deviation, y_deviation + glass_edge, z_deviation);
  137. glVertex3f (x_deviation + glass_edge, y_deviation + glass_edge, z_deviation);
  138. glVertex3f (x_deviation + glass_edge, y_deviation , z_deviation);
  139. glEnd();
  140. glBegin (GL_POLYGON);
  141. y_deviation = window_height + 2 * WINDOW_BORDER + glass_edge;
  142. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glass_texture);
  143. glVertex3f (x_deviation, y_deviation, z_deviation);
  144. glVertex3f (x_deviation, y_deviation + glass_edge - WINDOW_BORDER, z_deviation);
  145. glVertex3f (x_deviation + glass_edge, y_deviation + glass_edge - WINDOW_BORDER, z_deviation);
  146. glVertex3f (x_deviation + glass_edge, y_deviation , z_deviation);
  147. glEnd();
  148. glBegin (GL_POLYGON);
  149. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glass_texture);
  150. x_deviation += glass_edge + WINDOW_BORDER;
  151. y_deviation = window_height + WINDOW_BORDER;
  152. glVertex3f (x_deviation, y_deviation, z_deviation);
  153. glVertex3f (x_deviation, y_deviation + glass_edge, z_deviation);
  154. glVertex3f (x_deviation + glass_edge, y_deviation + glass_edge, z_deviation);
  155. glVertex3f (x_deviation + glass_edge, y_deviation , z_deviation);
  156. glEnd();
  157. glBegin (GL_POLYGON);
  158. y_deviation = window_height + 2 * WINDOW_BORDER + glass_edge;
  159. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glass_texture);
  160. glVertex3f (x_deviation, y_deviation, z_deviation);
  161. glVertex3f (x_deviation, y_deviation + glass_edge - WINDOW_BORDER, z_deviation);
  162. glVertex3f (x_deviation + glass_edge, y_deviation + glass_edge - WINDOW_BORDER, z_deviation);
  163. glVertex3f (x_deviation + glass_edge, y_deviation , z_deviation);
  164. glEnd();
  165. // Create a chimney
  166. glPushMatrix ();
  167. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, chimney_texture);
  168. glTranslated ((2) * base1, height_cuboid2, base2_l / 3);
  169. bwRectangle2 (chimney_base, chimney_base, chimney_height);
  170. glPopMatrix ();
  171. // Create Smoke clouds above chimney
  172. glPushMatrix ();
  173. glTranslated ((2) * base1, height_cuboid2 + chimney_height * 1.2, base2_l / 5);
  174. bwSmoke();
  175. glPopMatrix ();
  176. glPushMatrix ();
  177. glTranslated ((2) * base1, height_cuboid2 + chimney_height * 1.3, base2_l / 20);
  178. bwSmoke();
  179. glPopMatrix ();
  180. glPushMatrix ();
  181. glTranslated ((2.02) * base1, height_cuboid2 + chimney_height * 1.4, 0);
  182. bwSmoke();
  183. glPopMatrix ();
  184. }
  185. #define RANDGRAY (rand() % 10) / 100.0 + 0.2 //[0.2, 0.3]
  186. /* Function to draw a single segment of road length */
  187. void bwRoadSegment (GLdouble length, GLdouble width, GLdouble height)
  188. {
  189. GLdouble cuboid_width = 0.75;
  190. GLdouble cur_width = 0;
  191. static GLfloat road_texture[] = {0.3, 0.3, 0.3, 1};
  192. static char first = 1;
  193. if (first)
  194. {
  195. srand(time(NULL));
  196. first = 0;
  197. }
  198. #define RESET_VAL() road_texture[0] = road_texture[1] = road_texture[2] = \
  199. RANDGRAY;
  200. #define PRINT_VALS() printf ("%g %g %g\n", road_texture[0], road_texture[1], road_texture[2]);
  201. while ( cur_width + cuboid_width <= width )
  202. {
  203. glPushMatrix ();
  204. glTranslated (0, 0, cur_width);
  205. glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, road_texture);
  206. bwCuboid (length, cuboid_width, height);
  207. glPopMatrix ();
  208. cur_width += cuboid_width;
  209. RESET_VAL();
  210. }
  211. }
  212. void bwRoad (GLdouble length, GLdouble width, GLdouble height)
  213. {
  214. GLdouble layer_length = 0.5;
  215. GLdouble cur_length = 0;
  216. while ( cur_length < length )
  217. {
  218. glPushMatrix ();
  219. glTranslated (cur_length, 0, 0);
  220. // bwCuboid (layer_length, width, height);
  221. bwRoadSegment (layer_length, width, height);
  222. glPopMatrix ();
  223. cur_length += layer_length;
  224. }
  225. }