mirror of
https://github.com/MichaelFisher1997/opengl-cpp.git
synced 2025-04-27 22:23:10 +00:00
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:
parent
dcfd6031d7
commit
d8895a1930
14
res/shaders/Basic.shader
Normal file
14
res/shaders/Basic.shader
Normal 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);
|
||||||
|
};
|
64
src/sdl.cpp
64
src/sdl.cpp
@ -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() };
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
11
src/sdl.hpp
11
src/sdl.hpp
@ -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);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user