source: opengl-game/graphics-pipeline_opengl.cpp@ e1f88a9

feature/imgui-sdl
Last change on this file since e1f88a9 was 0b1b52d, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

In openglgame, port over the functionality to specify and initialize varying attributes

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#include "graphics-pipeline_opengl.hpp"
2
3#include <iostream>
4#include <fstream>
5
6using namespace std;
7
8GraphicsPipeline_OpenGL::GraphicsPipeline_OpenGL(Viewport viewport) {
9 this->viewport = viewport;
10}
11
12GraphicsPipeline_OpenGL::~GraphicsPipeline_OpenGL() {
13}
14
15void GraphicsPipeline_OpenGL::addVaryingAttribute(VaryingAttribType attribType, GLint size, GLenum type,
16 size_t fieldOffset) {
17 // TODO: Throw an exception instead
18 if (type != GL_FLOAT && type != GL_UNSIGNED_INT) {
19 cout << "Unknown shader program attribute type: " << type << endl;
20 return;
21 }
22
23 VaryingAttribInfo attributeDesc;
24
25 attributeDesc.attribType = attribType;
26 attributeDesc.index = this->varyingAttribs.size();
27 attributeDesc.size = size;
28 attributeDesc.type = type;
29 attributeDesc.fieldOffset = fieldOffset;
30
31 this->varyingAttribs.push_back(attributeDesc);
32}
33
34void GraphicsPipeline_OpenGL::createPipeline(string vertShaderFile, string fragShaderFile) {
35 shaderProgram = loadShaderProgram(vertShaderFile, fragShaderFile);
36
37 this->numPoints = 0;
38 glGenVertexArrays(1, &this->vao);
39 glBindVertexArray(this->vao);
40
41 vector<VaryingAttribInfo>::iterator it;
42 for (it = this->varyingAttribs.begin(); it != this->varyingAttribs.end(); it++) {
43 glEnableVertexAttribArray(it->index);
44
45 glGenBuffers(1, &it->buffer);
46 glBindBuffer(GL_ARRAY_BUFFER, it->buffer);
47
48 switch (it->type) {
49 case GL_FLOAT: {
50 glVertexAttribPointer(it->index, it->size, it->type, GL_FALSE, 0, NULL);
51 break;
52 }
53 case GL_UNSIGNED_INT: {
54 glVertexAttribIPointer(it->index, it->size, it->type, 0, NULL);
55 break;
56 }
57 }
58 }
59}
60
61GLuint GraphicsPipeline_OpenGL::loadShaderProgram(string vertShaderFile, string fragShaderFile) {
62 GLuint vs = loadShader(GL_VERTEX_SHADER, vertShaderFile);
63 GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragShaderFile);
64
65 GLuint shader_program = glCreateProgram();
66 glAttachShader(shader_program, vs);
67 glAttachShader(shader_program, fs);
68
69 glLinkProgram(shader_program);
70
71 return shader_program;
72}
73
74GLuint GraphicsPipeline_OpenGL::loadShader(GLenum type, string file) {
75 cout << "Loading shader from file " << file << endl;
76
77 ifstream shaderFile(file);
78 GLuint shaderId = 0;
79
80 if (shaderFile.is_open()) {
81 string line, shaderString;
82
83 while (getline(shaderFile, line)) {
84 shaderString += line + "\n";
85 }
86 shaderFile.close();
87 const char* shaderCString = shaderString.c_str();
88
89 shaderId = glCreateShader(type);
90 glShaderSource(shaderId, 1, &shaderCString, NULL);
91 glCompileShader(shaderId);
92
93 cout << "Loaded successfully" << endl;
94 }
95 else {
96 cout << "Failed to load the file" << endl;
97 }
98
99 return shaderId;
100}
Note: See TracBrowser for help on using the repository browser.