how read a shader from another file c 2b 2b

Solutions on MaxInterview for how read a shader from another file c 2b 2b by the best coders in the world

showing results for - "how read a shader from another file c 2b 2b"
Earnest
05 Mar 2016
1#include "GLShader.hpp"
2
3#include <string>
4#include <iostream>
5#include <fstream>
6#include <vector>
7#include <algorithm>
8
9
10std::string readFile(const char *filePath) {
11    std::string content;
12    std::ifstream fileStream(filePath, std::ios::in);
13
14    if(!fileStream.is_open()) {
15        std::cerr << "Could not read file " << filePath << ". File does not exist." << std::endl;
16        return "";
17    }
18
19    std::string line = "";
20    while(!fileStream.eof()) {
21        std::getline(fileStream, line);
22        content.append(line + "\n");
23    }
24
25    fileStream.close();
26    return content;
27}
28
29
30GLuint LoadShader(const char *vertex_path, const char *fragment_path) {
31    GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
32    GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
33
34    // Read shaders
35
36    std::string vertShaderStr = readFile(vertex_path);
37    std::string fragShaderStr = readFile(fragment_path);
38    const char *vertShaderSrc = vertShaderStr.c_str();
39    const char *fragShaderSrc = fragShaderStr.c_str();
40
41    GLint result = GL_FALSE;
42    int logLength;
43
44    // Compile vertex shader
45
46    std::cout << "Compiling vertex shader." << std::endl;
47    glShaderSource(vertShader, 1, &vertShaderSrc, NULL);
48    glCompileShader(vertShader);
49
50    // Check vertex shader
51
52    glGetShaderiv(vertShader, GL_COMPILE_STATUS, &result);
53    glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &logLength);
54    std::vector vertShaderError((logLength > 1) ? logLength : 1);
55    glGetShaderInfoLog(vertShader, logLength, NULL, &vertShaderError[0]);
56    std::cout << &vertShaderError[0] << std::endl;
57
58    // Compile fragment shader
59
60    std::cout << "Compiling fragment shader." << std::endl;
61    glShaderSource(fragShader, 1, &fragShaderSrc, NULL);
62    glCompileShader(fragShader);
63
64    // Check fragment shader
65
66    glGetShaderiv(fragShader, GL_COMPILE_STATUS, &result);
67    glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength);
68    std::vector fragShaderError((logLength > 1) ? logLength : 1);
69    glGetShaderInfoLog(fragShader, logLength, NULL, &fragShaderError[0]);
70    std::cout << &fragShaderError[0] << std::endl;
71
72    std::cout << "Linking program" << std::endl;
73    GLuint program = glCreateProgram();
74    glAttachShader(program, vertShader);
75    glAttachShader(program, fragShader);
76    glLinkProgram(program);
77
78    glGetProgramiv(program, GL_LINK_STATUS, &result);
79    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
80    std::vector<char> programError( (logLength > 1) ? logLength : 1 );
81    glGetProgramInfoLog(program, logLength, NULL, &programError[0]);
82    std::cout << &programError[0] << std::endl;
83
84    glDeleteShader(vertShader);
85    glDeleteShader(fragShader);
86
87    return program;
88}