2021-11-30 21:29:18 -06:00
|
|
|
#include "openglrender.h"
|
|
|
|
|
|
|
|
#include "camera.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "config.h"
|
|
|
|
#include "datastream.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
#include "debugdraw.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "font.h"
|
|
|
|
#include "gameobject.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
#include "log.h"
|
2023-02-02 17:52:15 -06:00
|
|
|
#include "nuke.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "shader.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include "model.h"
|
|
|
|
#include "stb_ds.h"
|
|
|
|
|
|
|
|
#include "HandmadeMath.h"
|
2022-12-22 16:58:06 -06:00
|
|
|
|
2023-02-16 16:13:07 -06:00
|
|
|
int renderMode = LIT;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
struct shader *spriteShader = NULL;
|
2023-02-16 16:13:07 -06:00
|
|
|
struct shader *wireframeShader = NULL;
|
2022-11-19 17:13:57 -06:00
|
|
|
struct shader *animSpriteShader = NULL;
|
|
|
|
static struct shader *textShader;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
mfloat_t editorClearColor[4] = {0.2f, 0.4f, 0.3f, 1.f};
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
float shadowLookahead = 8.5f;
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
mfloat_t gridSmallColor[3] = {0.35f, 1.f, 0.9f};
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
mfloat_t gridBigColor[3] = {0.92f, 0.92f, 0.68f};
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
float gridScale = 500.f;
|
|
|
|
float smallGridUnit = 1.f;
|
|
|
|
float bigGridUnit = 10.f;
|
|
|
|
float gridSmallThickness = 2.f;
|
|
|
|
float gridBigThickness = 7.f;
|
|
|
|
float gridOpacity = 0.3f;
|
|
|
|
|
|
|
|
mfloat_t proj[16];
|
|
|
|
|
|
|
|
// Debug render modes
|
|
|
|
bool renderGizmos = false;
|
|
|
|
bool showGrid = true;
|
|
|
|
bool debugDrawPhysics = false;
|
|
|
|
bool renderNav = false;
|
|
|
|
|
|
|
|
// Lighting effect flags
|
|
|
|
bool renderAO = true;
|
|
|
|
bool renderDynamicShadows = true;
|
|
|
|
bool renderRefraction = true;
|
|
|
|
bool renderReflection = true;
|
|
|
|
|
|
|
|
///// for editing
|
2022-11-19 17:13:57 -06:00
|
|
|
struct gameobject *selectedobject = NULL;
|
2023-05-12 13:22:05 -05:00
|
|
|
char objectName[200] = {'\0'}; // object name buffer
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-11-17 16:48:20 -06:00
|
|
|
struct sprite *tsprite = NULL;
|
2022-02-06 10:14:57 -06:00
|
|
|
|
2023-05-07 11:11:33 -05:00
|
|
|
const char *donquixote;
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
static struct model *duck;
|
|
|
|
|
|
|
|
sg_image ddimg;
|
|
|
|
|
2022-12-21 19:24:59 -06:00
|
|
|
void debug_draw_phys(int draw) {
|
2023-05-12 13:22:05 -05:00
|
|
|
debugDrawPhysics = draw;
|
2022-12-21 19:24:59 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void opengl_rendermode(enum RenderMode r) {
|
2023-02-16 16:13:07 -06:00
|
|
|
renderMode = r;
|
|
|
|
}
|
|
|
|
|
2023-05-04 17:07:00 -05:00
|
|
|
sg_pipeline mainpip;
|
|
|
|
sg_pass_action pass_action = {0};
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
static struct {
|
|
|
|
sg_pass_action pass_action;
|
|
|
|
sg_pass pass;
|
|
|
|
sg_pipeline pipe;
|
|
|
|
sg_shader shader;
|
|
|
|
} sg_shadow;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
sg_shader shader;
|
|
|
|
sg_pipeline pipe;
|
|
|
|
sg_bindings bind;
|
|
|
|
sg_pass pass;
|
|
|
|
sg_image img;
|
|
|
|
sg_image depth_img;
|
|
|
|
} crt_post;
|
|
|
|
|
|
|
|
|
|
|
|
void openglInit() {
|
|
|
|
if (!mainwin) {
|
|
|
|
YughError("No window to init OpenGL on.", 1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
font_init(NULL);
|
|
|
|
debugdraw_init();
|
|
|
|
sprite_initialize();
|
|
|
|
nuke_init(mainwin);
|
|
|
|
|
|
|
|
model_init();
|
|
|
|
// duck = MakeModel("sponza.glb");
|
|
|
|
|
|
|
|
pass_action = (sg_pass_action){
|
|
|
|
.colors[0] = {.action = SG_ACTION_CLEAR, .value = {editorClearColor[1], editorClearColor[2], editorClearColor[3], 1.f}},
|
|
|
|
};
|
|
|
|
|
|
|
|
crt_post.shader = sg_make_shader(&(sg_shader_desc){
|
|
|
|
.vs.source = slurp_text("shaders/postvert.glsl"),
|
|
|
|
.fs.source = slurp_text("shaders/crtfrag.glsl"),
|
|
|
|
|
|
|
|
.fs.images[0] = {
|
|
|
|
.name = "diffuse_texture",
|
|
|
|
.image_type = SG_IMAGETYPE_2D,
|
|
|
|
.sampler_type = SG_SAMPLERTYPE_FLOAT
|
2022-06-27 14:12:26 -05:00
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
crt_post.pipe = sg_make_pipeline(&(sg_pipeline_desc){
|
|
|
|
.shader = crt_post.shader,
|
|
|
|
.layout = {
|
|
|
|
.attrs = {
|
|
|
|
[0].format = SG_VERTEXFORMAT_FLOAT2,
|
|
|
|
[1].format = SG_VERTEXFORMAT_FLOAT2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
crt_post.img = sg_make_image(&(sg_image_desc){
|
|
|
|
.render_target = true,
|
|
|
|
.width = 1200,
|
|
|
|
.height = 720,
|
|
|
|
});
|
|
|
|
|
|
|
|
crt_post.depth_img = sg_make_image(&(sg_image_desc){
|
|
|
|
.render_target = true,
|
|
|
|
.width = 1200,
|
|
|
|
.height = 720,
|
|
|
|
.pixel_format = SG_PIXELFORMAT_DEPTH_STENCIL
|
|
|
|
});
|
|
|
|
|
|
|
|
crt_post.pass = sg_make_pass(&(sg_pass_desc){
|
|
|
|
.color_attachments[0].image = crt_post.img,
|
|
|
|
.depth_stencil_attachment.image = crt_post.depth_img,
|
|
|
|
});
|
|
|
|
|
|
|
|
float crt_quad[] = {
|
|
|
|
-1, 1, 0, 1,
|
|
|
|
-1, -1, 0, 0,
|
|
|
|
1, -1, 1, 0,
|
|
|
|
-1, 1, 0, 1,
|
|
|
|
1, -1, 1, 0,
|
|
|
|
1, 1, 1, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
crt_post.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
|
|
|
|
.size = sizeof(crt_quad),
|
|
|
|
.data = crt_quad
|
|
|
|
});
|
|
|
|
|
|
|
|
crt_post.bind.fs_images[0] = crt_post.img;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-04 17:07:00 -05:00
|
|
|
/*
|
2023-05-12 13:22:05 -05:00
|
|
|
sg_image_desc shadow_desc = {
|
|
|
|
.render_target = true,
|
|
|
|
.width = 1024,
|
|
|
|
.height = 1024,
|
|
|
|
.pixel_format = SG_PIXELFORMAT_R32F,
|
|
|
|
};
|
|
|
|
sg_image depth_img = sg_make_image(&shadow_desc);
|
|
|
|
shadow_desc.pixel_format = SG_PIXELFORMAT_DEPTH;
|
|
|
|
ddimg = sg_make_image(&shadow_desc);
|
|
|
|
|
2023-05-13 22:01:53 -05:00
|
|
|
// duck = MakeModel("sponza.glb");
|
2023-05-12 13:22:05 -05:00
|
|
|
|
|
|
|
sg_shadow.pass = sg_make_pass(&(sg_pass_desc){
|
|
|
|
.color_attachments[0].image = depth_img,
|
|
|
|
.depth_stencil_attachment.image = ddimg,
|
|
|
|
});
|
|
|
|
|
|
|
|
sg_shadow.pass_action = (sg_pass_action) {
|
|
|
|
.colors[0] = { .action=SG_ACTION_CLEAR, .value = {1,1,1,1} } };
|
|
|
|
|
|
|
|
sg_shadow.shader = sg_make_shader(&(sg_shader_desc){
|
|
|
|
.vs.source = slurp_text("shaders/shadowvert.glsl"),
|
|
|
|
.fs.source = slurp_text("shaders/shadowfrag.glsl"),
|
|
|
|
.vs.uniform_blocks[0] = {
|
|
|
|
.size = sizeof(float) * 16 * 2,
|
|
|
|
.uniforms = {
|
|
|
|
[0] = {.name = "lightSpaceMatrix", .type = SG_UNIFORMTYPE_MAT4},
|
|
|
|
[1] = {.name = "model", .type = SG_UNIFORMTYPE_MAT4},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
sg_shadow.pipe = sg_make_pipeline(&(sg_pipeline_desc){
|
|
|
|
.shader = sg_shadow.shader,
|
|
|
|
.layout = {
|
|
|
|
.attrs = {
|
|
|
|
[0].format = SG_VERTEXFORMAT_FLOAT3,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.depth = {
|
|
|
|
.compare = SG_COMPAREFUNC_LESS_EQUAL,
|
|
|
|
.write_enabled = true,
|
|
|
|
.pixel_format = SG_PIXELFORMAT_DEPTH
|
|
|
|
},
|
|
|
|
.colors[0].pixel_format = SG_PIXELFORMAT_R32F,
|
|
|
|
.index_type = SG_INDEXTYPE_UINT16,
|
|
|
|
.cull_mode = SG_CULLMODE_BACK,
|
|
|
|
});
|
|
|
|
|
2023-05-04 17:07:00 -05:00
|
|
|
*/
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
static cpBody *camera = NULL;
|
|
|
|
void set_cam_body(cpBody *body) {
|
2023-05-12 13:22:05 -05:00
|
|
|
camera = body;
|
2022-12-22 16:58:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
cpVect cam_pos() {
|
2023-05-12 13:22:05 -05:00
|
|
|
return camera ? cpBodyGetPosition(camera) : cpvzero;
|
2022-12-22 16:58:06 -06:00
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
static float zoom = 1.f;
|
2023-02-20 16:28:07 -06:00
|
|
|
float cam_zoom() { return zoom; }
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void add_zoom(float val) { zoom = val; }
|
|
|
|
|
2023-05-04 17:07:00 -05:00
|
|
|
mfloat_t projection[16] = {0.f};
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
HMM_Vec3 dirl_pos = {4, 100, 20};
|
2022-06-30 10:31:23 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void openglRender(struct window *window) {
|
|
|
|
/*
|
|
|
|
HMM_Mat4 model = HMM_M4D(1.f);
|
|
|
|
float scale = 0.08;
|
|
|
|
model = HMM_MulM4(model, HMM_Scale((HMM_Vec3){scale,scale,scale}));
|
2022-12-24 13:18:06 -06:00
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
// Shadow pass
|
|
|
|
sg_begin_pass(sg_shadow.pass, &sg_shadow.pass_action);
|
|
|
|
sg_apply_pipeline(sg_shadow.pipe);
|
2023-01-19 13:06:32 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
HMM_Mat4 light_proj = HMM_Orthographic_RH_ZO(-100.f, 100.f, -100.f, 100.f, 1.f, 100.f);
|
|
|
|
HMM_Mat4 light_view = HMM_LookAt_RH(dirl_pos, (HMM_Vec3){0,0,0}, (HMM_Vec3){0,1,0});
|
2023-01-19 13:06:32 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
HMM_Mat4 lsm = HMM_MulM4(light_proj, light_view);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
HMM_Mat4 subo[2];
|
|
|
|
subo[0] = lsm;
|
|
|
|
subo[1] = model;
|
2023-05-04 17:07:00 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(subo));
|
|
|
|
|
|
|
|
for (int i = 0; i < arrlen(duck->meshes); i++) {
|
|
|
|
sg_bindings sbind = {0};
|
|
|
|
sbind.vertex_buffers[0] = duck->meshes[i].bind.vertex_buffers[0];
|
|
|
|
sbind.index_buffer = duck->meshes[i].bind.index_buffer;
|
|
|
|
sg_apply_bindings(&sbind);
|
|
|
|
sg_draw(0,duck->meshes[i].face_count,1);
|
|
|
|
}
|
2023-05-04 17:07:00 -05:00
|
|
|
sg_end_pass();
|
2023-05-12 13:22:05 -05:00
|
|
|
|
|
|
|
draw_model(duck,model, lsm);
|
|
|
|
*/
|
|
|
|
|
|
|
|
// sg_begin_default_pass(&pass_action, window->width, window->height);
|
|
|
|
sg_begin_pass(crt_post.pass, &pass_action);
|
|
|
|
|
|
|
|
//////////// 2D projection
|
|
|
|
cpVect pos = cam_pos();
|
|
|
|
|
|
|
|
mat4_ortho(projection, pos.x - zoom * window->width / 2,
|
|
|
|
pos.x + zoom * window->width / 2,
|
|
|
|
pos.y - zoom * window->height / 2,
|
|
|
|
pos.y + zoom * window->height / 2, -1.f, 1.f);
|
|
|
|
|
|
|
|
mfloat_t ui_projection[16] = {0.f};
|
|
|
|
mat4_ortho(ui_projection, 0, window->width, 0, window->height, -1.f, 1.f);
|
2023-05-07 19:47:49 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
sprite_draw_all();
|
2023-05-07 19:47:49 -05:00
|
|
|
sprite_flush();
|
2023-05-12 13:22:05 -05:00
|
|
|
|
|
|
|
//// DEBUG
|
|
|
|
if (debugDrawPhysics)
|
|
|
|
gameobject_draw_debugs();
|
2023-05-13 22:01:53 -05:00
|
|
|
|
2023-05-16 01:31:13 -05:00
|
|
|
struct rgba c = {100,130,200,255};
|
2023-05-13 22:01:53 -05:00
|
|
|
draw_circle(100,100,40,2,c,0);
|
2023-05-12 13:22:05 -05:00
|
|
|
call_debugs();
|
|
|
|
debug_flush();
|
|
|
|
|
|
|
|
////// TEXT && GUI
|
|
|
|
call_gui();
|
|
|
|
|
|
|
|
text_flush();
|
|
|
|
|
|
|
|
nuke_start();
|
|
|
|
call_nk_gui();
|
|
|
|
nuke_end();
|
|
|
|
|
|
|
|
sg_end_pass();
|
|
|
|
|
|
|
|
sg_begin_default_pass(&pass_action, window->width, window->height);
|
|
|
|
sg_apply_pipeline(crt_post.pipe);
|
|
|
|
sg_apply_bindings(&crt_post.bind);
|
|
|
|
sg_draw(0,6,1);
|
|
|
|
sg_end_pass();
|
|
|
|
|
|
|
|
|
|
|
|
sg_commit();
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|