sr.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef SR_H
  2. #define SR_H
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #define SR_MAX_ATTRIBUTE_COUNT 32
  6. #define SR_MAX_LIGHT_COUNT 8
  7. #define SR_WINDING_ORDER_CCW 1
  8. #define SR_WINDING_ORDER_CW -1
  9. enum sr_clip_plane {
  10. SR_CLIP_LEFT_PLANE = 1 << 0,
  11. SR_CLIP_BOTTOM_PLANE = 1 << 1,
  12. SR_CLIP_NEAR_PLANE = 1 << 2,
  13. SR_CLIP_RIGHT_PLANE = 1 << 3,
  14. SR_CLIP_TOP_PLANE = 1 << 4
  15. };
  16. enum sr_primitive {
  17. SR_POINT_LIST,
  18. SR_LINE_LIST,
  19. SR_LINE_STRIP,
  20. SR_TRIANGLE_LIST,
  21. SR_TRIANGLE_STRIP
  22. };
  23. enum sr_matrix_mode {
  24. SR_MODEL_MATRIX,
  25. SR_VIEW_MATRIX,
  26. SR_PROJECTION_MATRIX,
  27. SR_MVP_MATRIX
  28. };
  29. enum sr_light {
  30. SR_LIGHT_1 = 0,
  31. SR_LIGHT_2 = 1,
  32. SR_LIGHT_3 = 2,
  33. SR_LIGHT_4 = 3,
  34. SR_LIGHT_5 = 4,
  35. SR_LIGHT_6 = 5,
  36. SR_LIGHT_7 = 6,
  37. SR_LIGHT_8 = 7,
  38. };
  39. enum sr_light_attr {
  40. SR_TYPE,
  41. SR_POSITION,
  42. SR_COLOR,
  43. SR_AMBIENT,
  44. SR_DIFFUSE,
  45. SR_SPECULAR,
  46. SR_BLEND,
  47. SR_SHININESS,
  48. SR_DIRECTION,
  49. SR_SPOT_ANGLE,
  50. SR_SPOT_PENUMBRA,
  51. SR_CONSTANT_ATTENUATION,
  52. SR_LINEAR_ATTENUATION,
  53. SR_QUADRATIC_ATTENUATION
  54. };
  55. enum sr_light_type {
  56. SR_DIRECTIONAL,
  57. SR_POINT,
  58. SR_SPOT
  59. };
  60. /*********************************************************************
  61. * *
  62. * shader function pointers *
  63. * *
  64. *********************************************************************/
  65. typedef void (*fs_f)(uint32_t* out, float* in, void* uniform);
  66. /******************
  67. * sr_framebuffer *
  68. ******************/
  69. /* interface to whatever writes to the screen */
  70. struct sr_framebuffer {
  71. uint32_t* colors;
  72. float* depths;
  73. int width;
  74. int height;
  75. };
  76. /***************
  77. * sr_pipeline *
  78. ***************/
  79. /**
  80. * stores all data required to pre-allocate
  81. * memory required to render an indexed list of
  82. * arbitrary length
  83. */
  84. struct sr_pipeline {
  85. struct sr_framebuffer* fbuf;
  86. void* uniform;
  87. vs_f vs;
  88. fs_f fs;
  89. float* pts_in;
  90. int n_pts;
  91. int n_attr_in;
  92. int n_attr_out;
  93. int winding;
  94. };
  95. /*********************************************************************
  96. * *
  97. * render pipeline *
  98. * *
  99. *********************************************************************/
  100. /* render interface */
  101. void sr_bind_vertices(float* pts, int n_pts, int n_attr);
  102. void sr_bind_framebuffer(int width, int height, uint32_t* colors, float* depths);
  103. void sr_bind_uniform(void* uniform);
  104. void sr_restore_uniform();
  105. void sr_bind_texture(uint32_t* colors, int width, int height);
  106. void sr_bind_base_color(float r, float g, float b);
  107. void sr_renderl(int* indices, int n_indices, enum sr_primitive prim_type);
  108. void sr_render(struct sr_pipeline* pipe, int* indices,
  109. int n_indices, enum sr_primitive prim_type);
  110. /*********************************************************************
  111. * *
  112. * light interface *
  113. * *
  114. *********************************************************************/
  115. void sr_light(enum sr_light slot, enum sr_light_attr attr, float* data);
  116. void sr_glight(enum sr_light_attr attr, float* data);
  117. void sr_light_type(enum sr_light slot, enum sr_light_type type);
  118. void sr_light_enable(enum sr_light slot);
  119. void sr_light_disable(enum sr_light slot);
  120. void sr_material(enum sr_light_attr attr, float* data);
  121. /*********************************************************************
  122. * *
  123. * matrix stack interface *
  124. * *
  125. *********************************************************************/
  126. void sr_matrix_mode(enum sr_matrix_mode mode);
  127. void sr_dump_matrix(float* dest);
  128. void sr_load_matrix(float* src);
  129. void sr_load_identity();
  130. void sr_perspective(float fovy, float aspect, float near, float far);
  131. void sr_frustum(float left, float right, float bottom,
  132. float top, float near, float far);
  133. void sr_translate(float x, float y, float z);
  134. void sr_rotate_x(float t);
  135. void sr_rotate_y(float t);
  136. void sr_rotate_z(float t);
  137. void sr_scale(float sx, float sy, float sz);
  138. void sr_look_at(float ex, float ey, float ez,
  139. float cx, float cy, float cz,
  140. float ux, float uy, float uz);
  141. /*********************************************************************
  142. * *
  143. * prebuilt shader bindings *
  144. * *
  145. *********************************************************************/
  146. void sr_bind_custom_vs(vs_f vs, int n_attr_out);
  147. void sr_bind_custom_fs(fs_f fs);
  148. void sr_bind_color_vs();
  149. void sr_bind_color_fs();
  150. void sr_bind_texture_vs();
  151. void sr_bind_texture_fs();
  152. void sr_bind_std_vs();
  153. void sr_bind_phong_fs();
  154. #endif /* SR_H */