1 | #include "graphics-pipeline_opengl.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <fstream>
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | GraphicsPipeline_OpenGL::GraphicsPipeline_OpenGL() {
|
---|
9 | }
|
---|
10 |
|
---|
11 | GraphicsPipeline_OpenGL::~GraphicsPipeline_OpenGL() {
|
---|
12 | }
|
---|
13 |
|
---|
14 | void GraphicsPipeline_OpenGL::createPipeline(string vertShaderFile, string fragShaderFile) {
|
---|
15 | shaderProgram = loadShaderProgram(vertShaderFile, fragShaderFile);
|
---|
16 |
|
---|
17 | glGenVertexArrays(1, &vao);
|
---|
18 | numPoints = 0;
|
---|
19 | }
|
---|
20 |
|
---|
21 | GLuint GraphicsPipeline_OpenGL::loadShaderProgram(string vertShaderFile, string fragShaderFile) {
|
---|
22 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertShaderFile);
|
---|
23 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragShaderFile);
|
---|
24 |
|
---|
25 | GLuint shader_program = glCreateProgram();
|
---|
26 | glAttachShader(shader_program, vs);
|
---|
27 | glAttachShader(shader_program, fs);
|
---|
28 |
|
---|
29 | glLinkProgram(shader_program);
|
---|
30 |
|
---|
31 | return shader_program;
|
---|
32 | }
|
---|
33 |
|
---|
34 | GLuint GraphicsPipeline_OpenGL::loadShader(GLenum type, string file) {
|
---|
35 | cout << "Loading shader from file " << file << endl;
|
---|
36 |
|
---|
37 | ifstream shaderFile(file);
|
---|
38 | GLuint shaderId = 0;
|
---|
39 |
|
---|
40 | if (shaderFile.is_open()) {
|
---|
41 | string line, shaderString;
|
---|
42 |
|
---|
43 | while (getline(shaderFile, line)) {
|
---|
44 | shaderString += line + "\n";
|
---|
45 | }
|
---|
46 | shaderFile.close();
|
---|
47 | const char* shaderCString = shaderString.c_str();
|
---|
48 |
|
---|
49 | shaderId = glCreateShader(type);
|
---|
50 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
51 | glCompileShader(shaderId);
|
---|
52 |
|
---|
53 | cout << "Loaded successfully" << endl;
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | cout << "Failed to load the file" << endl;
|
---|
57 | }
|
---|
58 |
|
---|
59 | return shaderId;
|
---|
60 | }
|
---|