First pass at PBO
[clouds.git] / program.cpp
1 #include "program.hpp"
2 #include <fstream>
3 #include <sstream>
4 #include <iostream>
5
6 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
7
8 using namespace std;
9
10 void attachShader(GLuint progId, string filePath, GLenum type) {
11         GLuint shader = glCreateShader(type);
12
13         if (!shader) {
14                 cerr << "error creating shader" << endl;
15                 abort();
16         }
17
18         ifstream file(filePath);
19         stringstream buffer;
20         buffer << file.rdbuf();
21         string str = buffer.str();
22         const char* contents = str.c_str();
23
24         glShaderSource(shader, 1, (const GLchar**)&contents, NULL);
25         glCompileShader(shader);
26         GLint success;
27         glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
28         if (!success) {
29                 GLchar log[1024];
30                 glGetShaderInfoLog(shader, 1024, NULL, log);
31                 fprintf(stderr, "Error compiling %s\n%s\n", filePath.c_str(), log);
32                 exit(1);
33         }
34         glAttachShader(progId, shader);
35 }
36
37 Program::Program(const string vertexShader, const string fragmentShader) {
38         progId = glCreateProgram();
39
40         attachShader(progId, vertexShader, GL_VERTEX_SHADER);
41         attachShader(progId, fragmentShader, GL_FRAGMENT_SHADER);
42
43         glLinkProgram(progId);
44         GLint success = 0;
45         glGetProgramiv(progId, GL_LINK_STATUS, &success);
46         if (!success) {
47                 GLchar log[1024];
48                 glGetProgramInfoLog(progId, sizeof(log), NULL, log);
49                 fprintf(stderr, "error linking %s and %s\n%s\n", vertexShader.c_str(), fragmentShader.c_str(), log);
50                 exit(1);
51         }
52 }
53
54 void Program::validate() const {
55         glValidateProgram(progId);
56         
57         GLint success;
58         glGetProgramiv(progId, GL_VALIDATE_STATUS, &success);
59         if (!success) {
60                 GLchar log[1024];
61                 glGetProgramInfoLog(progId, sizeof(log), NULL, log);
62                 fprintf(stderr, "Error validating: %s\n", log);
63                 exit(1);
64         }
65 }
66