Added a way to load shader from file, we use a .shader file which contains both our vertex and fragment shaders, we parse them out the file

This commit is contained in:
MichaelFisher1997 2025-01-06 20:01:22 +00:00
parent dcfd6031d7
commit d8895a1930
3 changed files with 69 additions and 20 deletions

14
res/shaders/Basic.shader Normal file
View File

@ -0,0 +1,14 @@
#shader vertex
#version 330 core
layout(location = 0) in vec4 position;
void main() {
gl_Position = position;
};
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
void main() {
color = vec4(0.2, 0.3, 0.8, 1.0);
};

View File

@ -1,10 +1,17 @@
#include <GL/glew.h> // Include GLEW before <SDL2/SDL.h>? #include <GL/glew.h> // Include GLEW before <SDL2/SDL.h>?
#include "sdl.hpp" #include "sdl.hpp"
#include <alloca.h> #include <alloca.h>
#include <cstdio>
#include <iostream> #include <iostream>
#include <string>
#include <fstream>
//#include <iterator> //#include <iterator>
#include <ostream> #include <ostream>
#include <string> #include <string>
#include <sstream>
struct ShaderProgramSource {
std::string VertexSource, FragmentSource;
};
SdlWindow::SdlWindow(const char* title, int width, int height) SdlWindow::SdlWindow(const char* title, int width, int height)
: m_window(nullptr), : m_window(nullptr),
@ -73,26 +80,8 @@ SdlWindow::SdlWindow(const char* title, int width, int height)
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
std::string vertexShader = ShaderProgramSource source = parseShader("res/shaders/Basic.shader");
"#version 330 core\n" unsigned int shader = createShader(source.VertexSource, source.FragmentSource);
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n";
std::string fragmentShader =
"#version 330 core\n"
"\n"
"layout(location = 0) out vec4 color;\n"
"\n"
"void main()\n"
"{\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
unsigned int shader = createShader(vertexShader, fragmentShader);
glUseProgram(shader); glUseProgram(shader);
} }
@ -254,3 +243,38 @@ unsigned int SdlWindow::createShader(const std::string& vertexShader, const std:
return program; return program;
} }
SdlWindow::ShaderProgramSource SdlWindow::parseShader(const std::string& filepath) {
//can be changed to c standard which is a bit faster
std::ifstream stream(filepath);
if (!stream.is_open()) {
std::cerr << "parseShader ERROR: Could not open file " << filepath << std::endl;
// Return empty (or handle however you prefer)
return {"", ""};
}
enum class ShaderType {
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream ss[2]; //vertex - fragment
ShaderType type = ShaderType::NONE;
while (getline(stream, line)) {
if (line.find("#shader") != std::string::npos) {
if (line.find("vertex") != std::string::npos) {
type = ShaderType::VERTEX;
}
else if (line.find("fragment") != std::string::npos) {
type = ShaderType::FRAGMENT;
}
}
else {
ss[(int)type] << line << '\n';
}
}
return {ss[0].str(), ss[1].str() };
}

View File

@ -10,6 +10,9 @@
#endif #endif
//#include <iostream> //#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
// Forward declaration of classes and structs if needed // Forward declaration of classes and structs if needed
// class SomethingElse; // class SomethingElse;
@ -26,6 +29,14 @@ public:
void run(); void run();
unsigned int compileShader(unsigned int type, const std::string& source); unsigned int compileShader(unsigned int type, const std::string& source);
unsigned int createShader(const std::string& vetexShader, const std::string& fragmentShader); unsigned int createShader(const std::string& vetexShader, const std::string& fragmentShader);
// for spliting our shaders
//struct ShaderProgramSource {
// std::string VetexSource, FragmentSource;
struct ShaderProgramSource {
std::string VertexSource;
std::string FragmentSource;
};
ShaderProgramSource parseShader(const std::string& filepath);
void setFullscreen(bool fullscreen); void setFullscreen(bool fullscreen);