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