Load shaders from file
This commit is contained in:
		
							
								
								
									
										7
									
								
								shaders/frag.glsl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								shaders/frag.glsl
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
#version 460 core
 | 
			
		||||
 | 
			
		||||
out vec4 color;
 | 
			
		||||
 | 
			
		||||
void main() {
 | 
			
		||||
  color = vec4(0.93f, 0.42f, 0.35f, 1.0f);
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										7
									
								
								shaders/vert.glsl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								shaders/vert.glsl
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
#version 460 core
 | 
			
		||||
 | 
			
		||||
in vec4 position;
 | 
			
		||||
 | 
			
		||||
void main() {
 | 
			
		||||
  gl_Position = vec4(position.x, position.y, position.z, position.w);
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										45
									
								
								src/main.cc
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								src/main.cc
									
									
									
									
									
								
							@@ -41,13 +41,14 @@ struct App {
 | 
			
		||||
  bool          running;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int    init                    (App &app);
 | 
			
		||||
void   create_vertex_spec      (App &app);
 | 
			
		||||
void   create_graphics_pipeline(App &app);
 | 
			
		||||
void   run_main_loop           (App &app);
 | 
			
		||||
void   cleanup                 (App &app);
 | 
			
		||||
GLuint create_shader_program   (const std::string &vertex_shader_source, const std::string &fragment_shader_source);
 | 
			
		||||
GLuint compile_shader          (GLuint type, const std::string &source);
 | 
			
		||||
int         init                    (App &app);
 | 
			
		||||
void        create_vertex_spec      (App &app);
 | 
			
		||||
void        create_graphics_pipeline(App &app);
 | 
			
		||||
void        run_main_loop           (App &app);
 | 
			
		||||
void        cleanup                 (App &app);
 | 
			
		||||
GLuint      create_shader_program   (const std::string &vertex_shader_source, const std::string &fragment_shader_source);
 | 
			
		||||
GLuint      compile_shader          (GLuint type, const std::string &source);
 | 
			
		||||
std::string load_shader             (const std::string &filepath);
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
  App app    = {};
 | 
			
		||||
@@ -148,18 +149,8 @@ void create_vertex_spec(App &app) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void create_graphics_pipeline(App &app) {
 | 
			
		||||
  const std::string vs_source =
 | 
			
		||||
    "#version 460 core\n"
 | 
			
		||||
    "in vec4 position;\n"
 | 
			
		||||
    "void main() {\n"
 | 
			
		||||
    "  gl_Position = vec4(position.x, position.y, position.z, position.w);\n"
 | 
			
		||||
    "}";
 | 
			
		||||
  const std::string fs_source =
 | 
			
		||||
    "#version 460 core\n"
 | 
			
		||||
    "out vec4 color;\n"
 | 
			
		||||
    "void main() {\n"
 | 
			
		||||
    "  color = vec4(0.93f, 0.42f, 0.35f, 1.0f);\n"
 | 
			
		||||
    "}";
 | 
			
		||||
  const std::string vs_source = load_shader("shaders/vert.glsl");
 | 
			
		||||
  const std::string fs_source = load_shader("shaders/frag.glsl");
 | 
			
		||||
 | 
			
		||||
  app.shader_program = create_shader_program(vs_source, fs_source);
 | 
			
		||||
}
 | 
			
		||||
@@ -256,3 +247,19 @@ GLuint compile_shader(GLuint type, const std::string &source) {
 | 
			
		||||
 | 
			
		||||
  return shader;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string load_shader(const std::string &filepath) {
 | 
			
		||||
  FILE *fp = fopen(filepath.c_str(), "r");
 | 
			
		||||
  if (!fp) {
 | 
			
		||||
    return "";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  std::string output = {};
 | 
			
		||||
 | 
			
		||||
  char buf[1024] = {0};
 | 
			
		||||
  while (fgets(buf, sizeof(buf), fp)) {
 | 
			
		||||
    output += buf;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return output;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user