prosperon/source/engine/openglrender.c

208 lines
5 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
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;
2023-02-26 11:28:52 -06:00
static unsigned int resUBO;
2021-11-30 21:29:18 -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;
}
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 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
//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);
2023-02-26 11:28:52 -06:00
/*
glGenBuffers(1,&resUBO);
glBindBuffer(GL_UNIFORM_BUFFER, resUBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(float)*2, NULL, GL_DYNAMIC_DRAW);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, resUBO, 0, sizeof(float)*2);
glBindBuffer(GL_UNIFORM_BUFFER,0);
*/
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; }
void openglRender(struct window *window)
2021-11-30 21:29:18 -06:00
{
2023-01-19 18:30:23 -06:00
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
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();
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
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);
2023-02-26 11:28:52 -06:00
/*
float res[2] = {window->width, window->height};
glBindBuffer(GL_UNIFORM_BUFFER, resUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(float)*2, res);
*/
2023-02-16 16:13:07 -06:00
/* Game sprites */
switch (renderMode) {
case LIT:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
shader_use(spriteShader);
break;
case WIREFRAME:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
shader_use(wireframeShader);
break;
};
2022-12-24 13:18:06 -06:00
glEnable(GL_DEPTH_TEST);
2022-07-01 11:14:43 -05:00
glDepthFunc(GL_LESS);
sprite_draw_all();
2022-06-30 10:31:23 -05:00
2023-02-16 16:13:07 -06:00
/* UI Elements & Debug elements */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
2022-12-24 13:18:06 -06:00
glDisable(GL_DEPTH_TEST);
//// DEBUG
if (debugDrawPhysics)
2023-03-10 13:13:48 -06:00
gameobject_draw_debugs();
2022-12-24 13:18:06 -06:00
2023-03-17 10:25:35 -05:00
call_debugs();
2022-12-24 13:18:06 -06:00
////// TEXT && GUI
glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, ui_projection);
2023-01-19 13:06:32 -06:00
2022-12-24 13:18:06 -06:00
call_gui();
2023-01-19 13:06:32 -06:00
nuke_start();
call_nk_gui();
nuke_end();
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
}