main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include <gs.h>
  2. /*================================================================
  3. * Copyright: 2020 John Jackson
  4. * simple_texture
  5. The purpose of this example is to demonstrate how to load textures from file and
  6. explicitly construct a GPU resources to use for your application.
  7. Included:
  8. * Constructing textures via tetxure despcriptors
  9. * Construct vertex/index buffers from user defined declarations
  10. * Construct shaders from source
  11. * Rendering via command buffers
  12. Press `esc` to exit the application.
  13. =================================================================*/
  14. // Globals
  15. gs_global gs_shader_t g_shader = gs_default_val();
  16. gs_global gs_uniform_t u_model = gs_default_val();
  17. gs_global gs_uniform_t u_view = gs_default_val();
  18. gs_global gs_uniform_t u_proj = gs_default_val();
  19. gs_global gs_uniform_t u_tex = gs_default_val();
  20. gs_global gs_index_buffer_t g_ibo = gs_default_val();
  21. gs_global gs_vertex_buffer_t g_vbo = gs_default_val();
  22. gs_global gs_command_buffer_t g_cb = gs_default_val();
  23. gs_global gs_texture_t g_tex = gs_default_val();
  24. gs_global gs_camera_t g_camera = gs_default_val();
  25. const char* v_src = "\n"
  26. "#version 330 core\n"
  27. "layout(location = 0) in vec2 a_pos;\n"
  28. "layout(location = 1) in vec2 a_uv;\n"
  29. "uniform mat4 u_model;\n"
  30. "uniform mat4 u_view;\n"
  31. "uniform mat4 u_proj;\n"
  32. "out vec2 uv;\n"
  33. "void main()\n"
  34. "{\n"
  35. " gl_Position = u_proj * u_view * u_model * vec4(a_pos, 0.0, 1.0);\n"
  36. " uv = a_uv;\n"
  37. "}";
  38. const char* f_src = "\n"
  39. "#version 330 core\n"
  40. "uniform sampler2D u_tex;"
  41. "in vec2 uv;\n"
  42. "out vec4 frag_color;\n"
  43. "void main()\n"
  44. "{\n"
  45. " frag_color = texture(u_tex, uv);\n"
  46. "}";
  47. // Forward Decls.
  48. gs_result app_init(); // Use to init your application
  49. gs_result app_update(); // Use to update your application
  50. gs_result app_shutdown(); // Use to shutdown your appliaction
  51. int main(int argc, char** argv)
  52. {
  53. gs_application_desc_t app = gs_default_val();
  54. app.window_title = "Simple Texture";
  55. app.window_width = 800;
  56. app.window_height = 600;
  57. app.init = &app_init;
  58. app.update = &app_update;
  59. app.shutdown = &app_shutdown;
  60. // Construct internal instance of our engine
  61. gs_engine_t* engine = gs_engine_construct(app);
  62. // Run the internal engine loop until completion
  63. gs_result res = engine->run();
  64. // Check result of engine after exiting loop
  65. if (res != gs_result_success)
  66. {
  67. gs_println("Error: Engine did not successfully finish running.");
  68. return -1;
  69. }
  70. gs_println("Gunslinger exited successfully.");
  71. return 0;
  72. }
  73. // Here, we'll initialize all of our application data, which in this case is our graphics resources
  74. gs_result app_init()
  75. {
  76. // Cache instance of graphics/platform apis from engine
  77. gs_graphics_i* gfx = gs_engine_instance()->ctx.graphics;
  78. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  79. // Construct command buffer (the command buffer is used to allow for immediate drawing at any point in our program)
  80. g_cb = gs_command_buffer_new();
  81. // Construct shader from our source above
  82. g_shader = gfx->construct_shader(v_src, f_src);
  83. // Construct uniform for shader
  84. u_view = gfx->construct_uniform(g_shader, "u_view", gs_uniform_type_mat4);
  85. u_model = gfx->construct_uniform(g_shader, "u_model", gs_uniform_type_mat4);
  86. u_proj = gfx->construct_uniform(g_shader, "u_proj", gs_uniform_type_mat4);
  87. u_tex = gfx->construct_uniform(g_shader, "u_tex", gs_uniform_type_sampler2d);
  88. // Vertex data layout for our mesh
  89. gs_vertex_attribute_type layout[] = {
  90. gs_vertex_attribute_float2, // Position
  91. gs_vertex_attribute_float2 // UV
  92. };
  93. // Vertex data for quad
  94. f32 v_data[] =
  95. {
  96. // Positions UVs
  97. -0.5f, -0.5f, 0.0f, 0.0f, // Top Left
  98. 0.5f, -0.5f, 1.0f, 0.0f, // Top Right
  99. -0.5f, 0.5f, 0.0f, 1.0f, // Bottom Left
  100. 0.5f, 0.5f, 1.0f, 1.0f // Bottom Right
  101. };
  102. u32 i_data[] =
  103. {
  104. 0, 3, 2, // First Triangle
  105. 0, 1, 3 // Second Triangle
  106. };
  107. // Construct vertex and index buffers
  108. g_vbo = gfx->construct_vertex_buffer(layout, sizeof(layout), v_data, sizeof(v_data));
  109. g_ibo = gfx->construct_index_buffer(i_data, sizeof(i_data));
  110. // Get appropriate file path for our texture (depending on where the app is running from)
  111. const char* tfp = platform->file_exists("./assets/gs.png") ? "./assets/gs.png" : "./../assets/gs.png";
  112. gs_assert(platform->file_exists(tfp)); // We'll assert if the file doesn't exist
  113. g_tex = gfx->construct_texture_from_file(tfp, NULL);
  114. // Construct camera parameters
  115. g_camera.transform = gs_vqs_default();
  116. g_camera.transform.position = (gs_vec3){0.f, 0.f, -1.f};
  117. g_camera.fov = 60.f;
  118. g_camera.near_plane = 0.1f;
  119. g_camera.far_plane = 1000.f;
  120. g_camera.ortho_scale = 1.f;
  121. g_camera.proj_type = gs_projection_type_orthographic;
  122. return gs_result_success;
  123. }
  124. gs_result app_update()
  125. {
  126. // Grab global instance of engine
  127. gs_engine_t* engine = gs_engine_instance();
  128. // Platform api
  129. gs_platform_i* platform = engine->ctx.platform;
  130. // If we press the escape key, exit the application
  131. if (platform->key_pressed(gs_keycode_esc))
  132. {
  133. return gs_result_success;
  134. }
  135. /*===============
  136. // Render scene
  137. ================*/
  138. // Graphics api instance
  139. gs_graphics_i* gfx = engine->ctx.graphics;
  140. gs_command_buffer_t* cb = &g_cb;
  141. // Main window size
  142. gs_vec2 ws = platform->window_size(platform->main_window());
  143. gs_vec2 fbs = platform->frame_buffer_size(platform->main_window());
  144. // Set clear color and clear screen
  145. f32 clear_color[4] = { 0.2f, 0.2f, 0.2f, 1.f };
  146. gfx->set_view_clear(cb, clear_color);
  147. gfx->set_viewport(cb, 0.f, 0.f, fbs.x, fbs.y);
  148. // Bind shader
  149. gfx->bind_shader(cb, g_shader);
  150. // Bind matrix uniforms
  151. gfx->bind_uniform_mat4(cb, u_proj, gs_camera_get_projection(&g_camera, ws.x, ws.y));
  152. gfx->bind_uniform_mat4(cb, u_view, gs_camera_get_view(&g_camera));
  153. gfx->bind_uniform_mat4(cb, u_model, gs_mat4_identity());
  154. // Bind texture slot
  155. gfx->bind_texture(cb, u_tex, g_tex, 0);
  156. // Bind vertex buffer
  157. gfx->bind_vertex_buffer(cb, g_vbo);
  158. // Bind index buffer
  159. gfx->bind_index_buffer(cb, g_ibo);
  160. // Draw
  161. gfx->draw_indexed(cb, 6, 0);
  162. // Do some debug drawing using immediate mode
  163. gfx->immediate.begin_drawing(cb);
  164. {
  165. gfx->immediate.draw_text(cb, 10.f, 30.f, "Example: Simple Texture", gs_color_white);
  166. }
  167. gfx->immediate.end_drawing(cb);
  168. // Submit command buffer for rendering
  169. gfx->submit_command_buffer(cb);
  170. return gs_result_in_progress;
  171. }
  172. gs_result app_shutdown()
  173. {
  174. gs_println("Goodbye, Gunslinger.");
  175. return gs_result_success;
  176. }