More prep of shader files.

This commit is contained in:
Brandon Wright 2019-01-13 11:39:54 -06:00
parent 29241c9cde
commit 2b5d9af6ca
3 changed files with 103 additions and 86 deletions

View File

@ -19,67 +19,6 @@ static const GLfloat mvp_ortho[16] = { 2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f };
static void reduce_to_path(char* filename)
{
for (int i = strlen(filename); i >= 0; i--)
{
if (filename[i] == '\\' || filename[i] == '/')
{
filename[i] = 0;
break;
}
}
}
static char *read_file(const char *filename)
{
FILE *file = NULL;
int size;
char *contents;
file = fopen(filename, "rb");
if (!file)
return NULL;
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
contents = new char[size + 1];
fread(contents, size, 1, file);
contents[size] = '\0';
fclose(file);
return contents;
}
std::vector<std::string> read_file_lines(const char *filename)
{
std::vector<std::string> lines;
char *file_contents = read_file(filename);
if (!file_contents)
return lines;
std::string string_contents(file_contents);
delete[] file_contents;
for (char &c : string_contents)
{
if (c == '\r')
c = '\n';
}
std::istringstream ss(string_contents);
std::string line;
while (std::getline(ss, line, '\n'))
if (!line.empty())
lines.push_back(line);
return std::move(lines);
}
static int scale_string_to_enum(const char *string, bool last)
{
if (!strcasecmp(string, "source"))
@ -398,20 +337,6 @@ bool GLSLShader::load_shader(char *filename)
if (!load_shader_preset_file(filename))
return false;
/*
for (unsigned int i = 1; i < pass.size(); i++)
{
if (pass[i].alias && *pass[i].alias)
{
aliases += "#define ";
aliases += pass[i].alias;
aliases += " Pass";
aliases += std::to_string(i - 1);
aliases += "Texture\n";
printf ("%s\n", aliases.c_str());
}
}*/
for (unsigned int i = 1; i < pass.size(); i++)
{
GLSLPass *p = &pass[i];
@ -420,7 +345,9 @@ bool GLSLShader::load_shader(char *filename)
realpath(p->filename, temp);
strcpy(p->filename, temp);
auto lines = read_file_lines(p->filename);
std::vector<std::string> lines;
read_shader_file_with_includes(p->filename, lines);
if (lines.empty())
{
printf("Couldn't read shader file %s\n", temp);

View File

@ -151,14 +151,10 @@ bool loadPngImage(const char *name, int &outWidth, int &outHeight,
memcpy(*outData + (row_bytes * i), row_pointers[i], row_bytes);
}
/* Clean up after the read,
* and free any memory allocated */
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
/* Close the file */
fclose(fp);
/* That's it */
return true;
#else
return false;
@ -199,19 +195,17 @@ bool loadTGA(const char *filename, STGA &tgaFile)
long imageSize = tgaFile.width * tgaFile.height * tgaFile.byteCount;
// allocate memory for image data
unsigned char *tempBuf = new unsigned char[imageSize];
tgaFile.data = new unsigned char[tgaFile.width * tgaFile.height * 4];
// read in image data
fread(tempBuf, sizeof(unsigned char), imageSize, file);
// swap line order and convert to RBGA
// swap line order and convert to RGBA
for (int i = 0; i < tgaFile.height; i++)
{
unsigned char *source = tempBuf + tgaFile.width *
(tgaFile.height - 1 - i) *
tgaFile.byteCount;
(tgaFile.height - 1 - i) *
tgaFile.byteCount;
unsigned char *destination = tgaFile.data + tgaFile.width * i * 4;
for (int j = 0; j < tgaFile.width; j++)
{
@ -226,8 +220,99 @@ bool loadTGA(const char *filename, STGA &tgaFile)
delete[] tempBuf;
tgaFile.byteCount = 4;
// close file
fclose(file);
return true;
}
void reduce_to_path(char *filename)
{
for (int i = strlen(filename); i >= 0; i--)
{
if (filename[i] == '\\' || filename[i] == '/')
{
filename[i] = 0;
break;
}
}
}
static std::string folder_from_path(std::string filename)
{
for (int i = filename.length() - 1; i >= 0; i--)
if (filename[i] == '\\' || filename[i] == '/')
return filename.substr(0, i);
return std::string(".");
}
static char *read_file(const char *filename)
{
FILE *file = NULL;
int size;
char *contents;
file = fopen(filename, "rb");
if (!file)
return NULL;
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
contents = new char[size + 1];
fread(contents, size, 1, file);
contents[size] = '\0';
fclose(file);
return contents;
}
static std::string canonicalize(const std::string &noncanonical)
{
char *temp = realpath(noncanonical.c_str(), NULL);
std::string filename_string(temp);
free(temp);
return std::move(filename_string);
}
// filename must be canonical
void read_shader_file_with_includes(std::string filename,
std::vector<std::string> &lines)
{
char *file_contents = read_file(filename.c_str());
if (!file_contents)
return;
std::string string_contents(file_contents);
delete[] file_contents;
for (char &c : string_contents)
{
if (c == '\r')
c = '\n';
}
std::istringstream ss(string_contents);
std::string line;
while (std::getline(ss, line, '\n'))
{
if (line.empty())
continue;
if (line.find("#include") == 0)
{
char tmp[PATH_MAX];
sscanf(line.c_str(), "#include \"%[^\"]\"", tmp);
std::string fullpath = canonicalize(folder_from_path(filename) + "/" + tmp);
read_shader_file_with_includes(fullpath.c_str(), lines);
continue;
}
lines.push_back(line);
}
return;
}

View File

@ -8,6 +8,9 @@
#define __SHADER_HELPERS_H
#include "shader_platform.h"
#include <vector>
#include <string>
#include <sstream>
typedef struct _STGA
{
@ -44,5 +47,7 @@ void gl_log_errors();
bool gl_srgb_available();
int gl_version();
bool gl_float_texture_available();
void reduce_to_path(char* filename);
void read_shader_file_with_includes(std::string filename, std::vector<std::string> &lines);
#endif // __SHADER_HELPERS_H