geometry.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "geometry.h"
  2. #include "program.h"
  3. #include <glplatform/glplatform-glcore.h>
  4. #include <stdint.h>
  5. #include <math.h>
  6. #include "lua.h"
  7. void buffer_type_init(struct buffer_type *t, struct attribute *attribute, int num_attributes)
  8. {
  9. glGenVertexArrays(1, &t->vao);
  10. glBindVertexArray(t->vao);
  11. int pos;
  12. for (pos = 0; pos < num_attributes; pos++) {
  13. struct attribute *attrib = attribute + pos;
  14. if (!attribute[pos].size)
  15. continue;
  16. glEnableVertexAttribArray(pos);
  17. glVertexAttribBinding(pos, attrib->binding_index);
  18. if (attribute[pos].integer) {
  19. glVertexAttribIFormat(pos,
  20. attribute[pos].size,
  21. attribute[pos].type,
  22. attribute[pos].offset);
  23. } else {
  24. glVertexAttribFormat(pos,
  25. attribute[pos].size,
  26. attribute[pos].type,
  27. GL_FALSE,
  28. attribute[pos].offset);
  29. }
  30. }
  31. t->valid = 1;
  32. }
  33. #if 0
  34. struct buffer_type buffer_type_v3f = {
  35. .element_size = 3 * sizeof(float),
  36. .attribute = {
  37. [ATTRIBUTE_VERTEX] = {
  38. .integer = false,
  39. .size = 3,
  40. .type = GL_FLOAT,
  41. .offset = 0
  42. }
  43. }
  44. };
  45. #endif
  46. void create_geometry_begin(struct geometry *g, void ** const vertex_buffer, uint16_t ** const index_buffer)
  47. {
  48. glGenBuffers(1, &g->vertex_array);
  49. glGenBuffers(1, &g->index_array);
  50. glBindBuffer(GL_ARRAY_BUFFER, g->vertex_array);
  51. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g->index_array);
  52. glBufferStorage(GL_ARRAY_BUFFER,
  53. g->type->element_size[0] * g->num_verticies,
  54. NULL,
  55. GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
  56. glBufferStorage(GL_ELEMENT_ARRAY_BUFFER,
  57. sizeof(uint16_t) * 3 * g->num_triangles,
  58. NULL,
  59. GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
  60. *vertex_buffer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
  61. *index_buffer = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
  62. }
  63. void create_geometry_end(struct geometry *g)
  64. {
  65. glUnmapBuffer(GL_ARRAY_BUFFER);
  66. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  67. }
  68. void delete_geometry(struct geometry *g)
  69. {
  70. glDeleteBuffers(1, &g->vertex_array);
  71. glDeleteBuffers(1, &g->index_array);
  72. }
  73. #if 0
  74. void geometry_init()
  75. {
  76. buffer_type_init(&buffer_type_v3f);
  77. }
  78. //
  79. // m: Number of subdivisions of the unit circle
  80. // n: Number of stacks
  81. //
  82. void create_cylinder_geometry(struct geometry *g, int m, int n)
  83. {
  84. g->num_triangles = 2 * (m * (n + 1) - 1);
  85. g->num_verticies = m * (n + 1);
  86. g->type = &buffer_type_v3f;
  87. uint16_t *idx;
  88. float *r;
  89. create_geometry_begin(g, (void **)&r, &idx);
  90. int i, j;
  91. for (j = 0; j < (n + 1); j++) {
  92. float z = 1 - j * (2.0 / n);
  93. for (i = 0; i < m; i++) {
  94. float theta = i * ((2 * M_PI) / m);
  95. float x = sin(theta);
  96. float y = cos(theta);
  97. r[0] = x;
  98. r[1] = y;
  99. r[2] = z;
  100. r += 3;
  101. if (j < n) {
  102. idx[0] = m * j + i;
  103. idx[1] = m * (j + 1) + i;
  104. idx[2] = m * j + ((i + 1) % m);
  105. idx[3] = idx[1];
  106. idx[4] = idx[2];
  107. idx[5] = m * (j + 1) + ((i + 1) % m);
  108. idx += 6;
  109. }
  110. }
  111. }
  112. for (j = 1; j < m; j++) {
  113. idx[0] = 0;
  114. idx[1] = j;
  115. idx[2] = (j + 1) % m;
  116. idx[3] = idx[0] + m * n;
  117. idx[4] = idx[1] + m * n;
  118. idx[5] = idx[2] + m * n;
  119. idx += 6;
  120. }
  121. create_geometry_end(g);
  122. }
  123. void create_cone_geometry(struct geometry *g, int n)
  124. {
  125. g->num_triangles = n * 2;
  126. g->num_verticies = n + 1;
  127. g->type = &buffer_type_v3f;
  128. uint16_t *idx;
  129. float *r;
  130. create_geometry_begin(g, (void **)&r, &idx);
  131. int i;
  132. for (i = 0; i < n; i++) {
  133. float theta = i * ((2 * M_PI) / n);
  134. float x = sin(theta);
  135. float z = cos(theta);
  136. float y = -1;
  137. r[0] = x;
  138. r[1] = y;
  139. r[2] = z;
  140. r += 3;
  141. idx[0] = i;
  142. idx[1] = n;
  143. idx[2] = (i + 1) % n;
  144. idx[3] = 0;
  145. idx[4] = (i + 1) % n;
  146. idx[5] = (i + 2) % n;
  147. idx += 6;
  148. }
  149. r[0] = 0;
  150. r[1] = 1;
  151. r[2] = 0;
  152. create_geometry_end(g);
  153. }
  154. #endif
  155. void render_geometry(struct geometry *g)
  156. {
  157. glBindVertexArray(g->type->vao);
  158. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g->index_array);
  159. glBindVertexBuffer(0, g->vertex_array, 0, g->type->element_size[0]);
  160. glDrawElements(GL_TRIANGLES,
  161. g->triangle_count * 3,
  162. GL_UNSIGNED_SHORT,
  163. (void *)(g->triangle_no * 3 * sizeof(uint16_t)));
  164. }