Compare commits
4 Commits
865fa558e9
...
d481aa4d19
Author | SHA1 | Date |
---|---|---|
|
d481aa4d19 | |
|
a22f719ea8 | |
|
beaf176819 | |
|
df48698ab7 |
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.13)
|
cmake_minimum_required(VERSION 3.13)
|
||||||
project(blah)
|
project(blah)
|
||||||
#set(CMAKE_BUILD_TYPE Debug)
|
set(CMAKE_BUILD_TYPE Debug)
|
||||||
include_directories("${PROJECT_SOURCE_DIR}/src/")
|
include_directories("${PROJECT_SOURCE_DIR}/src/")
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
|
|
@ -2,9 +2,11 @@
|
||||||
#define _SHADERLOADER_H_
|
#define _SHADERLOADER_H_
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <libgen.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
@ -37,7 +39,8 @@ struct ShaderRegistry {
|
||||||
void register_shader(struct ShaderRegistry *r, char *name, int type, GLuint id) {
|
void register_shader(struct ShaderRegistry *r, char *name, int type, GLuint id) {
|
||||||
// Allocate storage for the name
|
// Allocate storage for the name
|
||||||
int name_size = strlen(name);
|
int name_size = strlen(name);
|
||||||
char *n = (char*) malloc(name_size * sizeof(char));
|
char *n = (char*) calloc(name_size, sizeof(char));
|
||||||
|
strncpy(n, name, strlen(name));
|
||||||
|
|
||||||
// Grow and copy
|
// Grow and copy
|
||||||
int new_size = r->size + 1;
|
int new_size = r->size + 1;
|
||||||
|
@ -149,6 +152,9 @@ int loadShadersFromFile(const char* file_path, struct ShaderRegistry *registry)
|
||||||
char *name;
|
char *name;
|
||||||
char *type;
|
char *type;
|
||||||
char *path;
|
char *path;
|
||||||
|
char *file_path_copy = (char*) calloc(strlen(file_path), sizeof(char));
|
||||||
|
strncpy(file_path_copy, file_path, strlen(file_path));
|
||||||
|
char *base = dirname((char*) file_path_copy);
|
||||||
while((size = getline(&line, &size, fp)) != -1) {
|
while((size = getline(&line, &size, fp)) != -1) {
|
||||||
if(size == 0) {
|
if(size == 0) {
|
||||||
// Skipping, line length 0
|
// Skipping, line length 0
|
||||||
|
@ -168,10 +174,21 @@ int loadShadersFromFile(const char* file_path, struct ShaderRegistry *registry)
|
||||||
// Invalid line, not enough elements.
|
// Invalid line, not enough elements.
|
||||||
// @TODO Inform the user.
|
// @TODO Inform the user.
|
||||||
free(line_copy);
|
free(line_copy);
|
||||||
name = type = path = NULL;
|
name = type = path = NULL;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Strip trailing new lines from the path.
|
||||||
|
path[strcspn(path, "\n")] = 0;
|
||||||
|
|
||||||
|
// Convert any paths as relative to the basename of the file_path they
|
||||||
|
// were loaded from.
|
||||||
|
int real_path_size = strlen(base) + 1 + strlen(path) + 1;
|
||||||
|
char *shader_path = (char*) calloc(real_path_size, sizeof(char));
|
||||||
|
strncat(shader_path, base, strlen(base));
|
||||||
|
strcat(shader_path + strlen(base), "/");
|
||||||
|
strncat(shader_path + strlen(base) + 1, path, strlen(path));
|
||||||
|
|
||||||
// Validate type
|
// Validate type
|
||||||
enum ShaderType shader_type;
|
enum ShaderType shader_type;
|
||||||
if(strcmp("fragment", type) == 0) {
|
if(strcmp("fragment", type) == 0) {
|
||||||
|
@ -188,18 +205,27 @@ int loadShadersFromFile(const char* file_path, struct ShaderRegistry *registry)
|
||||||
// Invalid line, unknown type.
|
// Invalid line, unknown type.
|
||||||
// @TODO Inform the user.
|
// @TODO Inform the user.
|
||||||
free(line_copy);
|
free(line_copy);
|
||||||
|
free(shader_path);
|
||||||
name = type = path = NULL;
|
name = type = path = NULL;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint shader_id = compile_shader(path, shader_type);
|
GLuint shader_id = compile_shader(shader_path, shader_type);
|
||||||
if(shader_id == 0) {
|
if(shader_id == 0) {
|
||||||
// Error in compilation of the shader
|
// Error in compilation of the shader
|
||||||
// @TODO Inform the user.
|
// @TODO Inform the user.
|
||||||
free(line_copy);
|
free(line_copy);
|
||||||
|
free(shader_path);
|
||||||
name = type = path = NULL;
|
name = type = path = NULL;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add to registry
|
||||||
|
register_shader(registry, name, shader_type, shader_id);
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
free(line_copy);
|
||||||
|
free(shader_path);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#version 330 core
|
#version 300 es
|
||||||
layout(location = 0) in vec3 vertexPosition_modelspace;
|
layout(location = 0) in vec3 vertexPosition_modelspace;
|
||||||
void main(){
|
void main(){
|
||||||
gl_Position.xyz = vertexPosition_modelspace;
|
gl_Position.xyz = vertexPosition_modelspace;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#version 330 core
|
#version 300 es
|
||||||
out vec3 color;
|
out vec3 color;
|
||||||
void main(){
|
void main(){
|
||||||
color = vec3(1,0,0);
|
color = vec3(1,0,0);
|
||||||
|
|
Loading…
Reference in New Issue