prosperon/source/engine/openglrender.c

173 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 "static_actor.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"
2021-11-30 21:29:18 -06:00
2022-12-22 16:58:06 -06:00
2021-11-30 21:29:18 -06:00
int renderMode = 0;
static GLuint UBO;
2022-02-06 10:14:57 -06:00
2022-11-19 17:13:57 -06:00
struct shader *spriteShader = NULL;
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
2022-02-06 10:14:57 -06:00
GLuint debugColorPickBO = 0;
GLuint debugColorPickTEX = 0;
2021-11-30 21:29:18 -06:00
struct sprite *tsprite = NULL;
2022-02-06 10:14:57 -06:00
2021-11-30 21:29:18 -06:00
static unsigned int projUBO;
void debug_draw_phys(int draw) {
debugDrawPhysics = draw;
}
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");
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-08-26 09:19:17 -05:00
2022-06-30 13:03:17 -05:00
shader_use(textShader);
shader_setint(textShader, "text", 0);
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
2022-07-01 11:14:43 -05:00
glEnable(GL_CULL_FACE);
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);
}
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;
void add_zoom(float val) { zoom = val; }
void openglRender(struct window *window)
2021-11-30 21:29:18 -06:00
{
2022-08-07 01:43:45 -05:00
2021-11-30 21:29:18 -06:00
//////////// 2D projection
mfloat_t projection[16] = { 0.f };
2022-12-22 16:58:06 -06:00
cpVect pos = cam_pos();
mat4_ortho(projection, pos.x,
window->width*zoom + pos.x,
pos.y,
window->height*zoom + pos.y, -1.f, 1.f);
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
2021-11-30 21:29:18 -06:00
// Clear color and depth
2022-06-30 10:31:23 -05:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2021-11-30 21:29:18 -06:00
2022-12-24 13:18:06 -06:00
glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection);
2022-06-26 08:36:38 -05:00
2022-12-24 13:18:06 -06:00
glEnable(GL_DEPTH_TEST);
2022-06-30 10:31:23 -05:00
///// Sprites
2022-07-01 11:14:43 -05:00
glDepthFunc(GL_LESS);
2022-06-30 10:31:23 -05:00
shader_use(spriteShader);
2022-07-01 11:14:43 -05:00
sprite_draw_all();
2022-06-30 10:31:23 -05:00
2022-07-02 03:40:50 -05:00
2022-12-24 13:18:06 -06:00
glDisable(GL_DEPTH_TEST);
//// DEBUG
if (debugDrawPhysics)
gameobject_draw_debugs();
////// TEXT && GUI
glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, ui_projection);
call_gui();
2021-11-30 21:29:18 -06:00
}
void BindUniformBlock(GLuint shaderID, const char *bufferName, GLuint bufferBind)
2021-11-30 21:29:18 -06:00
{
glUniformBlockBinding(shaderID, glGetUniformBlockIndex(shaderID, bufferName), bufferBind);
2021-11-30 21:29:18 -06:00
}