Shader from file

This commit is contained in:
Tom
2021-06-01 18:58:00 +02:00
parent e62657d7d6
commit 87e21c1f0f
10 changed files with 50 additions and 19 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -150,6 +150,9 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="src\Main.h" /> <ClInclude Include="src\Main.h" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="res\shaders\Basic.shader" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@@ -24,4 +24,7 @@
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="res\shaders\Basic.shader" />
</ItemGroup>
</Project> </Project>

View File

@@ -1,6 +1,44 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream> #include <iostream>
#include <fstream>
#include <string>
#include <sstream>
struct ShaderProgramSource {
std::string VertexSource;
std::string FragmentSource;
};
static ShaderProgramSource ParseShader(const std::string& filepath) {
std::ifstream stream(filepath);
enum class ShaderType {
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream ss[2]; //one is the vertex other is the fragment shader
ShaderType type = ShaderType::NONE;
while (getline(stream, line)) {
if (line.find("#shader") != std::string::npos) {
if (line.find("vertex") != std::string::npos) {
//set mode to vertex
type = ShaderType::VERTEX;
}
else if (line.find("fragment") != std::string::npos) {
//set mode to fragment
type = ShaderType::FRAGMENT;
}
}
else {
ss[(int)type] << line << "\n";
}
}
return { ss[0].str(), ss[1].str() };
}
static unsigned int CompileShader(unsigned int type, const std::string& source) { static unsigned int CompileShader(unsigned int type, const std::string& source) {
unsigned int id = glCreateShader(type); unsigned int id = glCreateShader(type);
@@ -89,26 +127,13 @@ int main(void){
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" std::cout << "VERTEX" << std::endl;
"\n" std::cout << source.VertexSource << std::endl;
"layout(location = 0) in vec4 position;\n" std::cout << "FRAGMENT" << std::endl;
"\n" std::cout << source.FragmentSource << std::endl;
"void main(){\n"
" gl_Position = position;\n"
"}\n";
std::string fragmentShader = unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
"#version 330 core\n"
"\n"
"layout(location = 0) out vec4 color;\n"
"\n"
"void main(){\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader); glUseProgram(shader);
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbinding the buffer glBindBuffer(GL_ARRAY_BUFFER, 0); //unbinding the buffer