prosperon/source/engine/shader.c

178 lines
4.3 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "shader.h"
#include "config.h"
2023-05-12 13:22:05 -05:00
#include "font.h"
2021-11-30 21:29:18 -06:00
#include "log.h"
2023-05-12 13:22:05 -05:00
#include "render.h"
2021-11-30 21:29:18 -06:00
#include "resources.h"
2022-08-26 09:19:17 -05:00
#include "stb_ds.h"
#include "timer.h"
2023-05-12 13:22:05 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-11-30 21:29:18 -06:00
#include "time.h"
2021-11-30 21:29:18 -06:00
#define SHADER_BUF 10000
2022-11-19 17:13:57 -06:00
static struct shader *shaders;
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
struct shader *MakeShader(const char *vertpath, const char *fragpath) {
if (arrcap(shaders) == 0)
arrsetcap(shaders, 20);
2022-07-02 03:40:50 -05:00
2023-05-12 13:22:05 -05:00
struct shader init = {
.vertpath = vertpath,
.fragpath = fragpath};
shader_compile(&init);
arrput(shaders, init);
return &arrlast(shaders);
2021-11-30 21:29:18 -06:00
}
2023-05-12 13:22:05 -05:00
int shader_compile_error(int shader) {
/*
GLint success = 0;
GLchar infoLog[ERROR_BUFFER] = { '\0' };
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success) return 0;
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
glGetShaderInfoLog(shader, ERROR_BUFFER, NULL, infoLog);
YughLog(0, LOG_ERROR, "Shader compilation error.\nLog: %s", infoLog);
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
return 1;
*/
2021-11-30 21:29:18 -06:00
}
2023-05-12 13:22:05 -05:00
int shader_link_error(int shader) {
/*
GLint success = 0;
GLchar infoLog[ERROR_BUFFER] = { '\0' };
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (success) return 0;
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
glGetProgramInfoLog(shader, ERROR_BUFFER, NULL, infoLog);
YughLog(0, LOG_ERROR, "Shader link error.\nLog: %s", infoLog);
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
return 1;
*/
2021-11-30 21:29:18 -06:00
}
2023-05-12 13:22:05 -05:00
int load_shader_from_file(const char *path, int type) {
char spath[MAXPATH] = {'\0'};
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
sprintf(spath, "%s%s", "shaders/", path);
FILE *f = fopen(make_path(spath), "r'");
if (!path)
perror(spath), exit(1);
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
char *buf;
long int fsize;
fseek(f, 0, SEEK_END);
fsize = ftell(f);
buf = malloc(fsize + 1);
rewind(f);
size_t r = fread(buf, sizeof(char), fsize, f);
buf[r] = '\0';
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
fclose(f);
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
/*
GLuint id = glCreateShader(type);
const char *code = buf;
glShaderSource(id, 1, &code, NULL);
glCompileShader(id);
if (shader_compile_error(id)) {
YughError("Error with shader %s.", path);
return 0;
}
free(buf);
2021-11-30 21:29:18 -06:00
2023-01-03 17:13:31 -06:00
2023-05-12 13:22:05 -05:00
return id;
*/
2021-11-30 21:29:18 -06:00
}
2023-05-12 13:22:05 -05:00
void shader_compile(struct shader *shader) {
YughInfo("Making shader with %s and %s.", shader->vertpath, shader->fragpath);
char spath[MAXPATH];
sprintf(spath, "%s%s", "shaders/", shader->vertpath);
const char *vsrc = slurp_text(spath);
sprintf(spath, "%s%s", "shaders/", shader->fragpath);
const char *fsrc = slurp_text(spath);
shader->shd = sg_make_shader(&(sg_shader_desc){
2023-05-04 17:07:00 -05:00
.vs.source = vsrc,
.fs.source = fsrc,
.label = shader->vertpath,
2023-05-12 13:22:05 -05:00
});
free(vsrc);
free(fsrc);
2021-11-30 21:29:18 -06:00
}
2023-05-12 13:22:05 -05:00
void shader_use(struct shader *shader) {
// glUseProgram(shader->id);
2021-11-30 21:29:18 -06:00
}
2023-05-04 17:07:00 -05:00
/*
2022-11-19 17:13:57 -06:00
void shader_setbool(struct shader *shader, const char *name, int val)
2021-11-30 21:29:18 -06:00
{
glUniform1i(glGetUniformLocation(shader->id, name), val);
}
2022-11-19 17:13:57 -06:00
void shader_setint(struct shader *shader, const char *name, int val)
2021-11-30 21:29:18 -06:00
{
glUniform1i(glGetUniformLocation(shader->id, name), val);
}
2022-11-19 17:13:57 -06:00
void shader_setfloat(struct shader *shader, const char *name, float val)
2021-11-30 21:29:18 -06:00
{
glUniform1f(glGetUniformLocation(shader->id, name), val);
}
2022-11-19 17:13:57 -06:00
void shader_setvec2(struct shader *shader, const char *name, mfloat_t val[2])
2021-11-30 21:29:18 -06:00
{
glUniform2fv(glGetUniformLocation(shader->id, name), 1, val);
}
2022-11-19 17:13:57 -06:00
void shader_setvec4(struct shader *shader, const char *name, mfloat_t val[4])
2021-11-30 21:29:18 -06:00
{
glUniform4fv(glGetUniformLocation(shader->id, name), 1, val);
}
2022-11-19 17:13:57 -06:00
void shader_setmat2(struct shader *shader, const char *name, mfloat_t val[4])
2021-11-30 21:29:18 -06:00
{
2022-06-30 10:31:23 -05:00
glUniformMatrix2fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val);
2021-11-30 21:29:18 -06:00
}
2022-11-19 17:13:57 -06:00
void shader_setmat3(struct shader *shader, const char *name, mfloat_t val[9])
2021-11-30 21:29:18 -06:00
{
2022-06-30 10:31:23 -05:00
glUniformMatrix3fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val);
2021-11-30 21:29:18 -06:00
}
2022-11-19 17:13:57 -06:00
void shader_setmat4(struct shader *shader, const char *name, mfloat_t val[16])
2021-11-30 21:29:18 -06:00
{
2022-06-30 10:31:23 -05:00
glUniformMatrix4fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val);
2021-11-30 21:29:18 -06:00
}
2022-11-19 17:13:57 -06:00
void shader_setUBO(struct shader *shader, const char *name, unsigned int index)
2021-11-30 21:29:18 -06:00
{
2022-06-30 10:31:23 -05:00
glUniformBlockBinding(shader->id, glGetUniformBlockIndex(shader->id, name), index);
2021-11-30 21:29:18 -06:00
}
2023-05-04 17:07:00 -05:00
*/
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
void shader_compile_all() {
for (int i = 0; i < arrlen(shaders); i++)
shader_compile(&shaders[i]);
2021-11-30 21:29:18 -06:00
}
2023-05-12 13:22:05 -05:00
void shader_setvec3(struct shader *shader, const char *name, mfloat_t val[3]) {
// glUniform3fv(glGetUniformLocation(shader->id, name), 1, val);
2023-05-04 17:07:00 -05:00
}
2023-05-12 13:22:05 -05:00
void shader_setmat4(struct shader *shader, const char *name, mfloat_t val[16]) {
// glUniformMatrix4fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val);
2023-05-04 17:07:00 -05:00
}