prosperon/source/engine/openglrender.c

168 lines
3.9 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "openglrender.h"
#include "sprite.h"
#include "shader.h"
#include "font.h"
#include "config.h"
#include "gameobject.h"
#include "camera.h"
#include "window.h"
#include "debugdraw.h"
#include "log.h"
2022-07-02 03:40:50 -05:00
#include "datastream.h"
#include "nuke.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
mfloat_t editorClearColor[4] = { 0.2f, 0.4f, 0.3f, 1.f };
float shadowLookahead = 8.5f;
mfloat_t gridSmallColor[3] = { 0.35f, 1.f, 0.9f };
mfloat_t gridBigColor[3] = { 0.92f, 0.92f, 0.68f };
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;
2021-11-30 21:29:18 -06:00
char objectName[200] = { '\0' }; // object name buffer
struct sprite *tsprite = NULL;
2022-02-06 10:14:57 -06:00
void debug_draw_phys(int draw) {
debugDrawPhysics = draw;
}
2023-02-16 16:13:07 -06:00
void opengl_rendermode(enum RenderMode r)
{
renderMode = r;
}
2023-05-04 17:07:00 -05:00
sg_pipeline mainpip;
sg_pass_action pass_action = {0};
2022-02-01 14:50:25 -06:00
void openglInit()
2021-11-30 21:29:18 -06:00
{
if (!mainwin) {
YughError("No window to init OpenGL on.", 1);
2022-06-30 10:31:23 -05:00
exit(1);
}
2021-11-30 21:29:18 -06:00
////// MAKE SHADERS
spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl");
2023-02-16 16:13:07 -06:00
wireframeShader = MakeShader("spritevert.glsl", "spritewireframefrag.glsl");
2022-06-30 10:31:23 -05:00
animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl");
2022-06-26 08:36:38 -05:00
textShader = MakeShader("textvert.glsl", "textfrag.glsl");
2021-11-30 21:29:18 -06:00
2022-06-30 10:31:23 -05:00
font_init(textShader);
sprite_initialize();
2022-08-14 14:19:36 -05:00
debugdraw_init();
2021-11-30 21:29:18 -06:00
2022-06-30 10:31:23 -05:00
glClearColor(editorClearColor[0], editorClearColor[1], editorClearColor[2], editorClearColor[3]);
2021-11-30 21:29:18 -06:00
2023-05-04 17:07:00 -05:00
/*
2022-12-29 04:26:21 -06:00
glCullFace(GL_BACK);
2021-11-30 21:29:18 -06:00
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2022-06-30 13:03:17 -05:00
2021-11-30 21:29:18 -06:00
glGenBuffers(1, &projUBO);
glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferData(GL_UNIFORM_BUFFER, 64, NULL, GL_DYNAMIC_DRAW);
2022-06-30 10:31:23 -05:00
glBindBufferRange(GL_UNIFORM_BUFFER, 0, projUBO, 0, sizeof(float) * 16);
2021-11-30 21:29:18 -06:00
glBindBuffer(GL_UNIFORM_BUFFER, 0);
shader_setUBO(spriteShader, "Projection", 0);
2022-06-26 08:36:38 -05:00
shader_setUBO(textShader, "Projection", 0);
2021-11-30 21:29:18 -06:00
shader_setUBO(animSpriteShader, "Projection", 0);
2023-05-04 17:07:00 -05:00
*/
2021-11-30 21:29:18 -06:00
}
2022-08-07 01:43:45 -05:00
2022-12-22 16:58:06 -06:00
static cpBody *camera = NULL;
void set_cam_body(cpBody *body) {
camera = body;
}
cpVect cam_pos() {
return camera ? cpBodyGetPosition(camera) : cpvzero;
}
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};
void openglRender(struct window *window)
2021-11-30 21:29:18 -06:00
{
2023-05-04 17:07:00 -05:00
sg_begin_default_pass(&pass_action, window->width, window->height);
2021-11-30 21:29:18 -06:00
//////////// 2D projection
2022-12-22 16:58:06 -06:00
cpVect pos = cam_pos();
2023-02-13 08:30:35 -06:00
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);
2022-12-22 16:58:06 -06:00
2022-12-24 13:18:06 -06:00
mfloat_t ui_projection[16] = { 0.f };
mat4_ortho(ui_projection, 0,
window->width,
0,
window->height, -1.f, 1.f);
2022-06-26 08:36:38 -05:00
2023-05-04 17:07:00 -05:00
// sprite_draw_all();
gui_draw_img("pill1.png", 200, 200);
// renderText("TEST RENDER", {100,100}, 1.f, {1.f,1.f,1.f}, 0,-1);
2022-06-30 10:31:23 -05:00
2023-02-16 16:13:07 -06:00
/* UI Elements & Debug elements */
2023-05-04 17:07:00 -05:00
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// glDisable(GL_DEPTH_TEST);
2022-12-24 13:18:06 -06:00
//// DEBUG
2023-05-04 17:07:00 -05:00
// if (debugDrawPhysics)
// gameobject_draw_debugs();
2022-12-24 13:18:06 -06:00
2023-05-04 17:07:00 -05:00
// call_debugs();
2023-03-17 10:25:35 -05:00
2022-12-24 13:18:06 -06:00
////// TEXT && GUI
2023-05-04 17:07:00 -05:00
// glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
// glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, ui_projection);
2023-01-19 13:06:32 -06:00
2023-05-04 17:07:00 -05:00
// call_gui();
2023-01-19 13:06:32 -06:00
2023-05-04 17:07:00 -05:00
// nuke_start();
// call_nk_gui();
// nuke_end();
2021-11-30 21:29:18 -06:00
2023-05-04 17:07:00 -05:00
sg_end_pass();
sg_commit();
2021-11-30 21:29:18 -06:00
}