Guest User

Untitled

a guest
Jul 26th, 2026
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #ifndef VERTEX_STAGE_HPP
  2. #define VERTEX_STAGE_HPP
  3.  
  4. #include <glm/mat4x4.hpp>
  5.  
  6. #define GLM_ENABLE_EXPERIMENTAL
  7.  
  8. #include <glm/gtx/string_cast.hpp>
  9. #include <glm/vec4.hpp>
  10. #include <cstdint>
  11. #include <vector>
  12. #include <utility>
  13. #include <iostream>
  14. #include <tuple>
  15.  
  16. #define MAX_ATTRIBS 16
  17.  
  18. #define VERTEX_ATTRIBUTE 0
  19. #define COLOR_RGBA_ATTRIBUTE 1
  20. #define COLOR_RGB_ATTRIBUTE 2
  21. #define TEXTURE_COORD_ATTRIBUTE 3
  22.  
  23. #define POINT_PRIMITIVE 0
  24. #define LINE_PRIMITIVE 1
  25. #define TRIANGLE_PRIMITIVE 2
  26.  
  27. namespace _vertex {
  28.  
  29. struct VertexAttrib {
  30. glm::vec4 data;
  31. uint8_t type;
  32. };
  33.  
  34.  
  35. struct VertexAttribData {
  36. size_t position;
  37. uint8_t type;
  38. };
  39.  
  40.  
  41. struct Vertex {
  42. Vertex(const float *data, const std::vector<VertexAttribData> &format) {
  43. attribs.fill((VertexAttrib){glm::vec4(0.0f), 0});
  44.  
  45. for (size_t i = 0, j = 0; i < format.size(); ++i) {
  46. VertexAttrib attribute;
  47. attribute.type = format[i].type;
  48. size_t position = format[i].position;
  49.  
  50. switch (attribute.type) {
  51. case VERTEX_ATTRIBUTE:
  52. coords = glm::vec4(data[position], data[position + 1], data[position + 2], 1.0f);
  53. break;
  54. case COLOR_RGBA_ATTRIBUTE:
  55. attribute.data = glm::vec4(data[position], data[position + 1], data[position + 2],
  56. data[position + 3]);
  57. attribs[j++] = attribute;
  58.  
  59. break;
  60. case COLOR_RGB_ATTRIBUTE:
  61. attribute.data = glm::vec4(data[position], data[position + 1], data[position + 2], 1.0f);
  62. attribs[j++] = attribute;
  63.  
  64. break;
  65. case TEXTURE_COORD_ATTRIBUTE:
  66. attribute.data = glm::vec4(data[position], data[position + 1], 0.0f, 1.0f);
  67. attribs[j++] = attribute;
  68.  
  69. break;
  70. }
  71. }
  72. }
  73.  
  74. Vertex() {}
  75.  
  76. glm::vec4 coords;
  77. std::array<VertexAttrib, MAX_ATTRIBS> attribs;
  78. };
  79.  
  80.  
  81. extern std::vector<Vertex> vertex_data;
  82.  
  83.  
  84. struct Primitive {
  85. uint8_t type;
  86. std::array<Vertex, 3> vertices;
  87. };
  88.  
  89.  
  90. class VertexStage {
  91. public:
  92. VertexStage(int x, int y, int width, int height) :
  93. width{width}, height{height} {};
  94.  
  95. void set_proj_matrix(const glm::mat4 &matrix) {
  96. proj_matrix = matrix;
  97. };
  98.  
  99. void set_view_matrix(const glm::mat4 &matrix) {
  100. view_matrix = matrix;
  101. };
  102.  
  103. void set_model_matrix(const glm::mat4 &matrix) {
  104. model_matrix = matrix;
  105. };
  106.  
  107. void process_vertex_data(uint8_t primitive_type, const std::vector<Vertex> &vertex_data);
  108.  
  109. std::vector<Primitive> &get_primitives() {
  110. return primitive_list;
  111. };
  112.  
  113. private:
  114. void do_object_to_clip_space_transform(glm::vec4 &vertex) {
  115. vertex = proj_matrix * view_matrix * model_matrix * vertex;
  116. }
  117.  
  118. void do_clip_to_viewport_transform(_vertex::Vertex &vertex) {
  119. for (_vertex::VertexAttrib &attrib : vertex.attribs) {
  120. if (!attrib.type) {
  121. break;
  122. }
  123.  
  124. attrib.data.x /= vertex.coords.w;
  125. attrib.data.y /= vertex.coords.w;
  126.  
  127. if (attrib.type == TEXTURE_COORD_ATTRIBUTE) {
  128. attrib.data.w /= vertex.coords.w;
  129. }
  130. }
  131.  
  132. vertex.coords /= vertex.coords.w;
  133. vertex.coords.z = 1 / vertex.coords.z;
  134. vertex.coords.x = ((width / 2) * vertex.coords.x) + ((width - 1) / 2);
  135. vertex.coords.y = (1.0f - (vertex.coords.y * 0.5f + 0.5f)) * height;
  136. }
  137.  
  138. std::vector<Primitive> primitive_list;
  139. glm::mat4 proj_matrix;
  140. glm::mat4 view_matrix;
  141. glm::mat4 model_matrix;
  142. int width;
  143. int height;
  144. };
  145. }
  146.  
  147. #endif
Add Comment
Please, Sign In to add comment