Transform shader sources to header files

This commit is contained in:
Dane Johnson 2021-01-17 18:03:09 -06:00
parent 5e8d693758
commit 0f3dbdff64
8 changed files with 59 additions and 43 deletions

View File

@ -14,7 +14,6 @@ find_package(SWIG REQUIRED)
find_package(Lua REQUIRED)
file(GLOB_RECURSE sources core/*.cpp core/*.h)
file(GLOB shaders shaders/*)
add_compile_definitions(LUA_SCRIPTING)
add_executable(couch ${sources})
@ -26,8 +25,6 @@ target_link_libraries(couch OpenGL::GL)
target_link_libraries(couch GLEW::GLEW)
target_link_libraries(couch ${LUA_LIBRARIES})
file(COPY shaders DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
include(UseSWIG)
set_property(SOURCE scripting/couch.i PROPERTY CPLUSPLUS ON)
swig_add_library(couchlua
@ -39,6 +36,11 @@ target_link_libraries(couch couchlua)
add_subdirectory(thirdparty)
add_subdirectory(shaders)
target_include_directories(couch
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/shaders")
if(WIN32)
target_link_libraries(couch glfw3dll)
target_link_libraries(couch ssp)

View File

@ -1,42 +1,12 @@
#include "Shader.h"
Shader::Shader(const char* vertexPath, const char* fragmentPath) {
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
} catch (std::ifstream::failure e) {
std::cerr << "Error reading shader file." << std::endl;
}
const char * vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
Shader::Shader(const char* vertexCode, const char* fragmentCode) {
Id vertex, fragment;
int success;
char infoLog[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glShaderSource(vertex, 1, &vertexCode, NULL);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success) {
@ -45,7 +15,7 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) {
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glShaderSource(fragment, 1, &fragmentCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success) {

View File

@ -1,11 +1,6 @@
#ifndef SHADER_H
#define SHADER_H
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <glm/gtc/type_ptr.hpp>
#include "types.h"
@ -15,7 +10,7 @@ class Shader {
public:
Id id;
Shader(const char *vertexPath, const char *fragmentPath);
Shader(const char *vertexCode, const char *fragmentCode);
void Use();
void UpdateView(Matrix view);
void UpdateModel(Matrix model);

View File

@ -1,6 +1,8 @@
#include "FlatShader.h"
#include "flat.vert.h"
#include "flat.frag.h"
FlatShader::FlatShader() : Shader("shaders/flat.vert", "shaders/flat.frag") {}
FlatShader::FlatShader() : Shader(flat_vert, flat_frag) {}
void FlatShader::UpdateColor(Vector3 color) {
glUniform3f(glGetUniformLocation(id, "color"), color.r, color.g, color.b);

View File

Before

Width:  |  Height:  |  Size: 413 KiB

After

Width:  |  Height:  |  Size: 413 KiB

14
shaders/CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
project(Couch)
file(GLOB shaders *.vert *.frag)
list(
TRANSFORM shaders
APPEND .h
OUTPUT_VARIABLE shader_header_files)
add_custom_target(shader_headers
BYPRODUCTS ${shader_header_files}
COMMAND perl "${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl"
DEPENDS ${shaders} ${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl)
add_dependencies(couch shader_headers)

33
shaders/makeheaders.pl Normal file
View File

@ -0,0 +1,33 @@
use strict;
use warnings;
sub convert {
my $filename = $1;
my $headerguard = "$filename.h";
$headerguard =~ tr/a-z./A-Z_/;
my $constname = "$filename";
$constname =~ tr/./_/;
open my $fin, '<', $filename;
open my $fout, '>', "$filename.h";
print $fout "#ifndef $headerguard \n#define $headerguard\nconst char * $constname = \n";
while(my $line = <$fin>) {
$line =~ s/\n/\\n/;
print $fout "\"$line\"\n";
}
print $fout ";\n#endif // $headerguard\n";
close $fin;
close $fout;
}
opendir DIR, ".";
while (readdir DIR) {
if (/(.*\.(frag|vert))/) {
convert;
}
}