Skip to content

Commit

Permalink
Finish moving 10_cubemap to Kong
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 22, 2023
1 parent 8714618 commit 73ca630
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 72 deletions.
6 changes: 3 additions & 3 deletions 10_cubemap/Shaders/shaders.kong
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fun fs_pos(input: fs_vertex_in): fs_vertex_out {
madd.y = 0.5;

var output: fs_vertex_out;
output.tex_coord = pos.xy * madd + madd;
output.tex_coord = input.pos.xy * madd + madd;

output.pos.x = input.pos.x;
output.pos.y = input.pos.y;
Expand All @@ -58,9 +58,9 @@ fun fs_pos(input: fs_vertex_in): fs_vertex_out {
}

const fs_texture: texcube;
const fs_sampler: samplercube;
const fs_sampler: sampler;

fun fs_pix(input: vertex_out): float4 {
fun fs_pix(input: fs_vertex_out): float4 {
var tex_coord: float3;
tex_coord.x = input.pos.x;
tex_coord.y = input.pos.y;
Expand Down
93 changes: 25 additions & 68 deletions 10_cubemap/Sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@
#include <kinc/io/filereader.h>
#include <kinc/system.h>

#include <kong.h>

#include <assert.h>
#include <stdlib.h>

static kinc_g4_shader_t vertex_shader;
static kinc_g4_shader_t fragment_shader;
static kinc_g4_pipeline_t pipeline;
static kinc_g4_vertex_buffer_t vertices;
static kinc_g4_index_buffer_t indices;
static kinc_g4_shader_t vertex_shader_fs;
static kinc_g4_shader_t fragment_shader_fs;
static kinc_g4_pipeline_t pipeline_fs;
static kinc_g4_vertex_buffer_t vertices_fs;
static kinc_g4_index_buffer_t indices_fs;
static kinc_g4_texture_unit_t tex_unit;
static kinc_g4_render_target_t target;

#define HEAP_SIZE 1024 * 1024
Expand Down Expand Up @@ -48,8 +43,8 @@ static void update(void *data) {

kinc_g4_restore_render_target();
kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0, 0.0f, 0);
kinc_g4_set_pipeline(&pipeline_fs);
kinc_g4_render_target_use_color_as_texture(&target, tex_unit);
kinc_g4_set_pipeline(&fs_pipeline);
kinc_g4_render_target_use_color_as_texture(&target, fs_texture);
kinc_g4_set_vertex_buffer(&vertices_fs);
kinc_g4_set_index_buffer(&indices_fs);
kinc_g4_draw_indexed_vertices();
Expand All @@ -58,56 +53,33 @@ static void update(void *data) {
kinc_g4_swap_buffers();
}

static void load_shader(const char *filename, kinc_g4_shader_t *shader, kinc_g4_shader_type_t shader_type) {
kinc_file_reader_t file;
kinc_file_reader_open(&file, filename, KINC_FILE_TYPE_ASSET);
size_t data_size = kinc_file_reader_size(&file);
uint8_t *data = allocate(data_size);
kinc_file_reader_read(&file, data, data_size);
kinc_file_reader_close(&file);
kinc_g4_shader_init(shader, data, data_size, shader_type);
}

int kickstart(int argc, char **argv) {
kinc_init("Example", 1024, 768, NULL, NULL);
kinc_set_update_callback(update, NULL);

heap = (uint8_t *)malloc(HEAP_SIZE);
assert(heap != NULL);

load_shader("shader.vert", &vertex_shader, KINC_G4_SHADER_TYPE_VERTEX);
load_shader("shader.frag", &fragment_shader, KINC_G4_SHADER_TYPE_FRAGMENT);

kinc_g4_vertex_structure_t structure;
kinc_g4_vertex_structure_init(&structure);
kinc_g4_vertex_structure_add(&structure, "pos", KINC_G4_VERTEX_DATA_F32_3X);
kinc_g4_pipeline_init(&pipeline);
pipeline.vertex_shader = &vertex_shader;
pipeline.fragment_shader = &fragment_shader;
pipeline.input_layout[0] = &structure;
pipeline.input_layout[1] = NULL;
pipeline.depth_write = true;
pipeline.depth_mode = KINC_G4_COMPARE_LESS;
kinc_g4_pipeline_compile(&pipeline);
// pipeline.depth_write = true;
// pipeline.depth_mode = KINC_G4_COMPARE_LESS;

kinc_g4_render_target_init_cube(&target, 512, KINC_G4_RENDER_TARGET_FORMAT_32BIT, 0, 0);

kinc_g4_vertex_buffer_init(&vertices, 3, &structure, KINC_G4_USAGE_STATIC, 0);
kinc_g4_vertex_buffer_init(&vertices, 3, &vertex_in_structure, KINC_G4_USAGE_STATIC, 0);
{
float *v = kinc_g4_vertex_buffer_lock_all(&vertices);
int i = 0;
vertex_in *v = (vertex_in *)kinc_g4_vertex_buffer_lock_all(&vertices);

v[i++] = -1.0;
v[i++] = kinc_g4_render_targets_inverted_y() ? -1.0f : 1.0f;
v[i++] = 0.0;
v[0].pos.x = -1.0;
v[0].pos.y = kinc_g4_render_targets_inverted_y() ? -1.0f : 1.0f;
v[0].pos.z = 0.0;

v[i++] = 1.0;
v[i++] = kinc_g4_render_targets_inverted_y() ? -1.0f : 1.0f;
v[i++] = 0.0;
v[1].pos.x = 1.0;
v[1].pos.y = kinc_g4_render_targets_inverted_y() ? -1.0f : 1.0f;
v[1].pos.z = 0.0;

v[i++] = 0.0;
v[i++] = kinc_g4_render_targets_inverted_y() ? 1.0f : -1.0f;
v[i++] = 0.0;
v[2].pos.x = 0.0;
v[2].pos.y = kinc_g4_render_targets_inverted_y() ? 1.0f : -1.0f;
v[2].pos.z = 0.0;

kinc_g4_vertex_buffer_unlock_all(&vertices);
}
Expand All @@ -121,33 +93,18 @@ int kickstart(int argc, char **argv) {
kinc_g4_index_buffer_unlock_all(&indices);
}

load_shader("shader_fs.vert", &vertex_shader_fs, KINC_G4_SHADER_TYPE_VERTEX);
load_shader("shader_fs.frag", &fragment_shader_fs, KINC_G4_SHADER_TYPE_FRAGMENT);

kinc_g4_vertex_structure_t structure_fs;
kinc_g4_vertex_structure_init(&structure_fs);
kinc_g4_vertex_structure_add(&structure_fs, "pos", KINC_G4_VERTEX_DATA_F32_2X);
kinc_g4_pipeline_init(&pipeline_fs);
pipeline_fs.vertex_shader = &vertex_shader_fs;
pipeline_fs.fragment_shader = &fragment_shader_fs;
pipeline_fs.input_layout[0] = &structure_fs;
pipeline_fs.input_layout[1] = NULL;
kinc_g4_pipeline_compile(&pipeline_fs);
tex_unit = kinc_g4_pipeline_get_texture_unit(&pipeline_fs, "tex");

kinc_g4_vertex_buffer_init(&vertices_fs, 3, &structure_fs, KINC_G4_USAGE_STATIC, 0);
kinc_g4_vertex_buffer_init(&vertices_fs, 3, &fs_vertex_in_structure, KINC_G4_USAGE_STATIC, 0);
{
float *v = kinc_g4_vertex_buffer_lock_all(&vertices_fs);
int i = 0;
fs_vertex_in *v = (fs_vertex_in *)kinc_g4_vertex_buffer_lock_all(&vertices_fs);

v[i++] = -1.0;
v[i++] = -1.0;
v[0].pos.x = -1.0;
v[0].pos.y = -1.0;

v[i++] = 3.0;
v[i++] = -1.0;
v[1].pos.x = 3.0;
v[1].pos.y = -1.0;

v[i++] = -1.0;
v[i++] = 3.0;
v[2].pos.x = -1.0;
v[2].pos.y = 3.0;

kinc_g4_vertex_buffer_unlock_all(&vertices_fs);
}
Expand Down
2 changes: 1 addition & 1 deletion 10_cubemap/kfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const project = new Project('Example');
const project = new Project('CubeMap');

await project.addProject('../Kinc', {kong: true});

Expand Down

0 comments on commit 73ca630

Please sign in to comment.