pull/2/head
E. Almqvist 2 years ago
parent ef3ff09bc2
commit 8fa8b95246
  1. 6
      shaders/fragment.glsl
  2. 3
      shaders/vertex.glsl
  3. 19
      src/main.cpp
  4. 30
      src/shaders.cpp

@ -0,0 +1,6 @@
#version 330 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0f, 1.0f, 1.0f, 0.0f);
}

@ -1,6 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
void main() { void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
} }

@ -8,19 +8,8 @@
#define WINDOW_WIDTH 640 #define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480 #define WINDOW_HEIGHT 480
const char *vertexShaderSource = "#version 330 core\n" #define VERT_SHADER_SRC_FILE "shaders/vertex.glsl"
"layout (location = 0) in vec3 aPos;\n" #define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
"}\0";
float verts[] = { float verts[] = {
0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f,
@ -53,8 +42,8 @@ struct RenderObj {
RenderObj preRenderCallback() { RenderObj preRenderCallback() {
// Compile a bunch of shaders etc // Compile a bunch of shaders etc
unsigned int vertShader = Shader::compile(GL_VERTEX_SHADER, vertexShaderSource); unsigned int vertShader = Shader::compileFromFile(GL_VERTEX_SHADER, VERT_SHADER_SRC_FILE);
unsigned int fragShader = Shader::compile(GL_FRAGMENT_SHADER, fragShaderSource); unsigned int fragShader = Shader::compileFromFile(GL_FRAGMENT_SHADER, FRAG_SHADER_SRC_FILE);
unsigned int shaders[2] = {vertShader, fragShader}; unsigned int shaders[2] = {vertShader, fragShader};
// Create a shader program & link the shaders etc // Create a shader program & link the shaders etc

@ -1,16 +1,32 @@
#include <fstream> #include <fstream>
#include <glad/glad.h> #include <glad/glad.h>
#include <vector> #include <vector>
#include <filesystem> #include <string>
#include <iostream>
namespace Shader { namespace Shader {
char* loadSourceFromFile(const char* file) { std::string loadSourceFromFile(const char* file) {
char* src; std::string content;
std::ifstream fh(file, std::ios::in);
return src; if(!fh.is_open()) {
printf("Unable to read file \"%s\"!", file);
return "";
}
std::string line = "";
while(!fh.eof()) {
std::getline(fh, line);
content.append(line + "\n");
}
fh.close();
return content.c_str();
} }
unsigned int compile(GLenum shadertype, const char* shaderSource) { unsigned int compile(GLenum shadertype, const char* shaderSource) {
printf("--------------SRC-PREVIEW--------------\n%s\n---------------------------------------\n", shaderSource);
unsigned int shader; unsigned int shader;
shader = glCreateShader(shadertype); shader = glCreateShader(shadertype);
glShaderSource(shader, 1, &shaderSource, NULL); glShaderSource(shader, 1, &shaderSource, NULL);
@ -27,6 +43,12 @@ namespace Shader {
return shader; return shader;
} }
unsigned int compileFromFile(GLenum shadertype, const char* file) {
std::string src = loadSourceFromFile(file);
unsigned int shader = compile(shadertype, src.c_str());
return shader;
}
unsigned int createProgram(std::vector<unsigned int> shaders) { unsigned int createProgram(std::vector<unsigned int> shaders) {
unsigned int shaderProg; unsigned int shaderProg;
shaderProg = glCreateProgram(); shaderProg = glCreateProgram();

Loading…
Cancel
Save