mirror of
https://github.com/guilhermewerner/opengl
synced 2025-06-15 13:04:17 +00:00
Load Shaders from Files
This commit is contained in:
8
OpenGL/Shaders/Fragment.glsl
Normal file
8
OpenGL/Shaders/Fragment.glsl
Normal file
@ -0,0 +1,8 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = vec4(0.0, 1.0, 0.0, 1.0);
|
||||
};
|
8
OpenGL/Shaders/Vertex.glsl
Normal file
8
OpenGL/Shaders/Vertex.glsl
Normal file
@ -0,0 +1,8 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec4 position;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = position;
|
||||
};
|
@ -2,6 +2,24 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
static std::string ParseShader(const std::string filePath)
|
||||
{
|
||||
std::ifstream stream(filePath);
|
||||
|
||||
std::string line;
|
||||
std::stringstream ss;
|
||||
|
||||
while (getline(stream, line))
|
||||
{
|
||||
ss << line << "\n";
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
static unsigned int CompileShader(unsigned int type, const std::string &source)
|
||||
{
|
||||
@ -92,25 +110,13 @@ int main()
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
std::string vertexShader =
|
||||
"#version 330 core\n"
|
||||
"\n"
|
||||
"layout(location = 0) in vec4 position;\n"
|
||||
"\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = position;\n"
|
||||
"}\n";
|
||||
std::string vertexShader = ParseShader("Shaders/Vertex.glsl");
|
||||
std::cout << "VERTEX:" << std::endl;
|
||||
std::cout << vertexShader << std::endl;
|
||||
|
||||
std::string fragmentShader =
|
||||
"#version 330 core\n"
|
||||
"\n"
|
||||
"layout(location = 0) out vec4 color;\n"
|
||||
"\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" color = vec4(0.0, 1.0, 0.0, 1.0);\n"
|
||||
"}\n";
|
||||
std::string fragmentShader = ParseShader("Shaders/Fragment.glsl");
|
||||
std::cout << "FRAGMENT:" << std::endl;
|
||||
std::cout << fragmentShader << std::endl;
|
||||
|
||||
unsigned int shader = CreateShader(vertexShader, fragmentShader);
|
||||
|
||||
|
Reference in New Issue
Block a user