diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-10-17 21:44:44 +1100 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-10-17 21:44:44 +1100 |
commit | 70175cfce551a6b534771bd2b1dea6cfb417be1f (patch) | |
tree | f29381cde897b024ecb781ff63cd75c3a16f6434 /examples | |
parent | a5f1733a09aa99379cf48ed326b4660fbc17cb25 (diff) |
starting on cube
Diffstat (limited to 'examples')
-rw-r--r-- | examples/cube.c | 57 | ||||
-rw-r--r-- | examples/triangle.c | 38 |
2 files changed, 87 insertions, 8 deletions
diff --git a/examples/cube.c b/examples/cube.c index e69de29..6642e2a 100644 --- a/examples/cube.c +++ b/examples/cube.c @@ -0,0 +1,57 @@ +#include <celeritas.h> + +// vertices + +pipeline_handle draw_pipeline; +buf_handle cube_vbuf; +tex_handle texture; + +void draw() { + render_pass_desc d = {}; + gpu_encoder* enc = ral_render_encoder(d); + ral_encode_bind_pipeline(enc, draw_pipeline); + ral_encode_set_vertex_buf(enc, cube_vbuf); + ral_encode_set_texture(enc, texture, 0); + ral_encode_draw_tris(enc, 0, 36); + ral_encoder_finish_and_submit(enc); +} + +int main() { + core_bringup("Celeritas Example: Triangle", NULL); + + // create rendering pipeline + gfx_pipeline_desc pipeline_desc = { + .label = "Textured cube pipeline", + .vertex_desc = static_3d_vertex_format(), + .vertex = { + .source = NULL, + .is_spirv = false, + .entry_point = "cubeVertexShader", + .stage = STAGE_VERTEX, + }, + .fragment = { + .source = NULL, + .is_spirv = false, + .entry_point = "cubeFragmentShader", + .stage = STAGE_FRAGMENT, + }, + }; + + draw_pipeline = ral_gfx_pipeline_create(pipeline_desc); + + // create the cube geometry + geometry cube = geo_cuboid(1.0, 1.0, 1.0); + + // upload vertex data to the gpu + cube_vbuf = ral_buffer_create(64 * 36, cube.vertex_data); + + while (!app_should_exit()) { + glfwPollEvents(); + + ral_frame_start(); + ral_frame_draw(&draw); + ral_frame_end(); + } + + return 0; +}
\ No newline at end of file diff --git a/examples/triangle.c b/examples/triangle.c index 401b5ac..e5cf92d 100644 --- a/examples/triangle.c +++ b/examples/triangle.c @@ -2,21 +2,39 @@ #include <celeritas.h> -static vec4 vertices[] = { - {-0.5f, -0.5f, 0.0f, 1.0}, - { 0.5f, -0.5f, 0.0f, 1.0}, - { 0.0f, 0.5f, 0.0f, 1.0} -}; +// static vec4 vertices[] = { +// {-0.5f, -0.5f, 0.0f, 1.0}, +// { 0.5f, -0.5f, 0.0f, 1.0}, +// { 0.0f, 0.5f, 0.0f, 1.0} +// }; + +typedef struct VertexData { + vec4 position; + vec2 texCoords; + f32 pad1; + f32 pad2; +} VertexData; + +VertexData squareVertices[] = { + {{-0.5, -0.5, 0.5, 1.0f}, {0.0f, 0.0f}, 0.0, 0.0}, + {{-0.5, 0.5, 0.5, 1.0f}, {0.0f, 1.0f}, 0.0, 0.0}, + {{ 0.5, 0.5, 0.5, 1.0f}, {1.0f, 1.0f}, 0.0, 0.0}, + {{-0.5, -0.5, 0.5, 1.0f}, {0.0f, 0.0f}, 0.0, 0.0}, + {{ 0.5, 0.5, 0.5, 1.0f}, {1.0f, 1.0f}, 0.0, 0.0}, + {{ 0.5, -0.5, 0.5, 1.0f}, {1.0f, 0.0f}, 0.0, 0.0} + }; pipeline_handle draw_pipeline; buf_handle tri_vert_buffer; +tex_handle texture; void draw() { render_pass_desc d = {}; gpu_encoder* enc = ral_render_encoder(d); ral_encode_bind_pipeline(enc, draw_pipeline); ral_encode_set_vertex_buf(enc, tri_vert_buffer); - ral_encode_draw_tris(enc, 0, 3); + ral_encode_set_texture(enc, texture, 0); + ral_encode_draw_tris(enc, 0, 6); ral_encoder_finish_and_submit(enc); } @@ -43,9 +61,13 @@ int main() { draw_pipeline = ral_gfx_pipeline_create(pipeline_desc); + // load texture from file + texture = ral_texture_load_from_file("assets/textures/mc_grass.jpeg"); + // create our buffer to hold vertices - printf("size of vertices %ld\n", sizeof(vec4) * 3); - tri_vert_buffer = ral_buffer_create(sizeof(vec4) * 3, &vertices); + size_t buffer_size = sizeof(VertexData) * 6; + printf("size of vertices %ld\n", buffer_size); + tri_vert_buffer = ral_buffer_create(buffer_size, &squareVertices); while (!app_should_exit()) { glfwPollEvents(); |