prosperon/source/engine/shader.c

167 lines
4 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "shader.h"
2022-02-06 10:14:57 -06:00
#include "render.h"
2021-11-30 21:29:18 -06:00
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "log.h"
#include "resources.h"
2022-07-02 03:40:50 -05:00
#include "vec.h"
2021-11-30 21:29:18 -06:00
#define SHADER_BUF 10000
2022-07-02 03:40:50 -05:00
struct vec shaders;
2021-11-30 21:29:18 -06:00
struct mShader *MakeShader(const char *vertpath, const char *fragpath)
{
2022-07-02 03:40:50 -05:00
if (shaders.data == NULL) shaders = vec_init(sizeof(struct mShader), 10);
2021-11-30 21:29:18 -06:00
struct mShader init = { 0, vertpath, fragpath };
2022-07-02 03:40:50 -05:00
struct mShader *new = vec_add(&shaders, NULL);
memcpy(new, &init, sizeof(*new));
shader_compile(new);
return new;
}
struct mShader *CreateShader(const char *vert, const char *frag)
{
return NULL;
2021-11-30 21:29:18 -06:00
}
int shader_compile_error(GLuint shader)
{
GLint success = 0;
GLchar infoLog[ERROR_BUFFER] = { '\0' };
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success) return 0;
glGetShaderInfoLog(shader, ERROR_BUFFER, NULL, infoLog);
2022-02-06 10:14:57 -06:00
YughLog(0, LOG_ERROR, "Shader compilation error.\nLog: %s", infoLog);
2021-11-30 21:29:18 -06:00
return 1;
}
int shader_link_error(GLuint shader)
{
GLint success = 0;
GLchar infoLog[ERROR_BUFFER] = { '\0' };
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (success) return 0;
glGetProgramInfoLog(shader, ERROR_BUFFER, NULL, infoLog);
2022-02-06 10:14:57 -06:00
YughLog(0, LOG_ERROR, "Shader link error.\nLog: %s", infoLog);
2021-11-30 21:29:18 -06:00
return 1;
}
2022-02-06 10:14:57 -06:00
GLuint load_shader_from_file(const char *path, int type)
2021-11-30 21:29:18 -06:00
{
char spath[MAXPATH] = {'\0'};
2022-01-19 16:43:21 -06:00
sprintf(spath, "%s%s", "shaders/", path);
FILE *f = fopen(make_path(spath), "r'");
2021-11-30 21:29:18 -06:00
if (!path)
perror(spath), exit(1);
char buf[SHADER_BUF] = {'\0'};
long int fsize;
fseek(f, 0, SEEK_END);
fsize = ftell(f);
rewind(f);
fread(buf, fsize, 1, f);
fclose(f);
GLuint id = glCreateShader(type);
const char *code = buf;
glShaderSource(id, 1, &code, NULL);
glCompileShader(id);
2022-07-02 03:40:50 -05:00
if (shader_compile_error(id)) {
printf("Error with shader %s.\n", path);
2021-11-30 21:29:18 -06:00
return 0;
2022-07-02 03:40:50 -05:00
}
2021-11-30 21:29:18 -06:00
return id;
}
void shader_compile(struct mShader *shader)
{
GLuint vert = load_shader_from_file(shader->vertpath, GL_VERTEX_SHADER);
GLuint frag = load_shader_from_file(shader->fragpath, GL_FRAGMENT_SHADER);
shader->id = glCreateProgram();
glAttachShader(shader->id, vert);
glAttachShader(shader->id, frag);
glLinkProgram(shader->id);
shader_link_error(shader->id);
glDeleteShader(vert);
glDeleteShader(frag);
}
void shader_use(struct mShader *shader)
{
glUseProgram(shader->id);
}
void shader_setbool(struct mShader *shader, const char *name, int val)
{
glUniform1i(glGetUniformLocation(shader->id, name), val);
}
void shader_setint(struct mShader *shader, const char *name, int val)
{
glUniform1i(glGetUniformLocation(shader->id, name), val);
}
void shader_setfloat(struct mShader *shader, const char *name, float val)
{
glUniform1f(glGetUniformLocation(shader->id, name), val);
}
2022-06-30 10:31:23 -05:00
void shader_setvec2(struct mShader *shader, const char *name, mfloat_t val[2])
2021-11-30 21:29:18 -06:00
{
glUniform2fv(glGetUniformLocation(shader->id, name), 1, val);
}
2022-06-30 10:31:23 -05:00
void shader_setvec3(struct mShader *shader, const char *name, mfloat_t val[3])
2021-11-30 21:29:18 -06:00
{
glUniform3fv(glGetUniformLocation(shader->id, name), 1, val);
}
2022-06-30 10:31:23 -05:00
void shader_setvec4(struct mShader *shader, const char *name, mfloat_t val[4])
2021-11-30 21:29:18 -06:00
{
glUniform4fv(glGetUniformLocation(shader->id, name), 1, val);
}
2022-06-30 10:31:23 -05:00
void shader_setmat2(struct mShader *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-06-30 10:31:23 -05:00
void shader_setmat3(struct mShader *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-06-30 10:31:23 -05:00
void shader_setmat4(struct mShader *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-06-30 10:31:23 -05:00
void shader_setUBO(struct mShader *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
}
void shader_compile_all()
{
2022-07-02 03:40:50 -05:00
vec_walk(&shaders, shader_compile);
2021-11-30 21:29:18 -06:00
}